Files
sumoc-backend/src/main/java/SuMoC/backend/gamification/badges/BadgeController.java
T
laaser 85ca90fa7c Merge branch 'main' into cachingAndPP
# Conflicts:
#	src/main/java/SuMoC/backend/gamification/badges/BadgeController.java
#	src/main/java/SuMoC/backend/gamification/scoretable/LeaderboardController.java
#	src/main/java/SuMoC/backend/gamification/scoretable/LeaderboardService.java
#	src/main/java/SuMoC/backend/user/controller/UserController.java
#	src/main/java/SuMoC/backend/user/models/User.java
2026-01-27 16:51:14 +01:00

130 lines
5.5 KiB
Java

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<BadgeDTO> 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<Void> 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<BadgeDTO> 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<BadgeDTO> 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<List<BadgeDTO>> getAllBadges(){
List<BadgeDTO> 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<List<UserBadgeDTO>> getBadgesByUser(@PathVariable("userId") Long userId) {
List<UserBadgeDTO> 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<Void> checkAndAwardBadges() {
badgeService.checkAndAwardBadges();
return ResponseEntity.ok().build();
}
}