Skip to content

Commit

Permalink
Merge pull request #52 from Namo-Mongmong/feature/48
Browse files Browse the repository at this point in the history
[Feature/48] 카테고리 validation 검증 로직 및 비기능 개발
  • Loading branch information
luke0408 authored Mar 30, 2024
2 parents f0833c7 + f8bdc9c commit a38b20e
Show file tree
Hide file tree
Showing 15 changed files with 140 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,16 @@ public List<CategoryResponse.CategoryDto> getCategories(Long userId) {
}

@Transactional(readOnly = false)
public CategoryResponse.CategoryIdDto modifyCategory(Long categoryId, CategoryRequest.PostCategoryDto dto) {
public CategoryResponse.CategoryIdDto modifyCategory(Long categoryId, CategoryRequest.PostCategoryDto dto,
Long userId) {
Palette palette = paletteService.getPalette(dto.getPaletteId());
Category modifiedCategory = categoryService.modifyCategory(categoryId, dto, palette);
Category modifiedCategory = categoryService.modifyCategory(categoryId, dto, palette, userId);

return CategoryResponseConverter.toCategoryIdDto(modifiedCategory);
}

@Transactional(readOnly = false)
public void deleteCategory(Long categoryId) {
categoryService.delete(categoryId);
public void deleteCategory(Long categoryId, Long userId) {
categoryService.delete(categoryId, userId);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.namo2.domain.category.application.converter;

import com.example.namo2.domain.category.domain.Category;
import com.example.namo2.domain.category.domain.CategoryKind;
import com.example.namo2.domain.category.domain.Palette;
import com.example.namo2.domain.category.ui.dto.CategoryRequest;

Expand All @@ -22,20 +23,23 @@ public static Category toCategory(
.user(user)
.palette(palette)
.share(dto.isShare())
.kind(CategoryKind.CUSTOM)
.build();
}

public static Category toCategory(
String name,
Palette palette,
Boolean isShare,
User user
User user,
CategoryKind kind
) {
return Category.builder()
.name(name)
.palette(palette)
.share(isShare)
.user(user)
.kind(kind)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@

import com.example.namo2.domain.category.dao.repository.CategoryRepository;
import com.example.namo2.domain.category.domain.Category;
import com.example.namo2.domain.category.domain.CategoryKind;
import com.example.namo2.domain.category.domain.CategoryStatus;
import com.example.namo2.domain.category.domain.Palette;
import com.example.namo2.domain.category.ui.dto.CategoryRequest;

import com.example.namo2.domain.user.domain.User;

import com.example.namo2.global.common.exception.BaseException;
import com.example.namo2.global.common.response.BaseResponseStatus;

import lombok.RequiredArgsConstructor;

Expand All @@ -31,8 +33,9 @@ public List<Category> getCategories(Long userId) {
return categoryRepository.findCategoriesByUserIdAndStatusEquals(userId, CategoryStatus.ACTIVE);
}

public void delete(Long categoryId) {
public void delete(Long categoryId, Long userId) {
Category category = getCategory(categoryId);
validateUsersCategory(userId, category);
validateBaseCategory(category);
category.delete();
}
Expand All @@ -46,20 +49,26 @@ public Category getCategory(Long categoryId) {
.orElseThrow(() -> new BaseException(NOT_FOUND_CATEGORY_FAILURE));
}

public Category modifyCategory(Long categoryId, CategoryRequest.PostCategoryDto dto, Palette palette) {
public Category modifyCategory(Long categoryId, CategoryRequest.PostCategoryDto dto, Palette palette, Long userId) {
Category category = getCategory(categoryId);
validateBaseCategory(category);
validateUsersCategory(userId, category);
category.update(dto.getName(), dto.isShare(), palette);
return category;
}

private static void validateBaseCategory(Category category) {
if (category.getName().equals("일정") || category.getName().equals("모임")) {
private void validateUsersCategory(Long userId, Category category) {
if (category.isNotCreatedByUser(userId)) {
throw new BaseException(BaseResponseStatus.NOT_USERS_CATEGORY);
}
}

private void validateBaseCategory(Category category) {
if (category.isBaseCategory()) {
throw new BaseException(NOT_DELETE_BASE_CATEGORY_FAILURE);
}
}

public List<Category> getMoimUsersCategories(List<User> users) {
return categoryRepository.findMoimCategoriesByUsers(users);
return categoryRepository.findMoimCategoriesByUsers(users, CategoryKind.MOIM);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,10 @@
import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import com.example.namo2.domain.category.domain.Category;
import com.example.namo2.domain.category.domain.CategoryStatus;

import com.example.namo2.domain.user.domain.User;

public interface CategoryRepository extends JpaRepository<Category, Long> {
public List<Category> findCategoriesByUserIdAndStatusEquals(Long userId, CategoryStatus status);

@Query(value = "select c"
+ " from Category c"
+ " join fetch c.user"
+ " where c.user in :users and c.name = '모임'")
List<Category> findMoimCategoriesByUsers(@Param("users") List<User> users);

void deleteAllByUser(User user);
public interface CategoryRepository extends JpaRepository<Category, Long>, CategoryRepositoryCustom {
List<Category> findCategoriesByUserIdAndStatusEquals(Long userId, CategoryStatus status);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.example.namo2.domain.category.dao.repository;

import java.util.List;

import org.springframework.data.repository.query.Param;

import com.example.namo2.domain.category.domain.Category;
import com.example.namo2.domain.category.domain.CategoryKind;

import com.example.namo2.domain.user.domain.User;

public interface CategoryRepositoryCustom {
List<Category> findMoimCategoriesByUsers(@Param("users") List<User> users, @Param("kind") CategoryKind kind);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.example.namo2.domain.category.dao.repository;

import static com.example.namo2.domain.category.domain.QCategory.*;

import java.util.List;

import jakarta.persistence.EntityManager;

import com.querydsl.jpa.impl.JPAQueryFactory;

import com.example.namo2.domain.category.domain.Category;
import com.example.namo2.domain.category.domain.CategoryKind;

import com.example.namo2.domain.user.domain.User;

public class CategoryRepositoryImpl implements CategoryRepositoryCustom {
private final JPAQueryFactory queryFactory;

public CategoryRepositoryImpl(EntityManager em) {
queryFactory = new JPAQueryFactory(em);
}

@Override
public List<Category> findMoimCategoriesByUsers(List<User> users, CategoryKind kind) {
return queryFactory
.selectFrom(category)
.join(category.user).fetchJoin()
.where(category.user.in(users),
category.kind.eq(kind))
.fetch();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,17 @@ public class Category extends BaseTimeEntity {
@Enumerated(EnumType.STRING)
private CategoryStatus status;

@Enumerated(EnumType.STRING)
private CategoryKind kind;

@Builder
public Category(Palette palette, User user, String name, Boolean share) {
public Category(Palette palette, User user, String name, Boolean share, CategoryKind kind) {
this.palette = palette;
this.user = user;
this.name = name;
this.share = share;
this.status = CategoryStatus.ACTIVE;
this.kind = kind;
}

public void update(String name, Boolean share, Palette palette) {
Expand All @@ -64,4 +68,15 @@ public void update(String name, Boolean share, Palette palette) {
public void delete() {
this.status = CategoryStatus.DELETE;
}

public boolean isNotCreatedByUser(Long userId) {
return this.user.getId() != userId;
}

public boolean isBaseCategory() {
if (kind == CategoryKind.SCHEDULE || kind == CategoryKind.MOIM) {
return true;
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.example.namo2.domain.category.domain;

public enum CategoryKind {
/*
SCHEDULE: 기본
MOIM: MOIM 기본
CUSTOM: 유저가 생성한 카테고리
*/
SCHEDULE("일정"), MOIM("모임"), CUSTOM("커스텀");

private String categoryName;

CategoryKind(String categoryName) {
this.categoryName = categoryName;
}

public String getCategoryName() {
return categoryName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,20 @@ public BaseResponse<List<CategoryResponse.CategoryDto>> findAllCategory(HttpServ
@Operation(summary = "카테고리 수정", description = "카테고리 수정 API")
@PatchMapping("/{categoryId}")
public BaseResponse<CategoryResponse.CategoryIdDto> updateCategory(@PathVariable("categoryId") Long categoryId,
@Valid @RequestBody CategoryRequest.PostCategoryDto postcategoryDto) {
CategoryResponse.CategoryIdDto categoryIdDto = categoryFacade.modifyCategory(categoryId, postcategoryDto);
@Valid @RequestBody CategoryRequest.PostCategoryDto postcategoryDto,
HttpServletRequest request) {
Long userId = (Long)request.getAttribute("userId");
CategoryResponse.CategoryIdDto categoryIdDto = categoryFacade.modifyCategory(categoryId, postcategoryDto,
userId);
return new BaseResponse<>(categoryIdDto);
}

@Operation(summary = "카테고리 삭제", description = "카테고리 삭제 API")
@DeleteMapping("/{categoryId}")
public BaseResponse<String> deleteCategory(@PathVariable("categoryId") Long categoryId) {
categoryFacade.deleteCategory(categoryId);
public BaseResponse<String> deleteCategory(@PathVariable("categoryId") Long categoryId,
HttpServletRequest request) {
Long userId = (Long)request.getAttribute("userId");
categoryFacade.deleteCategory(categoryId, userId);
return new BaseResponse<>("삭제에 성공하셨습니다.");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.example.namo2.domain.category.ui.dto;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;

import com.example.namo2.domain.category.validation.annotation.CategoryName;

import lombok.AllArgsConstructor;
import lombok.Getter;

Expand All @@ -16,7 +15,7 @@ private CategoryRequest() {
@AllArgsConstructor
@Getter
public static class PostCategoryDto {
@CategoryName
@NotBlank
private String name;
@NotNull
private Long paletteId;
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import com.example.namo2.domain.category.application.impl.CategoryService;
import com.example.namo2.domain.category.application.impl.PaletteService;
import com.example.namo2.domain.category.domain.Category;
import com.example.namo2.domain.category.domain.CategoryKind;

import com.example.namo2.domain.memo.application.impl.MoimMemoLocationService;

Expand Down Expand Up @@ -285,16 +286,18 @@ private User saveOrNot(User user) {

private void makeBaseCategory(User save) {
Category baseCategory = CategoryConverter.toCategory(
"일정",
CategoryKind.SCHEDULE.getCategoryName(),
paletteService.getReferenceById(1L),
Boolean.TRUE,
save
save,
CategoryKind.SCHEDULE
);
Category groupCategory = CategoryConverter.toCategory(
"모임",
CategoryKind.MOIM.getCategoryName(),
paletteService.getReferenceById(4L),
Boolean.TRUE,
save
save,
CategoryKind.MOIM
);

categoryService.create(baseCategory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public enum BaseResponseStatus {

/**
* NOT FOUND 오류
* 404 번오류
* 404
*/
NOT_FOUND_USER_FAILURE(404, "유저를 찾을 수 없습니다."),
NOT_FOUND_SCHEDULE_FAILURE(404, "스케줄을 찾을 수 없습니다."),
Expand All @@ -66,6 +66,11 @@ public enum BaseResponseStatus {
DUPLICATE_PARTICIPATE_FAILURE(404, "이미 가입한 모임입니다."),
DUPLICATE_MOIM_MEMO_FAILURE(404, "이미 모임 메모가 생성되어 있습니다."),

/**
* 404 오용 오류
*/
NOT_USERS_CATEGORY(404, "잘못된 접근 요청입니다.."),

/**
* 404: 인프라 에러
*/
Expand Down
Loading

0 comments on commit a38b20e

Please sign in to comment.