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] response 추가 및 코드 수정 #121

Merged
merged 4 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,14 @@ public ResponseEntity<SuccessResponse<?>> createNewGift(@UserId Long userId, @Re

@GetMapping("/my/{roomId}")
public ResponseEntity<SuccessResponse<?>> getMyGift(@UserId Long userId, @PathVariable Long roomId) {
String gifteename = giftService.getGifteeName(roomId);
final MyGiftsResponseDto myGiftsResponseDto = giftService.getMyGift(userId, roomId);
return SuccessResponse.ok(myGiftsResponseDto);

Map<String, Object> result = new HashMap<>();
result.put("gifteeName", gifteename);
result.put("myGiftsResponseDto", myGiftsResponseDto);

return SuccessResponse.ok(result);
}


Expand All @@ -42,6 +48,18 @@ public ResponseEntity<SuccessResponse<?>> deleteMyGift(@UserId Long userId, @Pat
return SuccessResponse.ok(null);
}

@GetMapping("/friend/{roomId}")
public ResponseEntity<SuccessResponse<?>> getFriendGift(@UserId Long userId, @PathVariable Long roomId) {
RoomInfoResponseDto roomInfoResponseDto = giftService.getRoomInfo(roomId);
final List<FriendGiftDto> friendGiftList = giftService.getFriendGift(userId, roomId);

Map<String, Object> result = new HashMap<>();
result.put("roomInfoResponseDto", roomInfoResponseDto);
result.put("friendGiftDto", friendGiftList);
return SuccessResponse.ok(result);

}

@GetMapping("/tournament/{roomId}")
public ResponseEntity<SuccessResponse<?>> getTournamentGiftList(@UserId Long userId, @PathVariable Long roomId) {
Boolean isOwner = memberService.isOwner(userId, roomId);
Expand All @@ -55,7 +73,7 @@ public ResponseEntity<SuccessResponse<?>> getTournamentGiftList(@UserId Long use

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

Expand All @@ -71,17 +89,7 @@ public ResponseEntity<SuccessResponse<?>> getRanking(@PathVariable Long roomId)
return SuccessResponse.ok(ranking);
}

@GetMapping("/friend/{roomId}")
public ResponseEntity<SuccessResponse<?>> getFriendGift(@UserId Long userId, @PathVariable Long roomId) {
RoomInfoResponseDto roomInfoResponseDto = giftService.getRoomInfo(roomId);
final List<FriendGiftDto> friendGiftList = giftService.getFriendGift(userId, roomId);

Map<String, Object> result = new HashMap<>();
result.put("roomInfoResponseDto", roomInfoResponseDto);
result.put("friendGiftDto", friendGiftList);
return SuccessResponse.ok(result);

}


}
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,28 @@

@Builder
public record TournamentInfoDto(

String firstplaceGiftName,
String firstplaceGiftImageUrl,
int firstplaceGiftCost,
LocalDateTime remainingTime,
int totalParticipantsCount,
int participantsCount

) {
public static TournamentInfoDto of(LocalDateTime remainingTime,
int TotalParticipantsCount,
int ParticipantsCount) {
public static TournamentInfoDto of( String firstplaceGiftName,
String firstplaceGiftImageUrl,
int firstplaceGiftCost,
LocalDateTime remainingTime,
int totalParticipantsCount,
int participantsCount) {
return TournamentInfoDto.builder()
.firstplaceGiftName(firstplaceGiftName)
.firstplaceGiftImageUrl(firstplaceGiftImageUrl)
.firstplaceGiftCost(firstplaceGiftCost)
.remainingTime(remainingTime)
.totalParticipantsCount(TotalParticipantsCount)
.participantsCount(ParticipantsCount)
.totalParticipantsCount(totalParticipantsCount)
.participantsCount(participantsCount)
.build();
}

Expand Down
7 changes: 7 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 @@ -7,8 +7,11 @@
import lombok.NoArgsConstructor;
import org.sopt.sweet.domain.member.entity.Member;
import org.sopt.sweet.domain.room.entity.Room;
import org.sopt.sweet.domain.room.entity.RoomMember;
import org.sopt.sweet.global.common.BaseTimeEntity;

import java.util.List;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Table(name = "gift")
Expand Down Expand Up @@ -42,6 +45,10 @@ public class Gift extends BaseTimeEntity {
@JoinColumn(name = "member_id", nullable = false)
private Member member;

@OneToMany(mappedBy = "id")
private List<RoomMember> roomMembers;


@Builder
public Gift(String url, String name, int cost, String imageUrl, Room room, Member member) {
this.url = url;
Expand Down
31 changes: 26 additions & 5 deletions src/main/java/org/sopt/sweet/domain/gift/service/GiftService.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,18 @@ private List<TournamentListsResponseDto> mapGiftsToTournamentLists(List<Gift> gi
.collect(Collectors.toList());
}

public void evaluateTournamentScore(TournamentScoreRequestDto tournamentScoreRequestDto) {
public void evaluateTournamentScore(Long memberId, TournamentScoreRequestDto tournamentScoreRequestDto) {
Gift gift = findByIdOrThrow(tournamentScoreRequestDto.finalGiftId());
Room room = gift.getRoom();

//1등 상품 저장하기
RoomMember roomMember = roomMemberRepository.findByRoomIdAndMemberId(room.getId(), memberId);
roomMember.setFirstplaceGiftId(tournamentScoreRequestDto.finalGiftId());
roomMemberRepository.save(roomMember);

//토너먼트 참여 여부 업데이트
updateTournamentParticipation(memberId, room.getId());

Long firstGiftId = tournamentScoreRequestDto.firstGiftId();
Long secondGiftId = tournamentScoreRequestDto.secondGiftId();
Long finalGiftId = tournamentScoreRequestDto.finalGiftId();
Expand Down Expand Up @@ -190,23 +201,25 @@ private Gift updateScore(Long giftId, int score) {

public TournamentInfoDto getTournamentInfo(Long memberId, Long roomId) {
Room room = findRoomByIdOrThrow(roomId);
RoomMember roomMember = roomMemberRepository.findByRoomIdAndMemberId(roomId, memberId);
Gift firstPlaceGift = giftRepository.findById(roomMember.getFirstplaceGiftId())
.orElseThrow(() -> new EntityNotFoundException(GIFT_NOT_FOUND));

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

int totalParticipantsCount = room.getGifterNumber();

updateTournamentParticipation(memberId, roomId);

int participatingMembersCount = roomMemberRepository.countByRoomIdAndTournamentParticipationIsTrue(roomId);

LocalDateTime tournamentEndTime = getTournamentEndDate(tournamentStartDate, tournamentDuration);
LocalDateTime remainingTime =getTournamentRemainingTime(tournamentEndTime);

return new TournamentInfoDto(remainingTime, totalParticipantsCount, participatingMembersCount);
return new TournamentInfoDto(
firstPlaceGift.getName(), firstPlaceGift.getImageUrl(), firstPlaceGift.getCost(),
remainingTime, totalParticipantsCount, participatingMembersCount);
}


private LocalDateTime getTournamentEndDate(LocalDateTime tournamentStartDate, TournamentDuration tournamentDuration) {
LocalDateTime tournamentEndTime = tournamentStartDate.plusHours(tournamentDuration.getHours());
return tournamentEndTime;
Expand Down Expand Up @@ -285,4 +298,12 @@ public RoomInfoResponseDto getRoomInfo(Long roomId) {
LocalDateTime tournamentStartDate = room.getTournamentStartDate();
return new RoomInfoResponseDto(gifteeName,tournamentStartDate);
}


public String getGifteeName(Long roomId) {
Room room = findRoomByIdOrThrow(roomId);
String gifteeName = room.getGifteeName();
return gifteeName;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ public class RoomMember extends BaseTimeEntity {
@Column(columnDefinition = "TINYINT(1) default 0")
private boolean tournamentParticipation;

@Column(name = "firstplace_gift_id")
private Long firstplaceGiftId;

@Builder
public RoomMember(Room room, Member member) {
this.room = room;
Expand All @@ -39,4 +42,7 @@ public void setTournamentParticipation(boolean tournamentParticipation) {
this.tournamentParticipation = tournamentParticipation;
}

public void setFirstplaceGiftId(Long firstplaceGiftId) {
this.firstplaceGiftId = firstplaceGiftId;
}
}
Loading