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] 선물방 정보 관련 기능 구현 #28

Merged
merged 8 commits into from
Jan 9, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@
import org.sopt.sweet.domain.gift.entity.Gift;
import org.sopt.sweet.domain.member.entity.Member;
import org.sopt.sweet.domain.room.entity.Room;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface GiftRepository extends JpaRepository<Gift, Long> {
long countByRoomAndMember(Room room, Member member);

List<Gift> findByRoomAndMember(Room room, Member member);

@Query("SELECT g FROM Gift g WHERE g.room = :room AND g.member <> :member ORDER BY g.id DESC")
List<Gift> findLatestGiftsByRoomAndNotMember(@Param("room") Room room, @Param("member") Member member, Pageable pageable);
}
10 changes: 10 additions & 0 deletions src/main/java/org/sopt/sweet/domain/gift/service/GiftService.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
Expand All @@ -40,6 +41,7 @@ public void createNewGift(Long memberId, CreateGiftRequestDto createGiftRequestD
Room room = findRoomByIdOrThrow(createGiftRequestDto.roomId());
checkRoomMemberNotExists(room, member);
checkGiftCountNotExceeded(room, member);
checkTournamentStartDatePassed(room);
Gift gift = buildGift(member, room, createGiftRequestDto);
giftRepository.save(gift);
}
Expand Down Expand Up @@ -78,6 +80,14 @@ private List<MyGiftDto> mapGiftsToMyGiftDtoList(List<Gift> gifts) {
.collect(Collectors.toList());
}

private void checkTournamentStartDatePassed(Room room) {
LocalDateTime tournamentStartDate = room.getTournamentStartDate();
LocalDateTime currentDateTime = LocalDateTime.now();
if (currentDateTime.isAfter(tournamentStartDate)) {
throw new BusinessException(TOURNAMENT_START_DATE_PASSED);
}
}

private void checkGiftCountNotExceeded(Room room, Member member) {
long giftCount = giftRepository.countByRoomAndMember(room, member);
if (giftCount >= MAX_GIFT_COUNT) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

import org.sopt.sweet.domain.product.entity.Product;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface ProductRepository extends JpaRepository<Product, Long> {
@Query(value = "SELECT p FROM Product p ORDER BY RAND() LIMIT :limit")
List<Product> findRandomProducts(@Param("limit") int limit);
}
54 changes: 53 additions & 1 deletion src/main/java/org/sopt/sweet/domain/room/controller/RoomApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,58 @@ ResponseEntity<SuccessResponse<?>> joinRoom(
example = "12345"
) @UserId Long userId,
@Valid @RequestBody JoinRoomRequestDto joinRoomRequestDto
);
);

@Operation(
summary = "선물방 메인 조회 API",
responses = {
@ApiResponse(
responseCode = "200",
content = @Content(
mediaType = "application/json",
schema = @Schema(implementation = SuccessResponse.class)
)
)
},
security = @SecurityRequirement(name = "token")
)
ResponseEntity<SuccessResponse<?>> getRoomMainInfo(
@Parameter(
description = "authorization token에서 얻은 userId, 임의입력하면 대체됩니다.",
required = true,
example = "12345"
) @UserId Long userId,
@Parameter(
description = "room 고유 id",
required = true,
example = "1"
) @PathVariable Long roomId
);

@Operation(
summary = "선물방 설정 편집 조회 API",
responses = {
@ApiResponse(
responseCode = "200",
content = @Content(
mediaType = "application/json",
schema = @Schema(implementation = SuccessResponse.class)
)
)
},
security = @SecurityRequirement(name = "token")
)
ResponseEntity<SuccessResponse<?>> getRoomDetailInfo(
@Parameter(
description = "authorization token에서 얻은 userId, 임의입력하면 대체됩니다.",
required = true,
example = "12345"
) @UserId Long userId,
@Parameter(
description = "room 고유 id",
required = true,
example = "1"
) @PathVariable Long roomId
);

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
import lombok.RequiredArgsConstructor;
import org.sopt.sweet.domain.room.dto.request.CreateRoomRequestDto;
import org.sopt.sweet.domain.room.dto.request.JoinRoomRequestDto;
import org.sopt.sweet.domain.room.dto.response.CreateRoomResponseDto;
import org.sopt.sweet.domain.room.dto.response.JoinRoomResponseDto;
import org.sopt.sweet.domain.room.dto.response.RoomInviteResponseDto;
import org.sopt.sweet.domain.room.dto.response.*;
import org.sopt.sweet.domain.room.service.RoomService;
import org.sopt.sweet.global.common.SuccessResponse;
import org.sopt.sweet.global.config.auth.UserId;
Expand Down Expand Up @@ -36,4 +34,16 @@ public ResponseEntity<SuccessResponse<?>> joinRoom(@UserId Long userId, @Request
final JoinRoomResponseDto joinRoomResponseDto = roomService.findAndJoinRoom(userId, joinRoomRequestDto);
return SuccessResponse.ok(joinRoomResponseDto);
}

