Commit 47823d22 by zhangkb

标签关系维护添加判断关系是否已经存在逻辑

parent 677f0d6a
...@@ -36,6 +36,10 @@ public class TagRelCtrl { ...@@ -36,6 +36,10 @@ public class TagRelCtrl {
if(sourceId.equals(targetId)) { if(sourceId.equals(targetId)) {
throw new Exception("can not make source and target as the same one."); throw new Exception("can not make source and target as the same one.");
} }
//判断关系是否存在
if(tagRelService.tagRelExist(sourceId, targetId)) {
throw new Exception("the tag relation is exist");
}
return tagRelService.addTagRel(sourceId, targetId, userId, org); return tagRelService.addTagRel(sourceId, targetId, userId, org);
} }
......
package com.keymobile.tagmanager.persistence; package com.keymobile.tagmanager.persistence;
import java.util.List; import java.util.List;
import java.util.Optional;
import org.springframework.data.mongodb.repository.MongoRepository; import org.springframework.data.mongodb.repository.MongoRepository;
...@@ -11,4 +12,6 @@ public interface TagRelRepository extends MongoRepository<TagRelation, String>{ ...@@ -11,4 +12,6 @@ public interface TagRelRepository extends MongoRepository<TagRelation, String>{
List<TagRelation> findBySourceId(String sourceId); List<TagRelation> findBySourceId(String sourceId);
List<TagRelation> findByTargetId(String targetId); List<TagRelation> findByTargetId(String targetId);
Optional<TagRelation> findBySourceIdAndTargetId(String sourceId,String targetId);
} }
package com.keymobile.tagmanager.service; package com.keymobile.tagmanager.service;
import java.util.List; import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
...@@ -14,6 +15,15 @@ public class TagRelService { ...@@ -14,6 +15,15 @@ public class TagRelService {
@Autowired @Autowired
private TagRelRepository tagRelRepo; private TagRelRepository tagRelRepo;
//判断关系是否存在
public boolean tagRelExist(String sourceId,String targetId) {
Optional<TagRelation> tagRel = tagRelRepo.findBySourceIdAndTargetId(sourceId, targetId);
if(tagRel.isPresent()) {
return true;
}
return false;
}
public TagRelation addTagRel(String sourceId,String targetId,String userName,String org) { public TagRelation addTagRel(String sourceId,String targetId,String userName,String org) {
TagRelation tagRel = new TagRelation(sourceId,targetId,userName,org); TagRelation tagRel = new TagRelation(sourceId,targetId,userName,org);
return tagRelRepo.save(tagRel); return tagRelRepo.save(tagRel);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment