Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/Team-GAJI/GAJI-Server in…
Browse files Browse the repository at this point in the history
…to develop
  • Loading branch information
ShimFFF committed Aug 20, 2024
2 parents faf52b2 + 7b57004 commit b685aee
Show file tree
Hide file tree
Showing 17 changed files with 270 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
import gaji.service.domain.enums.Role;
import gaji.service.domain.recruit.entity.RecruitPostBookmark;
import gaji.service.domain.recruit.entity.RecruitPostLikes;
import gaji.service.domain.studyMate.entity.StudyApplicant;
import gaji.service.domain.user.entity.User;
import gaji.service.domain.recruit.entity.StudyComment;
import gaji.service.domain.recruit.web.dto.RecruitRequestDTO;
import gaji.service.domain.recruit.web.dto.RecruitResponseDTO;
import gaji.service.domain.room.entity.Material;
import gaji.service.domain.room.entity.Room;
import gaji.service.domain.studyMate.entity.StudyMate;
import gaji.service.domain.user.entity.User;
import gaji.service.global.converter.DateConverter;
import org.springframework.data.domain.Slice;
import org.springframework.stereotype.Component;
Expand Down Expand Up @@ -58,11 +59,11 @@ public static Material toMaterial(String materialPath, Room room) {
.build();
}

