package SuMoC.backend.gamification.badges; import SuMoC.backend.gamification.badges.models.UserBadgeDTO; import SuMoC.backend.gamification.badges.models.BadgeDTO; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.media.ArraySchema; import io.swagger.v3.oas.annotations.media.Content; import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.responses.ApiResponse; import io.swagger.v3.oas.annotations.responses.ApiResponses; import jakarta.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.CacheControl; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.util.List; import java.util.concurrent.TimeUnit; @RestController @RequestMapping("/badges") public class BadgeController { private BadgeService badgeService; @Autowired public BadgeController(BadgeService badgeService) { this.badgeService = badgeService; } @Operation(summary = "Create a new badge") @ApiResponses(value = { @ApiResponse(responseCode = "201", description = "Badge created successfully", content = @Content(mediaType = "application/json", schema = @Schema(implementation = BadgeDTO.class))), @ApiResponse(responseCode = "400", description = "Invalid input data"), @ApiResponse(responseCode = "409", description = "Badge already exists") }) @PostMapping("/createBadge") public ResponseEntity createBadge(@RequestBody @Valid BadgeDTO badge) { BadgeDTO createdBadge = badgeService.createBadge(badge); return ResponseEntity.status(HttpStatus.CREATED).body(createdBadge); } @Operation(summary = "Delete a badge by ID") @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Badge deleted successfully"), @ApiResponse(responseCode = "404", description = "Badge not found") }) @DeleteMapping("/deleteBadge/{badgeId}") public ResponseEntity deleteBadgeById(@PathVariable Long badgeId) { badgeService.deleteBadge(badgeId); return ResponseEntity.ok().build(); } @Operation(summary = "Modify a badge by ID") @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Badge modified successfully", content = @Content(mediaType = "application/json", schema = @Schema(implementation = BadgeDTO.class))), @ApiResponse(responseCode = "404", description = "Badge not found"), @ApiResponse(responseCode = "400", description = "Invalid input data") }) @PutMapping("/modifyBadge") public ResponseEntity modifyBadgeById( @RequestBody @Valid BadgeDTO badge) { BadgeDTO modifiedBadge = badgeService.modifyBadge(badge); return ResponseEntity.status(HttpStatus.OK).body(modifiedBadge); } @Operation(summary = "Get a badge by ID") @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Badge retrieved successfully", content = @Content(mediaType = "application/json", schema = @Schema(implementation = BadgeDTO.class))), @ApiResponse(responseCode = "304", description = "Not modified since last request"), @ApiResponse(responseCode = "404", description = "Badge not found") }) @GetMapping("/getBadge/{badgeId}") public ResponseEntity getBadgeById(@PathVariable Long badgeId) { BadgeDTO badge = badgeService.getBadgeById(badgeId); return ResponseEntity.ok() .cacheControl(CacheControl.maxAge(356, TimeUnit.DAYS)) .body(badge); } @Operation(summary = "Get all badges") @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Badges retrieved successfully", content = @Content(mediaType = "application/json", array = @ArraySchema(schema = @Schema(implementation = BadgeDTO.class)) )), @ApiResponse(responseCode = "304", description = "Not modified since last request"), @ApiResponse(responseCode = "404", description = "No badges found") }) @GetMapping("/getAllBadges") public ResponseEntity> getAllBadges(){ List badges = badgeService.getAllBadges(); return ResponseEntity.ok() .cacheControl(CacheControl.maxAge(365, TimeUnit.DAYS)) .body(badges); } @Operation(summary = "Get Badges by user") @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Badges retrieved successfully"), @ApiResponse(responseCode = "404", description = "No badges found for user") }) @GetMapping("/getBadges/{userId}") public ResponseEntity> getBadgesByUser(@PathVariable("userId") Long userId) { List badges = badgeService.getUserBadges(userId); return ResponseEntity.ok(badges); } @Operation(summary = "Check badges for current user and award newly unlocked ones") @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Check successful") }) @PostMapping("/checkBadgeAndAward") public ResponseEntity checkAndAwardBadges() { badgeService.checkAndAwardBadges(); return ResponseEntity.ok().build(); } }