Skip to content

Commit

Permalink
Merge pull request #33 from Leets-Official/refactor/#30/수정-사항-추가
Browse files Browse the repository at this point in the history
[refactor] #33 수정 사항 추가
  • Loading branch information
hyxklee authored Nov 11, 2024
2 parents 14c18ff + 85fce00 commit 9ec86be
Show file tree
Hide file tree
Showing 15 changed files with 181 additions and 26 deletions.
17 changes: 17 additions & 0 deletions src/main/java/com/leets/X/domain/image/domain/Image.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.leets.X.domain.image.dto.request.ImageDto;
import com.leets.X.domain.post.domain.Post;
import com.leets.X.domain.user.domain.User;
import com.leets.X.global.common.domain.BaseTimeEntity;
import jakarta.persistence.*;
import lombok.*;
Expand All @@ -22,6 +23,10 @@ public class Image extends BaseTimeEntity {

private String url;

@OneToOne
@JoinColumn(name = "user_id")
private User user;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "post_Id")
private Post post;
Expand All @@ -34,5 +39,17 @@ public static Image from(ImageDto dto, Post post) {
.build();
}

public static Image from(ImageDto dto, User user) {
return Image.builder()
.name(dto.name())
.url(dto.url())
.user(user)
.build();
}

public void update(ImageDto dto) {
this.name = dto.name();
this.url = dto.url();
}

}
16 changes: 16 additions & 0 deletions src/main/java/com/leets/X/domain/image/service/ImageService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.leets.X.domain.image.dto.request.ImageDto;
import com.leets.X.domain.image.repository.ImageRepository;
import com.leets.X.domain.post.domain.Post;
import com.leets.X.domain.user.domain.User;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -30,4 +31,19 @@ public List<Image> save(List<MultipartFile> file, Post post) throws IOException
return imageRepository.saveAll(imageList);
}

@Transactional
public Image save(MultipartFile image, User user) throws IOException {
ImageDto imageDto = imageUploadService.uploadImage(image);
return imageRepository.save(Image.from(imageDto, user));
}

public ImageDto getImage(MultipartFile image) throws IOException {
return imageUploadService.uploadImage(image);
}

@Transactional
public void delete(Image image) {
imageRepository.delete(image);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,39 @@ public List<ImageDto> uploadImages(List<MultipartFile> files) throws IOException

return images;
}

public ImageDto uploadImage(MultipartFile image) throws IOException {

String originalName = image.getOriginalFilename();
String fileName = generateFileName(originalName);

try {
// PutObjectRequest 생성 및 설정
PutObjectRequest putObjectRequest = PutObjectRequest.builder()
.bucket(bucketName)
.key(fileName)
.contentType(image.getContentType())
.build();

// S3에 파일 업로드
PutObjectResponse response = s3Client.putObject(
putObjectRequest,
RequestBody.fromInputStream(image.getInputStream(), image.getSize())
);

// 업로드 성공 여부 확인
if (response.sdkHttpResponse().isSuccessful()) {
// 업로드된 파일의 URL을 ImageDto로 추가
return ImageDto.of(originalName, generateFileUrl(fileName));
} else {
throw new S3UploadException();
}
} catch (S3Exception e) {
throw new S3UploadException();
}
}


