Add update endpoints for tags and categories

This commit is contained in:
Tim
2025-07-02 14:36:26 +08:00
parent a6fd1b52ed
commit 19fb175cce
6 changed files with 80 additions and 0 deletions

View File

@@ -21,6 +21,12 @@ public class CategoryController {
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());
return toDto(c);
}
@DeleteMapping("/{id}")
public void delete(@PathVariable Long id) {
categoryService.deleteCategory(id);

View File

@@ -21,6 +21,12 @@ public class TagController {
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());
return toDto(tag);
}
@DeleteMapping("/{id}")
public void delete(@PathVariable Long id) {
tagService.deleteTag(id);

View File

@@ -20,6 +20,21 @@ public class CategoryService {
return categoryRepository.save(category);
}
public Category updateCategory(Long id, String name, String describe, String icon) {
Category category = categoryRepository.findById(id)
.orElseThrow(() -> new IllegalArgumentException("Category not found"));
if (name != null) {
category.setName(name);
}
if (describe != null) {
category.setDescribe(describe);
}
if (icon != null) {
category.setIcon(icon);
}
return categoryRepository.save(category);
}
public void deleteCategory(Long id) {
categoryRepository.deleteById(id);
}

View File

@@ -20,6 +20,21 @@ public class TagService {
return tagRepository.save(tag);
}
public Tag updateTag(Long id, String name, String describe, String icon) {
Tag tag = tagRepository.findById(id)
.orElseThrow(() -> new IllegalArgumentException("Tag not found"));
if (name != null) {
tag.setName(name);
}
if (describe != null) {
tag.setDescribe(describe);
}
if (icon != null) {
tag.setIcon(icon);
}
return tagRepository.save(tag);
}
public void deleteTag(Long id) {
tagRepository.deleteById(id);
}