更新Tag

This commit is contained in:
小海
2019-12-04 16:30:51 +08:00
parent 1745a5d821
commit 7ba287d261
3 changed files with 65 additions and 50 deletions

View File

@@ -277,10 +277,10 @@ public class ArticleServiceImpl implements ArticleService {
if (reqBody.getCategory() == null || reqBody.getCategory().replaceAll(" ", "").isEmpty()) { if (reqBody.getCategory() == null || reqBody.getCategory().replaceAll(" ", "").isEmpty()) {
throw new MyException(ResponseEnum.PARAMETERS_ERROR); throw new MyException(ResponseEnum.PARAMETERS_ERROR);
} }
// 暂时不更新tags // 暂时不更新tags
// if (reqBody.getTags() == null || reqBody.getTags().replaceAll(" ", "").isEmpty()) { if (reqBody.getTags() == null || reqBody.getTags().replaceAll(" ", "").isEmpty()) {
// throw new MyException(ResponseEnum.PARAMETERS_ERROR); throw new MyException(ResponseEnum.PARAMETERS_ERROR);
// } }
//写入数据库的数据 //写入数据库的数据
Article article = new Article(); Article article = new Article();
@@ -317,26 +317,44 @@ public class ArticleServiceImpl implements ArticleService {
article.setCategoryId(oldArticle.getCategoryId()); article.setCategoryId(oldArticle.getCategoryId());
} }
String[] newTags = reqBody.getTags().split(",");
// String[] newTags = reqBody.getTags().replaceAll(" ", "").split(","); String[] tagIds = oldArticle.getTagsId().split(",");
//防止出现 null2这种情况
// //防止出现 null2这种情况 article.setTagsId("");
// article.setTagsId(""); for (String t : newTags) {
// for (String t : newTags) { Tag tag = tagMapper.findTagByName(t);
// Tag tag = tagMapper.findTagByName(t); if (tag == null) {
// if (tag == null) { tag = new Tag();
// tag = new Tag(); tag.setName(t);
// tag.setName(t); tag.setArticles(oldArticle.getId() + ",");
// tag.setArticles(oldArticle.getId() + ","); int status = tagMapper.insert(tag);
// tag = tagMapper.save(tag); if (status == 0) {
// article.setTagsId(article.getTagsId() + tag.getId() + ","); // 插入失败
// continue; throw new MyException(ResponseEnum.FAILURE);
// } }
// article.setTagsId(article.getTagsId() + tag.getId() + ","); article.setTagsId(article.getTagsId() + tag.getId() + ",");
// } continue;
}
// TODO:::: tag的更新 article.setTagsId(article.getTagsId() + tag.getId() + ",");
article.setTagsId(oldArticle.getTagsId()); }
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()); article.setUpdateDate(new Date());

View File

@@ -8,10 +8,12 @@ import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc; 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.ResultHandler;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers; import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
@@ -20,6 +22,8 @@ import org.springframework.web.context.WebApplicationContext;
import javax.servlet.http.Cookie; import javax.servlet.http.Cookie;
import java.util.UUID;
import static org.junit.Assert.*; import static org.junit.Assert.*;
/** /**
@@ -39,10 +43,12 @@ public class BaseTest {
@Autowired @Autowired
private WebApplicationContext wac; private WebApplicationContext wac;
protected MockHttpSession session;
@Before @Before
public void before() { public void before() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
session = new MockHttpSession();
System.out.println("==========> 开始测试 <========="); System.out.println("==========> 开始测试 <=========");
} }
@@ -60,7 +66,7 @@ public class BaseTest {
req.setIsRememberMe(false); req.setIsRememberMe(false);
JSONObject loginReq = JSONObject.fromObject(req); JSONObject loginReq = JSONObject.fromObject(req);
String str = mockMvc.perform(MockMvcRequestBuilders.post("/login").content(loginReq.toString()).contentType("application/json")) String str = mockMvc.perform(MockMvcRequestBuilders.post("/login").content(loginReq.toString()).contentType("application/json"))
// .andDo(MockMvcResultHandlers.print()) // .andDo(MockMvcResultHandlers.print())
.andReturn().getResponse().getContentAsString(); .andReturn().getResponse().getContentAsString();
String token = JSONObject.fromObject(str).getJSONObject(Result).getString("token"); String token = JSONObject.fromObject(str).getJSONObject(Result).getString("token");
assertNotNull(token); assertNotNull(token);
@@ -79,7 +85,7 @@ public class BaseTest {
req.setIsRememberMe(false); req.setIsRememberMe(false);
JSONObject loginReq = JSONObject.fromObject(req); JSONObject loginReq = JSONObject.fromObject(req);
String str = mockMvc.perform(MockMvcRequestBuilders.post("/login").content(loginReq.toString()).contentType("application/json")) String str = mockMvc.perform(MockMvcRequestBuilders.post("/login").content(loginReq.toString()).contentType("application/json"))
// .andDo(MockMvcResultHandlers.print()) // .andDo(MockMvcResultHandlers.print())
.andReturn().getResponse().getContentAsString(); .andReturn().getResponse().getContentAsString();
String token = JSONObject.fromObject(str).getJSONObject(Result).getString("token"); String token = JSONObject.fromObject(str).getJSONObject(Result).getString("token");
assertNotNull(token); assertNotNull(token);
@@ -94,4 +100,8 @@ public class BaseTest {
public void test() { public void test() {
} }
protected String randomStr(int len) {
return UUID.randomUUID().toString().replaceAll("-", "").substring(0, len);
}
} }

View File

@@ -13,8 +13,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import java.util.Arrays; import java.util.*;
import java.util.UUID;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
@@ -139,32 +138,17 @@ public class ArticleControllerTest extends BaseTest {
articleReq.setCategory("test"); articleReq.setCategory("test");
articleReq.setMdContent("test-" + article.getMdContent()); articleReq.setMdContent("test-" + article.getMdContent());
articleReq.setOpen(!article.getOpen()); 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()); articleReq.setTitle("test-" + article.getTitle());
try { 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 权限 // Admin 权限
token = adminLogin();
mockMvc.perform(put("/admin/article/update") mockMvc.perform(put("/admin/article/update")
.content(JSONObject.fromObject(articleReq).toString()) .content(JSONObject.fromObject(articleReq).toString())
.contentType("application/json") .contentType("application/json")
.header("Authorization", token)) .header("Authorization", adminLogin()))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andDo(result -> { .andDo(result -> {
JSONObject jsonObject = JSONObject.fromObject(result.getResponse().getContentAsString()); JSONObject jsonObject = JSONObject.fromObject(result.getResponse().getContentAsString());
@@ -175,8 +159,11 @@ public class ArticleControllerTest extends BaseTest {
assertEquals(articleReq.getMdContent(), a.getMdContent()); assertEquals(articleReq.getMdContent(), a.getMdContent());
assertEquals(articleReq.getTitle(), a.getTitle()); assertEquals(articleReq.getTitle(), a.getTitle());
assertEquals(articleReq.getType(), a.getOriginal()); assertEquals(articleReq.getType(), a.getOriginal());
// Tag 暂时不支持更新 // Tag
// assertEquals(articleReq.getTags(), Arrays.toString(a.getTags()).replaceAll(" ", "".replace("[", "".replace("]", "")))); 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.getOpen(), a.getOpen());
assertEquals(articleReq.getId(), a.getId()); assertEquals(articleReq.getId(), a.getId());
}); });