package SuMoC.backend.user.controller; import SuMoC.backend.security.dtos.LoginResponseDTO; import SuMoC.backend.user.dtos.AddStatRequestDTO; import SuMoC.backend.user.dtos.MaximalUserDTO; import SuMoC.backend.user.dtos.MinimalUserDTO; import SuMoC.backend.user.dtos.StatScope; import SuMoC.backend.user.enums.VisibilityPrivacy; import SuMoC.backend.user.service.UserService; import SuMoC.backend.user.stats.StatsService; import io.swagger.v3.oas.annotations.Operation; 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 jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotNull; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.CacheControl; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import java.util.concurrent.TimeUnit; @Validated @RestController @RequestMapping("/user") public class UserController { private final UserService userService; private final StatsService statsService; @Autowired public UserController(UserService userService, StatsService statsService) { this.userService = userService; this.statsService = statsService; } @Operation(summary = "Nutzer fragt ein fremdes Nutzerprofil an", description = "Ein Nutzer kann das Profil eines anderen Nutzers anfragen, um dessen öffentliche Informationen einzusehen.") @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Nutzerprofil erfolgreich abgerufen", content = @Content(mediaType = "application/json", schema = @Schema(implementation = MinimalUserDTO.class))), @ApiResponse(responseCode = "403", description = "Nutzerprofil darf nicht aufgerufen werden", content = @Content(mediaType = "application/json")), @ApiResponse(responseCode = "404", description = "Nutzerprofil nicht gefunden", content = @Content(mediaType = "application/json")) }) @GetMapping("/profile/{id}") public ResponseEntity getUser(@PathVariable @NotNull long id) { MinimalUserDTO user = this.userService.getUser(id); return ResponseEntity.ok() .cacheControl(CacheControl.noCache()) .body(user); } @Operation(summary = "Nutzer fragt sein eigenes Profil an", description = "Ein Nutzer kann sein eigenes Profil anfragen, um dessen Informationen einzusehen.") @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Nutzerprofil erfolgreich abgerufen", content = @Content(mediaType = "application/json", schema = @Schema(implementation = MaximalUserDTO.class))), @ApiResponse(responseCode = "304", description = "Nutzerprofil wurde seit dem letzten Abruf nicht verändert", content = @Content(mediaType = "application/json")), @ApiResponse(responseCode = "404", description = "Der Nutzer existiert nicht", content = @Content(mediaType = "application/json")) }) @GetMapping("/profile") public ResponseEntity getUser() { MaximalUserDTO dto = this.userService.getSelf(); return ResponseEntity.ok() .cacheControl(CacheControl.maxAge(30, TimeUnit.DAYS)) .body(dto); } @Operation(summary = "Nutzer löscht sein eigenes Profil", description = "Ein Nutzer kann sein eigenes Profil löschen. Dies entfernt alle seine Daten aus dem System.") @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Nutzerprofil erfolgreich gelöscht"), @ApiResponse(responseCode = "403", description = "Keine Berechtigung, fremdes Profil zu löschen"), @ApiResponse(responseCode = "404", description = "Der Nutzer existiert nicht") }) @DeleteMapping("/delete/{id}") public ResponseEntity deleteUser(@PathVariable @NotNull Long id) { this.userService.deleteUser(id); return ResponseEntity.ok().build(); } public void updateUser(Long id) { } @Operation(summary = "Update a users private name.", description = "A user can change it's own private name that is shown to friends.") @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Passwort erfolgreich geändert", content = @Content(mediaType = "application/json")), @ApiResponse(responseCode = "400", description = "Ungültige Eingabedaten", content = @Content(mediaType = "application/json")), @ApiResponse(responseCode = "403", description = "Nur autorisierte Personen dürfen das Passwort ändern", content = @Content(mediaType = "application/json")), @ApiResponse(responseCode = "404", description = "Der Nutzer existiert nicht", content = @Content(mediaType = "application/json")) }) @PutMapping("/setPrivateName/{id}") public ResponseEntity setPrivateName(@PathVariable @NotNull Long id, @RequestParam @NotBlank String newPrivateName) { this.userService.setPrivateName(id, newPrivateName); return ResponseEntity.ok().build(); } @Operation(summary = "Nutzer ändert sein Passwort", description = "Ein Nutzer kann sein Passwort ändern, um die Sicherheit seines Kontos zu gewährleisten.") @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Passwort erfolgreich geändert", content = @Content(mediaType = "application/json", schema = @Schema(implementation = LoginResponseDTO.class))), @ApiResponse(responseCode = "400", description = "Ungültige Eingabedaten", content = @Content(mediaType = "application/json")), @ApiResponse(responseCode = "403", description = "Nur autorisierte Personen dürfen das Passwort ändern", content = @Content(mediaType = "application/json")), @ApiResponse(responseCode = "404", description = "Der Nutzer existiert nicht", content = @Content(mediaType = "application/json")) }) @PutMapping("/setPassword/{id}") public ResponseEntity setPassword(@PathVariable @NotNull Long id, @RequestParam @NotBlank String newPassword) { return new ResponseEntity<>(this.userService.setPassword(id, newPassword), HttpStatus.OK); } @Operation(summary = "Nutzer ändert seine E-Mail-Adresse", description = "Ein Nutzer kann seine E-Mail-Adresse ändern, um sicherzustellen, dass seine Kontaktdaten aktuell sind.") @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "E-Mail-Adresse erfolgreich geändert", content = @Content(mediaType = "application/json", schema = @Schema(implementation = LoginResponseDTO.class))), @ApiResponse(responseCode = "400", description = "Ungültige Eingabedaten", content = @Content(mediaType = "application/json")), @ApiResponse(responseCode = "403", description = "Nur autorisierte Personen dürfen die Email ändern", content = @Content(mediaType = "application/json")), @ApiResponse(responseCode = "404", description = "Der Nutzer existiert nicht", content = @Content(mediaType = "application/json")), @ApiResponse(responseCode = "409", description = "Die Email wurde bereits vergeben", content = @Content(mediaType = "application/json")) }) @PutMapping("/setEmail/{id}") public ResponseEntity setEmail(@PathVariable @NotNull Long id, @RequestParam @NotBlank String newEmail) { return new ResponseEntity<>(this.userService.setEmail(id, newEmail), HttpStatus.OK); } @Operation(summary = "Nutzer ändert seine Sichtbarkeits- und Datenschutzeinstellungen", description = "Ein Nutzer kann seine Sichtbarkeits- und Datenschutzeinstellungen ändern, um zu steuern, wer seine Informationen sehen kann.") @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Sichtbarkeits- und Datenschutzeinstellungen erfolgreich geändert", content = @Content(mediaType = "application/json")), @ApiResponse(responseCode = "400", description = "Ungültige Eingabedaten", content = @Content(mediaType = "application/json")), @ApiResponse(responseCode = "403", description = "Ungültige Eingabedaten", content = @Content(mediaType = "application/json")), @ApiResponse(responseCode = "409", description = "Ungültige Eingabedaten", content = @Content(mediaType = "application/json")) }) @PutMapping("/setVisibilityPrivacy") public ResponseEntity setVisibilityPrivacy(@RequestParam @NotNull VisibilityPrivacy visibilityPrivacy) { this.userService.setVisibilityPrivacy(visibilityPrivacy); return ResponseEntity.ok().build(); } @Operation(summary = "Add stats to the current user", description = "Adds a value to a stat field for the current user. Optionally checks and awards badges afterwards.") @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Stats updated successfully"), @ApiResponse(responseCode = "400", description = "Invalid stat field / invalid input", content = @Content(mediaType = "application/json")), @ApiResponse(responseCode = "403", description = "Not authorized", content = @Content(mediaType = "application/json")) }) @RolesAllowed({"USER", "ADMIN"}) @PostMapping("/stats/add") public ResponseEntity addStats(@RequestBody @Valid AddStatRequestDTO body) { StatScope scope = body.getScope(); boolean awardBadges = body.getAwardBadges() == null || body.getAwardBadges(); this.statsService.addStatsToCurrentUser( body.getStat(), body.getValue(), scope, awardBadges ); return ResponseEntity.ok().build(); } @Operation(summary = "Add a profile Picture to the user.", description = "A user can add or update a profile picture to be displayed on his profile.") @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Profile picture successfully added/updated", content = @Content(mediaType = "application/json")), @ApiResponse(responseCode = "400", description = "Invalid input data", content = @Content(mediaType = "application/json")), @ApiResponse(responseCode = "403", description = "Only authorized persons may add/update the profile picture", content = @Content(mediaType = "application/json")), @ApiResponse(responseCode = "404", description = "The user does not exist", content = @Content(mediaType = "application/json")) }) @PutMapping("/setProfilePicture/{id}") public ResponseEntity setProfilePicture(@RequestParam String base64ProfilePicture, @PathVariable @NotNull Long id) { this.userService.setProfilePicture(id, base64ProfilePicture); return ResponseEntity.ok().build(); } @Operation(summary = "Get the profile Picture of the user.", description = "A user can get their displayed profile picture.") @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Profile picture successfully retrieved", content = @Content(mediaType = "application/json")), @ApiResponse(responseCode = "304", description = "Profile picture not modified since last request"), @ApiResponse(responseCode = "403", description = "Only authorized persons may see the profiles picture"), @ApiResponse(responseCode = "404", description = "The user does not exist") }) @GetMapping("/getProfilePicture/{id}") public ResponseEntity getProfilePicture(@PathVariable @NotNull Long id) { String imageData = userService.getProfilePicture(id); return ResponseEntity.ok() .contentType(MediaType.IMAGE_JPEG) .cacheControl(CacheControl.maxAge(365, TimeUnit.DAYS)) .body(imageData); } }