implement BadgeController with CRUD operations for badges and update BadgeService for improved error handling
LocationBasedServices/sumoc-backend/pipeline/head This commit looks good
LocationBasedServices/sumoc-backend/pipeline/head This commit looks good
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
package SuMoC.backend.gamification.badges;
|
||||
|
||||
import SuMoC.backend.gamification.badges.models.Badge;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.responses.ApiResponse;
|
||||
import io.swagger.v3.oas.annotations.responses.ApiResponses;
|
||||
import jakarta.annotation.security.RolesAllowed;
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@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"),
|
||||
@ApiResponse(responseCode = "400", description = "Invalid input data"),
|
||||
@ApiResponse(responseCode = "409", description = "Badge already exists")
|
||||
})
|
||||
@PostMapping("/createBadge")
|
||||
public ResponseEntity<Badge> createBadge(@RequestBody @Valid Badge badge) {
|
||||
Badge 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"),
|
||||
@ApiResponse(responseCode = "404", description = "Badge not found"),
|
||||
@ApiResponse(responseCode = "400", description = "Invalid input data")
|
||||
})
|
||||
@PutMapping("/modifyBadge/{badgeId}")
|
||||
public ResponseEntity<Badge> modifyBadgeById(@RequestBody @Valid Badge badge) {
|
||||
Badge 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"),
|
||||
@ApiResponse(responseCode = "404", description = "Badge not found")
|
||||
})
|
||||
@GetMapping("/getBadge/{badgeId}")
|
||||
public ResponseEntity<Badge> getBadgeById(@PathVariable Long badgeId) {
|
||||
Badge badge = badgeService.getBadgeById(badgeId);
|
||||
return ResponseEntity.status(HttpStatus.OK).body(badge);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Operation(summary = "Get all badges")
|
||||
@ApiResponses(value = {
|
||||
@ApiResponse(responseCode = "200", description = "Badges retrieved successfully"),
|
||||
@ApiResponse(responseCode = "404", description = "No badges found")
|
||||
})
|
||||
@GetMapping("/getAllBadges")
|
||||
public ResponseEntity<List<Badge>> getAllBadges(){
|
||||
List<Badge> badges = badgeService.getAllBadges();
|
||||
return ResponseEntity.status(HttpStatus.OK).body(badges);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user