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] 선물 토너먼트 점수 입력 API & 토너먼트 정보 조회 API 구현 #50

Merged
merged 15 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from 14 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
59 changes: 59 additions & 0 deletions src/main/java/org/sopt/sweet/domain/gift/controller/GiftApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import jakarta.validation.Valid;
import org.sopt.sweet.domain.gift.dto.request.CreateGiftRequestDto;
import org.sopt.sweet.domain.gift.dto.request.MyGiftsRequestDto;
import org.sopt.sweet.domain.gift.dto.request.TournamentScoreRequestDto;
import org.sopt.sweet.global.common.SuccessResponse;
import org.sopt.sweet.global.config.auth.UserId;
import org.springframework.http.ResponseEntity;
Expand Down Expand Up @@ -122,4 +123,62 @@ ResponseEntity<SuccessResponse<?>> getTournamentGiftList(
) @PathVariable Long roomId
);

@Operation(
summary = "선물 토너먼트 점수 등록 API",
responses = {
@ApiResponse(
responseCode = "201",
content = @Content(
mediaType = "application/json",
schema = @Schema(implementation = SuccessResponse.class)
)
)
},
security = @SecurityRequirement(name = "token")
)
ResponseEntity<SuccessResponse<?>> evaluateTournamentScore(
@Parameter(
description = "authorization token에서 얻은 userId, 임의입력하면 대체됩니다.",
required = true,
example = "12345"
) @UserId Long userId,
@Valid @RequestBody TournamentScoreRequestDto tournamentScoreRequestDto
);


@Operation(
summary = "토너먼트 정보 조회 API",
responses = {
@ApiResponse(
responseCode = "200",
content = @Content(
mediaType = "application/json",
schema = @Schema(implementation = SuccessResponse.class)
)
),
@ApiResponse(
responseCode = "404",
description = "토너먼트나 사용자가 존재하지 않음",
content = @Content
),
@ApiResponse(
responseCode = "403",
description = "토너먼트 시작일이 지났거나 사용자가 방에 속해있지 않음",
content = @Content
)
},
security = @SecurityRequirement(name = "token")
)
ResponseEntity<SuccessResponse<?>> getTournamentInfo(
@Parameter(
description = "authorization token에서 얻은 userId, 임의입력하면 대체됩니다.",
required = true,
example = "12345"
) @UserId Long userId,
@Parameter(
description = "조회하려는 토너먼트가 진행 중인 방의 ID",
required = true,
example = "2"
) @PathVariable Long roomId
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import lombok.RequiredArgsConstructor;
import org.sopt.sweet.domain.gift.dto.request.CreateGiftRequestDto;
import org.sopt.sweet.domain.gift.dto.request.MyGiftsRequestDto;
import org.sopt.sweet.domain.gift.dto.request.TournamentScoreRequestDto;
import org.sopt.sweet.domain.gift.dto.response.MyGiftsResponseDto;
import org.sopt.sweet.domain.gift.dto.response.TournamentListsResponseDto;
import org.sopt.sweet.domain.gift.dto.response.TournamentInfoDto;
import org.sopt.sweet.domain.gift.service.GiftService;
import org.sopt.sweet.global.common.SuccessResponse;
import org.sopt.sweet.global.config.auth.UserId;
Expand Down Expand Up @@ -44,6 +46,19 @@ public ResponseEntity<SuccessResponse<?>> getTournamentGiftList(@UserId Long use
return SuccessResponse.ok(tournamentGiftList);
}

@PostMapping("/tonermant-score")
public ResponseEntity<SuccessResponse<?>> evaluateTournamentScore(@UserId Long userId, @RequestBody TournamentScoreRequestDto tournamentScoreRequestDto) {
giftService.evaluateTournamentScore(tournamentScoreRequestDto);
return SuccessResponse.created(null);
}

@GetMapping("tournament-info/{roomId}")
public ResponseEntity<SuccessResponse<?>> getTournamentInfo(@UserId Long userId, @PathVariable Long roomId) {
final TournamentInfoDto tournamentInfo = giftService.getTournamentInfo(userId, roomId);
return SuccessResponse.ok(tournamentInfo);
}




}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.sopt.sweet.domain.gift.dto.request;

