Skip to content

Commit

Permalink
#346 Feat: 보유와인, 와인위시리스트 테스트 코드 작성 (#350)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeongyeon0208 authored Jan 12, 2025
2 parents c7c5c49 + d762096 commit 0179bec
Show file tree
Hide file tree
Showing 24 changed files with 1,595 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,23 @@ public class MyWineController {

@Operation(summary = "보유 와인 추가", description = "MyWineRequest로 보유와인을 추가한다.")
@PostMapping("")
public ApiResponse<String> getMyWines(@RequestBody @Valid MyWineRequest myWineRequest,
public ApiResponse<String> saveMyWine(@RequestBody @Valid MyWineRequest myWineRequest,
@AuthenticationPrincipal PrincipalDetail principalDetail){

myWineService.saveMyWine(myWineRequest, principalDetail.getUsername());
return ApiResponse.onSuccess("보유 와인 저장 성공");
}

@Operation(summary = "보유 와인 목록 열람", description = "보유 와인 목록을 열람한다.")
@Operation(summary = "보유 와인 조회", description = "보유 와인을 조회한다.")
@GetMapping("/{myWineId}")
public ApiResponse<MyWineResponse> getMyWine(@PathVariable("myWineId") Long myWineId, @AuthenticationPrincipal PrincipalDetail principalDetail){
MyWineResponse myWineResponse = myWineService.getMyWineById(myWineId, principalDetail.getUsername());
return ApiResponse.onSuccess(myWineResponse);
}

@Operation(summary = "보유 와인 목록 조회", description = "보유 와인 목록을 조회한다.")
@GetMapping("")
public ApiResponse<List<MyWineResponse>> getMyWines(@AuthenticationPrincipal PrincipalDetail principalDetail){
public ApiResponse<List<MyWineResponse>> getMyWineList(@AuthenticationPrincipal PrincipalDetail principalDetail){

List<MyWineResponse> myWineResponseList = myWineService.getMyWinesByUsername(principalDetail.getUsername());
return ApiResponse.onSuccess(myWineResponseList);
Expand All @@ -50,7 +57,7 @@ public ApiResponse<String> updateMyWine(@PathVariable("myWindId") Long wineWish
@Operation(summary = "보유 와인 삭제", description = "보유 와인 Id로 보유 와인을 삭제한다.")
@DeleteMapping("/{myWineId}")
public ApiResponse<String> deleteMyWine(@PathVariable("myWineId") Long myWineId, @AuthenticationPrincipal PrincipalDetail principalDetail) {
myWineService.deleteWineWishlistById(myWineId, principalDetail.getUsername());
myWineService.deleteMyWineById(myWineId, principalDetail.getUsername());
return ApiResponse.onSuccess("보유 와인 삭제 완료");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import java.time.LocalDate;

@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class MyWineRequest {

Expand All @@ -18,5 +16,12 @@ public class MyWineRequest {
@NotNull(message = "구매 날짜 입력은 필수입니다.")
private LocalDate purchaseDate;

private int purchasePrice;
private Integer purchasePrice;

@Builder
public MyWineRequest(Long wineId, LocalDate purchaseDate, int purchasePrice) {
this.wineId = wineId;
this.purchaseDate = purchaseDate;
this.purchasePrice = purchasePrice;
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package com.drinkeg.drinkeg.domain.myWine.controller.request;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.*;

import java.time.LocalDate;

@Getter
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class MyWineUpdateRequest {

private LocalDate purchaseDate;
private Integer purchasePrice;

@Builder
public MyWineUpdateRequest(LocalDate purchaseDate, Integer purchasePrice) {
this.purchaseDate = purchaseDate;
this.purchasePrice = purchasePrice;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.drinkeg.drinkeg.domain.myWine.domain;

import com.drinkeg.drinkeg.domain.member.domain.Member;
import com.drinkeg.drinkeg.domain.model.BaseEntity;
import com.drinkeg.drinkeg.domain.wine.domain.Wine;
import jakarta.persistence.*;
import lombok.*;
Expand All @@ -11,7 +12,7 @@
@Getter
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class MyWine {
public class MyWine extends BaseEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand All @@ -26,7 +27,7 @@ public class MyWine {
private Wine wine;

private LocalDate purchaseDate;
private int purchasePrice;
private Integer purchasePrice;

@Builder
public MyWine(Member member, Wine wine, LocalDate purchaseDate, int purchasePrice){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class MyWineResponse {
private Long myWineid;
private Long myWineId;

private Long wineId;
private String wineName;
Expand All @@ -35,7 +35,7 @@ public static MyWineResponse of(MyWine myWine){
int period = (int) ChronoUnit.DAYS.between(purchaseDate, currentDate);

return MyWineResponse.builder()
.myWineid(myWine.getId())
.myWineId(myWine.getId())
.wineId(wine.getId())
.wineName(wine.getName())
.wineSort(wine.getSort())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@

public interface MyWineRepository extends JpaRepository<MyWine, Long> {

@Query("SELECT w FROM MyWine w JOIN FETCH w.wine WHERE w.member = :member ORDER BY w.purchaseDate")
List<MyWine> findByMemberOrderByPurchaseDate(Member member);
@Query("SELECT w FROM MyWine w JOIN FETCH w.wine WHERE w.member = :member ORDER BY w.createdAt DESC")
List<MyWine> findByMemberOrderByCreatedAt(Member member);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ public interface MyWineService {

Long saveMyWine(MyWineRequest myWineRequest, String username);

MyWineResponse getMyWineById(Long myWineId, String username);

List<MyWineResponse> getMyWinesByUsername(String username);

void updateMyWine(Long wineWishlistId, MyWineUpdateRequest myWineUpdateRequest, String username);

void deleteWineWishlistById(Long myWineId, String username);
void deleteMyWineById(Long myWineId, String username);
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,32 +28,43 @@ public class MyWineServiceImpl implements MyWineService{

@Override
public Long saveMyWine(MyWineRequest myWineRequest, String username) {

// 회원을 조회한다.
Member member = memberRepository.findByUsername(username).orElseThrow(
() -> new GeneralException(ErrorStatus.MEMBER_NOT_FOUND)
Member member = memberRepository.findByUsername(username)
.orElseThrow(() -> new GeneralException(ErrorStatus.MEMBER_NOT_FOUND)
);

// 와인을 찾는다.
Long wineId = myWineRequest.getWineId();
Wine wine = wineRepository.findById(wineId).orElseThrow(
() -> new GeneralException(ErrorStatus.WINE_NOT_FOUND));
Wine wine = wineRepository.findById(wineId)
.orElseThrow(() -> new GeneralException(ErrorStatus.WINE_NOT_FOUND));

// MyWine을 저장한다.
MyWine myWine = MyWine.create(member, wine, myWineRequest.getPurchaseDate(), myWineRequest.getPurchasePrice());

MyWine savedMyWine = myWineRepository.save(myWine);
return savedMyWine.getId();
}

@Override
public MyWineResponse getMyWineById(Long myWineId, String username) {
Member member = memberRepository.findByUsername(username)
.orElseThrow(() -> new GeneralException(ErrorStatus.MEMBER_NOT_FOUND)
);

MyWine myWine = myWineRepository.findById(myWineId)
.orElseThrow(() -> new GeneralException(ErrorStatus.MY_WINE_NOT_FOUND));

if (!member.equals(myWine.getMember())) {
throw new GeneralException(ErrorStatus.MY_WINE_UNAUTHORIZED);
} else {
return MyWineResponse.of(myWine);
}
}

@Override
public List<MyWineResponse> getMyWinesByUsername(String username) {
// 회원을 조회한다.
Member member = memberRepository.findByUsername(username).orElseThrow(
() -> new GeneralException(ErrorStatus.MEMBER_NOT_FOUND)
Member member = memberRepository.findByUsername(username)
.orElseThrow(() -> new GeneralException(ErrorStatus.MEMBER_NOT_FOUND)
);

List<MyWine> myWineList = myWineRepository.findByMemberOrderByPurchaseDate(member);
List<MyWine> myWineList = myWineRepository.findByMemberOrderByCreatedAt(member);

return myWineList.stream().map(MyWineResponse::of).toList();
}
Expand All @@ -77,7 +88,7 @@ public void updateMyWine(Long myWineId, MyWineUpdateRequest myWineUpdateRequest,
}

@Override
public void deleteWineWishlistById(Long myWineId, String username) {
public void deleteMyWineById(Long myWineId, String username) {
Member member = memberRepository.findByUsername(username)
.orElseThrow(() -> new GeneralException(ErrorStatus.MEMBER_NOT_FOUND));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,11 @@ public class WinePreviewResponse {
private String name;
private String nameEng;
private String imageUrl;

private String sort;
private String country;
private String region;
private String variety;

private float vivinoRating;

private int price;

@Builder
Expand All @@ -41,21 +38,15 @@ public WinePreviewResponse(Long wineId, String name, String nameEng, String imag
public static WinePreviewResponse of(Wine wine) {
return WinePreviewResponse.builder()
.wineId(wine.getId())

.name(wine.getName())
.nameEng(wine.getNameEng())

.imageUrl(wine.getImageUrl())

.sort(wine.getSort())
.country(wine.getCountry())
.region(wine.getRegion())
.variety(wine.getVariety())

.vivinoRating(wine.getVivinoRating())
.price(((wine.getPrice() * 1300) / 1000) * 1000)

.price(wine.getPrice())
.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public class WineWishlistController {
@PostMapping("/{wineId}")
public ApiResponse<String> createWineWishlist(@PathVariable("wineId") Long wineId, @AuthenticationPrincipal PrincipalDetail principalDetail) {
wineWishlistService.createWineWishlist(wineId, principalDetail.getUsername());

return ApiResponse.onSuccess("와인 위시리스트 담기 성공");
}

Expand All @@ -37,7 +36,7 @@ public ApiResponse<List<WinePreviewResponse>> getWineWishlist(@AuthenticationPri
@Operation(summary = "위시리스트에 담긴 와인 삭제", description = "wineId로 위시리스트에 담긴 와인을 삭제한다.")
@DeleteMapping("/{wineId}")
public ApiResponse<String> deleteWineWishlist(@PathVariable("wineId") Long wineId, @AuthenticationPrincipal PrincipalDetail principalDetail) {
wineWishlistService.deleteWineWishlistById(wineId, principalDetail.getUsername());
wineWishlistService.deleteWineWishlist(wineId, principalDetail.getUsername());
return ApiResponse.onSuccess("와인 위시리스트 삭제 완료");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import java.util.Optional;


public interface WineWishlistRepository extends JpaRepository<WineWishlist, Long>, WineWishlistRepositoryCustom {
public interface WineWishlistRepository extends JpaRepository<WineWishlist, Long> {

Optional<WineWishlist> findWineWishlistByMemberAndWine(Member member, Wine wine);

Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

public interface WineWishlistService {

void createWineWishlist(Long wineId, String username);
Long createWineWishlist(Long wineId, String username);

List<WinePreviewResponse> getAllWineWishlistByMember(String username);

void deleteWineWishlistById(Long wineWishlistId, String username);
void deleteWineWishlist(Long wineId, String username);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class WineWishlistServiceImpl implements WineWishlistService{
private final WineRepository wineRepository;

@Override
public void createWineWishlist(Long wineId, String username) {
public Long createWineWishlist(Long wineId, String username) {
Member member = memberRepository.findByUsername(username)
.orElseThrow(() -> new GeneralException(ErrorStatus.MEMBER_NOT_FOUND));
Wine wine = wineRepository.findById(wineId)
Expand All @@ -31,7 +31,8 @@ public void createWineWishlist(Long wineId, String username) {
if (wineWishlistRepository.existsByMemberAndWine(member, wine))
throw new GeneralException(ErrorStatus.WINE_WISHLIST_ALREADY_EXISTS);

wineWishlistRepository.save(WineWishlist.create(member, wine));
WineWishlist wineWishlist = wineWishlistRepository.save(WineWishlist.create(member, wine));
return wineWishlist.getId();
}

@Override
Expand All @@ -46,7 +47,7 @@ public List<WinePreviewResponse> getAllWineWishlistByMember(String username) {
}

@Override
public void deleteWineWishlistById(Long wineId, String username) {
public void deleteWineWishlist(Long wineId, String username) {
Member member = memberRepository.findByUsername(username)
.orElseThrow(() -> new GeneralException(ErrorStatus.MEMBER_NOT_FOUND));
Wine wine = wineRepository.findById(wineId)
Expand All @@ -55,10 +56,6 @@ public void deleteWineWishlistById(Long wineId, String username) {
WineWishlist wineWishlist = wineWishlistRepository.findWineWishlistByMemberAndWine(member, wine).orElseThrow(
() -> new GeneralException(ErrorStatus.WINE_WISHLIST_NOT_FOUND));

if (!wineWishlist.getMember().equals(member))
throw new GeneralException(ErrorStatus.WINE_WISHLIST_UNAUTHORIZED);

wineWishlistRepository.deleteById(wineWishlist.getId());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,6 @@ void memberInfoResponse_is_null_Test(){
null );
}



private Member createMember(
String username,
String name,
Expand Down
Loading

0 comments on commit 0179bec

Please sign in to comment.