Skip to content

Commit

Permalink
feat: 그룹 즐겨찾기 등록해제 기능 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
SangWoon123 committed May 6, 2024
1 parent 07f0cc7 commit 66e0ed5
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
import com.tukorea.planding.domain.user.dto.UserInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
Expand All @@ -20,9 +17,15 @@ public class GroupFavoriteController {
private final GroupFavoriteService groupFavoriteService;

@GetMapping("/{groupCode}")
public CommonResponse<?> addFavorite(@AuthenticationPrincipal UserInfo userInfo, @PathVariable String groupCode) {
public CommonResponse<GroupFavoriteResponse> addFavorite(@AuthenticationPrincipal UserInfo userInfo, @PathVariable String groupCode) {
GroupFavoriteResponse response = groupFavoriteService.addFavorite(userInfo, groupCode);
return CommonUtils.success(response);
}

@DeleteMapping("/{groupCode}")
public CommonResponse<?> deleteFavorite(@AuthenticationPrincipal UserInfo userInfo, @PathVariable String groupCode) {
groupFavoriteService.deleteFavorite(userInfo, groupCode);
return CommonUtils.success("즐겨찾기 해제 완료.");
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package com.tukorea.planding.domain.group.repository;

import com.tukorea.planding.domain.group.entity.GroupFavorite;
import com.tukorea.planding.domain.group.entity.GroupRoom;
import com.tukorea.planding.domain.user.entity.User;

public interface GroupFavoriteRepositoryCustom {
Long countMyFavoriteGroup(String userCode);

Boolean existsByUserAndGroupRoom(String userCode);
GroupFavorite findByUserAndGroupRoom(User user, GroupRoom groupRoom);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.tukorea.planding.domain.group.repository;

import com.querydsl.jpa.impl.JPAQueryFactory;
import com.tukorea.planding.domain.group.entity.GroupFavorite;
import com.tukorea.planding.domain.group.entity.GroupRoom;
import com.tukorea.planding.domain.user.entity.User;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -31,4 +32,11 @@ public Boolean existsByUserAndGroupRoom(String userCode) {
.fetchFirst();
return fetchOne != null;
}

@Override
public GroupFavorite findByUserAndGroupRoom(User user, GroupRoom groupRoom) {
return jpaQueryFactory.selectFrom(groupFavorite)
.where(groupFavorite.user.eq(user).and(groupFavorite.groupRoom.eq(groupRoom)))
.fetchOne();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
import com.tukorea.planding.global.error.ErrorCode;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
@RequiredArgsConstructor
public class GroupFavoriteService {

Expand All @@ -36,4 +38,15 @@ public GroupFavoriteResponse addFavorite(UserInfo userInfo, String groupCode) {

return GroupFavoriteResponse.from(save);
}

public void deleteFavorite(UserInfo userInfo, String groupCode) {
User user = userQueryService.getByUserInfo(userInfo.getUserCode());
GroupRoom groupRoom = groupQueryService.getGroupByCode(groupCode);

GroupFavorite groupFavorite = groupFavoriteRepositoryCustom.findByUserAndGroupRoom(user, groupRoom);
if (groupFavorite == null) {
throw new BusinessException(ErrorCode.FAVORITE_ALREADY_DELETE);
}
groupFavoriteRepository.delete(groupFavorite);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public enum ErrorCode {
GROUP_ROOM_NOT_FOUND("GROUP-003", "그룹룸이 존재하지 않습니다.", HttpStatus.NOT_FOUND),
UNAUTHORIZED_GROUP_ROOM_INVITATION("GROUP-004", "그룹룸에 초대할 권한이 없습니다.", HttpStatus.UNAUTHORIZED),
USER_ALREADY_INVITED("GROUP-005", "이미 그룹룸에 초대되었습니다.", HttpStatus.UNAUTHORIZED),
FAVORITE_ALREADY_ADD("GROUP-006", "이미 즐겨찾기에 등록하였습니다..", HttpStatus.UNAUTHORIZED),
FAVORITE_ALREADY_ADD("GROUP-006", "이미 즐겨찾기에 등록하였습니다.", HttpStatus.UNAUTHORIZED),
FAVORITE_ALREADY_DELETE("GROUP-007", "즐겨찾기에 등록된 그룹이 아닙니다.", HttpStatus.UNAUTHORIZED),

/**
* Invite Error
Expand All @@ -45,4 +46,4 @@ public enum ErrorCode {
private final String errorCode;
private final String message;
private final HttpStatus status;
}
}

0 comments on commit 66e0ed5

Please sign in to comment.