Implemented User Profilepicture | PLEASE check again if implemented right

This commit is contained in:
2026-01-21 02:28:38 +01:00
parent d92b71179a
commit 159c03142a
6 changed files with 124 additions and 0 deletions
@@ -17,6 +17,7 @@ import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
@Validated
@RestController
@@ -141,4 +142,24 @@ public class UserController {
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 MultipartFile profilePicture,
@PathVariable @NotNull Long id) {
this.userService.setProfilePicture(id, profilePicture);
return ResponseEntity.ok().build();
}
}
@@ -0,0 +1,37 @@
package SuMoC.backend.user.models;
import jakarta.persistence.*;
@Entity
public class ImageEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Lob
private byte[] imageData;
public ImageEntity() {
}
public ImageEntity(byte[] imageData) {
this.imageData = imageData;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public byte[] getImageData() {
return imageData;
}
public void setImageData(byte[] imageData) {
this.imageData = imageData;
}
}
@@ -0,0 +1,23 @@
package SuMoC.backend.user.models;
import jakarta.persistence.Entity;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToOne;
@Entity
public class ProfileImageEntity extends ImageEntity {
@OneToOne
@JoinColumn(name = "user_id", nullable = false)
private User user;
public ProfileImageEntity() {
super();
}
public ProfileImageEntity(User user, byte[] imageData) {
super(imageData);
this.user = user;
}
}
@@ -59,6 +59,9 @@ public class User {
@OneToOne(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
private StatsList statsList;
@OneToOne(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
private ProfileImageEntity profileImage;
public User() {}
// public User(Long id, String publicName, String privateName, String email, String password, VisibilityPrivacy visibilityPrivacy, String friendCode, Set<Trip> trips, Set<Friendship> friendships, Set<RoleName> roles) {
@@ -84,6 +87,7 @@ public class User {
this.roles = new HashSet<>();
this.roles.add(RoleName.USER);
this.statsList = new StatsList(this);
this.profileImage = new ProfileImageEntity(this, null);
}
public Long getId() {
@@ -173,4 +177,12 @@ public class User {
public StatsList getStats() { return statsList;}
public void setStats(StatsList statsList) { this.statsList = statsList; }
public ProfileImageEntity getProfileImage() {
return profileImage;
}
public void setProfileImage(ProfileImageEntity profileImage) {
this.profileImage = profileImage;
}
}
@@ -0,0 +1,9 @@
package SuMoC.backend.user.repository;
import SuMoC.backend.user.models.ProfileImageEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
interface ProfileImage extends JpaRepository<ProfileImageEntity, Long> {
}
@@ -18,6 +18,7 @@ import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.server.ResponseStatusException;
@@ -203,4 +204,25 @@ public class UserService {
return userMapper.toMaximalUserDTO(sender);
}
public void setProfilePicture(Long id, MultipartFile profilePicture) {
User sender = currentUser.get();
if (!sender.getId().equals(id) && !sender.getRoles().contains(RoleName.ADMIN)) {
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "Ein Nutzer kann nur seinen eigenen privaten Namen ändern.");
}
User userToChange = userRepository.findById(id)
.orElseThrow(() -> new ResponseStatusException(
HttpStatus.NOT_FOUND,
"Der Nutzer Existitert nicht."));
try {
userToChange.getProfileImage().setImageData(profilePicture.getBytes());
} catch (Exception e) {
throw new ResponseStatusException(
HttpStatus.BAD_REQUEST,
"Fehler beim Hochladen des Profilbildes."
);
}
userRepository.save(userToChange);
}
}