Skip to content

Commit

Permalink
Merge pull request #121 from codestates-seb/feat_be_profile
Browse files Browse the repository at this point in the history
Feat: Add member Profile CRUD(#108)
  • Loading branch information
eastmeet authored Jan 27, 2023
2 parents 71427e2 + 4f7c55d commit b41dd9f
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,43 +11,47 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

@Service
@Transactional
@RequiredArgsConstructor
public class CommentService {

private final CommentRepository commentRepository;

public Comment createComment(Comment comment){
public Comment createComment(Comment comment) {
return commentRepository.save(comment);
}

public Comment updateComment(Comment comment){ return commentRepository.save(comment);}

@Transactional(readOnly = true)
public Comment findComment(long commentId){
public Comment findComment(long commentId) {
return findVerifiedComment(commentId);
}

@Transactional(readOnly = true)
public Page<Comment> findComments(int page, int size) {
return commentRepository.findAll(PageRequest.of(
page,size, Sort.by("commentId").ascending()
page, size, Sort.by("commentId").ascending()
));
}

public void deleteComment(Comment comment){
public void deleteComment(Comment comment) {
commentRepository.delete(comment);
}

@Transactional(readOnly = true)
public Comment findVerifiedComment(long commentId){
public Comment findVerifiedComment(long commentId) {
return commentRepository.findById(commentId)
.orElseThrow(()-> new BusinessLogicException(ExceptionCode.COMMENT_NOT_FOUND));
.orElseThrow(() -> new BusinessLogicException(ExceptionCode.COMMENT_NOT_FOUND));
}
@Transactional(readOnly = true)

public Comment verifyClientId(long clientId, long commentId) {
Comment comment = findComment(commentId);

if (!comment.getMember().getMemberId().equals(clientId)) {
if (!(comment.getMember().getMemberId().equals(clientId))) {
throw new BusinessLogicException(ExceptionCode.USER_IS_NOT_EQUAL);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.main36.pikcha.domain.image.entity;

import com.main36.pikcha.domain.post.entity.Post;
import lombok.Getter;
import lombok.Setter;
import lombok.*;

import javax.persistence.*;

@Entity
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Getter
@Setter
public class PostImage {
Expand All @@ -19,4 +21,8 @@ public class PostImage {

@Column(nullable = false)
private String postImageUrl;

// @ManyToOne
// @JoinColumn(name = "post_id")
// private Post post;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import com.main36.pikcha.domain.member.dto.MemberDto;
import com.main36.pikcha.domain.member.dto.MemberResponseDto;
import com.main36.pikcha.domain.member.entity.Member;
import com.main36.pikcha.domain.member.mapper.MemberMapper;
import com.main36.pikcha.domain.member.service.MemberService;
Expand Down Expand Up @@ -61,8 +62,8 @@ public ResponseEntity<DataResponseDto<?>> patchMember(Member loginUser,

@LoginUser
@GetMapping("/users/profile/{member-id}")
public ResponseEntity<DataResponseDto<?>> getMemberProfile(Member loginUser,
@PathVariable("member-id") @Positive Long memberId) {
public ResponseEntity<DataResponseDto<MemberResponseDto.Profile>> getMemberProfile(Member loginUser,
@PathVariable("member-id") @Positive Long memberId) {
Member member = memberService.verifyLoginIdAndMemberId(loginUser.getMemberId(), memberId);

return new ResponseEntity<>(new DataResponseDto<>(mapper.memberToProfileHomeDto(member)),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package com.main36.pikcha.domain.member.dto;

import com.main36.pikcha.domain.post.dto.PostDto;
import com.main36.pikcha.domain.post.dto.PostResponseDto;
import com.main36.pikcha.domain.save.dto.SaveResponseDto;
import com.main36.pikcha.domain.save.entity.Save;
import lombok.Builder;
import lombok.Data;

import java.time.LocalDateTime;
import java.util.List;

public class MemberResponseDto {

Expand Down Expand Up @@ -31,6 +36,9 @@ public static class Profile {
private String email;
private int totalMyPosts;
private int totalMySaves;
private List<PostResponseDto.Profile> posts;
private List<SaveResponseDto> saves;
// TODO: save
private LocalDateTime createdAt;
private LocalDateTime modifiedAt;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

import com.main36.pikcha.domain.member.dto.*;
import com.main36.pikcha.domain.member.entity.Member;
import com.main36.pikcha.domain.post.dto.PostResponseDto;
import com.main36.pikcha.domain.save.dto.SaveResponseDto;
import com.main36.pikcha.global.security.dto.LoginResponseDto;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingConstants;

import java.util.stream.Collectors;


@Mapper(componentModel = MappingConstants.ComponentModel.SPRING)
public interface MemberMapper {
Expand All @@ -17,9 +21,47 @@ public interface MemberMapper {

MemberResponseDto.SignUp memberToSignUpResponseDto(Member member);

@Mapping(target = "totalMyPosts", expression = "java(member.getPosts().size())")
@Mapping(target = "totalMySaves", expression = "java(member.getSaves().size())")
MemberResponseDto.Profile memberToProfileHomeDto(Member member);
// @Mapping(target = "totalMyPosts", expression = "java(member.getPosts().size())")
// @Mapping(target = "totalMySaves", expression = "java(member.getSaves().size())")
default MemberResponseDto.Profile memberToProfileHomeDto(Member member) {
return MemberResponseDto.Profile.builder()
.memberId(member.getMemberId())
.username(member.getUsername())
.memberTitle(member.getMemberTitle())
.phoneNumber(member.getPhoneNumber())
.address(member.getAddress())
.picture(member.getPicture())
.email(member.getEmail())
.totalMyPosts(member.getPosts().size())
.totalMySaves(member.getSaves().size())
.posts(member.getPosts().stream()
.map(post -> {
return PostResponseDto.Profile.builder()
.postId(post.getPostId())
.postTitle(post.getPostTitle())
.pictureUrl(post.getPostImages().isEmpty() ? "" : post.getPostImages().get(0).getPostImageUrl())
.views(post.getViews())
.likes(post.getLikes())
.createdAt(post.getCreatedAt())
.modifiedAt(post.getModifiedAt())
.build();
}).collect(Collectors.toList()))
.saves(member.getSaves().stream()
.map(save -> {
return SaveResponseDto.builder()
.attractionId(save.getAttraction().getAttractionId())
.attractionName(save.getAttraction().getAttractionName())
.fixedImage(save.getAttraction().getFixedImage())
.likes(save.getAttraction().getLikes())
.saves(save.getAttraction().getSaves())
.build();
}).collect(Collectors.toList()))
.createdAt(member.getCreatedAt())
.modifiedAt(member.getModifiedAt())
.build();

}


@Mapping(target = "password", constant = "")
@Mapping(target = "phoneNumber", constant = "")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ private Member verifiedMemberByEmail(String email) {
return findMember
.orElseThrow(() -> new BusinessLogicException(ExceptionCode.MEMBER_NOT_FOUND));
}

@Transactional(readOnly = true)
public Member findMemberByOauth2Id(String oauthId) {
Optional<Member> byOauthId = memberRepository.findByOauthId(oauthId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ public ResponseEntity<DataResponseDto<?>> registerPost2(Member loginUser,
post.setHashTags(hashTagList);
post.setPostContents(postContentList);
post.setPostImages(postImageList);

// post.setPostImages(postImageList);
// PostImages.setPost(post)

post.setAttraction(attractionService.findAttraction(attractionId));
post.setMember(loginUser);
post.setComments(new ArrayList<>());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,16 @@ public static class MapsImageUrlResponse{

}

@Data
@Builder
public static class Profile {

private Long postId;
private String postTitle;
private String pictureUrl;
private int views;
private int likes;
private LocalDateTime createdAt;
private LocalDateTime modifiedAt;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,58 +76,4 @@ default List<PostResponseDto.Home> postListToPostHomeResponseDtoList(List<Post>
.build();
}).collect(Collectors.toList());
}
/*default PostResponseDto.MapsImageUrlResponse postsToPostMapsResponseDto(List<Post> postList){
if(postList == null) {
return null;
}
PostResponseDto.MapsImageUrlResponse response = new PostResponseDto.MapsImageUrlResponse();
response.setImageUrls(new ArrayList<>());
postList.forEach(post -> {
post.getPostImages()
.stream()
.forEach(postImage -> {
response.getImageUrls().add(postImage.getPostImageUrl());
});
});
return response;
}*/

/*default List<PostResponseDto.Detail> postListToPostPageResponseDtoList(List<Post> postList) {
if (postList == null) {
return null;
}
return postList.stream()
.map(post -> {
return PostResponseDto.Detail.builder()
.postId(post.getPostId())
.postTitle(post.getPostTitle())
.postContent(post.getPostContent())
.hashTagContent(post.getHashTagContent())
.attractionId(post.getAttraction().getAttractionId())
.attractionAddress(post.getAttraction().getAttractionAddress())
.views(post.getViews())
.likes(post.getLikes())
.username(post.getMember().getUsername())
.picture(post.getMember().getPicture())
.comments(post.getComments().stream()
.map(comment -> {
return CommentResponseDto.builder()
.commentId(comment.getCommentId())
.memberId(comment.getMember().getMemberId())
.username(comment.getMember().getUsername())
.memberPicture(comment.getMember().getPicture())
.commentContent(comment.getCommentContent())
.createdAt(comment.getCreatedAt())
.modifiedAt(comment.getModifiedAt())
.build();
}).collect(Collectors.toList()))
.createdAt(post.getCreatedAt())
.modifiedAt(post.getModifiedAt())
.build();
}).collect(Collectors.toList());
}*/
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.main36.pikcha.domain.save.dto;

import com.main36.pikcha.domain.attraction.dto.AttractionResponseDto;
import lombok.Builder;
import lombok.Data;

import java.util.List;

@Data
@Builder
public class SaveResponseDto {

private Long attractionId;
private String attractionName;
private String fixedImage;
private Long likes;
private Long saves;

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public Object getUserEmail(ProceedingJoinPoint joinPoint) throws Throwable {

return joinPoint.proceed(args);

}
}

@Around("@annotation(com.main36.pikcha.global.aop.LoginUser)")
public Object getUser(ProceedingJoinPoint joinPoint) throws Throwable {
Expand Down

0 comments on commit b41dd9f

Please sign in to comment.