Skip to content

Commit

Permalink
feature: Profiles API
Browse files Browse the repository at this point in the history
task:  8697d5a87
  • Loading branch information
KinTrae committed Jan 9, 2025
1 parent 3187861 commit dd34176
Show file tree
Hide file tree
Showing 31 changed files with 319 additions and 388 deletions.
52 changes: 0 additions & 52 deletions backend/src/main/java/meowhub/backend/jpa_buddy/ProfileData.java

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package meowhub.backend.profiles.controllers;

import lombok.RequiredArgsConstructor;
import meowhub.backend.profiles.dtos.ProfileDto;
import meowhub.backend.profiles.services.ProfileService;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping(path ="/api/profiles")
@RequiredArgsConstructor
public class ProfileController {
private final ProfileService profileService;

@GetMapping("/{login}")
public ResponseEntity<ProfileDto> getProfile(@PathVariable String login, @AuthenticationPrincipal UserDetails userDetails) {
return ResponseEntity.ok(profileService.getProfile(login, userDetails.getUsername()));
}

@PostMapping("")
public ResponseEntity<ProfileDto> updateProfile(@RequestParam String content, @AuthenticationPrincipal UserDetails userDetails) {
return ResponseEntity.ok(profileService.updateProfile(content, userDetails.getUsername()));
}

@PostMapping("/pictures")
public ResponseEntity<ProfileDto> addPictures(@RequestPart MultipartFile file, @AuthenticationPrincipal UserDetails userDetails) {
return ResponseEntity.ok(profileService.addProfilePicture(file, userDetails.getUsername()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package meowhub.backend.profiles.dtos;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@AllArgsConstructor
public class ProfileDto {
private String profilePicture;
private String content;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package meowhub.backend.jpa_buddy;
package meowhub.backend.profiles.models;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
Expand Down Expand Up @@ -60,8 +60,4 @@ public class Profile {

@OneToMany(mappedBy = "profile")
private Set<ProfilePicture> profilePictures = new LinkedHashSet<>();

@OneToMany(mappedBy = "profile")
private Set<ProfileUserData> profileUserData = new LinkedHashSet<>();

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package meowhub.backend.jpa_buddy;
package meowhub.backend.profiles.models;

import jakarta.persistence.Column;
import jakarta.persistence.Convert;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
Expand All @@ -13,6 +14,7 @@
import jakarta.validation.constraints.Size;
import lombok.Getter;
import lombok.Setter;
import meowhub.backend.profiles.services.BooleanConverter;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;

Expand Down Expand Up @@ -44,8 +46,9 @@ public class ProfilePicture {
private String ociUrl;

@NotNull
@Column(name = "PICTURE_INDEX", nullable = false)
private Long index;
@Column(name = "IS_CURRENT_PP", nullable = false)
@Convert(converter = BooleanConverter.class)
private Boolean isCurrentProfilePicture;

@Column(name = "CREATED_AT")
private LocalDateTime createdAt;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package meowhub.backend.profiles.repositories;

import meowhub.backend.profiles.models.ProfilePicture;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface ProfilePictureRepository extends JpaRepository<ProfilePicture, String> {
Optional<ProfilePicture> findByProfileIdAndIsCurrentProfilePicture(String profileId, Boolean currentProfilePicture);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package meowhub.backend.profiles.repositories;

import meowhub.backend.profiles.dtos.ProfileDto;
import meowhub.backend.profiles.models.Profile;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.Optional;

public interface ProfileRepository extends JpaRepository<Profile, String> {

@Query("""
SELECT new meowhub.backend.profiles.dtos.ProfileDto(pp.ociUrl, p.profileDetailsHtml)
FROM Profile p
LEFT JOIN ProfilePicture pp ON pp.profile.id = p.id AND pp.isCurrentProfilePicture = true
WHERE p.user.login = :login
""")
ProfileDto getOwnProfile(@Param("login") String login);

@Query("""
SELECT new meowhub.backend.profiles.dtos.ProfileDto(pp.ociUrl, p.profileDetailsHtml)
FROM Profile p
LEFT JOIN ProfilePicture pp ON pp.profile.id = p.id AND pp.isCurrentProfilePicture = true
JOIN User u ON u.id = p.user.id
JOIN u.profilePrivacy profilePrivacy
WHERE u.login = :login
AND (profilePrivacy.code = 'PUBLIC'
OR (profilePrivacy.code = 'FRIENDS_ONLY' AND u.id IN (
SELECT r.receiver.id
FROM User sender
JOIN sender.userRelationsSender r
JOIN r.relationType relType
WHERE relType.code = 'FRIENDS'
AND sender.login = :requestedBy
UNION
SELECT r.sender.id
FROM User receiver
JOIN receiver.userRelationsReceiver r
JOIN r.relationType relType
WHERE relType.code = 'FRIENDS'
AND receiver.login = :requestedBy)
)) ORDER BY p.createdAt DESC
""")
ProfileDto getProfileIfPublicOrFriends(@Param("login") String login, @Param("requestedBy") String requestedBy);

Optional<Profile> findByUserLogin(String login);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package meowhub.backend.profiles.services;

import jakarta.persistence.AttributeConverter;
import jakarta.persistence.Converter;

@Converter(autoApply = true)
public class BooleanConverter implements AttributeConverter<Boolean, Integer> {

@Override
public Integer convertToDatabaseColumn(Boolean attribute) {
if (attribute == null) {
return null;
}
return attribute ? 1 : 0;
}

@Override
public Boolean convertToEntityAttribute(Integer dbData) {
if (dbData == null) {
return null;
}
return dbData == 1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package meowhub.backend.profiles.services;

import meowhub.backend.profiles.dtos.ProfileDto;
import meowhub.backend.security.requests.SignUpRequest;
import org.springframework.web.multipart.MultipartFile;

public interface ProfileService {
void createProfile(SignUpRequest signUpRequest);
ProfileDto updateProfile(String content, String login);
ProfileDto getProfile(String login, String requesterLogin);
ProfileDto addProfilePicture(MultipartFile file, String login);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package meowhub.backend.profiles.services.facades;

import meowhub.backend.security.requests.SignUpRequest;

public interface ProfileAuthServiceFacade {
void createProfile(SignUpRequest signUpRequest);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package meowhub.backend.profiles.services.facades;

import lombok.RequiredArgsConstructor;
import meowhub.backend.profiles.services.ProfileService;
import meowhub.backend.security.requests.SignUpRequest;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class ProfileAuthServiceFacadeImpl implements ProfileAuthServiceFacade {
private final ProfileService profileService;

@Override
public void createProfile(SignUpRequest signUpRequest) {
profileService.createProfile(signUpRequest);
}
}
Loading

0 comments on commit dd34176

Please sign in to comment.