257 lines
14 KiB
Java
257 lines
14 KiB
Java
package SuMoC.backend.user.controller;
|
|
|
|
import SuMoC.backend.images.ImageRequestDTO;
|
|
import SuMoC.backend.security.dtos.LoginResponseDTO;
|
|
import SuMoC.backend.user.dtos.*;
|
|
import SuMoC.backend.user.enums.VisibilityPrivacy;
|
|
import SuMoC.backend.user.service.UserService;
|
|
import SuMoC.backend.user.stats.StatsList;
|
|
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<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 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<Void> addStats(@RequestBody @Valid AddStatRequestDTO body) {
|
|
|
|
StatScope scope = body.getScope();
|
|
boolean awardBadges = body.getAwardBadges() == null || body.getAwardBadges();
|
|
boolean awardChallenges = body.getAwardChallenges() == null || body.getAwardChallenges();
|
|
|
|
this.statsService.addStatsToCurrentUser(
|
|
body.getStat(),
|
|
body.getValue(),
|
|
scope,
|
|
awardBadges,
|
|
awardChallenges
|
|
);
|
|
|
|
return ResponseEntity.ok().build();
|
|
}
|
|
|
|
@Operation(summary = "get stats of current user",
|
|
description = "Returns all stats of a user")
|
|
@ApiResponses(value = {
|
|
@ApiResponse(responseCode = "200", description = "Stats were found",
|
|
content = @Content(mediaType = "application/json")),
|
|
@ApiResponse(responseCode = "403", description = "Not authorized",
|
|
content = @Content(mediaType = "application/json"))
|
|
})
|
|
@RolesAllowed({"USER", "ADMIN"})
|
|
@GetMapping("/stats")
|
|
public ResponseEntity<StatsList> getStats() {
|
|
StatsList stats = this.statsService.getStats();
|
|
return ResponseEntity.ok().body(stats);
|
|
}
|
|
|
|
|
|
@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(@RequestBody ImageRequestDTO base64ProfilePicture,
|
|
@PathVariable @NotNull Long id) {
|
|
this.userService.setProfilePicture(id, base64ProfilePicture.getBase64ProfilePicture());
|
|
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<ImageRequestDTO> getProfilePicture(@PathVariable @NotNull Long id) {
|
|
ImageRequestDTO imageData = userService.getProfilePicture(id);
|
|
|
|
return ResponseEntity.ok()
|
|
.contentType(MediaType.IMAGE_JPEG)
|
|
.cacheControl(CacheControl.maxAge(365, TimeUnit.DAYS))
|
|
.body(imageData);
|
|
}
|
|
|
|
@Operation(summary = "Get the users own xp", description = "A user can get their own xp.")
|
|
@ApiResponses(value = {
|
|
@ApiResponse(responseCode = "200", description = "XP successfully retrieved",
|
|
content = @Content(mediaType = "application/json")),
|
|
@ApiResponse(responseCode = "403", description = "Not authorized",
|
|
content = @Content(mediaType = "application/json")),
|
|
@ApiResponse(responseCode = "404", description = "The user does not exist",
|
|
content = @Content(mediaType = "application/json"))
|
|
})
|
|
@GetMapping("/getTotalXP")
|
|
public ResponseEntity<Long> getTotalXP() {
|
|
Long xp = userService.getTotalXP();
|
|
return ResponseEntity.ok().body(xp);
|
|
}
|
|
}
|