Просмотр исходного кода

添加对 UploadChannel 的唯一性约束

reghao 2 лет назад
Родитель
Сommit
44dd18a1a9

+ 4 - 3
oss-console/src/main/java/cn/reghao/oss/console/app/db/repository/UploadChannelRepository.java

@@ -14,8 +14,9 @@ import java.util.List;
 public interface UploadChannelRepository extends JpaRepository<UploadChannel, Integer> {
     int countByCreateBy(int createBy);
     UploadChannel findByChannelIdAndCreateBy(int channelId, int createBy);
-    UploadChannel findByPrefixAndCreateBy(String prefix, int createBy);
-    List<UploadChannel> findByBindDomain(String domain);
-    UploadChannel findByCreateByAndBindDomainAndChannelId(int createBy, String domain, int channelId);
+    UploadChannel findByPrefixAndCreateByAndBindDomain(String prefix, int createBy, String bindDomain);
+    UploadChannel findByPrefixAndBindDomain(String prefix, String bindDomain);
+    List<UploadChannel> findByBindDomain(String bindDomain);
+    UploadChannel findByCreateByAndBindDomainAndChannelId(int createBy, String bindDomain, int channelId);
     Page<UploadChannel> findByCreateBy(int createBy, Pageable pageable);
 }

+ 7 - 2
oss-console/src/main/java/cn/reghao/oss/console/app/service/UploadChannelService.java

@@ -36,7 +36,7 @@ public class UploadChannelService {
         this.storeNodeRepository = storeNodeRepository;
     }
 
-    public Result add(UploadChannelDto uploadChannelDto) {
+    public synchronized Result add(UploadChannelDto uploadChannelDto) {
         String domain = uploadChannelDto.getDomain();
         StoreNode storeNode = storeNodeRepository.findByDomain(domain);
         if (storeNode == null) {
@@ -45,11 +45,16 @@ public class UploadChannelService {
 
         int createBy = UserContext.getUser().getId();
         String channelPrefix = uploadChannelDto.getChannelPrefix();
-        UploadChannel uploadChannel = uploadChannelRepository.findByPrefixAndCreateBy(channelPrefix, createBy);
+        UploadChannel uploadChannel = uploadChannelRepository.findByPrefixAndCreateByAndBindDomain(channelPrefix, createBy, domain);
         if (uploadChannel != null) {
             return Result.fail(String.format("channel_prefix %s exist", channelPrefix));
         }
 
+        uploadChannel = uploadChannelRepository.findByPrefixAndBindDomain(channelPrefix, domain);
+        if (uploadChannel != null) {
+            return Result.fail(String.format("someone has already created channel_prefix %s", channelPrefix));
+        }
+
         int channelId = getNextChannelId(createBy);
         uploadChannel = new UploadChannel(channelId, uploadChannelDto, createBy);
         uploadChannelRepository.save(uploadChannel);