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:
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user