From 1f406c4bc56a175f9b6c74de4f32b20115937024 Mon Sep 17 00:00:00 2001 From: Tim <135014430+nagisa77@users.noreply.github.com> Date: Sat, 5 Jul 2025 17:17:32 +0800 Subject: [PATCH] Add optional small icon for categories and tags --- .../controller/CategoryController.java | 7 +++++-- .../openisle/controller/TagController.java | 7 +++++-- .../java/com/openisle/model/Category.java | 3 +++ src/main/java/com/openisle/model/Tag.java | 3 +++ .../com/openisle/service/CategoryService.java | 8 ++++++-- .../java/com/openisle/service/TagService.java | 8 ++++++-- .../controller/CategoryControllerTest.java | 20 ++++++++++++------- .../controller/TagControllerTest.java | 20 ++++++++++++------- 8 files changed, 54 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/openisle/controller/CategoryController.java b/src/main/java/com/openisle/controller/CategoryController.java index 3a9834143..ac1fa85dd 100644 --- a/src/main/java/com/openisle/controller/CategoryController.java +++ b/src/main/java/com/openisle/controller/CategoryController.java @@ -19,13 +19,13 @@ public class CategoryController { @PostMapping public CategoryDto create(@RequestBody CategoryRequest req) { - Category c = categoryService.createCategory(req.getName(), req.getDescribe(), req.getIcon()); + Category c = categoryService.createCategory(req.getName(), req.getDescribe(), req.getIcon(), req.getSmallIcon()); return toDto(c); } @PutMapping("/{id}") public CategoryDto update(@PathVariable Long id, @RequestBody CategoryRequest req) { - Category c = categoryService.updateCategory(id, req.getName(), req.getDescribe(), req.getIcon()); + Category c = categoryService.updateCategory(id, req.getName(), req.getDescribe(), req.getIcon(), req.getSmallIcon()); return toDto(c); } @@ -66,6 +66,7 @@ public class CategoryController { dto.setId(c.getId()); dto.setName(c.getName()); dto.setIcon(c.getIcon()); + dto.setSmallIcon(c.getSmallIcon()); dto.setDescribe(c.getDescribe()); return dto; } @@ -75,6 +76,7 @@ public class CategoryController { private String name; private String describe; private String icon; + private String smallIcon; } @Data @@ -83,6 +85,7 @@ public class CategoryController { private String name; private String describe; private String icon; + private String smallIcon; } @Data diff --git a/src/main/java/com/openisle/controller/TagController.java b/src/main/java/com/openisle/controller/TagController.java index 313c40133..33808bd8f 100644 --- a/src/main/java/com/openisle/controller/TagController.java +++ b/src/main/java/com/openisle/controller/TagController.java @@ -19,13 +19,13 @@ public class TagController { @PostMapping public TagDto create(@RequestBody TagRequest req) { - Tag tag = tagService.createTag(req.getName(), req.getDescribe(), req.getIcon()); + Tag tag = tagService.createTag(req.getName(), req.getDescribe(), req.getIcon(), req.getSmallIcon()); return toDto(tag); } @PutMapping("/{id}") public TagDto update(@PathVariable Long id, @RequestBody TagRequest req) { - Tag tag = tagService.updateTag(id, req.getName(), req.getDescribe(), req.getIcon()); + Tag tag = tagService.updateTag(id, req.getName(), req.getDescribe(), req.getIcon(), req.getSmallIcon()); return toDto(tag); } @@ -66,6 +66,7 @@ public class TagController { dto.setId(tag.getId()); dto.setName(tag.getName()); dto.setIcon(tag.getIcon()); + dto.setSmallIcon(tag.getSmallIcon()); dto.setDescribe(tag.getDescribe()); return dto; } @@ -75,6 +76,7 @@ public class TagController { private String name; private String describe; private String icon; + private String smallIcon; } @Data @@ -83,6 +85,7 @@ public class TagController { private String name; private String describe; private String icon; + private String smallIcon; } @Data diff --git a/src/main/java/com/openisle/model/Category.java b/src/main/java/com/openisle/model/Category.java index 6954890c2..4aea1f00e 100644 --- a/src/main/java/com/openisle/model/Category.java +++ b/src/main/java/com/openisle/model/Category.java @@ -21,6 +21,9 @@ public class Category { @Column(nullable = false) private String icon; + @Column + private String smallIcon; + @Column(nullable = false) private String describe; } diff --git a/src/main/java/com/openisle/model/Tag.java b/src/main/java/com/openisle/model/Tag.java index 29e8b05cf..cd1b3484d 100644 --- a/src/main/java/com/openisle/model/Tag.java +++ b/src/main/java/com/openisle/model/Tag.java @@ -21,6 +21,9 @@ public class Tag { @Column private String icon; + @Column + private String smallIcon; + @Column(nullable = false) private String describe; } diff --git a/src/main/java/com/openisle/service/CategoryService.java b/src/main/java/com/openisle/service/CategoryService.java index 9cbbcd5f3..794251502 100644 --- a/src/main/java/com/openisle/service/CategoryService.java +++ b/src/main/java/com/openisle/service/CategoryService.java @@ -12,15 +12,16 @@ import java.util.List; public class CategoryService { private final CategoryRepository categoryRepository; - public Category createCategory(String name, String describe, String icon) { + public Category createCategory(String name, String describe, String icon, String smallIcon) { Category category = new Category(); category.setName(name); category.setDescribe(describe); category.setIcon(icon); + category.setSmallIcon(smallIcon); return categoryRepository.save(category); } - public Category updateCategory(Long id, String name, String describe, String icon) { + public Category updateCategory(Long id, String name, String describe, String icon, String smallIcon) { Category category = categoryRepository.findById(id) .orElseThrow(() -> new IllegalArgumentException("Category not found")); if (name != null) { @@ -32,6 +33,9 @@ public class CategoryService { if (icon != null) { category.setIcon(icon); } + if (smallIcon != null) { + category.setSmallIcon(smallIcon); + } return categoryRepository.save(category); } diff --git a/src/main/java/com/openisle/service/TagService.java b/src/main/java/com/openisle/service/TagService.java index 8413e4853..614a0c430 100644 --- a/src/main/java/com/openisle/service/TagService.java +++ b/src/main/java/com/openisle/service/TagService.java @@ -12,15 +12,16 @@ import java.util.List; public class TagService { private final TagRepository tagRepository; - public Tag createTag(String name, String describe, String icon) { + public Tag createTag(String name, String describe, String icon, String smallIcon) { Tag tag = new Tag(); tag.setName(name); tag.setDescribe(describe); tag.setIcon(icon); + tag.setSmallIcon(smallIcon); return tagRepository.save(tag); } - public Tag updateTag(Long id, String name, String describe, String icon) { + public Tag updateTag(Long id, String name, String describe, String icon, String smallIcon) { Tag tag = tagRepository.findById(id) .orElseThrow(() -> new IllegalArgumentException("Tag not found")); if (name != null) { @@ -32,6 +33,9 @@ public class TagService { if (icon != null) { tag.setIcon(icon); } + if (smallIcon != null) { + tag.setSmallIcon(smallIcon); + } return tagRepository.save(tag); } diff --git a/src/test/java/com/openisle/controller/CategoryControllerTest.java b/src/test/java/com/openisle/controller/CategoryControllerTest.java index 383d10238..f1fada7b8 100644 --- a/src/test/java/com/openisle/controller/CategoryControllerTest.java +++ b/src/test/java/com/openisle/controller/CategoryControllerTest.java @@ -37,16 +37,18 @@ class CategoryControllerTest { c.setName("tech"); c.setDescribe("d"); c.setIcon("i"); - Mockito.when(categoryService.createCategory(eq("tech"), eq("d"), eq("i"))).thenReturn(c); + c.setSmallIcon("s1"); + Mockito.when(categoryService.createCategory(eq("tech"), eq("d"), eq("i"), eq("s1"))).thenReturn(c); Mockito.when(categoryService.getCategory(1L)).thenReturn(c); mockMvc.perform(post("/api/categories") .contentType(MediaType.APPLICATION_JSON) - .content("{\"name\":\"tech\",\"describe\":\"d\",\"icon\":\"i\"}")) + .content("{\"name\":\"tech\",\"describe\":\"d\",\"icon\":\"i\",\"smallIcon\":\"s1\"}")) .andExpect(status().isOk()) .andExpect(jsonPath("$.name").value("tech")) .andExpect(jsonPath("$.describe").value("d")) - .andExpect(jsonPath("$.icon").value("i")); + .andExpect(jsonPath("$.icon").value("i")) + .andExpect(jsonPath("$.smallIcon").value("s1")); mockMvc.perform(get("/api/categories/1")) .andExpect(status().isOk()) @@ -60,13 +62,15 @@ class CategoryControllerTest { c.setName("life"); c.setDescribe("d2"); c.setIcon("i2"); + c.setSmallIcon("s2"); Mockito.when(categoryService.listCategories()).thenReturn(List.of(c)); mockMvc.perform(get("/api/categories")) .andExpect(status().isOk()) .andExpect(jsonPath("$[0].name").value("life")) .andExpect(jsonPath("$[0].describe").value("d2")) - .andExpect(jsonPath("$[0].icon").value("i2")); + .andExpect(jsonPath("$[0].icon").value("i2")) + .andExpect(jsonPath("$[0].smallIcon").value("s2")); } @Test @@ -76,15 +80,17 @@ class CategoryControllerTest { c.setName("tech"); c.setDescribe("d3"); c.setIcon("i3"); - Mockito.when(categoryService.updateCategory(eq(3L), eq("tech"), eq("d3"), eq("i3"))).thenReturn(c); + c.setSmallIcon("s3"); + Mockito.when(categoryService.updateCategory(eq(3L), eq("tech"), eq("d3"), eq("i3"), eq("s3"))).thenReturn(c); mockMvc.perform(put("/api/categories/3") .contentType(MediaType.APPLICATION_JSON) - .content("{\"name\":\"tech\",\"describe\":\"d3\",\"icon\":\"i3\"}")) + .content("{\"name\":\"tech\",\"describe\":\"d3\",\"icon\":\"i3\",\"smallIcon\":\"s3\"}")) .andExpect(status().isOk()) .andExpect(jsonPath("$.id").value(3)) .andExpect(jsonPath("$.name").value("tech")) .andExpect(jsonPath("$.describe").value("d3")) - .andExpect(jsonPath("$.icon").value("i3")); + .andExpect(jsonPath("$.icon").value("i3")) + .andExpect(jsonPath("$.smallIcon").value("s3")); } } diff --git a/src/test/java/com/openisle/controller/TagControllerTest.java b/src/test/java/com/openisle/controller/TagControllerTest.java index 8ddf08cb9..070aa86e0 100644 --- a/src/test/java/com/openisle/controller/TagControllerTest.java +++ b/src/test/java/com/openisle/controller/TagControllerTest.java @@ -37,16 +37,18 @@ class TagControllerTest { t.setName("java"); t.setDescribe("d"); t.setIcon("i"); - Mockito.when(tagService.createTag(eq("java"), eq("d"), eq("i"))).thenReturn(t); + t.setSmallIcon("s1"); + Mockito.when(tagService.createTag(eq("java"), eq("d"), eq("i"), eq("s1"))).thenReturn(t); Mockito.when(tagService.getTag(1L)).thenReturn(t); mockMvc.perform(post("/api/tags") .contentType(MediaType.APPLICATION_JSON) - .content("{\"name\":\"java\",\"describe\":\"d\",\"icon\":\"i\"}")) + .content("{\"name\":\"java\",\"describe\":\"d\",\"icon\":\"i\",\"smallIcon\":\"s1\"}")) .andExpect(status().isOk()) .andExpect(jsonPath("$.name").value("java")) .andExpect(jsonPath("$.describe").value("d")) - .andExpect(jsonPath("$.icon").value("i")); + .andExpect(jsonPath("$.icon").value("i")) + .andExpect(jsonPath("$.smallIcon").value("s1")); mockMvc.perform(get("/api/tags/1")) .andExpect(status().isOk()) @@ -60,6 +62,7 @@ class TagControllerTest { t.setName("spring"); t.setDescribe("d2"); t.setIcon("i2"); + t.setSmallIcon("s2"); Mockito.when(tagService.listTags()).thenReturn(List.of(t)); mockMvc.perform(get("/api/tags")) @@ -67,7 +70,8 @@ class TagControllerTest { .andExpect(status().isOk()) .andExpect(jsonPath("$[0].name").value("spring")) .andExpect(jsonPath("$[0].describe").value("d2")) - .andExpect(jsonPath("$[0].icon").value("i2")); + .andExpect(jsonPath("$[0].icon").value("i2")) + .andExpect(jsonPath("$[0].smallIcon").value("s2")); } @Test @@ -77,15 +81,17 @@ class TagControllerTest { t.setName("java"); t.setDescribe("d3"); t.setIcon("i3"); - Mockito.when(tagService.updateTag(eq(3L), eq("java"), eq("d3"), eq("i3"))).thenReturn(t); + t.setSmallIcon("s3"); + Mockito.when(tagService.updateTag(eq(3L), eq("java"), eq("d3"), eq("i3"), eq("s3"))).thenReturn(t); mockMvc.perform(put("/api/tags/3") .contentType(MediaType.APPLICATION_JSON) - .content("{\"name\":\"java\",\"describe\":\"d3\",\"icon\":\"i3\"}")) + .content("{\"name\":\"java\",\"describe\":\"d3\",\"icon\":\"i3\",\"smallIcon\":\"s3\"}")) .andExpect(status().isOk()) .andExpect(jsonPath("$.id").value(3)) .andExpect(jsonPath("$.name").value("java")) .andExpect(jsonPath("$.describe").value("d3")) - .andExpect(jsonPath("$.icon").value("i3")); + .andExpect(jsonPath("$.icon").value("i3")) + .andExpect(jsonPath("$.smallIcon").value("s3")); } }