Fix existed tag insert failed for articles
This commit is contained in:
parent
be7aa410e6
commit
d8bb60f999
@ -11,7 +11,7 @@ public interface ArticleMapper {
|
||||
|
||||
Article findById(@Param("id") String id);
|
||||
|
||||
boolean findTag(@Param("tagName") String tagName);
|
||||
Tag findTag(@Param("tagName") String tagName);
|
||||
|
||||
void insertTag(@Param("tag") Tag tag);
|
||||
|
||||
|
@ -29,10 +29,11 @@ public class MyBatisArticleRepository implements ArticleRepository {
|
||||
|
||||
private void createNew(Article article) {
|
||||
for (Tag tag : article.getTags()) {
|
||||
if (!articleMapper.findTag(tag.getName())) {
|
||||
Tag targetTag = Optional.ofNullable(articleMapper.findTag(tag.getName())).orElseGet(() -> {
|
||||
articleMapper.insertTag(tag);
|
||||
}
|
||||
articleMapper.insertArticleTagRelation(article.getId(), tag.getId());
|
||||
return tag;
|
||||
});
|
||||
articleMapper.insertArticleTagRelation(article.getId(), targetTag.getId());
|
||||
}
|
||||
articleMapper.insert(article);
|
||||
}
|
||||
|
@ -54,9 +54,10 @@
|
||||
where A.id = #{id}
|
||||
</select>
|
||||
|
||||
<select id="findTag" resultType="java.lang.Boolean">
|
||||
select count(*) from tags where name = #{tagName}
|
||||
<select id="findTag" resultType="io.spring.core.article.Tag">
|
||||
select id, name from tags where name = #{tagName}
|
||||
</select>
|
||||
|
||||
<select id="findBySlug" resultMap="article">
|
||||
<include refid="selectArticle"/>
|
||||
where A.slug = #{slug}
|
||||
|
@ -13,6 +13,7 @@ import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
@ -38,7 +39,7 @@ public class ArticleRepositoryTransactionTest {
|
||||
try {
|
||||
articleRepository.save(anotherArticle);
|
||||
} catch (Exception e) {
|
||||
assertFalse(articleMapper.findTag("other"));
|
||||
assertNull(articleMapper.findTag("other"));
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user