implement BadgeController with CRUD operations for badges and update BadgeService for improved error handling
LocationBasedServices/sumoc-backend/pipeline/head This commit looks good

This commit is contained in:
2026-01-06 17:19:18 +01:00
parent 8c5fff5a3b
commit bff1659186
2 changed files with 116 additions and 19 deletions
@@ -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);
}
}
@@ -4,7 +4,9 @@ import SuMoC.backend.gamification.badges.models.Badge;
import SuMoC.backend.gamification.badges.models.UserBadge;
import SuMoC.backend.models.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;
import java.time.LocalDateTime;
import java.util.List;
@@ -20,19 +22,15 @@ public class BadgeService {
private UserBadgeRepository userBadgeRepository;
public Badge createBadge(String name, String description, Map<String, Integer> criteria) {
if (badgeRepository.findByName(name).isPresent()) {
throw new RuntimeException("Badge existiert bereits");
}
Badge badge = new Badge();
badge.setName(name);
badge.setDescription(description);
badge.setCriteria(criteria);
public Badge createBadge(Badge badge) {
badgeRepository.findByName(badge.getName())
.ifPresent(b -> {
throw new ResponseStatusException(HttpStatus.CONFLICT, "Badge existiert bereits");
});
return badgeRepository.save(badge);
}
public void checkAndAwardBadges(User user) {
List<Badge> badges = badgeRepository.findAll();
@@ -62,22 +60,31 @@ public class BadgeService {
}
}
public Badge modifyBadge(Long badgeId, String name, String description, Map<String, Integer> criteria) {
Badge badge = badgeRepository.findById(badgeId)
public Badge modifyBadge(Badge modifiedBadge) {
Badge badge = badgeRepository.findById(modifiedBadge.getId())
.orElseThrow(() -> new RuntimeException("Badge nicht gefunden"));
badge.setName(name);
badge.setDescription(description);
badge.setCriteria(criteria);
badge.setName(modifiedBadge.getName());
badge.setDescription(modifiedBadge.getDescription());
badge.setCriteria(modifiedBadge.getCriteria());
return badgeRepository.save(badge);
}
public void deleteBadge(Long badgeId) {
Badge badge = badgeRepository.findById(badgeId)
.orElseThrow(() -> new RuntimeException("Badge nicht gefunden"));
public void deleteBadge(Long id) {
Badge badge = badgeRepository.findById(id)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Badge nicht gefunden"));
badgeRepository.delete(badge);
}
public Badge getBadgeById(Long badgeId) {
return badgeRepository.findById(badgeId)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Badge nicht gefunden"));
}
public List<Badge> getAllBadges() {
return badgeRepository.findAll();
}
}