import lombok.Builder;
import org.sopt.sweet.domain.gift.dto.response.TournamentListsResponseDto;

@Builder
public record TournamentScoreRequestDto(
Long firstPlaceGiftId,
Long secondPlaceGiftId
) {
public static TournamentScoreRequestDto of(Long firstPlaceGiftId, Long secondPlaceGiftId) {
return TournamentScoreRequestDto.builder()
.firstPlaceGiftId(firstPlaceGiftId)
.secondPlaceGiftId(secondPlaceGiftId)
.build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.sopt.sweet.domain.gift.dto.response;

import lombok.Builder;
import org.sopt.sweet.domain.room.constant.TournamentDuration;

import java.time.LocalDateTime;
@Builder
public record TournamentInfoDto(
LocalDateTime tournamentStartDate,
TournamentDuration tournamentDuration,

int TotalParticipantsCount,
int ParticipantsCount

) {
public static TournamentInfoDto of(LocalDateTime tournamentStartDate,
TournamentDuration tournamentDuration,
int TotalParticipantsCount,
int ParticipantsCount) {
return TournamentInfoDto.builder()
.tournamentStartDate(tournamentStartDate)
.tournamentDuration(tournamentDuration)
.TotalParticipantsCount(TotalParticipantsCount)
.ParticipantsCount(ParticipantsCount)
.build();
}

}
8 changes: 8 additions & 0 deletions src/main/java/org/sopt/sweet/domain/gift/entity/Gift.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,12 @@ public Gift(String url, String name, int cost, String imageUrl, Room room, Membe
this.room = room;
this.member = member;
}

public void setScore(int score) {
this.score = score;
}




}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Optional;

public interface GiftRepository extends JpaRepository<Gift, Long> {
long countByRoomAndMember(Room room, Member member);
Expand All @@ -19,4 +20,5 @@ public interface GiftRepository extends JpaRepository<Gift, Long> {
List<Gift> findLatestGiftsByRoomAndNotMember(@Param("room") Room room, @Param("member") Member member, Pageable pageable);

List<Gift> findByRoom(Room room);

}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

토너먼트 점수 변경할 때 (결과 API) roommember 테이블에 있는 토너먼트 참여 여부도 변경해줘야 할 것 같아요! 기본값 false(DB에는 tinyint 라서 0, 1)로 되어 있는 걸 true로 반환해줘야 토너먼트 완료한 사람 수 보내줄 수 있을 거 같습니다!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

넹넹 확인!!

Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
import lombok.RequiredArgsConstructor;
import org.sopt.sweet.domain.gift.dto.request.CreateGiftRequestDto;
import org.sopt.sweet.domain.gift.dto.request.MyGiftsRequestDto;
import org.sopt.sweet.domain.gift.dto.request.TournamentScoreRequestDto;
import org.sopt.sweet.domain.gift.dto.response.MyGiftDto;
import org.sopt.sweet.domain.gift.dto.response.MyGiftsResponseDto;
import org.sopt.sweet.domain.gift.dto.response.TournamentListsResponseDto;
import org.sopt.sweet.domain.gift.dto.response.TournamentInfoDto;
import org.sopt.sweet.domain.gift.entity.Gift;
import org.sopt.sweet.domain.gift.repository.GiftRepository;
import org.sopt.sweet.domain.member.entity.Member;
import org.sopt.sweet.domain.member.repository.MemberRepository;
import org.sopt.sweet.domain.room.constant.TournamentDuration;
import org.sopt.sweet.domain.room.entity.Room;
import org.sopt.sweet.domain.room.entity.RoomMember;
import org.sopt.sweet.domain.room.repository.RoomMemberRepository;
Expand All @@ -36,6 +39,8 @@ public class GiftService {
private final RoomRepository roomRepository;
private final RoomMemberRepository roomMemberRepository;
private static final int MAX_GIFT_COUNT = 2;
private static final int FirstPlaceScore = 10;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기서는 final로 상수를 정의한 거니까 두 가지 변수 명만 규칙에 맞게 대문자로 바꿔주면 좋을 거 같아요!!!
FIRST_PLACE_SCORE 처럼 적어주면 좋을 거 같아요 ~~

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

네~~~꼼꼼한 리뷰 감사합니당

private static final int SecondPlaceScore = 5;

public void createNewGift(Long memberId, CreateGiftRequestDto createGiftRequestDto) {
Member member = findMemberByIdOrThrow(memberId);
Expand All @@ -57,7 +62,7 @@ public MyGiftsResponseDto getMyGift(Long memberId, MyGiftsRequestDto myGiftsRequ
return new MyGiftsResponseDto(myGiftsDtoList);
}

public void deleteMyGift(Long memberId, Long giftId){
public void deleteMyGift(Long memberId, Long giftId) {
Member member = findMemberByIdOrThrow(memberId);
Gift gift = findByIdOrThrow(giftId);
validateMemberGiftOwner(member, gift);
Expand Down Expand Up @@ -123,11 +128,17 @@ private Room findRoomByIdOrThrow(Long roomId) {
.orElseThrow(() -> new EntityNotFoundException(ROOM_NOT_FOUND));
}


private Gift findByIdOrThrow(Long giftId) {
return giftRepository.findById(giftId)
.orElseThrow(() -> new EntityNotFoundException(GIFT_NOT_FOUND));
}

private Room findGiftByIdOrThrow(Long roomId) {
return roomRepository.findById(roomId)
.orElseThrow(() -> new EntityNotFoundException(GIFT_NOT_FOUND));
}


@Transactional(readOnly = true)
public List<TournamentListsResponseDto> getTournamentGiftList(Long roomId) {
Expand All @@ -142,4 +153,44 @@ private List<TournamentListsResponseDto> mapGiftsToTournamentLists(List<Gift> gi
.collect(Collectors.toList());
}

public void evaluateTournamentScore(TournamentScoreRequestDto tournamentScoreRequestDto) {

Gift firstPlaceGift = updateScore(tournamentScoreRequestDto.firstPlaceGiftId(), FirstPlaceScore);
Gift secondPlaceGift = updateScore(tournamentScoreRequestDto.secondPlaceGiftId(), SecondPlaceScore);

giftRepository.save(firstPlaceGift);
giftRepository.save(secondPlaceGift);
}


private Gift updateScore(Long giftId, int score) {
Gift gift = findByIdOrThrow(giftId);
int newScore = gift.getScore() + score;
gift.setScore(newScore);
return gift;
}

public TournamentInfoDto getTournamentInfo(Long memberId, Long roomId) {
Room room = findRoomByIdOrThrow(roomId);

LocalDateTime tournamentStartDate = room.getTournamentStartDate();
TournamentDuration tournamentDuration = room.getTournamentDuration();
int totalParticipantsCount = room.getGifterNumber();

updateTournamentParticipation(memberId, roomId);

int participatingMembersCount = roomMemberRepository.countByRoomIdAndTournamentParticipationIsTrue(roomId);

return new TournamentInfoDto(tournamentStartDate, tournamentDuration, totalParticipantsCount, participatingMembersCount);
}

public void updateTournamentParticipation(Long memberId, Long roomId) {
RoomMember roomMember = roomMemberRepository.findByRoomIdAndMemberId(roomId, memberId);
roomMember.setTournamentParticipation(true);
roomMemberRepository.save(roomMember);
}




}
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,8 @@ public RoomMember(Room room, Member member){
this.member = member;
}

public void setTournamentParticipation(boolean tournamentParticipation) {
this.tournamentParticipation = tournamentParticipation;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@
public interface RoomMemberRepository extends JpaRepository<RoomMember, Long> {
Optional<RoomMember> findByRoomAndMember(Room room, Member member);
List<RoomMember> findByRoomId(Long roomId);

int countByRoomIdAndTournamentParticipationIsTrue(Long roomId);
RoomMember findByRoomIdAndMemberId(Long roomId, Long memberId);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.sopt.sweet.domain.room.repository;

import org.sopt.sweet.domain.room.entity.Room;
import org.sopt.sweet.domain.room.entity.RoomMember;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;
Expand All @@ -9,4 +10,6 @@ public interface RoomRepository extends JpaRepository<Room, Long> {
boolean existsByInvitationCode(String invitationCode);

Optional<Room> findByInvitationCode(String invitationCode);


}