@GetMapping("/{roomId}")
public ResponseEntity<SuccessResponse<?>> getRoomMainInfo(@UserId Long userId, @PathVariable Long roomId){
final RoomMainResponseDto roomMainResponseDto = roomService.getRoomMainInfo(userId, roomId);
return SuccessResponse.ok(roomMainResponseDto);
}

@GetMapping("/detail/{roomId}")
public ResponseEntity<SuccessResponse<?>> getRoomDetailInfo(@UserId Long userId, @PathVariable Long roomId){
final RoomDetailResponseDto roomDetailResponseDto = roomService.getRoomDetailInfo(userId, roomId);
return SuccessResponse.ok(roomDetailResponseDto);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.sopt.sweet.domain.room.dto.response;

import lombok.Builder;

@Builder
public record HotProductGiftDto(
Long productId,
String imageUrl,
String name,
String url,
int cost
) {
public static HotProductGiftDto of(Long productId, String imageUrl, String name, String url, int cost){
return HotProductGiftDto.builder()
.productId(productId)
.imageUrl(imageUrl)
.name(name)
.url(url)
.cost(cost)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.sopt.sweet.domain.room.dto.response;

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

import java.time.LocalDateTime;

@Builder
public record RoomDetailResponseDto(
Long roomId,
String imageUrl,
String gifteeName,
int gifterNumber,
LocalDateTime deliveryDate,
LocalDateTime tournamentStartDate,
TournamentDuration tournamentDuration
) {
public static RoomDetailResponseDto of(Long roomId,
String imageUrl,
String gifteeName,
int gifterNumber,
LocalDateTime deliveryDate,
LocalDateTime tournamentStartDate,
TournamentDuration tournamentDuration){
return RoomDetailResponseDto.builder()
.roomId(roomId)
.imageUrl(imageUrl)
.gifteeName(gifteeName)
.gifterNumber(gifterNumber)
.deliveryDate(deliveryDate)
.tournamentStartDate(tournamentStartDate)
.tournamentDuration(tournamentDuration)
.build();
}

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

import lombok.Builder;

@Builder
public record RoomFriendsGiftDto(
Long giftId,
String imageUrl,
String name,
String url,
int cost,
String ownerName
) {
public static RoomFriendsGiftDto of(Long giftId, String imageUrl, String name, String url, int cost, String ownerName) {
return RoomFriendsGiftDto.builder()
.giftId(giftId)
.imageUrl(imageUrl)
.name(name)
.url(url)
.cost(cost)
.ownerName(ownerName)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.sopt.sweet.domain.room.dto.response;

import lombok.Builder;

import java.time.LocalDateTime;
import java.util.List;
@Builder
public record RoomMainResponseDto(
int gifterNumber,
String gifteeName,
String invitationCode,
LocalDateTime tournamentStartDate,
List<RoomMyGiftDto> roomMyGiftDtoList,
List<RoomFriendsGiftDto> roomFriendsGiftDtoList,
List<HotProductGiftDto> hotProductGiftDtoList
) {
public static RoomMainResponseDto of(int gifterNumber,
String gifteeName,
String invitationCode,
LocalDateTime tournamentStartDate,
List<RoomMyGiftDto> roomMyGiftDtoList,
List<RoomFriendsGiftDto> roomFriendsGiftDtoList,
List<HotProductGiftDto> hotProductGiftDtoList){
return RoomMainResponseDto.builder()
.gifterNumber(gifterNumber)
.gifteeName(gifteeName)
.invitationCode(invitationCode)
.tournamentStartDate(tournamentStartDate)
.roomMyGiftDtoList(roomMyGiftDtoList)
.roomFriendsGiftDtoList(roomFriendsGiftDtoList)
.hotProductGiftDtoList(hotProductGiftDtoList)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.sopt.sweet.domain.room.dto.response;

import lombok.Builder;

@Builder
public record RoomMyGiftDto(
Long giftId,
String imageUrl,
String name,
String url,
int cost
) {
public static RoomMyGiftDto of(Long giftId, String imageUrl, String name, String url, int cost){
return RoomMyGiftDto.builder()
.giftId(giftId)
.imageUrl(imageUrl)
.name(name)
.url(url)
.cost(cost)
.build();
}
}
Loading