Skip to content

Commit

Permalink
Refactor/#186 group refactor (#189)
Browse files Browse the repository at this point in the history
* refactor: ApiResponse 반환 형식 중복 제거

* refactor: Facade repository 계층 분리

* refactor: group leader 교체 로직 리팩토링

* refactor: group 이름 변경시 기존과 이름이 같아도 문제 없음

* refactor: group 이름 변경 로직 단순화

* refactor: kick group member 리팩토링

* refactor: leader 검증 로직 리팩토링

* refactor: leave group 로직 처리 순서 변경

* refactor: delete group 메서드 리팩토링

* refactor: 불필요한 메세지 로직 제거

* refactor: group pray 생성 로직 정적 팩토리 메서드 패턴으로 변경

* refactor: NotificationLog 생성 로직 정적 팩토리 메서드 패턴으로 변경

* refactor: 불필요한 에러처리 로직 제거

* refactor: 알림 로직 최적화

* refactor: group pray 스크랩 로직 최적화

* refactor: group pray 계층구조 분리

* refactor: group pray response dto 생성 과정 최적화

* refactor: get group pray 트리맵 생성 로직 분리

* refactor: category 조회 로직 분리 및 스크랩 로직 최적화

* refactor: 스크랩 로직 Optional 처리 최적화

* refactor: delete Category 로직 개선

* fix: scrap시 ScrapAndHeart 객체 생성 로직 수정
  • Loading branch information
dong2ast authored Nov 5, 2024
1 parent d27fc94 commit d789f61
Show file tree
Hide file tree
Showing 26 changed files with 427 additions and 364 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public ApiResponseDto<CategoryResponseDto> deleteCategory(
@PathVariable("categoryId") Long categoryId
) {
return ApiResponseDto.success(SuccessStatus.DELETE_CATEGORY_SUCCESS,
prayFacade.deleteCategory(user.getUsername(), categoryId));
prayFacade.deleteCategory(categoryId));
}

@PutMapping("/{categoryId}")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package com.uspray.uspray.domain.category.model;

