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

[Feature/event-seller] Event-Seller 매핑 #82

Merged
merged 7 commits into from
Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -21,12 +21,12 @@

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/events")
@RequestMapping("/api/v1/event")
public class EventController {

private final EventService eventService;

@PostMapping()
@PostMapping
public ResponseEntity<ParaboleResponse> createEvent(@RequestBody EventCreateRequestDto eventDto) {
Long eventId = -1L;
try {
Expand All @@ -44,7 +44,7 @@ public ResponseEntity<ParaboleResponse> getEvent(@PathVariable("eventId") Long e
return ParaboleResponse.CommonResponse(HttpStatus.OK, true, eventId+"번 이벤트 조회 성공", response);
}

@GetMapping()
@GetMapping
public ResponseEntity<ParaboleResponse> getEvent() {
List<Event> eventEntities = eventService.getEventsAllNotDeleted();
List<EventListResponseDto> response = eventEntities.stream()
Expand Down
42 changes: 10 additions & 32 deletions src/main/java/com/feelmycode/parabole/domain/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,10 @@ public class Event extends BaseEntity implements Serializable {
@Column(name = "event_id")
private Long id;

// @ManyToOne
// @JoinColumn(name = "seller_id")
// @JsonBackReference
// private Seller seller;
@Column(name = "seller_id")
private Long sellerId;
@ManyToOne
@JoinColumn(name = "seller_id")
@JsonBackReference
private Seller seller;

@Column(name = "created_by")
private String createdBy; // [ADMIN, SELLER]
Expand Down Expand Up @@ -79,22 +77,21 @@ public void setDeleted() {
this.isDeleted = false;
}

// public void setSeller(Seller seller) {
// this.seller = seller;
// seller.getEvents().add(this);
// }
public void setSeller(Seller seller) {
this.seller = seller;
seller.getEvents().add(this);
}

public void addEventPrize(EventPrize eventPrize) {
eventPrizes.add(eventPrize);
eventPrize.setEvent(this);
}

@Builder
public Event(/*Seller seller*/Long sellerId, String createdBy, String type, String title,
public Event(Seller seller, String createdBy, String type, String title,
LocalDateTime startAt, LocalDateTime endAt, String descript, EventImage eventImage,
List<EventPrize> eventPrizes) {
//this.seller = seller;
this.sellerId = sellerId;
this.seller = seller;
this.createdBy = createdBy;
this.type = type;
this.title = title;
Expand All @@ -116,25 +113,6 @@ public void cancel() {
if(status!=0 || (LocalDateTime.now()).isAfter(startAt)) {
throw new IllegalStateException("이미 시작된 이벤트는 취소가 불가능합니다.");
}

setDeleted();
}

@Override
public String toString() {
return "Event{" +
"id=" + id +
//", seller=" + seller +
", sellerId=" + sellerId + '\'' +
", createdBy='" + createdBy + '\'' +
", type='" + type + '\'' +
", title='" + title + '\'' +
", startAt='" + startAt + '\'' +
", endAt='" + endAt + '\'' +
", status=" + status +
", descript='" + descript + '\'' +
", eventImage=" + eventImage +
", eventPrizes=" + eventPrizes +
'}';
}
}
6 changes: 6 additions & 0 deletions src/main/java/com/feelmycode/parabole/domain/Seller.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.feelmycode.parabole.domain;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
Expand Down Expand Up @@ -38,6 +41,9 @@ public class Seller extends BaseEntity {
@NotNull
private String registrationNo;

@OneToMany(mappedBy = "seller")
private List<Event> events = new ArrayList<>();

public Seller(String storeName, String registrationNo) {
this.storeName = storeName;
this.registrationNo = registrationNo;
Expand Down
7 changes: 2 additions & 5 deletions src/main/java/com/feelmycode/parabole/dto/EventDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.feelmycode.parabole.domain.EventPrize;
import com.feelmycode.parabole.domain.Seller;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import lombok.AccessLevel;
Expand All @@ -21,8 +20,7 @@
public class EventDto {

private Long id;
// private Seller seller;
private Long sellerId;
private Seller seller;
private String createdBy;
private String type;
private String title;
Expand All @@ -36,8 +34,7 @@ public class EventDto {
static public EventDto of(Event event) {
return EventDto.builder()
.id(event.getId())
//.seller(event.getSeller())
.sellerId(event.getSellerId())
.seller(event.getSeller())
.createdBy(event.getCreatedBy())
.type(event.getType())
.title(event.getTitle())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ public class EventListResponseDto {
static public EventListResponseDto of(Event event) {
return EventListResponseDto.builder()
.id(event.getId())
//.sellerId(event.getSeller().getId())
.sellerId(event.getSellerId())
.sellerId(event.getSeller().getId())
.createdBy(event.getCreatedBy())
.type(event.getType())
.title(event.getTitle())
Expand Down
38 changes: 18 additions & 20 deletions src/main/java/com/feelmycode/parabole/service/EventService.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package com.feelmycode.parabole.service;

//import com.feelmycode.parabole.domain.Coupon;
//import com.feelmycode.parabole.domain.Seller;
//import com.feelmycode.parabole.repository.CouponRepository;
//import com.feelmycode.parabole.repository.SellerRepository;
import com.feelmycode.parabole.domain.Coupon;
import com.feelmycode.parabole.domain.Seller;
import com.feelmycode.parabole.repository.CouponRepository;
import com.feelmycode.parabole.repository.SellerRepository;
import com.feelmycode.parabole.dto.EventPrizeCreateRequestDto;
import com.feelmycode.parabole.domain.Event;
import com.feelmycode.parabole.domain.EventPrize;
import com.feelmycode.parabole.domain.Product;
//import com.feelmycode.parabole.domain.Seller;
import com.feelmycode.parabole.dto.EventCreateRequestDto;
import com.feelmycode.parabole.global.error.exception.ParaboleException;
import com.feelmycode.parabole.repository.EventRepository;
Expand All @@ -28,26 +27,26 @@ public class EventService {

private final EventRepository eventRepository;

//private final SellerRepository sellerRepository;
private final SellerRepository sellerRepository;

//private final CouponRepository couponRepository;
private final CouponRepository couponRepository;

private final ProductRepository productRepository;

// private Seller getSeller(Long sellerId) {
// return sellerRepository.findById(sellerId)
// .orElseThrow(() -> new ParaboleException(HttpStatus.NOT_FOUND, "해당하는 ID의 판매자가 없습니다"));
// }
private Seller getSeller(Long sellerId) {
return sellerRepository.findById(sellerId)
.orElseThrow(() -> new ParaboleException(HttpStatus.NOT_FOUND, "해당하는 ID의 판매자가 없습니다"));
}

private Product getProduct(Long productId) {
return productRepository.findById(productId)
.orElseThrow(() -> new ParaboleException(HttpStatus.NOT_FOUND, "해당하는 ID의 상품이 없습니다"));
}

// private Coupon getCoupon(Long couponId) {
// return couponRepository.findById(couponId)
// .orElseThrow(() -> new ParaboleException(HttpStatus.NOT_FOUND, "해당하는 ID의 쿠폰이 없습니다."));
// }
private Coupon getCoupon(Long couponId) {
return couponRepository.findById(couponId)
.orElseThrow(() -> new ParaboleException(HttpStatus.NOT_FOUND, "해당하는 ID의 쿠폰이 없습니다."));
}

/**
* 이벤트 생성
Expand All @@ -56,8 +55,7 @@ private Product getProduct(Long productId) {
public Long createEvent(EventCreateRequestDto eventDto) {

// 엔티티 조회
//Seller seller = getSeller(eventDto.getSellerId());
Long sellerId = eventDto.getSellerId();
Seller seller = getSeller(eventDto.getSellerId());

// 이벤트-경품정보 생성
List<EventPrize> eventPrizeList = new ArrayList<>();
Expand All @@ -72,15 +70,15 @@ public Long createEvent(EventCreateRequestDto eventDto) {
if (prizeType.equals("PRODUCT")) {
eventPrizeList.add(new EventPrize(prizeType, eventPrizeParam.getStock(), getProduct(id)));
} else {
//eventPrizeList.add(new EventPrize(prizeType, eventPrizeParam.getStock(), getCoupon(id)));
eventPrizeList.add(new EventPrize(prizeType, eventPrizeParam.getStock(), getCoupon(id)));
}
}
}

// 이벤트 생성
Event event = Event.builder()
//.seller(seller)
.sellerId(sellerId).createdBy(eventDto.getCreatedBy()).type(eventDto.getType())
.seller(seller)
.createdBy(eventDto.getCreatedBy()).type(eventDto.getType())
.title(eventDto.getTitle()).startAt(eventDto.getStartAt()).endAt(eventDto.getEndAt())
.descript(eventDto.getDescript()).eventImage(eventDto.getEventImage())
.eventPrizes(eventPrizeList).build();
Expand Down