Feat: 카테고리 존재 여부 API

This commit is contained in:
김용수 2024-09-13 17:36:21 +09:00
parent c451ea606b
commit a108096eba
7 changed files with 47 additions and 27 deletions

View File

@ -1,8 +1,8 @@
package com.worlabel.domain.category.controller;
package com.worlabel.domain.labelcategory.controller;
import com.worlabel.domain.category.entity.dto.LabelCategoryRequest;
import com.worlabel.domain.category.entity.dto.LabelCategoryResponse;
import com.worlabel.domain.category.service.LabelCategoryService;
import com.worlabel.domain.labelcategory.entity.dto.LabelCategoryRequest;
import com.worlabel.domain.labelcategory.entity.dto.LabelCategoryResponse;
import com.worlabel.domain.labelcategory.service.LabelCategoryService;
import com.worlabel.global.annotation.CurrentUser;
import com.worlabel.global.config.swagger.SwaggerApiError;
import com.worlabel.global.config.swagger.SwaggerApiSuccess;
@ -12,6 +12,8 @@ import com.worlabel.global.response.SuccessResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.data.repository.query.Param;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@ -35,7 +37,7 @@ public class LabelCategoryController {
return categoryService.createCategory(memberId, projectId, categoryRequest);
}
@Operation(summary = "레이블 카테고리 단일 조회", description = "레이블 카테고리를 조회합니다..")
@Operation(summary = "레이블 카테고리 단일 조회", description = "레이블 카테고리를 조회합니다.")
@SwaggerApiSuccess(description = "카테고리 성공적으로 조회합니다.")
@SwaggerApiError({ErrorCode.EMPTY_REQUEST_PARAMETER, ErrorCode.SERVER_ERROR})
@GetMapping("/{category_id}")
@ -47,6 +49,18 @@ public class LabelCategoryController {
return categoryService.getCategoryById(memberId, projectId, categoryId);
}
@Operation(summary = "레이블 카테고리 존재 여부 조회", description = "해당 프로젝트에 같은 레이블 카테고리 이름이 있는지 조회합니다.")
@SwaggerApiSuccess(description = "카테고리 존재 여부를 조회합니다.")
@SwaggerApiError({ErrorCode.EMPTY_REQUEST_PARAMETER, ErrorCode.SERVER_ERROR})
@GetMapping("/exist")
public boolean existByCategoryName(
@CurrentUser final Integer memberId,
@PathVariable("project_id") final Integer projectId,
@Param("categoryName") final String categoryName
) {
return categoryService.existByCategoryName(memberId, projectId, categoryName);
}
@Operation(summary = "레이블 카테고리 리스트 조회", description = "레이블 카테고리 리스트를 조회합니다..")
@SwaggerApiSuccess(description = "카테고리 리스트를 성공적으로 조회합니다.")
@SwaggerApiError({ErrorCode.EMPTY_REQUEST_PARAMETER, ErrorCode.SERVER_ERROR})
@ -57,17 +71,19 @@ public class LabelCategoryController {
) {
return categoryService.getCategoryList(memberId, projectId);
}
@Operation(summary = "카테고리 삭제", description = "카테고리를 삭제합니다.")
@SwaggerApiError({ErrorCode.EMPTY_REQUEST_PARAMETER, ErrorCode.SERVER_ERROR})
@SwaggerApiSuccess(description = "카테고리를 성공적으로 삭제합니다.")
@DeleteMapping("/{category_id}")
public BaseResponse<Void> deleteFolder(
public void deleteCategoryById(
@CurrentUser final Integer memberId,
@PathVariable("project_id") final Integer projectId,
@PathVariable("category_id") final Integer categoryId) {
categoryService.deleteCategory(memberId, projectId, categoryId);
return SuccessResponse.empty();
}
}

View File

@ -1,4 +1,4 @@
package com.worlabel.domain.category.entity;
package com.worlabel.domain.labelcategory.entity;
import com.worlabel.domain.project.entity.Project;

View File

@ -1,4 +1,4 @@
package com.worlabel.domain.category.entity.dto;
package com.worlabel.domain.labelcategory.entity.dto;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;

View File

@ -1,6 +1,6 @@
package com.worlabel.domain.category.entity.dto;
package com.worlabel.domain.labelcategory.entity.dto;
import com.worlabel.domain.category.entity.LabelCategory;
import com.worlabel.domain.labelcategory.entity.LabelCategory;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Getter;

