COUPON CODES INITITIALIZED
This commit is contained in:
@@ -0,0 +1,109 @@
|
||||
package com.rit.canteen.sales.controller;
|
||||
|
||||
import com.rit.canteen.sales.model.CouponCode;
|
||||
import com.rit.canteen.sales.repository.CouponRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/coupons")
|
||||
public class CouponController {
|
||||
|
||||
@Autowired
|
||||
private CouponRepository couponRepository;
|
||||
|
||||
@Autowired
|
||||
private com.rit.canteen.sales.repository.CouponRedemptionRepository redemptionRepository;
|
||||
|
||||
@Autowired
|
||||
private com.rit.canteen.sales.service.TokenService tokenService;
|
||||
|
||||
@GetMapping
|
||||
public List<CouponCode> getAllCoupons() {
|
||||
return couponRepository.findAll();
|
||||
}
|
||||
|
||||
@PostMapping("/redeem")
|
||||
@org.springframework.transaction.annotation.Transactional
|
||||
public ResponseEntity<?> redeemCoupon(@RequestBody Map<String, Object> request) {
|
||||
String code = ((String) request.get("code")).toUpperCase().trim();
|
||||
Long userId = Long.valueOf(request.get("userId").toString());
|
||||
|
||||
Optional<CouponCode> couponOpt = couponRepository.findByCode(code);
|
||||
if (couponOpt.isEmpty()) {
|
||||
return ResponseEntity.status(404).body(Map.of("success", false, "message", "Invalid coupon code"));
|
||||
}
|
||||
|
||||
CouponCode coupon = couponOpt.get();
|
||||
|
||||
// 1. Basic Validations
|
||||
if (!coupon.getIsActive()) {
|
||||
return ResponseEntity.status(400).body(Map.of("success", false, "message", "Coupon is currently inactive"));
|
||||
}
|
||||
|
||||
if (java.time.LocalDateTime.now().isAfter(coupon.getExpiryDate())) {
|
||||
return ResponseEntity.status(400).body(Map.of("success", false, "message", "Coupon has expired"));
|
||||
}
|
||||
|
||||
// 2. Global Usage Limit
|
||||
if (coupon.getCurrentClaims() >= coupon.getMaxClaims()) {
|
||||
return ResponseEntity.status(400).body(Map.of("success", false, "message", "Coupon claim limit reached"));
|
||||
}
|
||||
|
||||
// 3. Per-User Limit (Single redemption per code)
|
||||
if (redemptionRepository.existsByUserIdAndCouponId(userId, coupon.getId())) {
|
||||
return ResponseEntity.status(400).body(Map.of("success", false, "message", "You have already redeemed this code"));
|
||||
}
|
||||
|
||||
// 4. Execution
|
||||
try {
|
||||
// Update coupon stats
|
||||
coupon.setCurrentClaims(coupon.getCurrentClaims() + 1);
|
||||
couponRepository.save(coupon);
|
||||
|
||||
// Credit tokens
|
||||
tokenService.topUp(userId, coupon.getRewardAmount(), "COUPON-" + code);
|
||||
|
||||
// Log redemption
|
||||
redemptionRepository.save(new com.rit.canteen.sales.model.CouponRedemption(userId, coupon.getId()));
|
||||
|
||||
return ResponseEntity.ok(Map.of(
|
||||
"success", true,
|
||||
"message", "Successfully redeemed " + coupon.getRewardAmount() + " Ritz tokens!",
|
||||
"rewardAmount", coupon.getRewardAmount()
|
||||
));
|
||||
} catch (Exception e) {
|
||||
return ResponseEntity.status(500).body(Map.of("success", false, "message", "Redemption failed: " + e.getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public ResponseEntity<?> createCoupon(@RequestBody CouponCode coupon) {
|
||||
if (couponRepository.findByCode(coupon.getCode()).isPresent()) {
|
||||
return ResponseEntity.badRequest().body("Coupon code already exists");
|
||||
}
|
||||
return ResponseEntity.ok(couponRepository.save(coupon));
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public ResponseEntity<?> deleteCoupon(@PathVariable Long id) {
|
||||
couponRepository.deleteById(id);
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
|
||||
@PatchMapping("/{id}/toggle")
|
||||
public ResponseEntity<?> toggleStatus(@PathVariable Long id) {
|
||||
Optional<CouponCode> couponOpt = couponRepository.findById(id);
|
||||
if (couponOpt.isPresent()) {
|
||||
CouponCode coupon = couponOpt.get();
|
||||
coupon.setIsActive(!coupon.getIsActive());
|
||||
return ResponseEntity.ok(couponRepository.save(coupon));
|
||||
}
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package com.rit.canteen.sales.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDateTime;
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
|
||||
|
||||
@Entity
|
||||
@Table(name = "coupon_codes")
|
||||
public class CouponCode {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@Column(nullable = false, unique = true)
|
||||
private String code;
|
||||
|
||||
@Column(nullable = false)
|
||||
private BigDecimal rewardAmount;
|
||||
|
||||
@Column(nullable = false)
|
||||
@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
|
||||
private LocalDateTime expiryDate;
|
||||
|
||||
|
||||
@Column(nullable = false)
|
||||
private Integer maxClaims;
|
||||
|
||||
@Column(nullable = false)
|
||||
private Integer currentClaims = 0;
|
||||
|
||||
@Column(nullable = false)
|
||||
private Boolean isActive = true;
|
||||
|
||||
@Column(nullable = true)
|
||||
private String description;
|
||||
|
||||
@Column(nullable = false, updatable = false)
|
||||
@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
|
||||
private LocalDateTime createdAt;
|
||||
|
||||
|
||||
public CouponCode() {}
|
||||
|
||||
@PrePersist
|
||||
protected void onCreate() {
|
||||
if (createdAt == null) createdAt = LocalDateTime.now();
|
||||
if (currentClaims == null) currentClaims = 0;
|
||||
if (isActive == null) isActive = true;
|
||||
if (code != null) code = code.toUpperCase().trim();
|
||||
}
|
||||
|
||||
// Getters and Setters
|
||||
public Long getId() { return id; }
|
||||
public void setId(Long id) { this.id = id; }
|
||||
|
||||
public String getCode() { return code; }
|
||||
public void setCode(String code) { this.code = code; }
|
||||
|
||||
public BigDecimal getRewardAmount() { return rewardAmount; }
|
||||
public void setRewardAmount(BigDecimal rewardAmount) { this.rewardAmount = rewardAmount; }
|
||||
|
||||
public LocalDateTime getExpiryDate() { return expiryDate; }
|
||||
public void setExpiryDate(LocalDateTime expiryDate) { this.expiryDate = expiryDate; }
|
||||
|
||||
public Integer getMaxClaims() { return maxClaims; }
|
||||
public void setMaxClaims(Integer maxClaims) { this.maxClaims = maxClaims; }
|
||||
|
||||
public Integer getCurrentClaims() { return currentClaims; }
|
||||
public void setCurrentClaims(Integer currentClaims) { this.currentClaims = currentClaims; }
|
||||
|
||||
public Boolean getIsActive() { return isActive; }
|
||||
public void setIsActive(Boolean active) { isActive = active; }
|
||||
|
||||
public String getDescription() { return description; }
|
||||
public void setDescription(String description) { this.description = description; }
|
||||
|
||||
public LocalDateTime getCreatedAt() { return createdAt; }
|
||||
public void setCreatedAt(LocalDateTime createdAt) { this.createdAt = createdAt; }
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.rit.canteen.sales.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Entity
|
||||
@Table(name = "coupon_redemptions", uniqueConstraints = {
|
||||
@UniqueConstraint(columnNames = {"user_id", "coupon_id"})
|
||||
})
|
||||
public class CouponRedemption {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@Column(name = "user_id", nullable = false)
|
||||
private Long userId;
|
||||
|
||||
@Column(name = "coupon_id", nullable = false)
|
||||
private Long couponId;
|
||||
|
||||
@Column(nullable = false)
|
||||
private LocalDateTime redeemedAt;
|
||||
|
||||
public CouponRedemption() {}
|
||||
|
||||
public CouponRedemption(Long userId, Long couponId) {
|
||||
this.userId = userId;
|
||||
this.couponId = couponId;
|
||||
this.redeemedAt = LocalDateTime.now();
|
||||
}
|
||||
|
||||
public Long getId() { return id; }
|
||||
public void setId(Long id) { this.id = id; }
|
||||
|
||||
public Long getUserId() { return userId; }
|
||||
public void setUserId(Long userId) { this.userId = userId; }
|
||||
|
||||
public Long getCouponId() { return couponId; }
|
||||
public void setCouponId(Long couponId) { this.couponId = couponId; }
|
||||
|
||||
public LocalDateTime getRedeemedAt() { return redeemedAt; }
|
||||
public void setRedeemedAt(LocalDateTime redeemedAt) { this.redeemedAt = redeemedAt; }
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.rit.canteen.sales.repository;
|
||||
|
||||
import com.rit.canteen.sales.model.CouponRedemption;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import java.util.Optional;
|
||||
|
||||
@Repository
|
||||
public interface CouponRedemptionRepository extends JpaRepository<CouponRedemption, Long> {
|
||||
Optional<CouponRedemption> findByUserIdAndCouponId(Long userId, Long couponId);
|
||||
boolean existsByUserIdAndCouponId(Long userId, Long couponId);
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.rit.canteen.sales.repository;
|
||||
|
||||
import com.rit.canteen.sales.model.CouponCode;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import java.util.Optional;
|
||||
|
||||
@Repository
|
||||
public interface CouponRepository extends JpaRepository<CouponCode, Long> {
|
||||
Optional<CouponCode> findByCode(String code);
|
||||
}
|
||||
Reference in New Issue
Block a user