import com.uspray.uspray.domain.category.dto.CategoryRequestDto;
import com.uspray.uspray.global.enums.CategoryType;
import com.uspray.uspray.global.common.model.AuditingTimeEntity;
import com.uspray.uspray.domain.member.model.Member;
import com.uspray.uspray.domain.pray.model.Pray;
import java.util.List;
import com.uspray.uspray.global.common.model.AuditingTimeEntity;
import com.uspray.uspray.global.enums.CategoryType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
Expand All @@ -15,7 +13,6 @@
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.validation.constraints.NotNull;
import lombok.AccessLevel;
import lombok.Builder;
Expand Down Expand Up @@ -48,9 +45,6 @@ public class Category extends AuditingTimeEntity {
@Enumerated(EnumType.STRING)
private CategoryType categoryType;

@OneToMany(mappedBy = "category")
private List<Pray> prays;

@Builder
public Category(
Long id,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.uspray.uspray.domain.category.repository;

import com.uspray.uspray.global.enums.CategoryType;
import com.uspray.uspray.domain.category.model.Category;
import com.uspray.uspray.domain.category.repository.querydsl.CategoryRepositoryCustom;
import com.uspray.uspray.domain.member.model.Member;
import com.uspray.uspray.global.enums.CategoryType;
import com.uspray.uspray.global.exception.ErrorStatus;
import com.uspray.uspray.global.exception.model.CustomException;
import com.uspray.uspray.global.exception.model.NotFoundException;
import com.uspray.uspray.domain.category.repository.querydsl.CategoryRepositoryCustom;
import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

Expand All @@ -17,26 +17,10 @@ public interface CategoryRepository extends JpaRepository<Category, Long>,

Category getCategoryById(Long categoryId);

List<Category> getCategoriesByMemberOrderByOrder(Member member);

List<Category> getCategoriesByMemberAndCategoryTypeOrderByOrder(Member member,
List<Category> findAllByMemberAndCategoryTypeOrderByOrderAsc(Member member,
CategoryType categoryType);

List<Category> findAllByMemberAndCategoryTypeOrderByOrderAsc(Member member, CategoryType categoryType);

default Category getCategoryByIdAndMember(Long categoryId, Member member) {
Category category = findById(categoryId)
.filter(c -> c.getMember().equals(member))
.orElseThrow(() -> new NotFoundException(
ErrorStatus.CATEGORY_NOT_FOUND_EXCEPTION
));

if (!category.getCategoryType().equals(CategoryType.PERSONAL)) {
throw new CustomException(ErrorStatus.PRAY_CATEGORY_TYPE_MISMATCH);
}

return category;
}
Optional<Category> findByIdAndMemberAndCategoryType(Long categoryId, Member member, CategoryType categoryType);

default void checkDuplicate(String name, Member member, CategoryType type) {
if (existsByNameAndMemberAndCategoryType(name, member, type)) {
Expand All @@ -47,7 +31,7 @@ default void checkDuplicate(String name, Member member, CategoryType type) {

default int checkDuplicateAndReturnMaxOrder(String name, Member member, CategoryType type) {
checkDuplicate(name, member, type);
if (getCategoriesByMemberAndCategoryTypeOrderByOrder(member, type).isEmpty()) {
if (findAllByMemberAndCategoryTypeOrderByOrderAsc(member, type).isEmpty()) {
return 0;
}
return getMaxCategoryOrder(member, type);
Expand All @@ -56,7 +40,7 @@ default int checkDuplicateAndReturnMaxOrder(String name, Member member, Category
boolean existsByNameAndMemberAndCategoryType(String name, Member member, CategoryType type);

default int getMaxCategoryOrder(Member member, CategoryType type) {
Category category = getCategoriesByMemberAndCategoryTypeOrderByOrder(member, type).stream()
Category category = findAllByMemberAndCategoryTypeOrderByOrderAsc(member, type).stream()
.reduce((first, second) -> second)
.orElseThrow(() -> new NotFoundException(ErrorStatus.CATEGORY_NOT_FOUND_EXCEPTION));
return category.getOrder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

import com.uspray.uspray.domain.category.dto.CategoryRequestDto;
import com.uspray.uspray.domain.category.dto.CategoryResponseDto;
import com.uspray.uspray.global.enums.CategoryType;
import com.uspray.uspray.domain.category.model.Category;
import com.uspray.uspray.domain.member.model.Member;
import com.uspray.uspray.global.exception.ErrorStatus;
import com.uspray.uspray.global.exception.model.NotFoundException;
import com.uspray.uspray.domain.category.repository.CategoryRepository;
import com.uspray.uspray.domain.member.model.Member;
import com.uspray.uspray.domain.member.repository.MemberRepository;
import com.uspray.uspray.domain.pray.dto.pray.PrayListResponseDto;
import com.uspray.uspray.domain.pray.repository.PrayRepository;
import com.uspray.uspray.domain.pray.service.PrayFacade;
import com.uspray.uspray.global.enums.CategoryType;
import com.uspray.uspray.global.exception.ErrorStatus;
import com.uspray.uspray.global.exception.model.CustomException;
import com.uspray.uspray.global.exception.model.NotFoundException;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
Expand All @@ -26,10 +27,6 @@ public class CategoryService {
private final PrayRepository prayRepository;
// private final PrayFacade prayFacade;

public Category getCategoryByIdAndMember(Long categoryId, Member member) {
return categoryRepository.getCategoryByIdAndMember(categoryId, member);
}

public List<Category> getCategoryListByMemberAndCategoryType(Member member, CategoryType categoryType) {
return categoryRepository.findAllByMemberAndCategoryTypeOrderByOrderAsc(member, categoryType);
}
Expand Down Expand Up @@ -73,6 +70,21 @@ private static int getMiddlePositionOrder(Category targetCategory, Category curr
return (targetCategory.getOrder() + categories.get(prevIndex).getOrder()) / 2;
}

public Category getCategoryById(Long categoryId) {
return categoryRepository.findById(categoryId).orElseThrow(() -> new CustomException(ErrorStatus.CATEGORY_NOT_FOUND_EXCEPTION));
}

public Category getCategoryByIdAndMemberAndType(Long categoryId, Member member,
CategoryType categoryType) {
return categoryRepository.findByIdAndMemberAndCategoryType(categoryId, member, categoryType)
.orElseThrow(() -> new CustomException(ErrorStatus.PRAY_CATEGORY_TYPE_MISMATCH));
}

public List<PrayListResponseDto> findAllWithOrderAndType(String username, String prayType,
List<Long> prayIds) {
return categoryRepository.findAllWithOrderAndType(username, prayType, prayIds);
}

public CategoryResponseDto createCategory(String username,
CategoryRequestDto categoryRequestDto) {
Member member = memberRepository.getMemberByUserId(username);
Expand All @@ -92,9 +104,11 @@ public CategoryResponseDto deleteCategory(Category category) {
@Transactional
public CategoryResponseDto updateCategory(String username, Long categoryId,
CategoryRequestDto categoryRequestDto) {
Category category = categoryRepository.getCategoryByIdAndMember(categoryId,
memberRepository.getMemberByUserId(username));
if (categoryRequestDto.getName() != null && !categoryRequestDto.getName().equals(category.getName())) {
Category category = categoryRepository.findByIdAndMemberAndCategoryType(categoryId,
memberRepository.getMemberByUserId(username), CategoryType.PERSONAL)
.orElseThrow(() -> new CustomException(ErrorStatus.PRAY_CATEGORY_TYPE_MISMATCH));
if (categoryRequestDto.getName() != null && !categoryRequestDto.getName()
.equals(category.getName())) {
categoryRepository.checkDuplicate(categoryRequestDto.getName(), category.getMember(),
CategoryType.valueOf(categoryRequestDto.getType().toUpperCase()));
}
Expand All @@ -103,16 +117,19 @@ public CategoryResponseDto updateCategory(String username, Long categoryId,
}

public CategoryResponseDto getCategory(String username, Long categoryId) {
Category category = categoryRepository.getCategoryByIdAndMember(categoryId,
memberRepository.getMemberByUserId(username));
Category category = categoryRepository.findByIdAndMemberAndCategoryType(categoryId,
memberRepository.getMemberByUserId(username), CategoryType.PERSONAL)
.orElseThrow(() -> new CustomException(ErrorStatus.PRAY_CATEGORY_TYPE_MISMATCH));
return CategoryResponseDto.of(category);
}

@Transactional
public CategoryResponseDto updateCategoryOrder(String username, Long categoryId, int index) {
Member member = memberRepository.getMemberByUserId(username);
Category category = categoryRepository.getCategoryByIdAndMember(categoryId, member);
List<Category> categories = categoryRepository.getCategoriesByMemberAndCategoryTypeOrderByOrder(
Category category = categoryRepository.findByIdAndMemberAndCategoryType(categoryId, member,
CategoryType.PERSONAL)
.orElseThrow(() -> new CustomException(ErrorStatus.PRAY_CATEGORY_TYPE_MISMATCH));
List<Category> categories = categoryRepository.findAllByMemberAndCategoryTypeOrderByOrderAsc(
member, category.getCategoryType());

validateIndex(index, categories.size());
Expand All @@ -128,7 +145,7 @@ public CategoryResponseDto updateCategoryOrder(String username, Long categoryId,
public List<CategoryResponseDto> getCategoryList(String username, String categoryType) {
Member member = memberRepository.getMemberByUserId(username);
CategoryType convertCategoryType = CategoryType.valueOf(categoryType.toUpperCase());
List<Category> categories = categoryRepository.getCategoriesByMemberAndCategoryTypeOrderByOrder(
List<Category> categories = categoryRepository.findAllByMemberAndCategoryTypeOrderByOrderAsc(
member, convertCategoryType);
return categories.stream()
.map(CategoryResponseDto::of)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import com.uspray.uspray.domain.group.service.GroupService;
import com.uspray.uspray.global.common.dto.ApiResponseDto;
import com.uspray.uspray.global.exception.SuccessStatus;
import io.swagger.v3.oas.annotations.Parameter;
import java.util.List;
import javax.validation.Valid;
import lombok.RequiredArgsConstructor;
Expand All @@ -34,7 +33,7 @@ public class GroupController implements GroupApi {

@GetMapping
public ApiResponseDto<GroupListResponseDto> getGroupList(
@Parameter(hidden = true) @AuthenticationPrincipal User user) {
@AuthenticationPrincipal User user) {
return ApiResponseDto.success(SuccessStatus.GET_GROUP_LIST_SUCCESS,
groupService.getGroupList(user.getUsername()));
}
Expand All @@ -44,8 +43,7 @@ public ApiResponseDto<?> createGroup(
@AuthenticationPrincipal User user,
@Valid @RequestBody GroupRequestDto groupRequestDto) {
groupFacade.createGroup(user.getUsername(), groupRequestDto);
return ApiResponseDto.success(SuccessStatus.CREATE_GROUP_SUCCESS,
SuccessStatus.CREATE_GROUP_SUCCESS.getMessage());
return ApiResponseDto.success(SuccessStatus.CREATE_GROUP_SUCCESS);
}

@PutMapping("/{groupId}/change-name")
Expand All @@ -54,8 +52,7 @@ public ApiResponseDto<?> changeGroupName(
@PathVariable Long groupId,
@Valid @RequestBody GroupRequestDto groupRequestDto) {
groupFacade.changeGroupName(user.getUsername(), groupId, groupRequestDto);
return ApiResponseDto.success(SuccessStatus.CHANGE_GROUP_NAME_SUCCESS,
SuccessStatus.CHANGE_GROUP_NAME_SUCCESS.getMessage());
return ApiResponseDto.success(SuccessStatus.CHANGE_GROUP_NAME_SUCCESS);
}

@PutMapping("/{groupId}/change-leader")
Expand All @@ -65,8 +62,7 @@ public ApiResponseDto<?> changeGroupLeader(
@Valid @RequestBody GroupMemberRequestDto groupLeaderRequestDto) {
groupFacade.changeGroupLeader(user.getUsername(), groupId,
groupLeaderRequestDto.getMemberId());
return ApiResponseDto.success(SuccessStatus.CHANGE_GROUP_LEADER_SUCCESS,
SuccessStatus.CHANGE_GROUP_LEADER_SUCCESS.getMessage());
return ApiResponseDto.success(SuccessStatus.CHANGE_GROUP_LEADER_SUCCESS);
}

@DeleteMapping("/{groupId}/kick")
Expand All @@ -76,35 +72,31 @@ public ApiResponseDto<?> kickGroupMember(
@Valid @RequestBody GroupMemberRequestDto groupMemberRequestDto) {
groupFacade.kickGroupMember(user.getUsername(), groupId,
groupMemberRequestDto.getMemberId());
return ApiResponseDto.success(SuccessStatus.KICK_GROUP_MEMBER_SUCCESS,
SuccessStatus.KICK_GROUP_MEMBER_SUCCESS.getMessage());
return ApiResponseDto.success(SuccessStatus.KICK_GROUP_MEMBER_SUCCESS);
}

@PostMapping("/{groupId}/join")
public ApiResponseDto<?> addGroupMember(
@AuthenticationPrincipal User user,
@PathVariable Long groupId) {
groupFacade.addGroupMember(user.getUsername(), groupId);
return ApiResponseDto.success(SuccessStatus.ADD_GROUP_MEMBER_SUCCESS,
SuccessStatus.ADD_GROUP_MEMBER_SUCCESS.getMessage());
return ApiResponseDto.success(SuccessStatus.ADD_GROUP_MEMBER_SUCCESS);
}

@DeleteMapping("/{groupId}/leave")
public ApiResponseDto<?> leaveGroup(
@AuthenticationPrincipal User user,
@PathVariable Long groupId) {
groupFacade.leaveGroup(user.getUsername(), groupId);
return ApiResponseDto.success(SuccessStatus.LEAVE_GROUP_SUCCESS,
SuccessStatus.LEAVE_GROUP_SUCCESS.getMessage());
return ApiResponseDto.success(SuccessStatus.LEAVE_GROUP_SUCCESS);
}

@DeleteMapping("/{groupId}")
public ApiResponseDto<?> deleteGroup(
@Parameter(hidden = true) @AuthenticationPrincipal User user,
@AuthenticationPrincipal User user,
@PathVariable Long groupId) {
groupFacade.deleteGroup(user.getUsername(), groupId);
return ApiResponseDto.success(SuccessStatus.DELETE_GROUP_SUCCESS,
SuccessStatus.DELETE_GROUP_SUCCESS.getMessage());
return ApiResponseDto.success(SuccessStatus.DELETE_GROUP_SUCCESS);
}

@GetMapping("/{groupId}/member/search")
Expand All @@ -121,7 +113,6 @@ public ApiResponseDto<?> changeGroupNotification(
@AuthenticationPrincipal User user,
@PathVariable Long groupId) {
groupFacade.changeGroupNotification(user.getUsername(), groupId);
return ApiResponseDto.success(SuccessStatus.CHANGE_GROUP_NOTIFICATION_SUCCESS,
SuccessStatus.CHANGE_GROUP_NOTIFICATION_SUCCESS.getMessage());
return ApiResponseDto.success(SuccessStatus.CHANGE_GROUP_NOTIFICATION_SUCCESS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.io.IOException;
import java.util.List;
import org.springframework.security.core.userdetails.User;

Expand Down Expand Up @@ -70,12 +71,12 @@ ApiResponseDto<GroupPrayRappingDto> getGroupPray(
responseCode = "200",
description = "모임 기도제목 좋아요")
ApiResponseDto<?> likeGroupPray(Long id,
@Parameter(hidden = true) User user);
@Parameter(hidden = true) User user) throws IOException;

@Operation(summary = "모임 기도제목 스크랩")
@ApiResponse(
responseCode = "200",
description = "모임 기도제목 스크랩")
ApiResponseDto<?> scarpGroupPray(ScrapRequestDto scrapRequestDto,
@Parameter(hidden = true) User user);
@Parameter(hidden = true) User user) throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.uspray.uspray.domain.pray.dto.pray.request.PrayToGroupPrayDto;
import com.uspray.uspray.global.common.dto.ApiResponseDto;
import com.uspray.uspray.global.exception.SuccessStatus;
import java.io.IOException;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
Expand Down Expand Up @@ -52,8 +53,7 @@ public ApiResponseDto<?> prayToGroupPray(
public ApiResponseDto<?> createGroupPray(@RequestBody GroupPrayRequestDto groupPrayRequestDto,
@AuthenticationPrincipal User user) {
groupPrayFacade.createGroupPray(groupPrayRequestDto, user.getUsername());
return ApiResponseDto.success(SuccessStatus.CREATE_GROUP_PRAY_SUCCESS,
SuccessStatus.CREATE_GROUP_PRAY_SUCCESS.getMessage());
return ApiResponseDto.success(SuccessStatus.CREATE_GROUP_PRAY_SUCCESS);
}

@GetMapping("/{groupId}")
Expand All @@ -67,23 +67,20 @@ public ApiResponseDto<GroupPrayRappingDto> getGroupPray(
@DeleteMapping("/{groupPrayId}")
public ApiResponseDto<?> deleteGroupPray(@PathVariable(name = "groupPrayId") Long id) {
groupPrayService.deleteGroupPray(id);
return ApiResponseDto.success(SuccessStatus.DELETE_GROUP_PRAY_SUCCESS,
SuccessStatus.DELETE_GROUP_PRAY_SUCCESS.getMessage());
return ApiResponseDto.success(SuccessStatus.DELETE_GROUP_PRAY_SUCCESS);
}

@PostMapping("/{groupPrayId}/like")
public ApiResponseDto<?> likeGroupPray(@PathVariable(name = "groupPrayId") Long id,
@AuthenticationPrincipal User user) {
@AuthenticationPrincipal User user) throws IOException {
groupPrayFacade.heartGroupPray(id, user.getUsername());
return ApiResponseDto.success(SuccessStatus.LIKE_GROUP_PRAY_SUCCESS,
SuccessStatus.LIKE_GROUP_PRAY_SUCCESS.getMessage());
return ApiResponseDto.success(SuccessStatus.LIKE_GROUP_PRAY_SUCCESS);
}

@PostMapping("/scrap")
public ApiResponseDto<?> scarpGroupPray(@RequestBody ScrapRequestDto scrapRequestDto,
@AuthenticationPrincipal User user) {
@AuthenticationPrincipal User user) throws IOException {
groupPrayFacade.scrapGroupPray(scrapRequestDto, user.getUsername());
return ApiResponseDto.success(SuccessStatus.SCARP_GROUP_PRAY_SUCCESS,
SuccessStatus.SCARP_GROUP_PRAY_SUCCESS.getMessage());
return ApiResponseDto.success(SuccessStatus.SCARP_GROUP_PRAY_SUCCESS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public GroupPrayResponseDto(GroupPray groupPray, Member member, ScrapAndHeart sc

private void setSC(ScrapAndHeart scrapAndHeart) {
if (scrapAndHeart != null) {
this.heart = scrapAndHeart.isHeart();
this.scrap = scrapAndHeart.isScrap();
this.heart = scrapAndHeart.getHeart();
this.scrap = scrapAndHeart.getScrap();
}
}

Expand Down
Loading

0 comments on commit d789f61

Please sign in to comment.