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

[#35] 제휴 가게 조회 API 스펙 변경 #38

Merged
merged 3 commits into from
Nov 22, 2023
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
16 changes: 6 additions & 10 deletions src/main/java/com/backend/domain/benefit/entity/Benefit.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.backend.domain.benefit.entity;

import com.backend.common.domain.BaseEntity;
import com.backend.domain.contract.dto.UpdateBenefitRequest;
import com.backend.domain.contract.entity.Contract;
import jakarta.persistence.*;
import lombok.AccessLevel;
Expand All @@ -23,17 +22,20 @@ public class Benefit extends BaseEntity {

private int amount;

private String content;
private String menu;

private String conditions;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "contract_id")
private Contract contract;

@Builder
public Benefit(BenefitType type, int amount, String content) {
public Benefit(BenefitType type, int amount, String conditions, String menu) {
this.type = type;
this.amount = amount;
this.content = content;
this.conditions = conditions;
this.menu = menu;
}

public void add(Contract contract) {
Expand All @@ -44,10 +46,4 @@ public void expire() {
contract = null;
}

public void update(UpdateBenefitRequest request) {
type = request.getType();
amount = request.getAmount();
content = request.getContent();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
import com.backend.common.dto.ResponseDto;
import com.backend.domain.auth.dto.Login;
import com.backend.domain.auth.dto.LoginUser;
import com.backend.domain.contract.dto.CreateContractRequest;
import com.backend.domain.contract.dto.ReadContractDetailsDto;
import com.backend.domain.contract.dto.ReadContractsDto;
import com.backend.domain.contract.dto.UpdateContractRequest;
import com.backend.domain.contract.dto.*;
import com.backend.domain.contract.service.ContractService;
import com.backend.domain.store.dto.ReadRequest;
import io.swagger.v3.oas.annotations.Operation;
Expand Down
22 changes: 16 additions & 6 deletions src/main/java/com/backend/domain/contract/dto/BenefitDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,29 @@ public class BenefitDto {

private Long benefitId;

private BenefitType type;

private int amount;
private String condition;

private String content;

public static BenefitDto from(Benefit benefit) {
return BenefitDto.builder()
.benefitId(benefit.getBenefitId())
.type(benefit.getType())
.amount(benefit.getAmount())
.content(benefit.getContent())
.content(createContent(benefit))
.condition(benefit.getConditions())
.build();
}

public static String createContent(Benefit benefit) {
if (benefit.getType().equals(BenefitType.FIX)) {
return benefit.getAmount() + "원 할인";
}
if (benefit.getType().equals(BenefitType.RATE)) {
return benefit.getAmount() + "% 할인";
}
if (benefit.getType().equals(BenefitType.MENU)) {
return benefit.getMenu() + " 무료 제공";
}
throw new RuntimeException();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,30 @@ public class CreateBenefitRequest {
private int amount;

@Schema(description = "조건 작성", example = "메인메뉴 구입시")
private String content;
private String condition;

private String menu;

public Benefit toEntity() {
validateType();
return Benefit.builder()
.type(type)
.amount(amount)
.content(content)
.menu(menu)
.conditions(condition)
.build();
}

public void validateType() {
if (type.equals(BenefitType.MENU)) {
if (menu == null) {
throw new RuntimeException();
}
} else {
if (menu != null) {
menu = null;
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,27 @@ public class CreateContractRequest {
private LocalDate endDate;

public Contract toEntity(User user, Store store) {
List<Benefit> benefits = this.benefits.stream()
.map(CreateBenefitRequest::toEntity)
.toList();
return Contract.builder()
.startDate(startDate == null ? LocalDate.now() : startDate)
.startDate(createStartDate())
.endDate(endDate)
.manager(user.getTypeName())
.university(user.getUniversity())
.store(store)
.benefits(benefits)
.benefits(createBenefits())
.build();
}

public LocalDate createStartDate() {
if (startDate == null) {
return LocalDate.now();
}
return startDate;
}

public List<Benefit> createBenefits() {
return this.benefits.stream()
.map(CreateBenefitRequest::toEntity)
.toList();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public class ReadContractDetailsDto {

private LocalDate endDate;

private String address;

private Double latitude;

private Double longitude;
Expand All @@ -51,6 +53,7 @@ public static ReadContractDetailsDto from(Contract contract) {
.mapUrl(contract.getStore().getMapUrl())
.startDate(contract.getStartDate())
.endDate(contract.getEndDate())
.address(contract.getStore().getAddress())
.latitude(contract.getStore().getLatitude())
.longitude(contract.getStore().getLongitude())
.benefits(getBenefitDtos(contract))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,21 @@ public class ReadContractDto {

private Category category;

private String address;

private List<BenefitDto> benefits;

public static ReadContractDto from(Contract contract) {
return ReadContractDto.builder()
.storeId(contract.getStore().getStoreId())
.storeName(contract.getStore().getName())
.category(contract.getStore().getCategory())
.benefits(getBenefitDtos(contract))
.address(contract.getStore().getAddress())
.benefits(createBenefitDtos(contract))
.build();
}

private static List<BenefitDto> getBenefitDtos(Contract contract) {
private static List<BenefitDto> createBenefitDtos(Contract contract) {
return contract.getBenefits().stream()
.map(BenefitDto::from)
.toList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,32 @@ public class UpdateBenefitRequest {
@Schema(description = "할인 받는 양(int형)", example = "10")
private int amount;

@Schema(example = "null")
private String menu;

@Schema(description = "조건 작성", example = "메인메뉴 구입시")
private String content;
private String condition;

public Benefit toEntity() {
validateType();
return Benefit.builder()
.type(type)
.amount(amount)
.content(content)
.menu(menu)
.conditions(condition)
.build();
}

public void validateType() {
if (type.equals(BenefitType.MENU)) {
if (menu == null) {
throw new RuntimeException();
}
} else {
if (menu != null) {
menu = null;
}
}
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.backend.domain.contract.dto;

import com.backend.domain.benefit.entity.Benefit;
import com.backend.domain.contract.entity.Contract;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
Expand Down Expand Up @@ -28,4 +30,11 @@ public class UpdateContractRequest {
@Schema(name = "manager", example = "숭실대 총학생회")
private String manager;

public List<Benefit> createBenefits(Contract contract) {
return benefits.stream()
.map(UpdateBenefitRequest::toEntity)
.peek(b -> b.add(contract))
.toList();
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.backend.domain.contract.entity;

import com.backend.domain.benefit.entity.Benefit;
import com.backend.common.domain.BaseEntity;
import com.backend.domain.contract.dto.UpdateBenefitRequest;
import com.backend.domain.benefit.entity.Benefit;
import com.backend.domain.contract.dto.UpdateContractRequest;
import com.backend.domain.event.entity.Event;
import com.backend.domain.store.entity.Store;
Expand Down Expand Up @@ -83,10 +82,7 @@ public void update(UpdateContractRequest request) {
this.endDate = request.getEndDate();
this.manager = request.getManager();
expireAll();
benefits = request.getBenefits().stream()
.map(UpdateBenefitRequest::toEntity)
.peek(b -> b.add(this))
.toList();
benefits.addAll(request.createBenefits(this));
}

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package com.backend.domain.contract.service;

import com.backend.domain.auth.dto.LoginUser;
import com.backend.domain.contract.dto.CreateContractRequest;
import com.backend.domain.contract.dto.ReadContractDetailsDto;
import com.backend.domain.contract.dto.ReadContractsDto;
import com.backend.domain.contract.dto.UpdateContractRequest;
import com.backend.domain.contract.dto.*;
import com.backend.domain.contract.entity.Contract;
import com.backend.domain.contract.repository.ContractRepository;
import com.backend.domain.store.dto.ReadRequest;
Expand Down
Loading