更新Tag
This commit is contained in:
@@ -278,9 +278,9 @@ public class ArticleServiceImpl implements ArticleService {
|
||||
throw new MyException(ResponseEnum.PARAMETERS_ERROR);
|
||||
}
|
||||
// 暂时不更新tags
|
||||
// if (reqBody.getTags() == null || reqBody.getTags().replaceAll(" ", "").isEmpty()) {
|
||||
// throw new MyException(ResponseEnum.PARAMETERS_ERROR);
|
||||
// }
|
||||
if (reqBody.getTags() == null || reqBody.getTags().replaceAll(" ", "").isEmpty()) {
|
||||
throw new MyException(ResponseEnum.PARAMETERS_ERROR);
|
||||
}
|
||||
|
||||
//写入数据库的数据
|
||||
Article article = new Article();
|
||||
@@ -317,26 +317,44 @@ public class ArticleServiceImpl implements ArticleService {
|
||||
article.setCategoryId(oldArticle.getCategoryId());
|
||||
}
|
||||
|
||||
|
||||
// String[] newTags = reqBody.getTags().replaceAll(" ", "").split(",");
|
||||
|
||||
// //防止出现 ‘null2’这种情况
|
||||
// article.setTagsId("");
|
||||
// for (String t : newTags) {
|
||||
// Tag tag = tagMapper.findTagByName(t);
|
||||
// if (tag == null) {
|
||||
// tag = new Tag();
|
||||
// tag.setName(t);
|
||||
// tag.setArticles(oldArticle.getId() + ",");
|
||||
// tag = tagMapper.save(tag);
|
||||
// article.setTagsId(article.getTagsId() + tag.getId() + ",");
|
||||
// continue;
|
||||
// }
|
||||
// article.setTagsId(article.getTagsId() + tag.getId() + ",");
|
||||
// }
|
||||
|
||||
// TODO:::: tag的更新
|
||||
article.setTagsId(oldArticle.getTagsId());
|
||||
String[] newTags = reqBody.getTags().split(",");
|
||||
String[] tagIds = oldArticle.getTagsId().split(",");
|
||||
//防止出现 ‘null2’这种情况
|
||||
article.setTagsId("");
|
||||
for (String t : newTags) {
|
||||
Tag tag = tagMapper.findTagByName(t);
|
||||
if (tag == null) {
|
||||
tag = new Tag();
|
||||
tag.setName(t);
|
||||
tag.setArticles(oldArticle.getId() + ",");
|
||||
int status = tagMapper.insert(tag);
|
||||
if (status == 0) {
|
||||
// 插入失败
|
||||
throw new MyException(ResponseEnum.FAILURE);
|
||||
}
|
||||
article.setTagsId(article.getTagsId() + tag.getId() + ",");
|
||||
continue;
|
||||
}
|
||||
article.setTagsId(article.getTagsId() + tag.getId() + ",");
|
||||
}
|
||||
for (String tagId : tagIds) {
|
||||
Tag tagById = tagMapper.findTagById(Long.parseLong(tagId));
|
||||
// 在新更新的tag中是否有原有的tag
|
||||
boolean isOldTag = false;
|
||||
for (String s : newTags) {
|
||||
if (s.equals(tagById.getName())) {
|
||||
isOldTag = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!isOldTag) {
|
||||
tagById.setArticles(tagById.getArticles().replace(oldArticle.getId() + ",", ""));
|
||||
}
|
||||
tagMapper.update(tagById);
|
||||
}
|
||||
//
|
||||
// // TODO:::: tag的更新
|
||||
// article.setTagsId(oldArticle.getTagsId());
|
||||
|
||||
|
||||
article.setUpdateDate(new Date());
|
||||
|
||||
@@ -8,10 +8,12 @@ import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.mock.web.MockHttpSession;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.MvcResult;
|
||||
import org.springframework.test.web.servlet.ResultHandler;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
|
||||
@@ -20,6 +22,8 @@ import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
import javax.servlet.http.Cookie;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
@@ -39,10 +43,12 @@ public class BaseTest {
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext wac;
|
||||
protected MockHttpSession session;
|
||||
|
||||
@Before
|
||||
public void before() {
|
||||
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
|
||||
session = new MockHttpSession();
|
||||
System.out.println("==========> 开始测试 <=========");
|
||||
}
|
||||
|
||||
@@ -94,4 +100,8 @@ public class BaseTest {
|
||||
public void test() {
|
||||
|
||||
}
|
||||
|
||||
protected String randomStr(int len) {
|
||||
return UUID.randomUUID().toString().replaceAll("-", "").substring(0, len);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,8 +13,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.UUID;
|
||||
import java.util.*;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
||||
@@ -139,32 +138,17 @@ public class ArticleControllerTest extends BaseTest {
|
||||
articleReq.setCategory("test");
|
||||
articleReq.setMdContent("test-" + article.getMdContent());
|
||||
articleReq.setOpen(!article.getOpen());
|
||||
articleReq.setTags("tag");
|
||||
String tag1 = randomStr(4);
|
||||
String tag2 = randomStr(4);
|
||||
String tag = "test," + tag1 + "," + tag2;
|
||||
articleReq.setTags(tag);
|
||||
articleReq.setTitle("test-" + article.getTitle());
|
||||
try {
|
||||
mockMvc.perform(put("/admin/article/update")
|
||||
.content(JSONObject.fromObject(articleReq).toString())
|
||||
.contentType("application/json"))
|
||||
.andExpect(status().isOk())
|
||||
.andDo(result -> {
|
||||
assertEquals(HAVE_NOT_LOG_IN.getCode(), JSONObject.fromObject(result.getResponse().getContentAsString()).getInt(Code));
|
||||
});
|
||||
// User 权限
|
||||
String token = userLogin();
|
||||
mockMvc.perform(put("/admin/article/update")
|
||||
.content(JSONObject.fromObject(articleReq).toString())
|
||||
.contentType("application/json")
|
||||
.header("Authorization", token))
|
||||
.andExpect(status().isOk())
|
||||
.andDo(result -> {
|
||||
assertEquals(PERMISSION_ERROR.getCode(), JSONObject.fromObject(result.getResponse().getContentAsString()).getInt(Code));
|
||||
});
|
||||
// Admin 权限
|
||||
token = adminLogin();
|
||||
mockMvc.perform(put("/admin/article/update")
|
||||
.content(JSONObject.fromObject(articleReq).toString())
|
||||
.contentType("application/json")
|
||||
.header("Authorization", token))
|
||||
.header("Authorization", adminLogin()))
|
||||
.andExpect(status().isOk())
|
||||
.andDo(result -> {
|
||||
JSONObject jsonObject = JSONObject.fromObject(result.getResponse().getContentAsString());
|
||||
@@ -175,8 +159,11 @@ public class ArticleControllerTest extends BaseTest {
|
||||
assertEquals(articleReq.getMdContent(), a.getMdContent());
|
||||
assertEquals(articleReq.getTitle(), a.getTitle());
|
||||
assertEquals(articleReq.getType(), a.getOriginal());
|
||||
// Tag 暂时不支持更新
|
||||
// assertEquals(articleReq.getTags(), Arrays.toString(a.getTags()).replaceAll(" ", "".replace("[", "".replace("]", ""))));
|
||||
// Tag
|
||||
List<String> asList = Arrays.asList(a.getTags());
|
||||
assertTrue(asList.contains("test"));
|
||||
assertTrue(asList.contains(tag1));
|
||||
assertTrue(asList.contains(tag2));
|
||||
assertEquals(articleReq.getOpen(), a.getOpen());
|
||||
assertEquals(articleReq.getId(), a.getId());
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user