Files
sumoc-backend/src/main/java/SuMoC/backend/user/controller/UserController.java
T
2026-01-27 15:55:16 +01:00

194 lines
11 KiB
Java

package SuMoC.backend.user.controller;
import SuMoC.backend.security.dtos.LoginResponseDTO;
import SuMoC.backend.user.dtos.MaximalUserDTO;
import SuMoC.backend.user.dtos.MinimalUserDTO;
import SuMoC.backend.user.enums.VisibilityPrivacy;
import SuMoC.backend.user.service.UserService;
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.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;
@Autowired
public UserController(UserService userService) {
this.userService = userService;
}
@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<MinimalUserDTO> 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<MaximalUserDTO> 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<Object> 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<Void> 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<LoginResponseDTO> 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<LoginResponseDTO> 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<Void> setVisibilityPrivacy(@RequestParam @NotNull VisibilityPrivacy visibilityPrivacy) {
this.userService.setVisibilityPrivacy(visibilityPrivacy);
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<Void> 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<String> 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);
}
}