public static StudyMate toStudyMate(User user, Room room) {
public static StudyMate toStudyMate(User user, Room room, Role role) {
return StudyMate.builder()
.user(user)
.room(room)
.role(Role.READER)
.role(role)
.build();
}

Expand Down Expand Up @@ -161,7 +162,7 @@ public static RecruitResponseDTO.PreviewResponseDTO toPreviewDTO(Room room) {
.recruitStatus(room.getRecruitPostTypeEnum())
.applicant(room.getStudyApplicantList().size())
.name(room.getName())
.deadLine(ChronoUnit.DAYS.between(room.getRecruitEndDay(), LocalDate.now()))
.deadLine(ChronoUnit.DAYS.between(LocalDate.now(), room.getRecruitEndDay()))
.description(room.getDescription())
.createdAt(DateConverter.convertToRelativeTimeFormat(room.getCreatedAt()))
.recruitMaxCount(room.getPeopleMaximum())
Expand All @@ -171,4 +172,10 @@ public static RecruitResponseDTO.PreviewResponseDTO toPreviewDTO(Room room) {
public static List<RecruitResponseDTO.PreviewResponseDTO> toPreviewDTOLIST(List<Room> roomList) {
return roomList.stream().map(RecruitConverter::toPreviewDTO).collect(Collectors.toList());
}

public static RecruitResponseDTO.JoinStudyResponseDTO toJoinStudyResponseDTO(Long roomId) {
return RecruitResponseDTO.JoinStudyResponseDTO.builder()
.roomId(roomId)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,10 @@ public interface RecruitCommandService {
RecruitResponseDTO.StudyBookmarkIdDTO bookmarkStudy(Long userId, Long roomId);

void unBookmarkStudy(Long userId, Long roomId);

RecruitResponseDTO.JoinStudyResponseDTO joinStudy(Long userId, Long roomId);

void leaveStudy(Long userId, Long roomId);

void kickStudy(Long userId, Long roomId, Long targetId);
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package gaji.service.domain.recruit.service;

import gaji.service.domain.common.converter.CategoryConverter;
import gaji.service.domain.common.entity.Category;
import gaji.service.domain.common.entity.SelectCategory;
import gaji.service.domain.common.service.CategoryService;
import gaji.service.domain.enums.PostTypeEnum;
import gaji.service.domain.enums.Role;
import gaji.service.domain.recruit.code.RecruitErrorStatus;
import gaji.service.domain.recruit.converter.RecruitConverter;
import gaji.service.domain.recruit.entity.RecruitPostBookmark;
Expand All @@ -17,11 +19,14 @@
import gaji.service.domain.room.service.MaterialCommandService;
import gaji.service.domain.room.service.RoomCommandService;
import gaji.service.domain.room.service.RoomQueryService;
import gaji.service.domain.studyMate.code.StudyMateErrorStatus;
import gaji.service.domain.studyMate.entity.StudyMate;
import gaji.service.domain.studyMate.repository.StudyMateRepository;
import gaji.service.domain.studyMate.service.StudyMateCommandService;
import gaji.service.domain.studyMate.service.StudyMateQueryService;
import gaji.service.domain.user.entity.User;
import gaji.service.domain.user.service.UserQueryService;
import gaji.service.global.exception.RestApiException;
import gaji.service.global.exception.code.status.GlobalErrorStatus;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -37,7 +42,8 @@ public class RecruitCommandServiceImpl implements RecruitCommandService {
private final UserQueryService userQueryService;
private final CategoryService categoryService;
private final RoomQueryService roomQueryService;
private final StudyMateRepository studyMateRepository;
private final StudyMateCommandService studyMateCommandService;
private final StudyMateQueryService studyMateQueryService;
private final MaterialCommandService materialCommandService;
private final RecruitPostLikesRepository recruitPostLikesRepository;
private final RecruitPostBookmarkRepository recruitPostBookmarkRepository;
Expand All @@ -62,8 +68,8 @@ public RecruitResponseDTO.CreateRoomResponseDTO createRoom(RecruitRequestDTO.Cre
User user = userQueryService.findUserById(userId);
Room room = RecruitConverter.toRoom(request, user, request.getThumbnailUrl(), inviteCode, peopleMaximum);

StudyMate studyMate = RecruitConverter.toStudyMate(user, room);
studyMateRepository.save(studyMate);
StudyMate studyMate = RecruitConverter.toStudyMate(user, room, Role.READER);
studyMateCommandService.saveStudyMate(studyMate);

if (request.getMaterialList() != null && !request.getMaterialList().isEmpty()){
Material material;
Expand All @@ -76,16 +82,14 @@ public RecruitResponseDTO.CreateRoomResponseDTO createRoom(RecruitRequestDTO.Cre

roomCommandService.saveRoom(room);

Category category = Category.builder()
.category(request.getCategory())
.build();
categoryService.saveCategory(category);
if (request.getCategoryId() == null) {
throw new RestApiException(GlobalErrorStatus._INVALID_CATEGORY);
}

Long categoryId = request.getCategoryId();
Category category = categoryService.findByCategoryId(categoryId);

SelectCategory selectCategory = SelectCategory.builder()
.category(category)
.entityId(room.getId())
.type(PostTypeEnum.ROOM)
.build();
SelectCategory selectCategory = CategoryConverter.toSelectCategory(category, room.getId(), PostTypeEnum.ROOM);
categoryService.saveSelectCategory(selectCategory);

return RecruitConverter.toResponseDTO(room);
Expand Down Expand Up @@ -163,4 +167,57 @@ public void unBookmarkStudy(Long userId, Long roomId) {
recruitPostBookmarkRepository.deleteByUserAndRoom(user, room);
room.decreaseBookmark();
}

@Override
@Transactional
public RecruitResponseDTO.JoinStudyResponseDTO joinStudy(Long userId, Long roomId) {
User user = userQueryService.findUserById(userId);
Room room = roomQueryService.findRoomById(roomId);

if (studyMateQueryService.existsByUserAndRoom(user, room)) {
throw new RestApiException(StudyMateErrorStatus._USER_ALREADY_JOIN);
}

StudyMate studyMate = RecruitConverter.toStudyMate(user, room, Role.MEMBER);
studyMateCommandService.saveStudyMate(studyMate);

return RecruitConverter.toJoinStudyResponseDTO(roomId);
}

@Override
@Transactional
public void leaveStudy(Long userId, Long roomId) {
User user = userQueryService.findUserById(userId);
Room room = roomQueryService.findRoomById(roomId);

if (!studyMateQueryService.existsByUserAndRoom(user, room)) {
throw new RestApiException(StudyMateErrorStatus._USER_NOT_IN_STUDYROOM);
} else {
if (studyMateQueryService.checkLeader(user, room)) {
throw new RestApiException(StudyMateErrorStatus._LEADER_IMPOSSIBLE_LEAVE);
}
studyMateCommandService.deleteByUserAndRoom(user, room);
}
}

@Override
@Transactional
public void kickStudy(Long userId, Long roomId, Long targetId) {
Room room = roomQueryService.findRoomById(roomId);
User target = userQueryService.findUserById(targetId);

StudyMate studyMate = studyMateQueryService.findByUserIdAndRoomId(userId, roomId);
if (studyMate.getRole() != Role.READER) {
throw new RestApiException(StudyMateErrorStatus._ONLY_LEADER_POSSIBLE);
}

if (!studyMateQueryService.existsByUserAndRoom(target, room)) {
throw new RestApiException(StudyMateErrorStatus._USER_NOT_IN_STUDYROOM);
} else {
if (studyMateQueryService.checkLeader(target, room)) {
throw new RestApiException(StudyMateErrorStatus._LEADER_IMPOSSIBLE_LEAVE);
}
studyMateCommandService.deleteByUserAndRoom(target, room);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public interface RecruitQueryService {
RecruitResponseDTO.studyDetailResponseDTO getStudyDetail(Long roomId);

RecruitResponseDTO.PreviewListResponseDTO getPreviewList(
CategoryEnum category, PreviewFilter filter, SortType sort, String query, Long value, int pageSize);
Long categoryId, PreviewFilter filter, SortType sort, String query, Long value, int pageSize);

RecruitResponseDTO.DefaultPreviewListResponseDTO getDefaultPreview(boolean isFirst, Integer nextCategoryIndex, int pageSize);
RecruitResponseDTO.DefaultPreviewListResponseDTO getDefaultPreview(boolean isFirst, int nextCategoryId, int pageSize);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import gaji.service.domain.common.entity.SelectCategory;
import gaji.service.domain.common.service.CategoryService;
import gaji.service.domain.common.web.dto.CategoryResponseDTO;
import gaji.service.domain.enums.CategoryEnum;
import gaji.service.domain.enums.PostTypeEnum;
import gaji.service.domain.enums.PreviewFilter;
Expand All @@ -23,7 +24,6 @@
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
Expand Down Expand Up @@ -57,7 +57,12 @@ public RecruitResponseDTO.studyDetailResponseDTO getStudyDetail(Long roomId) {
@Override
@Transactional(readOnly = true)
public RecruitResponseDTO.PreviewListResponseDTO getPreviewList(
CategoryEnum category, PreviewFilter filter, SortType sort, String query, Long value, int pageSize) {
Long categoryId, PreviewFilter filter, SortType sort, String query, Long value, int pageSize) {

CategoryEnum category = null;
if (categoryId != null) {
category = categoryService.findByCategoryId(categoryId).getCategory();
}

validateQuery(query);

Expand All @@ -67,36 +72,56 @@ public RecruitResponseDTO.PreviewListResponseDTO getPreviewList(
}

@Override
public RecruitResponseDTO.DefaultPreviewListResponseDTO getDefaultPreview(boolean isFirst, Integer nextCategoryIndex, int pageSize) {
public RecruitResponseDTO.DefaultPreviewListResponseDTO getDefaultPreview(boolean isFirst, int nextCategoryId, int pageSize) {
Pageable pageable = PageRequest.of(0, pageSize);
List<RecruitResponseDTO.DefaultPreviewDTO> defaultPreviewList = new ArrayList<>();
List<CategoryResponseDTO.BaseDTO> categoryList = categoryService.findAllCategory();

List<CategoryEnum> categoryList = new ArrayList<>(Arrays.asList(CategoryEnum.values()));
int count;
boolean hasNext = true;
int count = 0;
int getPreviewCount;

if (isFirst) {
nextCategoryIndex = 0;
count = 4;
nextCategoryId = 0;
getPreviewCount = 4;
} else {
count = nextCategoryIndex + 1;
nextCategoryId--;
getPreviewCount = 1;
}

for (int i = nextCategoryIndex; i < count; i++) {
CategoryEnum category = categoryList.get(i);
while (count < getPreviewCount) {
if (categoryList.size() <= nextCategoryId) {
hasNext = false;
break;
}

CategoryEnum category = categoryList.get(nextCategoryId++).getCategory();

RecruitResponseDTO.DefaultPreviewDTO previewList =
recruitRepository.findByCategory(category, pageable);

if (previewList.getPreviewList() == null || previewList.getPreviewList().isEmpty()) {
continue;
}

count++;
defaultPreviewList.add(previewList);
}

if (hasNext) {
RecruitResponseDTO.DefaultPreviewDTO previewList =
recruitRepository.findByCategory(categoryList.get(nextCategoryId).getCategory(), PageRequest.of(0, 1));

if (previewList.getPreviewList().isEmpty()) {
hasNext = false;
nextCategoryId = -1;
}
}


return RecruitResponseDTO.DefaultPreviewListResponseDTO.builder()
.defaultPreviewList(defaultPreviewList)
.nextIndex(count)
.nextCategoryId(++nextCategoryId)
.hasNext(hasNext)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class RecruitController {
private final StudyCommentQueryService studyCommentQueryService;

@PostMapping("")
@Operation(summary = "스터디 모집 게시글 생성 성성 API", description = "스터디 모집 게시글을 생성하는 API입니다.")
@Operation(summary = "스터디 모집 게시글 생성 API", description = "스터디 모집 게시글을 생성하는 API입니다.")
public BaseResponse<RecruitResponseDTO.CreateRoomResponseDTO> createRoom(
@RequestBody @Valid RecruitRequestDTO.CreateRoomDTO request,
@RequestHeader("Authorization") String authorizationHeader) {
Expand Down Expand Up @@ -96,7 +96,7 @@ public BaseResponse<RecruitResponseDTO.StudyLikesIdResponseDTO> likeStudy(
@Operation(summary = "스터디 모집 게시글 좋아요 취소 API", description = "스터디 모집 게시글 좋아요 취소하는 API 입니다.")
public BaseResponse unLikeStudy(
@RequestHeader("Authorization") String authorizationHeader,
@PathVariable @Min(value = 1, message = "roomId는 1 이상 이어야 합니다.") Long roomId) {
@PathVariable @Min(value = 1, message = "roomId는 1 이상 이어야 합니다.") Long roomId) {
Long userId = tokenProviderService.getUserIdFromToken(authorizationHeader);
recruitCommandService.unLikeStudy(userId, roomId);
return BaseResponse.onSuccess(null);
Expand All @@ -116,7 +116,7 @@ public BaseResponse<RecruitResponseDTO.StudyBookmarkIdDTO> bookmarkStudy(
@Operation(summary = "스터디 모집 게시글 북마크 취소 API", description = "스터디 모집 게시글 북마크 취소하는 API 입니다.")
public BaseResponse unBookmarkStudy(
@RequestHeader("Authorization") String authorizationHeader,
@PathVariable @Min(value = 1, message = "roomId는 1 이상 이어야 합니다.") Long roomId) {
@PathVariable @Min(value = 1, message = "roomId는 1 이상 이어야 합니다.") Long roomId) {
Long userId = tokenProviderService.getUserIdFromToken(authorizationHeader);
recruitCommandService.unBookmarkStudy(userId, roomId);
return BaseResponse.onSuccess(null);
Expand All @@ -125,26 +125,58 @@ public BaseResponse unBookmarkStudy(
@GetMapping("/preview")
@Operation(summary = "스터디 모집 게시글 미리보기 목록 조회 API", description = "모집 게시글 목록을 조회하는 API 입니다.")
public BaseResponse<RecruitResponseDTO.PreviewListResponseDTO> getPreviewList(
@RequestParam(required = false) CategoryEnum category,
@RequestParam(required = false) Long categoryId,
@RequestParam(required = false) PreviewFilter filter,
@RequestParam(defaultValue = "recent") SortType sort,
@RequestParam(required = false) String query,
@RequestParam(required = false) @Min(value = 0, message = "lastValue는 0 이상 입니다.") Long lastValue,
@RequestParam(value = "page", defaultValue = "20") @Min(value = 1, message = "pageSize는 0보다 커야 합니다.") int pageSize){
@RequestParam(value = "page", defaultValue = "20") @Min(value = 1, message = "pageSize는 0보다 커야 합니다.") int pageSize) {

RecruitResponseDTO.PreviewListResponseDTO responseDTO = recruitQueryService.getPreviewList(category, filter, sort, query, lastValue, pageSize);
RecruitResponseDTO.PreviewListResponseDTO responseDTO = recruitQueryService.getPreviewList(categoryId, filter, sort, query, lastValue, pageSize);
return BaseResponse.onSuccess(responseDTO);
}

@GetMapping("/preview-default")
@Operation(summary = "스터디 미리보기 목록 조회 기본 페이지 API", description = "스터디 목록 조회 기본 페이지입니다.")
public BaseResponse<RecruitResponseDTO.DefaultPreviewListResponseDTO> getDefaultPreviewList(
@RequestParam(defaultValue = "0") @Min(value = 0, message = "index는 0 이상 이어야 합니다.") Integer nextCategoryIndex,
@RequestParam(defaultValue = "0") @Min(value = 1, message = "nextCategoryId는 1이상 이어야 합니다.") int nextCategoryId,
@RequestParam(defaultValue = "true") boolean isFirst,
@RequestParam(value = "page", defaultValue = "5") @Min(value = 1, message = "pageSize는 0보다 커야 합니다.") int pageSize) {

RecruitResponseDTO.DefaultPreviewListResponseDTO responseDTO = recruitQueryService.getDefaultPreview(isFirst, nextCategoryIndex, pageSize);
RecruitResponseDTO.DefaultPreviewListResponseDTO responseDTO = recruitQueryService.getDefaultPreview(isFirst, nextCategoryId, pageSize);
return BaseResponse.onSuccess(responseDTO);
}

@PostMapping("/{roomId}")
@Operation(summary = "스터디 가지기 API", description = "스터디에 참여하는 API 입니다.")
public BaseResponse<RecruitResponseDTO.JoinStudyResponseDTO> joinStudy(
@RequestHeader("Authorization") String authorizationHeader,
@PathVariable @Min(value = 1, message = "roomId는 1 이상 이어야 합니다.") Long roomId) {
Long userId = tokenProviderService.getUserIdFromToken(authorizationHeader);
return BaseResponse.onSuccess(
recruitCommandService.joinStudy(userId, roomId)
);
}

@DeleteMapping("/{roomId}/leave")
@Operation(summary = "스터디 나가기 API", description = "스터디에서 나가는 API 입니다.")
public BaseResponse leaveStudy(
@RequestHeader("Authorization") String authorizationHeader,
@PathVariable @Min(value = 1, message = "roomId는 1 이상 이어야 합니다.") Long roomId) {
Long userId = tokenProviderService.getUserIdFromToken(authorizationHeader);
recruitCommandService.leaveStudy(userId, roomId);
return BaseResponse.onSuccess(null);
}

@DeleteMapping("/{roomId}/kick/{targetId}")
@Operation(summary = "스터디 내보내기 API", description = "스터디에서 내보내는 API 입니다.")
public BaseResponse kickStudy(
@RequestHeader("Authorization") String authorizationHeader,
@PathVariable @Min(value = 1, message = "roomId는 1 이상 이어야 합니다.") Long roomId,
@PathVariable @Min(value = 1, message = "targetId 1 이상 이어야 합니다.") Long targetId) {
Long userId = tokenProviderService.getUserIdFromToken(authorizationHeader);
recruitCommandService.kickStudy(userId, roomId, targetId);
return BaseResponse.onSuccess(null);
}
}

Loading

0 comments on commit b685aee

Please sign in to comment.