Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: 회원별 카테고리 생성/조회/삭제 #254

Merged
merged 3 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.codiary.backend.domain.category.controller;

import com.codiary.backend.domain.category.converter.CategoryConverter;
import com.codiary.backend.domain.category.dto.CategoryResponseDTO;
import com.codiary.backend.domain.category.service.CategoryService;
import com.codiary.backend.domain.member.entity.MemberCategory;
import com.codiary.backend.domain.member.security.CustomMemberDetails;
import com.codiary.backend.global.apiPayload.ApiResponse;
import com.codiary.backend.global.apiPayload.code.status.SuccessStatus;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v2/category")
@Tag(name = "카테고리 API", description = "카테고리 생성/수정/삭제 API입니다.")
public class CategoryController {

private final CategoryService categoryService;

@PostMapping("")
@Operation(summary = "멤버 카테고리 생성", description = "멤버의 카테고리를 생성합니다.")
public ApiResponse<CategoryResponseDTO.MemberCategoryDTO> createCategory(@RequestParam(value = "category_name") String categoryName, @AuthenticationPrincipal CustomMemberDetails customMemberDetails) {
MemberCategory memberCategory = categoryService.createCategory(categoryName, customMemberDetails.getId());
return ApiResponse.onSuccess(SuccessStatus.CATEGORY_OK, CategoryConverter.toMemberCategoryDTO(memberCategory));
}

@GetMapping("")
@Operation(summary = "멤버 카테고리 조회", description = "멤버의 카테고리를 조회합니다.")
public ApiResponse<List<CategoryResponseDTO.MemberCategoryDTO>> getCategoryList(@AuthenticationPrincipal CustomMemberDetails customMemberDetails) {
List<MemberCategory> memberCategoryList = categoryService.getCategoryList(customMemberDetails.getId());
return ApiResponse.onSuccess(SuccessStatus.CATEGORY_OK, CategoryConverter.toMemberCategoryListDTO(memberCategoryList));
}

@DeleteMapping("/{member_category_id}")
@Operation(summary = "멤버 카테고리 삭제", description = "멤버의 카테고리를 삭제합니다.")
public ApiResponse<String> deleteCategory(@PathVariable("member_category_id") Long memberCategoryId, @AuthenticationPrincipal CustomMemberDetails customMemberDetails) {
categoryService.deleteCategory(memberCategoryId, customMemberDetails.getId());
return ApiResponse.onSuccess(SuccessStatus.CATEGORY_OK, "카테고리 삭제가 완료되었습니다.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.codiary.backend.domain.category.converter;

import com.codiary.backend.domain.category.dto.CategoryResponseDTO;
import com.codiary.backend.domain.member.entity.MemberCategory;

import java.util.List;

public class CategoryConverter {
public static CategoryResponseDTO.MemberCategoryDTO toMemberCategoryDTO(MemberCategory memberCategory) {
return CategoryResponseDTO.MemberCategoryDTO.builder()
.memberCategoryId(memberCategory.getMemberCategoryId())
.categoryName(memberCategory.getCategory().getName())
.categoryId(memberCategory.getCategory().getCategoryId())
.build();
}

public static List<CategoryResponseDTO.MemberCategoryDTO> toMemberCategoryListDTO(List<MemberCategory> memberCategoryList) {
return memberCategoryList.stream()
.map(CategoryConverter::toMemberCategoryDTO)
.toList();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.codiary.backend.domain.category.dto;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import lombok.Builder;

public class CategoryResponseDTO {

@Builder
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
@JsonInclude(JsonInclude.Include.NON_NULL)
public record MemberCategoryDTO(
Long memberCategoryId,
String categoryName,
Long categoryId
) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Categories {
public class Category {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "category_id", nullable = false, columnDefinition = "bigint")
Expand All @@ -23,6 +23,11 @@ public class Categories {
@ManyToMany(mappedBy = "categoriesList")
private List<Post> posts = new ArrayList<>();

@OneToMany(mappedBy = "categories", cascade = CascadeType.ALL, orphanRemoval = true)
@OneToMany(mappedBy = "category", cascade = CascadeType.ALL, orphanRemoval = true)
private List<MemberCategory> memberCategoryList = new ArrayList<>();

@Builder
public Category(String name) {
this.name = name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.codiary.backend.domain.category.repository;

import com.codiary.backend.domain.category.entity.Category;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface CategoryRepository extends JpaRepository<Category, Long> {
Optional<Category> findByName(String name);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.codiary.backend.domain.category.service;

import com.codiary.backend.domain.category.entity.Category;
import com.codiary.backend.domain.category.repository.CategoryRepository;
import com.codiary.backend.domain.member.entity.Member;
import com.codiary.backend.domain.member.entity.MemberCategory;
import com.codiary.backend.domain.member.repository.MemberCategoryRepository;
import com.codiary.backend.domain.member.repository.MemberRepository;
import com.codiary.backend.global.apiPayload.code.status.ErrorStatus;
import com.codiary.backend.global.apiPayload.exception.GeneralException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
public class CategoryService {

private final CategoryRepository categoryRepository;
private final MemberRepository memberRepository;
private final MemberCategoryRepository memberCategoryRepository;

@Transactional
public MemberCategory createCategory(String categoryName, Long memberId) {
//validation: 멤버인지, 카테고리 이름이 이미 존재하는지 확인, 멤버가 이미 카테고리 설정했는지 확인
Category category = categoryRepository.findByName(categoryName)
.orElseGet(() ->categoryRepository.save(
Category.builder()
.name(categoryName)
.build()
));

Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new GeneralException(ErrorStatus.MEMBER_NOT_FOUND));

if(member.getMemberCategoryList().stream().anyMatch(memberCategory -> memberCategory.getCategory().getName().equals(categoryName))) {
throw new GeneralException(ErrorStatus.CATEGORY_ALREADY_EXISTS);
}

//business logic: 카테고리 생성
MemberCategory memberCategory = MemberCategory.builder()
.member(member)
.category(category)
.build();

//response: 카테고리 반환
return memberCategoryRepository.save(memberCategory);
}

public List<MemberCategory> getCategoryList(Long memberId){
//validation: 멤버인지 확인
Member member = memberRepository.findByIdWithCategory(memberId)
.orElseThrow(() -> new GeneralException(ErrorStatus.MEMBER_NOT_FOUND));

//business logic & return: 카테고리 조회
return member.getMemberCategoryList();
}

@Transactional
public void deleteCategory(Long memberCategoryId, Long memberId) {
// validation: 멤버인지, 카테고리가 존재하는지 확인
Member member = memberRepository.findByIdWithCategory(memberId)
.orElseThrow(() -> new GeneralException(ErrorStatus.MEMBER_NOT_FOUND));

MemberCategory memberCategory = memberCategoryRepository.findByMemberCategoryIdAndMember(memberCategoryId, member)
.orElseThrow(() -> new GeneralException(ErrorStatus.CATEGORY_NOT_FOUND));

// business logic: 카테고리 삭제
memberCategory.getMember().getMemberCategoryList().remove(memberCategory);
memberCategory.getCategory().getMemberCategoryList().remove(memberCategory);
memberCategoryRepository.delete(memberCategory);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.codiary.backend.domain.member.entity;

import com.codiary.backend.domain.category.entity.Category;
import com.codiary.backend.global.common.BaseEntity;
import com.codiary.backend.domain.category.entity.Categories;
import jakarta.persistence.*;
import lombok.*;

Expand All @@ -21,15 +21,11 @@ public class MemberCategory extends BaseEntity {

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "category_id")
private Categories categories;

public void setMember(Member member) {
if (this.member != null) {
member.getMemberCategoryList().remove(this);
}
private Category category;

@Builder
public MemberCategory(Member member, Category category) {
this.member = member;

member.getMemberCategoryList().add(this);
this.category = category;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.codiary.backend.domain.member.repository;

import com.codiary.backend.domain.member.entity.Member;
import com.codiary.backend.domain.member.entity.MemberCategory;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface MemberCategoryRepository extends JpaRepository<MemberCategory, Long> {
Optional<MemberCategory> findByMemberCategoryIdAndMember(Long memberCategoryId, Member member);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ public interface MemberRepositoryCustom {

Optional<Member> findByIdWithFollowings(Long id);
Optional<Member> findByIdWithFollowers(Long id);
Optional<Member> findByIdWithCategory(Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import java.util.List;
import java.util.Optional;

import static com.codiary.backend.domain.category.entity.QCategory.category;
import static com.codiary.backend.domain.member.entity.QMember.member;
import static com.codiary.backend.domain.member.entity.QMemberCategory.memberCategory;
import static com.codiary.backend.domain.techstack.entity.QTechStacks.techStacks;
import static com.codiary.backend.domain.member.entity.QMemberProjectMap.memberProjectMap;
import static com.codiary.backend.domain.project.entity.QProject.project;
Expand Down Expand Up @@ -104,4 +106,15 @@ private void fetchTeamMembers(Long userId, Member fetchedMember) {

fetchedMember.setTeamMemberList(teamMembers);
}

public Optional<Member> findByIdWithCategory(Long id) {
Member fetchedMember = queryFactory
.selectFrom(member)
.leftJoin(member.memberCategoryList, memberCategory).fetchJoin()
.leftJoin(memberCategory.category, category).fetchJoin()
.where(member.memberId.eq(id))
.fetchOne();

return Optional.ofNullable(fetchedMember);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.codiary.backend.domain.post.entity;

import com.codiary.backend.domain.category.entity.Category;
import com.codiary.backend.domain.comment.entity.Comment;
import com.codiary.backend.domain.member.entity.Member;
import com.codiary.backend.global.common.BaseEntity;
import com.codiary.backend.domain.project.entity.Project;
import com.codiary.backend.domain.team.entity.Team;
import com.codiary.backend.domain.coauthor.entity.Authors;
import com.codiary.backend.domain.category.entity.Categories;
import com.codiary.backend.domain.post.enumerate.PostAccess;
import jakarta.persistence.*;
import lombok.*;
Expand Down Expand Up @@ -58,7 +58,7 @@ public class Post extends BaseEntity {
joinColumns = @JoinColumn(name = "post_id"),
inverseJoinColumns = @JoinColumn(name = "category_id")
)
private List<Categories> categoriesList = new ArrayList<>();
private List<Category> categoriesList = new ArrayList<>();

@OneToMany(mappedBy = "post", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
private List<PostFile> postFileList = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public enum ErrorStatus implements BaseErrorCode {

// 카테고리 관련 에러 7000
CATEGORY_NOT_FOUND(HttpStatus.BAD_REQUEST, "CATEGORY_7001", "카테고리가 없습니다."),
CATEGORY_ALREADY_EXISTS(HttpStatus.CONFLICT, "CATEGORY_7002", "이미 존재하는 카테고리입니다."),

// 회원별 관심 카테고리 관련 에러 8000
MEMBERCATEGORY_NOT_FOUND(HttpStatus.BAD_REQUEST, "MEMBERCATEGORY_8001", "회원별 관심 카테고리가 없습니다."),

Expand Down