Skip to content

Commit

Permalink
Merge pull request #42 from KUSITMS-Team-A/feature/39-event-manage
Browse files Browse the repository at this point in the history
[#39] 이벤트 삭제 기능 추가
  • Loading branch information
yeop0740 authored Nov 22, 2023
2 parents 3931408 + f70b49a commit caa578e
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

public enum BenefitType {

FIX, RATE, MENU, COUPON, STAMP
FIX, RATE, MENU

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@
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.entity.EventType;
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.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/event")
Expand Down Expand Up @@ -64,4 +66,10 @@ public ResponseEntity<Void> deleteEvent(@Parameter(hidden = true) @Login LoginUs
return ResponseDto.ok();
}

@DeleteMapping
public ResponseEntity<Void> deleteEvent(@Parameter(hidden = true) @Login LoginUser loginUser, @RequestParam(name = "type") EventType type, @RequestParam(name = "ids") List<Long> ids) {
eventService.deleteEvents(loginUser, type, ids);
return ResponseDto.ok();
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
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.event.entity.EventType;
import com.backend.domain.popup.domain.EndDateType;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotEmpty;
Expand All @@ -24,7 +24,7 @@ public class CreateEventRequest {

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

@Schema(example = "5000원 할인 쿠폰")
@NotEmpty
Expand Down Expand Up @@ -63,7 +63,7 @@ public Event toEntity() {
}

private void validateDuration() {
if (type.equals(BenefitType.STAMP)) {
if (type.equals(EventType.STAMP)) {
if (duration == null) {
throw new RuntimeException();
}
Expand All @@ -77,7 +77,7 @@ public void validateQuantity() {
}

private int createDiscount() {
if (type.equals(BenefitType.STAMP)) {
if (type.equals(EventType.STAMP)) {
return 0;
}
return discount;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/backend/domain/event/dto/ReadEventDto.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
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.event.entity.EventType;
import com.backend.domain.store.dto.StoreDto;
import com.backend.domain.store.entity.Store;
import lombok.AllArgsConstructor;
Expand All @@ -22,7 +22,7 @@ public class ReadEventDto {

private Long eventId;

private BenefitType eventType;
private EventType eventType;

private String eventName;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.backend.domain.event.dto;

import com.backend.domain.benefit.entity.BenefitType;
import com.backend.domain.event.entity.EventType;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
Expand All @@ -13,7 +13,7 @@
public class ReadEventsRequest {

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

@Schema(example = "40")
private int pageSize;
Expand All @@ -24,7 +24,7 @@ public class ReadEventsRequest {
{
pageSize = 40;
pageNumber = 0;
type = BenefitType.COUPON;
type = EventType.COUPON;
}

@Schema(hidden = true)
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/com/backend/domain/event/entity/Event.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.backend.domain.event.entity;

import com.backend.common.domain.BaseEntity;
import com.backend.domain.benefit.entity.BenefitType;
import com.backend.domain.contract.entity.Contract;
import com.backend.domain.event.dto.UpdateEventRequest;
import jakarta.persistence.*;
Expand All @@ -25,7 +24,7 @@ public class Event extends BaseEntity {
private String name;

@Enumerated(EnumType.STRING)
private BenefitType type;
private EventType type;

@OneToMany(mappedBy = "event", cascade = CascadeType.PERSIST, orphanRemoval = true)
private List<Condition> conditions = new ArrayList<>();
Expand All @@ -43,7 +42,7 @@ public class Event extends BaseEntity {
private Contract contract;

@Builder
public Event(BenefitType type, String name, int discount, int quantity, LocalDate startDate, LocalDate endDate, Contract contract) {
public Event(EventType type, String name, int discount, int quantity, LocalDate startDate, LocalDate endDate, Contract contract) {
this.type = type;
this.name = name;
this.discount = discount;
Expand All @@ -68,7 +67,7 @@ public void addAll(List<Condition> conditions) {
}

public void setDate(Contract contract) {
if (type.equals(BenefitType.COUPON)) {
if (type.equals(EventType.COUPON)) {
this.startDate = contract.getStartDate();
this.endDate = contract.getEndDate();
}
Expand All @@ -82,7 +81,7 @@ public void update(UpdateEventRequest request) {
name = request.getName();
discount = request.getDiscount();
quantity = request.getQuantity();
if (type.equals(BenefitType.STAMP)) {
if (type.equals(EventType.STAMP)) {
startDate = request.getStartDate();
endDate = startDate.plusDays(request.getDuration().getPlusDate());
}
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/backend/domain/event/entity/EventType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.backend.domain.event.entity;

import lombok.Getter;

@Getter
public enum EventType {

COUPON, STAMP

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.backend.domain.event.repository;

import com.backend.domain.benefit.entity.BenefitType;
import com.backend.domain.contract.entity.Contract;
import com.backend.domain.event.entity.Event;
import com.backend.domain.event.entity.EventType;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.repository.JpaRepository;
Expand All @@ -19,6 +19,8 @@ public interface EventRepository extends JpaRepository<Event, Long> {
" from Event e" +
" where e.type = :type and e.contract in :contracts and :now between e.startDate and e.endDate"
)
Page<Event> findAllByTypeAndContractIn(@Param(value = "type") BenefitType type, @Param(value = "contracts") List<Contract> contracts, @Param(value = "now") LocalDate now, PageRequest page);
Page<Event> findAllByTypeAndContractIn(@Param(value = "type") EventType type, @Param(value = "contracts") List<Contract> contracts, @Param(value = "now") LocalDate now, PageRequest page);

List<Event> findAllByEventIdInAndType(List<Long> ids, EventType type);

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.backend.domain.contract.repository.ContractRepository;
import com.backend.domain.event.dto.*;
import com.backend.domain.event.entity.Event;
import com.backend.domain.event.entity.EventType;
import com.backend.domain.event.repository.EventRepository;
import com.backend.domain.store.entity.Store;
import com.backend.domain.store.repository.StoreRepository;
Expand Down Expand Up @@ -73,4 +74,9 @@ public void validateAuthority(User user, Event event) {
}
}

public void deleteEvents(LoginUser loginUser, EventType type, List<Long> ids) {
User user = userRepository.findByEmail(loginUser.getEmail()).orElseThrow(RuntimeException::new);
eventRepository.findAllByEventIdInAndType(ids, type).forEach(Event::expire);
}

}

0 comments on commit caa578e

Please sign in to comment.