Skip to content
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
@@ -1,12 +1,13 @@
package com.example.eightyage.domain.coupon.controller;

import com.example.eightyage.domain.coupon.dto.request.CouponRequestDto;
import com.example.eightyage.domain.coupon.dto.response.CouponResponseDto;
import com.example.eightyage.domain.coupon.service.CouponService;
import com.example.eightyage.global.dto.AuthUser;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
Expand All @@ -16,21 +17,25 @@ public class CouponController {

private final CouponService couponService;

@PostMapping("/v1/events/{eventId}/coupons")
public ResponseEntity<CouponResponseDto> issueCoupon(@AuthenticationPrincipal AuthUser authUser, @PathVariable Long eventId) {
return ResponseEntity.ok(couponService.issueCoupon(authUser, eventId));
@PreAuthorize("hasRole('ADMIN')")
@PostMapping("/v1/coupons")
public ResponseEntity<CouponResponseDto> createCoupon(@Valid @RequestBody CouponRequestDto couponRequestDto) {
return ResponseEntity.ok(couponService.saveCoupon(couponRequestDto));
}

@GetMapping("/v1/coupons/my")
public ResponseEntity<Page<CouponResponseDto>> getMyCoupons(
@AuthenticationPrincipal AuthUser authUser,
@RequestParam(defaultValue = "1") int page,
@RequestParam(defaultValue = "10") int size) {
return ResponseEntity.ok(couponService.getMyCoupons(authUser, page, size));
@GetMapping("/v1/coupons")
public ResponseEntity<Page<CouponResponseDto>> getCoupons(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int size) {
return ResponseEntity.ok(couponService.getCoupons(page, size));
}

@GetMapping("/v1/coupons/{couponId}")
public ResponseEntity<CouponResponseDto> getCoupon(@AuthenticationPrincipal AuthUser authUser,@PathVariable Long couponId) {
return ResponseEntity.ok(couponService.getCoupon(authUser, couponId));
public ResponseEntity<CouponResponseDto> getCoupon(@PathVariable long couponId) {
return ResponseEntity.ok(couponService.getCoupon(couponId));
}

@PreAuthorize("hasRole('ADMIN')")
@PatchMapping("/v1/coupons/{couponId}")
public ResponseEntity<CouponResponseDto> updateCoupon(@PathVariable long couponId, @Valid @RequestBody CouponRequestDto couponRequestDto) {
return ResponseEntity.ok(couponService.updateCoupon(couponId, couponRequestDto));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.example.eightyage.domain.coupon.controller;

import com.example.eightyage.domain.coupon.dto.response.IssuedCouponResponseDto;
import com.example.eightyage.domain.coupon.service.IssuedCouponService;
import com.example.eightyage.global.dto.AuthUser;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
@RequiredArgsConstructor
public class IssuedCouponController {

private final IssuedCouponService issuedCouponService;

@PostMapping("/v1/coupons/{couponId}/issues")
public ResponseEntity<IssuedCouponResponseDto> issueCoupon(@AuthenticationPrincipal AuthUser authUser, @PathVariable Long couponId) {
return ResponseEntity.ok(issuedCouponService.issueCoupon(authUser, couponId));
}

@GetMapping("/v1/coupons/my")
public ResponseEntity<Page<IssuedCouponResponseDto>> getMyCoupons(
@AuthenticationPrincipal AuthUser authUser,
@RequestParam(defaultValue = "1") int page,
@RequestParam(defaultValue = "10") int size) {
return ResponseEntity.ok(issuedCouponService.getMyCoupons(authUser, page, size));
}

@GetMapping("/v1/coupons/{issuedCouponId}")
public ResponseEntity<IssuedCouponResponseDto> getCoupon(@AuthenticationPrincipal AuthUser authUser, @PathVariable Long issuedCouponId) {
return ResponseEntity.ok(issuedCouponService.getCoupon(authUser, issuedCouponId));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.example.eightyage.domain.coupon.couponstate;

public enum CouponState {
VALID,
INVALID
}
Empty file.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.eightyage.domain.event.dto.request;
package com.example.eightyage.domain.coupon.dto.request;

import com.example.eightyage.global.dto.ValidationMessage;
import jakarta.validation.constraints.Min;
Expand All @@ -13,7 +13,7 @@
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class EventRequestDto {
public class CouponRequestDto {

@NotBlank(message = ValidationMessage.NOT_BLANK_EVENT_NAME)
private String name;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
package com.example.eightyage.domain.coupon.dto.response;

import com.example.eightyage.domain.coupon.entity.CouponState;
import com.example.eightyage.domain.coupon.couponstate.CouponState;
import lombok.Getter;

import java.time.LocalDateTime;

@Getter
public class CouponResponseDto {

private final String couponCode;
private final String name;
private final String description;
private final int quantity;
private final LocalDateTime startDate;
private final LocalDateTime endDate;
private final CouponState state;
private final String username;
private final String eventname;

private final LocalDateTime startAt;
private final LocalDateTime endAt;

public CouponResponseDto(String couponCode, CouponState state,
String username, String eventname,
LocalDateTime startAt, LocalDateTime endAt) {
this.couponCode = couponCode;
public CouponResponseDto(String name, String description, int quantity, LocalDateTime startDate, LocalDateTime endDate, CouponState state) {
this.name = name;
this.description = description;
this.quantity = quantity;
this.startDate = startDate;
this.endDate = endDate;
this.state = state;
this.username = username;
this.eventname = eventname;
this.startAt = startAt;
this.endAt = endAt;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.example.eightyage.domain.coupon.dto.response;

import com.example.eightyage.domain.coupon.status.Status;
import lombok.Getter;

import java.time.LocalDateTime;

@Getter
public class IssuedCouponResponseDto {

private final String serialCode;
private final Status status;
private final String username;
private final String eventname;

private final LocalDateTime startAt;
private final LocalDateTime endAt;

public IssuedCouponResponseDto(String serialCode, Status status,
String username, String eventname,
LocalDateTime startAt, LocalDateTime endAt) {
this.serialCode = serialCode;
this.status = status;
this.username = username;
this.eventname = eventname;
this.startAt = startAt;
this.endAt = endAt;
}
}
Original file line number Diff line number Diff line change
@@ -1,56 +1,66 @@
package com.example.eightyage.domain.coupon.entity;

import com.example.eightyage.domain.coupon.couponstate.CouponState;
import com.example.eightyage.domain.coupon.dto.request.CouponRequestDto;
import com.example.eightyage.domain.coupon.dto.response.CouponResponseDto;
import com.example.eightyage.domain.event.entity.Event;
import com.example.eightyage.domain.user.entity.User;
import com.example.eightyage.global.entity.TimeStamped;
import com.example.eightyage.global.util.RandomCodeGenerator;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

@Entity
@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class Coupon extends TimeStamped {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(unique = true)
private String couponCode;

private String name;
private String description;
private int quantity;
@Column(name="start_at")
private LocalDateTime startDate;
@Column(name = "end_at")
private LocalDateTime endDate;
@Enumerated(EnumType.STRING)
private CouponState state;

@ManyToOne(fetch = FetchType.LAZY)
private User user;

@ManyToOne(fetch = FetchType.LAZY)
private Event event;

public static Coupon create(User user, Event event) {
return Coupon.builder()
.couponCode(RandomCodeGenerator.generateCouponCode(10))
.state(CouponState.VALID)
.user(user)
.event(event)
.build();
public Coupon(String name, String description, int quantity, LocalDateTime startDate, LocalDateTime endDate) {
this.name = name;
this.description = description;
this.quantity = quantity;
this.startDate = startDate;
this.endDate = endDate;
}

public CouponResponseDto toDto() {
return new CouponResponseDto(
this.couponCode,
this.state,
this.user.getNickname(),
this.event.getName(),
this.event.getStartDate(),
this.event.getEndDate()
this.getName(),
this.getDescription(),
this.getQuantity(),
this.getStartDate(),
this.getEndDate(),
this.getState()
);
}

public void update(CouponRequestDto couponRequestDto) {
this.name = couponRequestDto.getName();
this.description = couponRequestDto.getDescription();
this.quantity = couponRequestDto.getQuantity();
this.startDate = couponRequestDto.getStartDate();
this.endDate = couponRequestDto.getEndDate();
}

public boolean isValidAt(LocalDateTime time) {
return (startDate.isBefore(time) || startDate.isEqual(time)) && (endDate.isAfter(time) || endDate.isEqual(time));
}

public void updateStateAt(LocalDateTime time) {
CouponState newState = isValidAt(time) ? CouponState.VALID : CouponState.INVALID;
this.state = newState;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.example.eightyage.domain.coupon.entity;

import com.example.eightyage.domain.coupon.dto.response.IssuedCouponResponseDto;
import com.example.eightyage.domain.coupon.status.Status;
import com.example.eightyage.domain.user.entity.User;
import com.example.eightyage.global.entity.TimeStamped;
import com.example.eightyage.global.util.RandomCodeGenerator;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class IssuedCoupon extends TimeStamped {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(unique = true)
private String serialCode;

@Enumerated(EnumType.STRING)
private Status status;

@ManyToOne(fetch = FetchType.LAZY)
private User user;

@ManyToOne(fetch = FetchType.LAZY)
private Coupon coupon;

public static IssuedCoupon create(User user, Coupon coupon) {
return IssuedCoupon.builder()
.serialCode(RandomCodeGenerator.generateCouponCode(10))
.status(Status.VALID)
.user(user)
.coupon(coupon)
.build();
}

public IssuedCouponResponseDto toDto() {
return new IssuedCouponResponseDto(
this.serialCode,
this.status,
this.user.getNickname(),
this.coupon.getName(),
this.coupon.getStartDate(),
this.coupon.getEndDate()
);
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
package com.example.eightyage.domain.coupon.repository;

import com.example.eightyage.domain.coupon.entity.Coupon;
import com.example.eightyage.domain.coupon.entity.CouponState;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface CouponRepository extends JpaRepository<Coupon, Long> {
boolean existsByUserIdAndEventId(Long userId, Long eventId);
Page<Coupon> findAllByUserIdAndState(Long userId, CouponState state, Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.example.eightyage.domain.coupon.repository;

import com.example.eightyage.domain.coupon.entity.IssuedCoupon;
import com.example.eightyage.domain.coupon.status.Status;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface IssuedCouponRepository extends JpaRepository<IssuedCoupon, Long> {
boolean existsByUserIdAndCouponId(Long userId, Long couponId);
Page<IssuedCoupon> findAllByUserIdAndStatus(Long userId, Status status, Pageable pageable);
}
Loading