// S3에 저장된 파일 URL 생성
private String generateFileUrl(String fileName) {
return String.format("https://%s.s3.%s.amazonaws.com/%s", bucketName, region, fileName);
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/com/leets/X/domain/post/domain/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,15 @@ public class Post extends BaseTimeEntity {

private Integer views;

@Enumerated(EnumType.STRING)
private IsDeleted isDeleted;

private LocalDateTime deletedAt;

private Long replyCount;

private Long repostCount;

// 좋아요 수를 관리하기 위한 필드

@Column(name = "like_count")
Expand Down Expand Up @@ -111,5 +116,24 @@ public static Post create(User user, String content, Post parent) {
public void addImage(List<Image> images) {
this.images.addAll(images); // 기존 리스트에 이미지 추가
}

public void increaseReplyCount() {
this.replyCount++;
}

public void decreaseReplyCount() {
if(this.replyCount > 0L) {
this.replyCount--;
}
}

public void increaseRepostCount(){
this.repostCount++;
}
public void decreaseRepostCount() {
if(this.repostCount > 0L) {
this.repostCount--;
}
}
}

16 changes: 14 additions & 2 deletions src/main/java/com/leets/X/domain/post/dto/mapper/PostMapper.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.leets.X.domain.post.dto.mapper;

import com.leets.X.domain.image.domain.Image;
import com.leets.X.domain.image.dto.request.ImageDto;
import com.leets.X.domain.image.dto.response.ImageResponse;
import com.leets.X.domain.like.repository.LikeRepository;
import com.leets.X.domain.post.domain.Post;
Expand All @@ -24,6 +26,8 @@ public PostResponseDto toPostResponseDto(Post post, User user, LikeRepository li
.isDeleted(post.getIsDeleted())
.createdAt(post.getCreatedAt())
.user(toPostUserResponse(post.getUser()))
.replyCount(post.getReplyCount())
.repostCount(post.getRepostCount())
.likeCount(post.getLikeCount())
.isLikedByUser(isLikedByUser(post, user, likeRepository))
.postType(postType)
Expand All @@ -34,16 +38,19 @@ public PostResponseDto toPostResponseDto(Post post, User user, LikeRepository li
.build();
}

public ParentPostResponseDto toParentPostResponseDto(Post post, User user, LikeRepository likeRepository, Type postType, Long repostingUserId) {
public ParentPostResponseDto toParentPostResponseDto(Post post, User user, LikeRepository likeRepository, Type postType, Long repostingUserId, String reposingUserName) {
return ParentPostResponseDto.builder()
.id(post.getId())
.content(post.getContent())
.views(post.getViews())
.isDeleted(post.getIsDeleted())
.createdAt(post.getCreatedAt())
.user(toPostUserResponse(post.getUser()))
.replyCount(post.getReplyCount())
.repostCount(post.getRepostCount())
.likeCount(post.getLikeCount())
.isLikedByUser(isLikedByUser(post, user, likeRepository))
.repostingUserName(reposingUserName)
.repostingUserId(repostingUserId)
.postType(postType)
.myPost(isMyPost(post, user))
Expand All @@ -53,7 +60,12 @@ public ParentPostResponseDto toParentPostResponseDto(Post post, User user, LikeR
}

public PostUserResponse toPostUserResponse(User user) {
return PostUserResponse.from(user);
ImageDto dto = null;
Image image = user.getImage();
if(image != null){
dto = ImageDto.of(image.getName(), image.getUrl());
}
return PostUserResponse.from(user, dto);
}

public List<ImageResponse> toImageResponse(Post post) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ public record ParentPostResponseDto(
IsDeleted isDeleted,
LocalDateTime createdAt,
PostUserResponse user,
Long replyCount,
Long repostCount,
Long likeCount,
String repostingUserName,
Long repostingUserId,
Type postType,
Boolean myPost,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public record PostResponseDto(
IsDeleted isDeleted,
LocalDateTime createdAt,
PostUserResponse user,
Long replyCount,
Long repostCount,
Long likeCount,
Boolean isLikedByUser, // 좋아요 여부 확인
Type postType,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package com.leets.X.domain.post.dto.response;

import com.leets.X.domain.image.dto.request.ImageDto;
import com.leets.X.domain.user.domain.User;


public record PostUserResponse(
Long userId,
String name,
String customId
String customId,
ImageDto profileImage

) {
public static PostUserResponse from(User user) {
public static PostUserResponse from(User user, ImageDto profileImage) {
return new PostUserResponse(
user.getId(),
user.getName(),
user.getCustomId()
user.getCustomId(),
profileImage
);
}
}
14 changes: 8 additions & 6 deletions src/main/java/com/leets/X/domain/post/service/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import com.leets.X.domain.post.dto.request.PostRequestDTO;
import com.leets.X.domain.post.dto.response.ParentPostResponseDto;
import com.leets.X.domain.post.dto.response.PostResponseDto;
import com.leets.X.domain.post.dto.response.PostUserResponse;
import com.leets.X.domain.post.exception.AlreadyLikedException;
import com.leets.X.domain.post.exception.NotLikedException;
import com.leets.X.domain.post.exception.PostNotFoundException;
Expand Down Expand Up @@ -52,7 +51,7 @@ public List<ParentPostResponseDto> getAllParentPosts(String email) {
return posts.stream()
.filter(post -> !followedUserIds.contains(post.getUser().getId()))
.map(post -> {
return postMapper.toParentPostResponseDto(post, user, likeRepository, Type.POST, null);
return postMapper.toParentPostResponseDto(post, user, likeRepository, Type.POST, null, null);
})
.collect(Collectors.toList());
}
Expand Down Expand Up @@ -141,6 +140,8 @@ public void createReply(Long parentId, PostRequestDTO postRequestDTO, List<Multi
Post reply = Post.create(user, postRequestDTO.content(), parentPost);
Post savedReply = postRepository.save(reply);

parentPost.increaseReplyCount();

if (files != null) {
List<Image> images = imageService.save(files, savedReply);
savedReply.addImage(images);
Expand All @@ -159,6 +160,7 @@ public void deletePost(Long postId, String email) {
}

post.delete();
post.getParent().decreaseReplyCount();
}

// 좋아요 취소
Expand Down Expand Up @@ -190,10 +192,10 @@ public Post findPost(Long postId) {
.orElseThrow(PostNotFoundException::new);
}

public PostUserResponse findUser(String email) {
User user = userService.find(email);
return PostUserResponse.from(user);
}
// public PostUserResponse findUser(String email) {
// User user = userService.find(email);
// return PostUserResponse.from(user);
// }


}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public void rePost(Long postId, String email) {

Repost repost = repostRepository.save(Repost.of(user, post));
user.addRepost(repost);
post.increaseReplyCount();
}

public List<ParentPostResponseDto> getFollowingPost(String email) {
Expand Down Expand Up @@ -81,14 +82,14 @@ private void check(Long userId, Long postId){
private List<ParentPostResponseDto> getUserPosts(User user, boolean isOwner) {
return user.getPosts().stream()
.filter(post -> post.getParent() == null)
.map(post -> postMapper.toParentPostResponseDto(post, user, likeRepository, Type.POST, null))
.map(post -> postMapper.toParentPostResponseDto(post, user, likeRepository, Type.POST, null, null))
.collect(Collectors.toList());
}

// 사용자의 리포스트를 가져오는 메서드
private List<ParentPostResponseDto> getUserReposts(User user, boolean isOwner) {
return user.getReposts().stream()
.map(repost -> postMapper.toParentPostResponseDto(repost.getPost(), user, likeRepository, Type.REPOST, repost.getUser().getId()))
.map(repost -> postMapper.toParentPostResponseDto(repost.getPost(), user, likeRepository, Type.REPOST, repost.getUser().getId(), repost.getUser().getName()))
.collect(Collectors.toList());
}

Expand All @@ -98,7 +99,7 @@ private List<ParentPostResponseDto> getFollowingUsersPosts(User user) {
.map(Follow::getFollowed)
.flatMap(followedUser -> followedUser.getPosts().stream())
.filter(post -> post.getParent() == null)
.map(post -> postMapper.toParentPostResponseDto(post, user, likeRepository, Type.POST, null))
.map(post -> postMapper.toParentPostResponseDto(post, user, likeRepository, Type.POST, null, null))
.collect(Collectors.toList());
}

Expand All @@ -107,7 +108,7 @@ private List<ParentPostResponseDto> getFollowingUsersReposts(User user) {
return user.getFollowingList().stream()
.map(Follow::getFollowed)
.flatMap(followedUser -> followedUser.getReposts().stream())
.map(repost -> postMapper.toParentPostResponseDto(repost.getPost(), user, likeRepository, Type.REPOST, repost.getUser().getId()))
.map(repost -> postMapper.toParentPostResponseDto(repost.getPost(), user, likeRepository, Type.REPOST, repost.getUser().getId(), repost.getUser().getName()))
.collect(Collectors.toList());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
package com.leets.X.domain.user.controller;

import com.leets.X.domain.user.domain.enums.LoginStatus;
import com.leets.X.domain.user.dto.request.UserInitializeRequest;
import com.leets.X.domain.user.dto.request.UserSocialLoginRequest;
import com.leets.X.domain.user.dto.request.UserUpdateRequest;
import com.leets.X.domain.user.dto.response.UserProfileResponse;
import com.leets.X.domain.user.dto.response.UserSocialLoginResponse;
import com.leets.X.domain.user.domain.enums.LoginStatus;
import com.leets.X.domain.user.service.UserService;
import com.leets.X.global.common.response.ResponseDto;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

import static com.leets.X.domain.user.controller.ResponseMessage.*;

Expand Down Expand Up @@ -55,10 +59,12 @@ public ResponseDto<UserProfileResponse> getUserProfile(@PathVariable Long userId
return ResponseDto.response(GET_PROFILE_SUCCESS.getCode(), GET_PROFILE_SUCCESS.getMessage(), userService.getProfile(userId, email));
}

@PatchMapping("/profile")
@PatchMapping(value="/profile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@Operation(summary = "유저 프로필 수정")
public ResponseDto<String> updateProfile(@RequestBody @Valid UserUpdateRequest request, @AuthenticationPrincipal @Parameter(hidden = true) String email){
userService.updateProfile(request, email);
public ResponseDto<String> updateProfile(@RequestPart @Valid UserUpdateRequest request,
@RequestPart(value = "image", required = false) MultipartFile image,
@AuthenticationPrincipal @Parameter(hidden = true) String email) throws IOException {
userService.updateProfile(request, image, email);
return ResponseDto.response(PROFILE_UPDATE_SUCCESS.getCode(), PROFILE_UPDATE_SUCCESS.getMessage());
}

Expand Down
Loading

0 comments on commit 9ec86be

Please sign in to comment.