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

refactor: 식단 품절, 변경 여부 (Boolean -> 시간)으로 변경 #310

Merged
merged 6 commits into from
Apr 6, 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 @@ -9,11 +9,11 @@

import in.koreatech.koin.domain.coop.dto.DiningImageRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import in.koreatech.koin.domain.coop.dto.SoldOutRequest;
import in.koreatech.koin.domain.coop.service.CoopService;
import in.koreatech.koin.global.auth.Auth;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;

@RestController
Expand All @@ -26,7 +26,7 @@ public class CoopController implements CoopApi {
@PatchMapping("/dining/soldout")
public ResponseEntity<Void> changeSoldOut(
@Auth(permit = {COOP}) Long userId,
@RequestBody SoldOutRequest soldOutRequest
@Valid @RequestBody SoldOutRequest soldOutRequest
) {
coopService.changeSoldOut(soldOutRequest);
return ResponseEntity.ok().build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
import com.fasterxml.jackson.databind.annotation.JsonNaming;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;

@JsonNaming(value = PropertyNamingStrategies.SnakeCaseStrategy.class)
public record SoldOutRequest(
@Schema(description = "메뉴 고유 ID", example = "1")
Long menuId,

@NotNull
@Schema(description = "품절 여부", example = "true")
Boolean soldOut
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package in.koreatech.koin.domain.coop.service;

import java.time.Clock;
import java.time.LocalDateTime;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -16,10 +19,18 @@ public class CoopService {

private final DiningRepository diningRepository;

private final Clock clock;

@Transactional
public void changeSoldOut(SoldOutRequest soldOutRequest) {
Dining dining = diningRepository.getById(soldOutRequest.menuId());
dining.setSoldOut(soldOutRequest.soldOut());

if (Boolean.TRUE.equals(soldOutRequest.soldOut())) {
dining.setSoldOut(LocalDateTime.now(clock));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

R

품절 취소를 하고 싶으면 요청을 어떻게 받아야 하는지, 로직을 어떻게 짜야 하는지 고민해봐야 할 것 같아요

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

품절 취소는 생각도 못했네요

수정했습니다

}
else {
dining.setSoldOut(null);
}
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,13 @@ public record DiningResponse(
@Schema(description = "최신화 일자", example = "2024-03-15 14:02:48")
LocalDateTime updatedAt,

@Schema(description = "품절 여부", example = "true")
Boolean soldOut,
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@Schema(description = "품절 시각", example = "2024-04-04 23:01:52")
LocalDateTime soldOut,

@Schema(description = "메뉴 변경 여부", example = "true")
Boolean isChanged
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@Schema(description = "메뉴 변경 시각", example = "2024-04-04 23:01:52")
LocalDateTime isChanged
) {

public static DiningResponse from(Dining dining) {
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/in/koreatech/koin/domain/dining/model/Dining.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package in.koreatech.koin.domain.dining.model;

import java.time.LocalDateTime;

import in.koreatech.koin.global.domain.BaseEntity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
Expand Down Expand Up @@ -52,17 +54,15 @@ public class Dining extends BaseEntity {
@Column(name = "image_url")
private String imageUrl;

@NotNull
@Column(name = "sold_out", nullable = false)
private Boolean soldOut = false;
@Column(name = "sold_out")
private LocalDateTime soldOut;

@NotNull
@Column(name = "is_changed", nullable = false)
private Boolean isChanged = false;
@Column(name = "is_changed")
private LocalDateTime isChanged;

@Builder
private Dining(Long id, String date, String type, String place, Integer priceCard, Integer priceCash,
Integer kcal, String menu, String imageUrl, Boolean soldOut, Boolean isChanged) {
Integer kcal, String menu, String imageUrl, LocalDateTime soldOut, LocalDateTime isChanged) {
this.id = id;
this.date = date;
this.type = type;
Expand All @@ -80,7 +80,7 @@ public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}

public void setSoldOut(Boolean soldout) {
public void setSoldOut(LocalDateTime soldout) {
this.soldOut = soldout;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
ALTER TABLE `dining_menus`
MODIFY COLUMN sold_out BOOLEAN NULL,
MODIFY COLUMN is_changed BOOLEAN NULL;

UPDATE `dining_menus`
SET sold_out = NULL,
is_changed = NULL;

ALTER TABLE `dining_menus`
MODIFY COLUMN sold_out DATETIME NULL,
MODIFY COLUMN is_changed DATETIME NULL;
44 changes: 18 additions & 26 deletions src/test/java/in/koreatech/koin/acceptance/DiningApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static org.mockito.Mockito.when;

import java.time.Clock;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
Expand Down Expand Up @@ -54,8 +55,6 @@ void findDinings() {
.kcal(881)
.menu("""
["병아리콩밥", "(탕)소고기육개장", "땡초부추전", "누룽지탕"]""")
.soldOut(false)
.isChanged(false)
.build();

Dining request2 = Dining.builder()
Expand All @@ -68,8 +67,6 @@ void findDinings() {
.kcal(881)
.menu("""
["혼합잡곡밥", "가쓰오장국", "땡초부추전", "누룽지탕"]""")
.soldOut(false)
.isChanged(false)
.build();

Dining request3 = Dining.builder()
Expand All @@ -82,8 +79,6 @@ void findDinings() {
.kcal(300)
.menu("""
["참치김치볶음밥", "유부된장국", "땡초부추전", "누룽지탕"]""")
.soldOut(false)
.isChanged(true)
.build();

Dining dining1 = diningRepository.save(request1);
Expand Down Expand Up @@ -119,7 +114,7 @@ void findDinings() {
.isEqualTo(dining1.getUpdatedAt().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
softly.assertThat(response.body().jsonPath().getList("[0].menu", String.class))
.containsExactlyInAnyOrderElementsOf(menus1);
softly.assertThat(response.body().jsonPath().getBoolean("[0].is_changed"))
softly.assertThat((LocalDateTime)response.body().jsonPath().get("[0].is_changed"))
.isEqualTo(dining1.getIsChanged());

softly.assertThat(response.body().jsonPath().getLong("[1].id")).isEqualTo(dining3.getId());
Expand All @@ -137,7 +132,7 @@ void findDinings() {
.isEqualTo(dining3.getUpdatedAt().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
softly.assertThat(response.body().jsonPath().getList("[1].menu", String.class))
.containsExactlyInAnyOrderElementsOf(menus2);
softly.assertThat(response.body().jsonPath().getBoolean("[1].is_changed"))
softly.assertThat((LocalDateTime)response.body().jsonPath().get("[1].is_changed"))
.isEqualTo(dining3.getIsChanged());

}
Expand All @@ -157,8 +152,6 @@ void invalidFormatDate() {
.kcal(881)
.menu("""
["병아리콩밥", "(탕)소고기육개장", "땡초부추전", "누룽지탕"]""")
.soldOut(false)
.isChanged(false)
.build();

Dining dining = diningRepository.save(request);
Expand Down Expand Up @@ -188,8 +181,6 @@ void nullDate() {
.kcal(881)
.menu("""
["병아리콩밥", "(탕)소고기육개장", "땡초부추전", "고구마순들깨볶음", "총각김치"]""")
.soldOut(false)
.isChanged(false)
.build();

Dining request2 = Dining.builder()
Expand All @@ -202,8 +193,6 @@ void nullDate() {
.kcal(881)
.menu("""
["혼합잡곡밥", "가쓰오장국", "땡초부추전", "누룽지탕"]""")
.soldOut(false)
.isChanged(false)
.build();

Dining dining1 = diningRepository.save(request1);
Expand Down Expand Up @@ -242,7 +231,7 @@ void nullDate() {
}

@Test
@DisplayName("영양사 권한으로 품절 요청을 보낸다")
@DisplayName("영양사 권한으로 품절 요청을 보내고 메뉴를 변경한다.")
void requestSoldOut() {
User user = User.builder()
.password("1234")
Expand All @@ -259,6 +248,13 @@ void requestSoldOut() {

String token = jwtProvider.createToken(user);

when(clock.instant()).thenReturn(ZonedDateTime.parse(
"2024-04-04 18:00:00 KST",
ofPattern("yyyy-MM-dd " + "HH:mm:ss z")
)
.toInstant());
when(clock.getZone()).thenReturn(Clock.systemDefaultZone().getZone());

Dining dining1 = Dining.builder()
.date("2024-03-11")
.type("LUNCH")
Expand All @@ -268,8 +264,7 @@ void requestSoldOut() {
.kcal(881)
.menu("""
["병아리콩밥", "(탕)소고기육개장", "땡초부추전", "누룽지탕"]""")
.soldOut(false)
.isChanged(false)
.isChanged(LocalDateTime.now(clock))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C

테스트 이름에 식단 변경 여부에 대한 내용도 포함되면 좋을 것 같아요!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

수정했습니다

.build();

Dining dining2 = Dining.builder()
Expand All @@ -281,8 +276,6 @@ void requestSoldOut() {
.kcal(881)
.menu("""
["병아리", "소고기", "땡초", "탕"]""")
.soldOut(false)
.isChanged(false)
.build();

diningRepository.save(dining1);
Expand All @@ -301,7 +294,12 @@ void requestSoldOut() {
.extract();

SoftAssertions.assertSoftly(
softly -> softly.assertThat(diningRepository.getById(2L).getSoldOut()).isEqualTo(true)
softly -> {
softly.assertThat(diningRepository.getById(1L).getIsChanged()).isEqualTo(LocalDateTime.now(clock));

softly.assertThat(diningRepository.getById(2L).getSoldOut()).isEqualTo(LocalDateTime.now(clock));
softly.assertThat(diningRepository.getById(2L).getIsChanged()).isNull();
}
);
}

Expand Down Expand Up @@ -332,8 +330,6 @@ void requestSoldOutNoAuth() {
.kcal(881)
.menu("""
["병아리콩밥", "(탕)소고기육개장", "땡초부추전", "누룽지탕"]""")
.soldOut(false)
.isChanged(false)
.build();

diningRepository.save(dining1);
Expand Down Expand Up @@ -379,8 +375,6 @@ void ImageUpload() {
.kcal(881)
.menu("""
["병아리콩밥", "(탕)소고기육개장", "땡초부추전", "누룽지탕"]""")
.soldOut(false)
.isChanged(false)
.build();

Dining dining = diningRepository.save(request);
Expand Down Expand Up @@ -433,8 +427,6 @@ void ImageUploadWithNoAuth() {
.kcal(881)
.menu("""
["병아리콩밥", "(탕)소고기육개장", "땡초부추전", "누룽지탕"]""")
.soldOut(false)
.isChanged(false)
.build();

Dining dining = diningRepository.save(request);
Expand Down
Loading