repair badge images -> set to Base64 for transport
LocationBasedServices/sumoc-backend/pipeline/head This commit looks good

This commit is contained in:
2026-01-27 00:37:33 +01:00
parent 72efa472fa
commit 43e91670b3
7 changed files with 256 additions and 107 deletions
@@ -1,14 +1,12 @@
package SuMoC.backend.gamification.badges;
import SuMoC.backend.gamification.badges.models.Badge;
import SuMoC.backend.user.dtos.MaximalUserDTO;
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.annotation.security.RolesAllowed;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.CacheControl;
@@ -34,13 +32,13 @@ public class BadgeController {
@Operation(summary = "Create a new badge")
@ApiResponses(value = {
@ApiResponse(responseCode = "201", description = "Badge created successfully",
content = @Content(mediaType = "application/json", schema = @Schema(implementation = Badge.class))),
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<Badge> createBadge(@RequestBody @Valid Badge badge) {
Badge createdBadge = badgeService.createBadge(badge);
public ResponseEntity<BadgeDTO> createBadge(@RequestBody @Valid BadgeDTO badge) {
BadgeDTO createdBadge = badgeService.createBadge(badge);
return ResponseEntity.status(HttpStatus.CREATED).body(createdBadge);
}
@@ -61,14 +59,14 @@ public class BadgeController {
@Operation(summary = "Modify a badge by ID")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Badge modified successfully",
content = @Content(mediaType = "application/json", schema = @Schema(implementation = Badge.class))),
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<Badge> modifyBadgeById(
@RequestBody @Valid Badge badge) {
Badge modifiedBadge = badgeService.modifyBadge(badge);
public ResponseEntity<BadgeDTO> modifyBadgeById(
@RequestBody @Valid BadgeDTO badge) {
BadgeDTO modifiedBadge = badgeService.modifyBadge(badge);
return ResponseEntity.status(HttpStatus.OK).body(modifiedBadge);
}
@@ -76,13 +74,13 @@ public class BadgeController {
@Operation(summary = "Get a badge by ID")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Badge retrieved successfully",
content = @Content(mediaType = "application/json", schema = @Schema(implementation = Badge.class))),
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<Badge> getBadgeById(@PathVariable Long badgeId) {
Badge badge = badgeService.getBadgeById(badgeId);
public ResponseEntity<BadgeDTO> getBadgeById(@PathVariable Long badgeId) {
BadgeDTO badge = badgeService.getBadgeById(badgeId);
return ResponseEntity.ok()
.cacheControl(CacheControl.maxAge(356, TimeUnit.DAYS))
.body(badge);
@@ -94,14 +92,14 @@ public class BadgeController {
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Badges retrieved successfully",
content = @Content(mediaType = "application/json",
array = @ArraySchema(schema = @Schema(implementation = Badge.class))
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<Badge>> getAllBadges(){
List<Badge> badges = badgeService.getAllBadges();
public ResponseEntity<List<BadgeDTO>> getAllBadges(){
List<BadgeDTO> badges = badgeService.getAllBadges();
return ResponseEntity.ok()
.cacheControl(CacheControl.maxAge(365, TimeUnit.DAYS))
.body(badges);
@@ -1,13 +1,15 @@
package SuMoC.backend.gamification.badges;
import SuMoC.backend.gamification.Timeframe;
import SuMoC.backend.gamification.badges.mapper.BadgeMapper;
import SuMoC.backend.gamification.badges.mapper.UserBadgeMapper;
import SuMoC.backend.gamification.badges.models.Badge;
import SuMoC.backend.gamification.badges.models.BadgeDTO;
import SuMoC.backend.gamification.badges.models.UserBadge;
import SuMoC.backend.gamification.badges.models.UserBadgeDTO;
import SuMoC.backend.user.models.User;
import SuMoC.backend.user.service.CurrentUserService;
import SuMoC.backend.user.service.PrivacyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;
@@ -35,14 +37,16 @@ public class BadgeService {
this.privacyService = privacyService;
}
public Badge createBadge(Badge badge) {
badgeRepository.findByName(badge.getName())
public BadgeDTO createBadge(BadgeDTO badgeDTO) {
badgeRepository.findByName(badgeDTO.getName())
.ifPresent(b -> {
throw new ResponseStatusException(HttpStatus.CONFLICT, "Badge existiert bereits");
});
return badgeRepository.save(badge);
}
Badge badge = BadgeMapper.toEntity(badgeDTO);
Badge savedBadge = badgeRepository.save(badge);
return BadgeMapper.toDTO(savedBadge);
}
public void checkAndAwardBadges(User user) {
List<Badge> badges = badgeRepository.findAll();
@@ -70,22 +74,19 @@ public class BadgeService {
user.getBadges().add(userBadge);
// TODO: Remove in production
System.out.println("Badge " + badge.getName() + " vergeben an " + user.getPrivateName() + ". Mit Condition: " + badge.getCriteria().toString());
System.out.println("Badge " + badge.getName() + " vergeben an " + user.getPrivateName() +
". Mit Condition: " + badge.getCriteria().toString());
}
}
}
public Badge modifyBadge(Badge modifiedBadge) {
Badge badge = badgeRepository.findById(modifiedBadge.getId())
public BadgeDTO modifyBadge(BadgeDTO badgeDTO) {
Badge badge = badgeRepository.findById(badgeDTO.getId())
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Badge nicht gefunden"));
badge.setName(modifiedBadge.getName());
badge.setDescription(modifiedBadge.getDescription());
badge.setCategory(modifiedBadge.getCategory());
badge.setCriteria(modifiedBadge.getCriteria());
badge.setImage(modifiedBadge.getImage());
return badgeRepository.save(badge);
BadgeMapper.updateEntityFromDTO(badge, badgeDTO);
Badge savedBadge = badgeRepository.save(badge);
return BadgeMapper.toDTO(savedBadge);
}
public void deleteBadge(Long id) {
@@ -94,14 +95,17 @@ public class BadgeService {
badgeRepository.delete(badge);
}
public Badge getBadgeById(Long badgeId) {
return badgeRepository.findById(badgeId)
public BadgeDTO getBadgeById(Long badgeId) {
Badge badge = badgeRepository.findById(badgeId)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Badge nicht gefunden"));
return BadgeMapper.toDTO(badge);
}
public List<Badge> getAllBadges() {
return badgeRepository.findAll();
public List<BadgeDTO> getAllBadges() {
List<Badge> badges = badgeRepository.findAll();
return badges.stream()
.map(BadgeMapper::toDTO)
.toList();
}
public List<UserBadgeDTO> getUserBadges(Long id) {
@@ -0,0 +1,64 @@
package SuMoC.backend.gamification.badges.mapper;
import SuMoC.backend.gamification.badges.models.Badge;
import SuMoC.backend.gamification.badges.models.BadgeDTO;
import SuMoC.backend.gamification.badges.models.BadgeImageEntity;
import org.mapstruct.Mapper;
import java.util.Base64;
@Mapper
public class BadgeMapper {
public static BadgeDTO toDTO(Badge badge) {
BadgeDTO dto = new BadgeDTO();
dto.setId(badge.getId());
dto.setName(badge.getName());
dto.setDescription(badge.getDescription());
dto.setCategory(badge.getCategory());
dto.setCriteria(badge.getCriteria());
// Base64-Encoding des Bildes
if (badge.getImage() != null && badge.getImage().getImageData() != null) {
String base64Image = Base64.getEncoder().encodeToString(badge.getImage().getImageData());
dto.setImageBase64(base64Image);
}
return dto;
}
public static Badge toEntity(BadgeDTO dto) {
Badge badge = new Badge();
badge.setId(dto.getId());
badge.setName(dto.getName());
badge.setDescription(dto.getDescription());
badge.setCategory(dto.getCategory());
badge.setCriteria(dto.getCriteria());
// Base64-Decoding des Bildes
if (dto.getImageBase64() != null && !dto.getImageBase64().isEmpty()) {
byte[] imageData = Base64.getDecoder().decode(dto.getImageBase64());
BadgeImageEntity imageEntity = new BadgeImageEntity(badge, imageData);
badge.setImage(imageEntity);
}
return badge;
}
public static void updateEntityFromDTO(Badge badge, BadgeDTO dto) {
badge.setName(dto.getName());
badge.setDescription(dto.getDescription());
badge.setCategory(dto.getCategory());
badge.setCriteria(dto.getCriteria());
// Bild aktualisieren
if (dto.getImageBase64() != null && !dto.getImageBase64().isEmpty()) {
byte[] imageData = Base64.getDecoder().decode(dto.getImageBase64());
if (badge.getImage() == null) {
badge.setImage(new BadgeImageEntity(badge, imageData));
} else {
badge.getImage().setImageData(imageData);
}
}
}
}
@@ -1,4 +1,4 @@
package SuMoC.backend.gamification.badges;
package SuMoC.backend.gamification.badges.mapper;
import SuMoC.backend.gamification.badges.models.Badge;
import SuMoC.backend.gamification.badges.models.UserBadge;
@@ -6,6 +6,7 @@ import SuMoC.backend.gamification.badges.models.UserBadgeDTO;
import org.mapstruct.Mapper;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
@Mapper
@@ -23,6 +24,11 @@ public class UserBadgeMapper {
dto.setCriteria(badge.getCriteria());
dto.setAwardedAt(userBadge.getAwardedAt());
if (badge.getImage() != null && badge.getImage().getImageData() != null) {
String base64Image = Base64.getEncoder().encodeToString(badge.getImage().getImageData());
dto.setImageBase64(base64Image);
}
return dto;
}
@@ -0,0 +1,79 @@
package SuMoC.backend.gamification.badges.models;
import java.util.Map;
public class BadgeDTO {
private Long id;
private String name;
private String description;
private BadgeCategory category;
private Map<String, Integer> criteria;
private String imageBase64;
// Constructors
public BadgeDTO() {}
public BadgeDTO(String name, String description, BadgeCategory category) {
this.name = name;
this.description = description;
this.category = category;
}
public BadgeDTO(Long id, String name, String description, BadgeCategory category) {
this.id = id;
this.name = name;
this.description = description;
this.category = category;
}
public BadgeDTO(String name, String description, BadgeCategory category, Map<String, Integer> criteria) {
this.name = name;
this.description = description;
this.category = category;
this.criteria = criteria;
}
public BadgeDTO(Long id, String name, String description, BadgeCategory category, Map<String, Integer> criteria) {
this.id = id;
this.name = name;
this.description = description;
this.category = category;
this.criteria = criteria;
}
public BadgeDTO(String name, String description, BadgeCategory category, Map<String, Integer> criteria, String imageBase64) {
this.name = name;
this.description = description;
this.category = category;
this.criteria = criteria;
this.imageBase64 = imageBase64;
}
public BadgeDTO(Long id, String name, String description, BadgeCategory category, Map<String, Integer> criteria, String imageBase64) {
this.id = id;
this.name = name;
this.description = description;
this.category = category;
this.criteria = criteria;
this.imageBase64 = imageBase64;
}
// Getter & Setter
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getDescription() { return description; }
public void setDescription(String description) { this.description = description; }
public BadgeCategory getCategory() { return category; }
public void setCategory(BadgeCategory category) { this.category = category; }
public Map<String, Integer> getCriteria() { return criteria; }
public void setCriteria(Map<String, Integer> criteria) { this.criteria = criteria; }
public String getImageBase64() { return imageBase64; }
public void setImageBase64(String imageBase64) { this.imageBase64 = imageBase64; }
}
@@ -11,6 +11,8 @@ public class UserBadgeDTO {
private BadgeCategory category;
private Map<String, Integer> criteria;
private LocalDateTime awardedAt;
private String imageBase64;
// Getter & Setter
@@ -62,4 +64,7 @@ public class UserBadgeDTO {
public void setAwardedAt(LocalDateTime awardedAt) {
this.awardedAt = awardedAt;
}
public String getImageBase64() { return imageBase64; }
public void setImageBase64(String imageBase64) { this.imageBase64 = imageBase64; }
}