Merge pull request #42 from nagisa77/codex/update-label-and-category-requirements

Add icon and description to tags and categories
This commit is contained in:
Tim
2025-07-02 13:58:59 +08:00
committed by GitHub
9 changed files with 65 additions and 12 deletions

View File

@@ -17,7 +17,7 @@ public class CategoryController {
@PostMapping @PostMapping
public CategoryDto create(@RequestBody CategoryRequest req) { public CategoryDto create(@RequestBody CategoryRequest req) {
Category c = categoryService.createCategory(req.getName()); Category c = categoryService.createCategory(req.getName(), req.getDescribe(), req.getIcon());
return toDto(c); return toDto(c);
} }
@@ -42,17 +42,23 @@ public class CategoryController {
CategoryDto dto = new CategoryDto(); CategoryDto dto = new CategoryDto();
dto.setId(c.getId()); dto.setId(c.getId());
dto.setName(c.getName()); dto.setName(c.getName());
dto.setIcon(c.getIcon());
dto.setDescribe(c.getDescribe());
return dto; return dto;
} }
@Data @Data
private static class CategoryRequest { private static class CategoryRequest {
private String name; private String name;
private String describe;
private String icon;
} }
@Data @Data
private static class CategoryDto { private static class CategoryDto {
private Long id; private Long id;
private String name; private String name;
private String describe;
private String icon;
} }
} }

View File

@@ -17,7 +17,7 @@ public class TagController {
@PostMapping @PostMapping
public TagDto create(@RequestBody TagRequest req) { public TagDto create(@RequestBody TagRequest req) {
Tag tag = tagService.createTag(req.getName()); Tag tag = tagService.createTag(req.getName(), req.getDescribe(), req.getIcon());
return toDto(tag); return toDto(tag);
} }
@@ -42,17 +42,23 @@ public class TagController {
TagDto dto = new TagDto(); TagDto dto = new TagDto();
dto.setId(tag.getId()); dto.setId(tag.getId());
dto.setName(tag.getName()); dto.setName(tag.getName());
dto.setIcon(tag.getIcon());
dto.setDescribe(tag.getDescribe());
return dto; return dto;
} }
@Data @Data
private static class TagRequest { private static class TagRequest {
private String name; private String name;
private String describe;
private String icon;
} }
@Data @Data
private static class TagDto { private static class TagDto {
private Long id; private Long id;
private String name; private String name;
private String describe;
private String icon;
} }
} }

View File

@@ -17,4 +17,10 @@ public class Category {
@Column(nullable = false, unique = true) @Column(nullable = false, unique = true)
private String name; private String name;
@Column(nullable = false)
private String icon;
@Column(nullable = false)
private String describe;
} }

View File

@@ -17,4 +17,10 @@ public class Tag {
@Column(nullable = false, unique = true) @Column(nullable = false, unique = true)
private String name; private String name;
@Column
private String icon;
@Column(nullable = false)
private String describe;
} }

View File

