-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPromoCodeController.java
More file actions
74 lines (65 loc) · 2.84 KB
/
PromoCodeController.java
File metadata and controls
74 lines (65 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package com.podzilla.cart.controller;
import com.podzilla.auth.annotations.AllowedRoles;
import com.podzilla.cart.model.PromoCode;
import com.podzilla.cart.service.PromoCodeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestBody;
import java.util.List;
@RestController
@RequestMapping("/admin/promocodes")
@AllowedRoles({"ROLE_ADMIN"})
@RequiredArgsConstructor
@Tag(name = "PromoCode Admin", description = "Manage promotional codes (Requires Admin Role)")
@Slf4j
public class PromoCodeController {
private final PromoCodeService promoCodeService;
@Operation(summary = "Create a Promo Code")
@PostMapping
public ResponseEntity<PromoCode> createOrUpdatePromoCode(
@Valid @RequestBody final PromoCode promoCode) {
log.info("Admin request to create promo code: {}", promoCode.getCode());
PromoCode savedPromoCode = promoCodeService.createOrUpdatePromoCode(promoCode);
return ResponseEntity.ok(savedPromoCode);
}
@Operation(summary = "Get all Promo Codes")
@GetMapping
public ResponseEntity<List<PromoCode>> getAllPromoCodes() {
log.info("Admin request to get all promo codes");
return ResponseEntity.ok(promoCodeService.findAll());
}
@Operation(summary = "Get a specific Promo Code by code")
@GetMapping("/{code}")
public ResponseEntity<PromoCode> getPromoCodeByCode(
@PathVariable final String code) {
log.info("Admin request to get promo code: {}", code);
return promoCodeService.findByCode(code)
.map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build());
}
@Operation(summary = "Delete a Promo Code by code")
@DeleteMapping("/{code}")
public ResponseEntity<Void> deletePromoCode(
@PathVariable final String code) {
log.info("Admin request to delete promo code: {}", code);
try {
promoCodeService.deletePromoCode(code);
return ResponseEntity.noContent().build();
} catch (Exception e) {
log.error("Error deleting promo code", code, e.getMessage());
return ResponseEntity.status(
HttpStatus.INTERNAL_SERVER_ERROR).build();
}
}
}