Skip to content

Commit

Permalink
Merge pull request #50 from SWEET-DEVELOPERS/feature/#48-gift
Browse files Browse the repository at this point in the history
[feat] ์„ ๋ฌผ ํ† ๋„ˆ๋จผํŠธ ์ ์ˆ˜ ์ž…๋ ฅ API & ํ† ๋„ˆ๋จผํŠธ ์ •๋ณด ์กฐํšŒ API ๊ตฌํ˜„
  • Loading branch information
hysong4u authored Jan 12, 2024
2 parents ffaa686 + 65d2733 commit e32ce29
Show file tree
Hide file tree
Showing 10 changed files with 192 additions and 1 deletion.
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);

}
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 FIRST_PLACE_SCORE = 10;
private static final int SECOND_PLACE_SCORE= 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(), FIRST_PLACE_SCORE);
Gift secondPlaceGift = updateScore(tournamentScoreRequestDto.secondPlaceGiftId(), SECOND_PLACE_SCORE);

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);


}

0 comments on commit e32ce29

Please sign in to comment.