View File

@ -1,13 +1,13 @@
package com.worlabel.domain.category.repository;
package com.worlabel.domain.labelcategory.repository;
import com.worlabel.domain.category.entity.LabelCategory;
import com.worlabel.domain.labelcategory.entity.LabelCategory;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
public interface LabelCategoryRepository extends JpaRepository<LabelCategory, Integer> {
boolean existsByName(String name);
List<LabelCategory> findAllByProjectId(Integer projectId);
boolean existsByNameAndProjectId(String categoryName, Integer projectId);
}

View File

@ -1,9 +1,9 @@
package com.worlabel.domain.category.service;
package com.worlabel.domain.labelcategory.service;
import com.worlabel.domain.category.entity.LabelCategory;
import com.worlabel.domain.category.entity.dto.LabelCategoryRequest;
import com.worlabel.domain.category.entity.dto.LabelCategoryResponse;
import com.worlabel.domain.category.repository.LabelCategoryRepository;
import com.worlabel.domain.labelcategory.entity.LabelCategory;
import com.worlabel.domain.labelcategory.entity.dto.LabelCategoryRequest;
import com.worlabel.domain.labelcategory.entity.dto.LabelCategoryResponse;
import com.worlabel.domain.labelcategory.repository.LabelCategoryRepository;
import com.worlabel.domain.participant.service.ParticipantService;
import com.worlabel.domain.project.entity.Project;
import com.worlabel.domain.project.service.ProjectService;
@ -28,7 +28,7 @@ public class LabelCategoryService {
participantService.checkEditorUnauthorized(memberId, projectId);
// 이미 존재하는지 확인 있다면 예외
if (labelCategoryRepository.existsByName(categoryRequest.getCategoryName())) {
if (labelCategoryRepository.existsByNameAndProjectId(categoryRequest.getCategoryName(), projectId)) {
throw new CustomException(ErrorCode.PROJECT_CATEGORY_EXIST);
}
@ -40,26 +40,30 @@ public class LabelCategoryService {
return LabelCategoryResponse.from(labelCategory);
}
public void deleteCategory(final int memberId, final int projectId, final int categoryId) {
public void deleteCategory(final int memberId, final int projectId, final int categoryId) {
participantService.checkEditorUnauthorized(memberId, projectId);
LabelCategory category = getCategory(categoryId);
labelCategoryRepository.delete(category);
}
public LabelCategoryResponse getCategoryById(final int memberId, final int projectId, final int categoryId){
participantService.checkViewerUnauthorized(memberId,projectId);
public LabelCategoryResponse getCategoryById(final int memberId, final int projectId, final int categoryId) {
participantService.checkViewerUnauthorized(memberId, projectId);
return LabelCategoryResponse.from(getCategory(categoryId));
}
public boolean existByCategoryName(final int memberId, final int projectId, final String categoryName) {
participantService.checkViewerUnauthorized(memberId, projectId);
return labelCategoryRepository.existsByNameAndProjectId(categoryName, projectId);
}
public List<LabelCategoryResponse> getCategoryList(final Integer memberId, final Integer projectId) {
participantService.checkViewerUnauthorized(memberId,projectId);
participantService.checkViewerUnauthorized(memberId, projectId);
List<LabelCategory> labelCategoryList = labelCategoryRepository.findAllByProjectId(projectId);
return labelCategoryList.stream().map(LabelCategoryResponse::from).toList();
}
private LabelCategory getCategory(final Integer categoryId) {
return labelCategoryRepository.findById(categoryId).orElseThrow(() ->new CustomException(ErrorCode.PROJECT_CATEGORY_NOT_FOUND));
return labelCategoryRepository.findById(categoryId).orElseThrow(() -> new CustomException(ErrorCode.PROJECT_CATEGORY_NOT_FOUND));
}
}

View File

@ -1,7 +1,7 @@
package com.worlabel.domain.project.entity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.worlabel.domain.category.entity.LabelCategory;
import com.worlabel.domain.labelcategory.entity.LabelCategory;
import com.worlabel.domain.workspace.entity.Workspace;
import com.worlabel.global.common.BaseEntity;
import jakarta.persistence.*;