Skip to content

Commit

Permalink
Merge pull request #139 from Team-GAJI/develop
Browse files Browse the repository at this point in the history
배포 서버 머지
  • Loading branch information
ShimFFF authored Aug 21, 2024
2 parents b1369cf + 47ccda2 commit 893eeee
Show file tree
Hide file tree
Showing 70 changed files with 915 additions and 407 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# Server
> 스터디 관리 서비스 "가지" 서버
> 스터디 관리 서비스 "가지" 서버입니다
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import gaji.service.domain.common.web.dto.CategoryResponseDTO;
import gaji.service.domain.enums.CategoryEnum;
import gaji.service.domain.enums.PostTypeEnum;
import gaji.service.global.exception.RestApiException;
import gaji.service.global.exception.code.status.GlobalErrorStatus;
import org.springframework.core.convert.converter.Converter;

import java.util.List;
Expand All @@ -15,7 +17,11 @@ public class CategoryConverter implements Converter<String, CategoryEnum> {
// @RequestParam으로 String->CategoryEnum으로 convert할 때 필요한 메서드
@Override
public CategoryEnum convert(String source) {
return CategoryEnum.from(source);
CategoryEnum category = CategoryEnum.from(source);
if (category == null) {
throw new RestApiException(GlobalErrorStatus._INVALID_CATEGORY);
}
return category;
}

public static Category toCategory(CategoryEnum category) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
import gaji.service.domain.enums.CategoryEnum;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface CategoryRepository extends JpaRepository<Category, Long> {
Category findByCategory(CategoryEnum category);
boolean existsByCategory(CategoryEnum category);
boolean existsById(Long id);
List<Category> findAllByCategory(CategoryEnum category);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
import java.util.List;

public interface SelectCategoryQueryDslRepository {
List<SelectCategory> findAllFetchJoinWithCategoryByEntityIdAndPostType(Long entityId, PostTypeEnum postType);
SelectCategory findOneFetchJoinWithCategoryByEntityIdAndPostType(Long entityId, PostTypeEnum postType);
List<Long> findEntityIdListByCategoryAndPostType(Category category, PostTypeEnum postType);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class SelectCategoryQueryDslRepositoryImpl implements SelectCategoryQuery
private final JPAQueryFactory jpaQueryFactory;

@Override
public List<SelectCategory> findAllFetchJoinWithCategoryByEntityIdAndPostType(Long entityId, PostTypeEnum postType) {
public SelectCategory findOneFetchJoinWithCategoryByEntityIdAndPostType(Long entityId, PostTypeEnum postType) {
return jpaQueryFactory
.selectFrom(selectCategory)
.join(selectCategory.category, category1)
Expand All @@ -29,8 +29,8 @@ public List<SelectCategory> findAllFetchJoinWithCategoryByEntityIdAndPostType(Lo
selectCategory.entityId.eq(entityId),
selectCategory.type.eq(postType)
)
.orderBy(selectCategory.id.asc())
.fetch();
.fetchOne()
;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ public interface CategoryService {
List<Long> findEntityIdListByCategoryIdAndPostType(Long categoryId, PostTypeEnum postType);
boolean existsByCategory(CategoryEnum category);
boolean existsByCategoryId(Long categoryId);
List<SelectCategory> findAllFetchJoinWithCategoryByEntityIdAndPostType(Long entityId, PostTypeEnum postType);
SelectCategory findOneFetchJoinWithCategoryByEntityIdAndPostType(Long entityId, PostTypeEnum postType);
List<CategoryResponseDTO.BaseDTO> findAllCategory();
void saveAllSelectCategory(List<SelectCategory> selectCategoryList);

SelectCategory findByEntityIdAndType(Long entityId, PostTypeEnum type);

List<Category> findAllByCategory(CategoryEnum category);

}

Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ public Category findByCategory(CategoryEnum category) {
return categoryRepository.findByCategory(category);
}

//todo: 나중에 카테고리 DB에 통일 하면 지워야함
@Override
public List<Category> findAllByCategory(CategoryEnum category) {
return categoryRepository.findAllByCategory(category);
}

@Override
public Category findByCategoryId(Long categoryId) {
return categoryRepository.findById(categoryId)
Expand All @@ -61,8 +67,8 @@ public boolean existsByCategoryId(Long categoryId) {
}

@Override
public List<SelectCategory> findAllFetchJoinWithCategoryByEntityIdAndPostType(Long entityId, PostTypeEnum postType) {
return selectCategoryRepository.findAllFetchJoinWithCategoryByEntityIdAndPostType(entityId, postType);
public SelectCategory findOneFetchJoinWithCategoryByEntityIdAndPostType(Long entityId, PostTypeEnum postType) {
return selectCategoryRepository.findOneFetchJoinWithCategoryByEntityIdAndPostType(entityId, postType);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

import gaji.service.domain.common.annotation.ExistsCategory;
import gaji.service.domain.common.service.CategoryService;
import gaji.service.domain.enums.CategoryEnum;
import gaji.service.global.exception.code.status.GlobalErrorStatus;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@RequiredArgsConstructor
public class CategoryExistsValidator implements ConstraintValidator<ExistsCategory, List<Long>> {
public class CategoryExistsValidator implements ConstraintValidator<ExistsCategory, String> {
private final CategoryService categoryService;

@Override
Expand All @@ -21,9 +21,8 @@ public void initialize(ExistsCategory constraintAnnotation) {
}

@Override
public boolean isValid(List<Long> values, ConstraintValidatorContext context) {
boolean isValid = values.stream()
.allMatch(value -> categoryService.existsByCategoryId(value));
public boolean isValid(String value, ConstraintValidatorContext context) {
boolean isValid = categoryService.existsByCategory(CategoryEnum.from(value));

if (!isValid) {
context.disableDefaultConstraintViolation();
Expand Down
16 changes: 13 additions & 3 deletions src/main/java/gaji/service/domain/enums/CategoryEnum.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ public enum CategoryEnum {
AI("인공지능"),
HW("하드웨어"),
SECURITY("보안"),
NETWORK("클라우드 네트워크"),
NETWORK("네트워크-클라우드"),
LANGUAGE("어학"),
DESIGN("디자인"),
BUSINESS("비즈니스"),
BOOK("독서 모임");
BOOK("독서"),
ETC("기타");

@JsonValue
private final String value;
Expand All @@ -35,6 +36,15 @@ public static CategoryEnum from(String param) {
}
}
log.error("CategoryEnum.from() exception occur param: {}", param);
throw new RestApiException(GlobalErrorStatus._INVALID_CATEGORY);
return null;
}

public static CategoryEnum fromValue(String value) {
for (CategoryEnum category : CategoryEnum.values()) {
if (category.value.equals(value)) {
return category;
}
}
throw new IllegalArgumentException("Unknown category: " + value);
}
}
Original file line number Diff line number Diff line change
@@ -1,37 +1,47 @@
package gaji.service.domain.post.converter;

import gaji.service.domain.post.entity.CommunityComment;
import gaji.service.domain.post.service.CommunityCommentService;
import gaji.service.domain.post.web.dto.CommunityPostCommentResponseDTO;
import gaji.service.global.converter.DateConverter;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Component
public class CommunityCommentConverter {
private final CommunityCommentService communityCommentService;

public static CommunityPostCommentResponseDTO.WriteCommentDTO toWriteCommentDTO(CommunityComment comment) {
return CommunityPostCommentResponseDTO.WriteCommentDTO.builder()
public static CommunityPostCommentResponseDTO.WriteCommentResponseDTO toWriteCommentResponseDTO(CommunityComment comment) {
return CommunityPostCommentResponseDTO.WriteCommentResponseDTO.builder()
.commentId(comment.getId())
.build();
}

public static CommunityPostCommentResponseDTO.PostCommentDTO toPostCommentDTO(CommunityComment comment) {
public static CommunityPostCommentResponseDTO.PostCommentDTO toPostCommentDTO(CommunityComment comment, boolean isWriter) {
return CommunityPostCommentResponseDTO.PostCommentDTO.builder()
.commentId(comment.getId())
.userId(comment.getUser().getId())
.username(comment.getUser().getName())
.body(comment.getBody())
.groupNum(comment.getGroupNum())
.depth(comment.getDepth())
.isWriter(isWriter)
.createdAt(DateConverter.convertWriteTimeFormat(LocalDate.from(comment.getCreatedAt()), " 작성"))
.build();
}

public static CommunityPostCommentResponseDTO.PostCommentListDTO toPostCommentListDTO(List<CommunityComment> commentList, boolean hasNext) {
List<CommunityPostCommentResponseDTO.PostCommentDTO> postCommentDTOList = commentList.stream()
.map(CommunityCommentConverter::toPostCommentDTO)
.collect(Collectors.toList());
public CommunityPostCommentResponseDTO.PostCommentListDTO toPostCommentListDTO(List<CommunityComment> commentList, boolean hasNext, Long userId) {
List<CommunityPostCommentResponseDTO.PostCommentDTO> postCommentDTOList = new ArrayList<>();

for (CommunityComment communityComment : commentList) {
boolean isWriter = (userId == null) ? false : communityCommentService.isCommentWriter(userId, communityComment);
postCommentDTOList.add(CommunityCommentConverter.toPostCommentDTO(communityComment, isWriter));
}

return CommunityPostCommentResponseDTO.PostCommentListDTO.builder()
.commentList(postCommentDTOList)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package gaji.service.domain.post.converter;

import gaji.service.domain.common.converter.HashtagConverter;
import gaji.service.domain.common.entity.Category;
import gaji.service.domain.common.entity.SelectCategory;
import gaji.service.domain.common.entity.SelectHashtag;
import gaji.service.domain.common.service.CategoryService;
import gaji.service.domain.common.service.HashtagService;
import gaji.service.domain.common.web.dto.HashtagResponseDTO;
import gaji.service.domain.enums.CategoryEnum;
import gaji.service.domain.enums.PostStatusEnum;
import gaji.service.domain.enums.PostTypeEnum;
import gaji.service.domain.post.entity.CommnuityPost;
Expand All @@ -12,32 +16,36 @@
import gaji.service.domain.post.entity.PostLikes;
import gaji.service.domain.post.service.CommunityPostBookMarkService;
import gaji.service.domain.post.service.CommunityPostLikesService;
import gaji.service.domain.post.service.CommunityPostQueryService;
import gaji.service.domain.post.web.dto.CommunityPostResponseDTO;
import gaji.service.domain.post.web.dto.PostRequestDTO;
import gaji.service.domain.post.web.dto.CommunityPostRequestDTO;
import gaji.service.domain.user.entity.User;
import gaji.service.global.converter.DateConverter;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Component
public class CommunityPostConverter {
private final HashtagService hashtagService;
private final CategoryService categoryService;
private final CommunityPostBookMarkService postBookMarkService;
private final CommunityPostLikesService postLikesService;
private final CommunityPostQueryService communityPostQueryService;

// 초기 PostStatus 지정
public static PostStatusEnum getInitialPostStatus(PostTypeEnum type) {
return (type == PostTypeEnum.QUESTION) ? PostStatusEnum.NEED_RESOLUTION :
(type == PostTypeEnum.PROJECT) ? PostStatusEnum.RECRUITING : PostStatusEnum.BLOGING;
}

public static CommunityPostResponseDTO.UploadPostDTO toUploadPostDTO(CommnuityPost post) {
return CommunityPostResponseDTO.UploadPostDTO
public static CommunityPostResponseDTO.UploadPostResponseDTO toUploadPostResponseDTO(CommnuityPost post) {
return CommunityPostResponseDTO.UploadPostResponseDTO
.builder()
.postId(post.getId())
.build();
Expand All @@ -57,7 +65,7 @@ public static CommunityPostResponseDTO.PostLikesIdDTO toPostLikesIdDTO(PostLikes
.build();
}

public static CommnuityPost toPost(PostRequestDTO.UploadPostDTO request, User user) {
public static CommnuityPost toPost(CommunityPostRequestDTO.UploadPostRequestDTO request, User user) {
return CommnuityPost.builder()
.user(user)
.title(request.getTitle())
Expand All @@ -69,7 +77,7 @@ public static CommnuityPost toPost(PostRequestDTO.UploadPostDTO request, User us
.build();
}

public static CommunityComment toComment(PostRequestDTO.WriteCommentDTO request, User user, CommnuityPost post, CommunityComment parentComment) {
public static CommunityComment toComment(CommunityPostRequestDTO.WriteCommentRequestDTO request, User user, CommnuityPost post, CommunityComment parentComment) {
return CommunityComment.builder()
.user(user)
.post(post)
Expand Down Expand Up @@ -107,12 +115,13 @@ public CommunityPostResponseDTO.PostPreviewDTO toPostPreviewDTO(CommnuityPost po
.uploadTime(DateConverter.convertToRelativeTimeFormat(post.getCreatedAt()))
.hit(post.getHit())
.popularityScore(post.getPopularityScore())
.status(post.getStatus())
.hashtagList(hashtagList)
.build();
}

public CommunityPostResponseDTO.PostPreviewListDTO toPostPreviewListDTO(List<CommnuityPost> postList, boolean hasNext) {
CommunityPostConverter postConverter = new CommunityPostConverter(hashtagService, postBookMarkService, postLikesService);
CommunityPostConverter postConverter = new CommunityPostConverter(hashtagService, categoryService, postBookMarkService, postLikesService, communityPostQueryService);
List<CommunityPostResponseDTO.PostPreviewDTO> postPreviewDTOList = postList.stream()
.map(postConverter::toPostPreviewDTO)
.collect(Collectors.toList());
Expand All @@ -123,24 +132,37 @@ public CommunityPostResponseDTO.PostPreviewListDTO toPostPreviewListDTO(List<Com
.build();
}

public CommunityPostResponseDTO.PostDetailDTO toPostDetailDTO(CommnuityPost post, Long userId) {
public CommunityPostResponseDTO.PostDetailDTO toPostDetailDTO(CommnuityPost post, Long userId, SelectCategory selectCategory) {
List<SelectHashtag> selectHashtagList = hashtagService.findAllFetchJoinWithHashtagByEntityIdAndPostType(post.getId(), post.getType());
List<HashtagResponseDTO.HashtagNameAndIdDTO> hashtagNameAndIdDTOList = HashtagConverter.toHashtagNameAndIdDTOList(selectHashtagList);

// ofNullable 메서드로 NPE 방지
/*CategoryEnum category = Optional.ofNullable(categoryService.findOneFetchJoinWithCategoryByEntityIdAndPostType(post.getId(), post.getType()))
.map(SelectCategory::getCategory)
.map(Category::getCategory)
.orElse(null);*/

boolean isBookmarked = (userId == null) ? false : postBookMarkService.existsByUserAndPost(userId, post);
boolean isLiked = (userId == null) ? false : postLikesService.existsByUserAndPost(userId, post);
boolean isWriter = (userId == null) ? false : communityPostQueryService.isPostWriter(userId, post);

return CommunityPostResponseDTO.PostDetailDTO.builder()
.userId(post.getUser().getId())
.type(post.getType())
.createdAt(DateConverter.convertWriteTimeFormat(LocalDate.from(post.getCreatedAt()), ""))
.hit(post.getHit())
.likeCnt(post.getLikeCnt())
.bookmarkCnt(post.getBookmarkCnt())
.commentCnt(post.getCommentCnt())
.userNickname(post.getUser().getNickname())
.title(post.getTitle())
.hashtagList(hashtagNameAndIdDTOList)
.isBookMarked(isBookmarked)
.isLiked(isLiked)
.isWriter(isWriter)
.bookMarkStatus(isBookmarked)
.likeStatus(isLiked)
.body(post.getBody())
.status(post.getStatus())
.category(selectCategory.getCategory().getCategory().getValue())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ public class PostStatusConverter implements Converter<String, PostStatusEnum> {

@Override
public PostStatusEnum convert(String param) {
if (!StringUtils.hasText(param)) throw new RestApiException(CommunityPostErrorStatus._INVALID_POST_STATUS);
return PostStatusEnum.from(param);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ public class PostTypeConverter implements Converter<String, PostTypeEnum> {

@Override
public PostTypeEnum convert(String param) {
if (!StringUtils.hasText(param)) throw new RestApiException(CommunityPostErrorStatus._INVALID_POST_TYPE);
return PostTypeEnum.from(param);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ public class SortTypeConverter implements Converter<String, SortType> {

@Override
public SortType convert(String param) {
if (!StringUtils.hasText(param)) throw new RestApiException(GlobalErrorStatus._SORT_TYPE_NOT_VALID);
return SortType.from(param);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@

public interface CommunityPostQueryDslRepository {

Slice<CommnuityPost> findAllFetchJoinWithUser(Integer lastPopularityScore,
Slice<CommnuityPost> findAllFetchJoinWithUser(String keyword,
Integer lastPopularityScore,
Long lastPostId,
Integer lastLikeCnt,
Integer lastHit,
Expand Down
Loading

0 comments on commit 893eeee

Please sign in to comment.