Skip to content

Commit

Permalink
Merge pull request #30 from KUSITMS-Team-A/feature/29-event-manage
Browse files Browse the repository at this point in the history
[#29] 이벤트 관리 API 구현
  • Loading branch information
yeop0740 authored Nov 22, 2023
2 parents 7d096d7 + 14a217e commit 59c4b74
Show file tree
Hide file tree
Showing 17 changed files with 658 additions and 43 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/backend/common/dto/ResponseDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static <T> ResponseEntity<T> ok(T data) {

public static ResponseEntity<Void> ok() {
return ResponseEntity
.status(HttpStatus.CREATED)
.status(HttpStatus.OK)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.backend.common.domain.BaseEntity;
import com.backend.domain.contract.dto.UpdateBenefitRequest;
import com.backend.domain.contract.dto.UpdateContractRequest;
import com.backend.domain.event.entity.Event;
import com.backend.domain.store.entity.Store;
import com.backend.domain.university.entity.University;
import jakarta.persistence.*;
Expand Down Expand Up @@ -36,7 +37,7 @@ public class Contract extends BaseEntity {
@OneToMany(mappedBy = "contract", cascade = CascadeType.PERSIST)
private List<Benefit> benefits = new ArrayList<>();

@OneToMany(mappedBy = "contract")
@OneToMany(mappedBy = "contract", cascade = CascadeType.PERSIST)
private List<Event> events = new ArrayList<>();

@ManyToOne(fetch = FetchType.LAZY)
Expand Down
41 changes: 0 additions & 41 deletions src/main/java/com/backend/domain/contract/entity/Event.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Optional;

public interface ContractRepository extends JpaRepository<Contract, Long> {
Expand Down Expand Up @@ -61,4 +62,6 @@ Page<Contract> findAllByUniversityAndStoreNameAndCategory(@Param(value = "univer

Optional<Contract> findByUniversityAndStore(University university, Store store);

List<Contract> findAllByUniversity(University university);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.backend.domain.event.controller;

import com.backend.common.dto.ResponseDto;
import com.backend.domain.auth.dto.Login;
import com.backend.domain.auth.dto.LoginUser;
import com.backend.domain.event.dto.*;
import com.backend.domain.event.service.EventService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("/event")
@SecurityRequirement(name = "bearer-key")
@Tag(name = "이벤트", description = "이벤트 관리 API")
public class EventController {

private final EventService eventService;

@PostMapping
@Operation(
summary = "이벤트 등록",
description =
"<p>쿠폰과 스탬프를 등록합니다</p>" +
"<p>쿠폰을 등록할 때에는 type을 COUPON으로 선택하고 startDate와 duration을 입력하지 않는다.</p>" +
"<p>스탬프를 등록할 때에는 type을 STAMP 선택하고 startDate와 duration을 입력한다. discount는 입력하지 않는다.</p>")
public ResponseEntity<Void> createEvent(@Parameter(hidden = true) @Login LoginUser loginUser, @RequestBody CreateEventRequest request) {
eventService.createEvent(loginUser, request);
return ResponseDto.created();
}

@GetMapping("/coupon")
@Operation(summary = "쿠폰 목록 조회")
public ResponseEntity<ReadCouponsDto> readCoupons(@Parameter(hidden = true) @Login LoginUser loginUser, @ModelAttribute ReadEventsRequest request) {
ReadCouponsDto events = eventService.readCoupons(loginUser, request);
return ResponseDto.ok(events);
}

@GetMapping
@Operation(summary = "이벤트 목록 조회")
public ResponseEntity<ReadEventsDto> readEvents(@Parameter(hidden = true) @Login LoginUser loginUser, @ModelAttribute ReadEventsRequest request) {
ReadEventsDto events = eventService.readEvents(loginUser, request);
return ResponseDto.ok(events);
}

@PatchMapping
@Operation(summary = "이벤트 수정")
public ResponseEntity<Void> updateEvent(@Parameter(hidden = true) @Login LoginUser loginUser, @RequestBody UpdateEventRequest request) {
eventService.updateEvent(loginUser, request);
return ResponseDto.ok();
}


@DeleteMapping("/{eventId}")
@Operation(summary = "이벤트 삭제")
public ResponseEntity<Void> deleteEvent(@Parameter(hidden = true) @Login LoginUser loginUser, @Parameter(name = "eventId") @PathVariable Long eventId) {
eventService.deleteEvent(loginUser, eventId);
return ResponseDto.ok();
}

}
68 changes: 68 additions & 0 deletions src/main/java/com/backend/domain/event/dto/CreateEventRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.backend.domain.event.dto;

import com.backend.domain.benefit.entity.BenefitType;
import com.backend.domain.event.entity.Condition;
import com.backend.domain.event.entity.Event;
import com.backend.domain.popup.domain.EndDateType;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDate;
import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class CreateEventRequest {

@Schema(example = "3")
private Long storeId;

@Schema(example = "COUPON")
private BenefitType type;

@Schema(example = "5000원 할인 쿠폰")
private String name;

private List<String> conditions;

private int discount;

@Schema(example = "50")
private int quantity;

@Schema(example = "2023-11-22")
private LocalDate startDate;

@Schema(example = "일주일간")
private EndDateType duration;

public Event toEntity() {
Event event = Event.builder()
.type(type)
.name(name)
.quantity(createDiscount())
.discount(discount)
.startDate(startDate)
.endDate(startDate.plusDays(duration.getPlusDate()))
.build();
event.addAll(createConditions());
return event;
}

private int createDiscount() {
if (type.equals(BenefitType.STAMP)) {
return 0;
}
return discount;
}

private List<Condition> createConditions() {
return conditions.stream()
.map(Condition::new)
.toList();
}

}
47 changes: 47 additions & 0 deletions src/main/java/com/backend/domain/event/dto/ReadCouponDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.backend.domain.event.dto;

import com.backend.domain.event.entity.Condition;
import com.backend.domain.event.entity.Event;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ReadCouponDto {

private Long couponStoreId;

private String couponStore;

private Long couponId;

private String couponName;

private List<String> couponCondition;

private int couponQuantity;

public static ReadCouponDto from(Event coupon) {
return ReadCouponDto.builder()
.couponStoreId(coupon.getContract().getStore().getStoreId())
.couponStore(coupon.getContract().getStore().getName())
.couponId(coupon.getEventId())
.couponName(coupon.getName())
.couponQuantity(coupon.getQuantity())
.couponCondition(getConditionStrings(coupon))
.build();
}

private static List<String> getConditionStrings(Event coupon) {
return coupon.getConditions().stream()
.map(Condition::getContent)
.toList();
}

}
41 changes: 41 additions & 0 deletions src/main/java/com/backend/domain/event/dto/ReadCouponsDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.backend.domain.event.dto;

import com.backend.domain.event.entity.Event;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.domain.Page;

import java.util.List;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ReadCouponsDto {

private List<ReadCouponDto> coupons;

private int pageNumber;

private Long totalCount;

private int totalPages;

public static ReadCouponsDto from(Page<Event> coupons) {
return ReadCouponsDto.builder()
.pageNumber(coupons.getNumber())
.totalCount(coupons.getTotalElements())
.totalPages(coupons.getTotalPages())
.coupons(getReadCouponDtos(coupons))
.build();
}

private static List<ReadCouponDto> getReadCouponDtos(Page<Event> coupons) {
return coupons.getContent().stream()
.map(ReadCouponDto::from)
.toList();
}

}
50 changes: 50 additions & 0 deletions src/main/java/com/backend/domain/event/dto/ReadEventDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.backend.domain.event.dto;

import com.backend.domain.benefit.entity.BenefitType;
import com.backend.domain.event.entity.Condition;
import com.backend.domain.event.entity.Event;
import com.backend.domain.store.dto.StoreDto;
import com.backend.domain.store.entity.Store;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ReadEventDto {

private StoreDto store;

private Long eventId;

private BenefitType eventType;

private String eventName;

private List<String> eventCondition;

private int eventQuantity;

public static ReadEventDto from(Event event, Store store) {
return ReadEventDto.builder()
.eventId(event.getEventId())
.eventType(event.getType())
.eventName(event.getName())
.eventQuantity(event.getQuantity())
.store(StoreDto.from(store))
.eventCondition(getConditionStrings(event))
.build();
}

private static List<String> getConditionStrings(Event event) {
return event.getConditions().stream()
.map(Condition::getContent)
.toList();
}

}
Loading

0 comments on commit 59c4b74

Please sign in to comment.