Skip to content

Commit

Permalink
Merge pull request #193 from Make-A-Wish-Sopt/feature/jiyoung-#190-de…
Browse files Browse the repository at this point in the history
…velop

[FEAT] 선물 선물 수정
  • Loading branch information
wlwpfh authored Sep 10, 2024
2 parents 9fae794 + fa015c3 commit b7b3938
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ public enum ErrorMessage {
SERVER_INTERNAL_ERROR("서버 내부 오류"),
INVALID_HTTP_REQUEST("지원하지 않는 HTTP Method 요청입니다."),
FORBIDDEN("해당 데이터에 접근할 수 없습니다."),
FAULT_DATE_FORMATTER("잘못된 날짜 형식입니다.");
FAULT_DATE_FORMATTER("잘못된 날짜 형식입니다."),

/** gift menu **/
INVALID_GIFT_MENU("존재하지 않는 선물 종류입니다."),
;

private final String message;
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public ResponseEntity<ApiResponse> createCake(@RequestBody CakeApproveRequestDTO
if (cake.getId() != 1) {
cakeService.getKakaoPayApprove(request);
}
val response = cakeService.createPresent(new CakeCreateRequest(request.name(), request.message(), request.cakeId(), request.wishId()));
val response = cakeService.createPresent(new CakeCreateRequest(request.name(), request.message(), request.cakeId(), request.wishId(), request.cakeId()));
return ResponseEntity.ok(ApiResponse.success(SUCCESS_CREATE_CAKE.getMessage(), response));
}

Expand Down
30 changes: 30 additions & 0 deletions src/main/java/com/sopterm/makeawish/domain/GiftMenu.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.sopterm.makeawish.domain;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import static jakarta.persistence.GenerationType.IDENTITY;

@Getter
@Entity
@NoArgsConstructor
@AllArgsConstructor
public class GiftMenu{
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "gift_menu_id")
private Long id;

private String name;

private int price;

public static GiftMenu getLetter(){
return new GiftMenu(0L, "letter", 0);
}
}
12 changes: 12 additions & 0 deletions src/main/java/com/sopterm/makeawish/domain/Present.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,16 @@ public class Present {
@CreatedDate
protected LocalDateTime createdAt;

@ManyToOne(fetch = LAZY)
@JoinColumn(name = "gift_menu_id")
private GiftMenu giftMenu;

public static class PresentBuilder {
private String name;
private String message;
private Wish wish;
private Cake cake;
private GiftMenu giftMenu;

private PresentBuilder() {
}
Expand Down Expand Up @@ -73,6 +78,11 @@ public PresentBuilder cake(Cake cake) {
return this;
}

public PresentBuilder giftMenu(GiftMenu giftMenu) {
this.giftMenu = giftMenu;
return this;
}

private void setWish(Present present, Wish wish) {
if (nonNull(present.wish)) {
present.wish.getPresents().remove(present);
Expand All @@ -89,6 +99,7 @@ public Present build() {
present.message = this.message;
setWish(present, this.wish);
present.cake = this.cake;
present.giftMenu = this.giftMenu;
return present;
}
}
Expand All @@ -104,6 +115,7 @@ public static Present initAdminPresent(Wish wish) {
.wish(wish)
.name("선물주 운영자")
.message("초기 선물 내용")
.giftMenu(GiftMenu.getLetter())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public record CakeCreateRequest(
String name,
String message,
Long cakeId,
Long wishId
Long wishId,
Long giftMenuId
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ public record CakeCreateResponseDTO(
String imageUrl,
String hint,
String initial,
String contribute,
String wisher
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.sopterm.makeawish.repository;

import com.sopterm.makeawish.domain.GiftMenu;
import org.springframework.data.jpa.repository.JpaRepository;

public interface GiftMenuRepository extends JpaRepository<GiftMenu, Long> {
}
14 changes: 11 additions & 3 deletions src/main/java/com/sopterm/makeawish/service/CakeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import com.sopterm.makeawish.common.KakaoPayProperties;
import com.sopterm.makeawish.common.Util;
import com.sopterm.makeawish.domain.Cake;
import com.sopterm.makeawish.domain.GiftMenu;
import com.sopterm.makeawish.domain.Present;
import com.sopterm.makeawish.domain.wish.Wish;
import com.sopterm.makeawish.dto.cake.*;
import com.sopterm.makeawish.dto.present.PresentDTO;
import com.sopterm.makeawish.dto.present.PresentResponseDTO;
import com.sopterm.makeawish.repository.CakeRepository;
import com.sopterm.makeawish.repository.GiftMenuRepository;
import com.sopterm.makeawish.repository.PresentRepository;
import jakarta.persistence.EntityNotFoundException;
import lombok.*;
Expand Down Expand Up @@ -36,6 +38,7 @@ public class CakeService {
private final WishService wishService;
private final CakeRepository cakeRepository;
private final PresentRepository presentRepository;
private final GiftMenuRepository giftMenuRepository;

public List<CakeResponseDTO> getAllCakes() {
return cakeRepository.findAll()
Expand Down Expand Up @@ -165,15 +168,20 @@ public List<PresentResponseDTO> getEachPresent(Long userId, Long wishId, Long ca
public CakeCreateResponseDTO createPresent(CakeCreateRequest request) {
val cake = getCake(request.cakeId());
val wish = wishService.getWish(request.wishId());
val giftMenu = getGiftMenuInfo(request.giftMenuId());
val present = Present.builder()
.name(request.name())
.message(request.message())
.cake(cake)
.wish(wish)
.giftMenu(giftMenu)
.build();
presentRepository.save(present);
wish.updateTotalPrice(cake.getPrice());
val contribute = Util.calculateContribution(cake.getPrice(), wish.getPresentPrice());
return new CakeCreateResponseDTO(cake.getId(), wish.getPresentImageUrl(), wish.getHint(), wish.getInitial(), contribute, wish.getWisher().getNickname());
wish.updateTotalPrice(giftMenu.getPrice());
return new CakeCreateResponseDTO(cake.getId(), wish.getPresentImageUrl(), wish.getHint(), wish.getInitial(), wish.getWisher().getNickname());
}

private GiftMenu getGiftMenuInfo(Long giftMenuId){
return giftMenuRepository.findById(giftMenuId).orElseThrow(() -> new EntityNotFoundException(INVALID_GIFT_MENU.getMessage()));
}
}
3 changes: 3 additions & 0 deletions src/main/resources/db/migration/V3.4__cakes.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
alter table present add column gift_menu_id int
constraint gift_menu_present references gift_menu(gift_menu_id)
on update cascade on delete cascade;
14 changes: 11 additions & 3 deletions src/test/java/com/sopterm/makeawish/service/CakeServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import com.sopterm.makeawish.common.Util;
import com.sopterm.makeawish.domain.Cake;
import com.sopterm.makeawish.domain.GiftMenu;
import com.sopterm.makeawish.domain.user.AccountInfo;
import com.sopterm.makeawish.domain.user.SocialType;
import com.sopterm.makeawish.domain.user.User;
import com.sopterm.makeawish.domain.wish.Wish;
import com.sopterm.makeawish.dto.cake.CakeCreateRequest;
import com.sopterm.makeawish.dto.cake.CakeReadyRequestDTO;
import com.sopterm.makeawish.repository.CakeRepository;
import com.sopterm.makeawish.repository.GiftMenuRepository;
import com.sopterm.makeawish.repository.UserRepository;
import com.sopterm.makeawish.repository.wish.WishRepository;
import org.assertj.core.api.ThrowableAssert;
Expand Down Expand Up @@ -48,6 +50,9 @@ class CakeServiceTest {
@Autowired
WishRepository wishRepository;

@Autowired
GiftMenuRepository giftMenuRepository;

@BeforeAll
void 케이크_세팅() {
cakeRepository.save(new Cake(1L, "케이크1", 0, "image1"));
Expand Down Expand Up @@ -92,10 +97,12 @@ class CakeServiceTest {
User user = userRepository.save(createUser());
Wish wish = wishRepository.save(createWish(user));
Cake cake = cakeService.getCake(1L);
GiftMenu giftMenu = giftMenuRepository.save(new GiftMenu(0L, "letter", 0));

int prevTotalPrice = wish.getTotalPrice();

// when
cakeService.createPresent(new CakeCreateRequest("최아무", "메세지", cake.getId(), wish.getId()));
cakeService.createPresent(new CakeCreateRequest("최아무", "메세지", cake.getId(), wish.getId(), giftMenu.getId()));

//then
assertThat(prevTotalPrice).isEqualTo(wishService.getWish(wish.getId()).getTotalPrice());
Expand All @@ -107,13 +114,14 @@ class CakeServiceTest {
// given
User user = userRepository.save(createUser());
Wish wish = wishRepository.save(createWish(user));
GiftMenu giftMenu = giftMenuRepository.save(new GiftMenu(1L, "sushi", 4900));
Cake cake = cakeService.getCake(5L);
int prevTotalPrice = wish.getTotalPrice();

// when
cakeService.createPresent(new CakeCreateRequest("최아무", "메세지", cake.getId(), wish.getId()));
cakeService.createPresent(new CakeCreateRequest("최아무", "메세지", cake.getId(), wish.getId(), giftMenu.getId()));
//then
assertThat(prevTotalPrice).isEqualTo(wishService.getWish(wish.getId()).getTotalPrice() - cake.getPrice());
assertThat(prevTotalPrice).isEqualTo(wishService.getWish(wish.getId()).getTotalPrice() - giftMenu.getPrice());
}

private String getLocalDateTime(int plusDays){
Expand Down

0 comments on commit b7b3938

Please sign in to comment.