@@ -12,9 +12,11 @@ import java.util.List;
public class CategoryService { public class CategoryService {
private final CategoryRepository categoryRepository; private final CategoryRepository categoryRepository;
public Category createCategory(String name) { public Category createCategory(String name, String describe, String icon) {
Category category = new Category(); Category category = new Category();
category.setName(name); category.setName(name);
category.setDescribe(describe);
category.setIcon(icon);
return categoryRepository.save(category); return categoryRepository.save(category);
} }

View File

@@ -12,9 +12,11 @@ import java.util.List;
public class TagService { public class TagService {
private final TagRepository tagRepository; private final TagRepository tagRepository;
public Tag createTag(String name) { public Tag createTag(String name, String describe, String icon) {
Tag tag = new Tag(); Tag tag = new Tag();
tag.setName(name); tag.setName(name);
tag.setDescribe(describe);
tag.setIcon(icon);
return tagRepository.save(tag); return tagRepository.save(tag);
} }

View File

@@ -31,14 +31,18 @@ class CategoryControllerTest {
Category c = new Category(); Category c = new Category();
c.setId(1L); c.setId(1L);
c.setName("tech"); c.setName("tech");
Mockito.when(categoryService.createCategory(eq("tech"))).thenReturn(c); c.setDescribe("d");
c.setIcon("i");
Mockito.when(categoryService.createCategory(eq("tech"), eq("d"), eq("i"))).thenReturn(c);
Mockito.when(categoryService.getCategory(1L)).thenReturn(c); Mockito.when(categoryService.getCategory(1L)).thenReturn(c);
mockMvc.perform(post("/api/categories") mockMvc.perform(post("/api/categories")
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.content("{\"name\":\"tech\"}")) .content("{\"name\":\"tech\",\"describe\":\"d\",\"icon\":\"i\"}"))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(jsonPath("$.name").value("tech")); .andExpect(jsonPath("$.name").value("tech"))
.andExpect(jsonPath("$.describe").value("d"))
.andExpect(jsonPath("$.icon").value("i"));
mockMvc.perform(get("/api/categories/1")) mockMvc.perform(get("/api/categories/1"))
.andExpect(status().isOk()) .andExpect(status().isOk())
@@ -50,10 +54,14 @@ class CategoryControllerTest {
Category c = new Category(); Category c = new Category();
c.setId(2L); c.setId(2L);
c.setName("life"); c.setName("life");
c.setDescribe("d2");
c.setIcon("i2");
Mockito.when(categoryService.listCategories()).thenReturn(List.of(c)); Mockito.when(categoryService.listCategories()).thenReturn(List.of(c));
mockMvc.perform(get("/api/categories")) mockMvc.perform(get("/api/categories"))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(jsonPath("$[0].name").value("life")); .andExpect(jsonPath("$[0].name").value("life"))
.andExpect(jsonPath("$[0].describe").value("d2"))
.andExpect(jsonPath("$[0].icon").value("i2"));
} }
} }

View File

@@ -49,9 +49,13 @@ class PostControllerTest {
Category cat = new Category(); Category cat = new Category();
cat.setId(1L); cat.setId(1L);
cat.setName("tech"); cat.setName("tech");
cat.setDescribe("d");
cat.setIcon("i");
Tag tag = new Tag(); Tag tag = new Tag();
tag.setId(1L); tag.setId(1L);
tag.setName("java"); tag.setName("java");
tag.setDescribe("td");
tag.setIcon("ti");
Post post = new Post(); Post post = new Post();
post.setId(1L); post.setId(1L);
post.setTitle("t"); post.setTitle("t");
@@ -82,9 +86,13 @@ class PostControllerTest {
Category cat = new Category(); Category cat = new Category();
cat.setId(1L); cat.setId(1L);
cat.setName("tech"); cat.setName("tech");
cat.setDescribe("d");
cat.setIcon("i");
Tag tag = new Tag(); Tag tag = new Tag();
tag.setId(1L); tag.setId(1L);
tag.setName("java"); tag.setName("java");
tag.setDescribe("td");
tag.setIcon("ti");
Post post = new Post(); Post post = new Post();
post.setId(2L); post.setId(2L);
post.setTitle("hello"); post.setTitle("hello");

View File

@@ -31,14 +31,18 @@ class TagControllerTest {
Tag t = new Tag(); Tag t = new Tag();
t.setId(1L); t.setId(1L);
t.setName("java"); t.setName("java");
Mockito.when(tagService.createTag(eq("java"))).thenReturn(t); t.setDescribe("d");
t.setIcon("i");
Mockito.when(tagService.createTag(eq("java"), eq("d"), eq("i"))).thenReturn(t);
Mockito.when(tagService.getTag(1L)).thenReturn(t); Mockito.when(tagService.getTag(1L)).thenReturn(t);
mockMvc.perform(post("/api/tags") mockMvc.perform(post("/api/tags")
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.content("{\"name\":\"java\"}")) .content("{\"name\":\"java\",\"describe\":\"d\",\"icon\":\"i\"}"))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(jsonPath("$.name").value("java")); .andExpect(jsonPath("$.name").value("java"))
.andExpect(jsonPath("$.describe").value("d"))
.andExpect(jsonPath("$.icon").value("i"));
mockMvc.perform(get("/api/tags/1")) mockMvc.perform(get("/api/tags/1"))
.andExpect(status().isOk()) .andExpect(status().isOk())
@@ -50,10 +54,15 @@ class TagControllerTest {
Tag t = new Tag(); Tag t = new Tag();
t.setId(2L); t.setId(2L);
t.setName("spring"); t.setName("spring");
t.setDescribe("d2");
t.setIcon("i2");
Mockito.when(tagService.listTags()).thenReturn(List.of(t)); Mockito.when(tagService.listTags()).thenReturn(List.of(t));
mockMvc.perform(get("/api/tags")) mockMvc.perform(get("/api/tags"))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(jsonPath("$[0].name").value("spring")); .andExpect(status().isOk())
.andExpect(jsonPath("$[0].name").value("spring"))
.andExpect(jsonPath("$[0].describe").value("d2"))
.andExpect(jsonPath("$[0].icon").value("i2"));
} }
} }