From 659a1c8eebae41530601ad044da1b4c4281e1dbe Mon Sep 17 00:00:00 2001 From: epdlrnldudnj Date: Mon, 22 Jan 2024 18:52:14 +0900 Subject: [PATCH 01/12] =?UTF-8?q?=ED=94=84=EB=A1=9C=ED=95=84=20=EC=A0=95?= =?UTF-8?q?=EB=B3=B4=20=EB=B6=88=EB=9F=AC=EC=98=A4=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 닉네임, 본인 여부 반환 (뱃지 관련 정보 미구현) --- .../profile/controller/profileController.java | 24 ++++++++++++++--- .../profile/service/ProfileService.java | 27 +++++++++++++++++++ .../kimgreen/backend/response/Message.java | 2 ++ 3 files changed, 50 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/kimgreen/backend/domain/profile/controller/profileController.java b/src/main/java/com/kimgreen/backend/domain/profile/controller/profileController.java index e2dcb11..bcf6667 100644 --- a/src/main/java/com/kimgreen/backend/domain/profile/controller/profileController.java +++ b/src/main/java/com/kimgreen/backend/domain/profile/controller/profileController.java @@ -1,12 +1,30 @@ package com.kimgreen.backend.domain.profile.controller; +import com.kimgreen.backend.domain.profile.service.ProfileService; +import com.kimgreen.backend.response.Response; +import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.*; + +import java.util.*; + +import static com.kimgreen.backend.response.Message.PROFILE_INFO_SUCCESS; +import static com.kimgreen.backend.response.Response.success; +import static org.springframework.http.HttpStatus.OK; @Tag(name = "Profile") @RestController @RequestMapping(value="/profile") +@RequiredArgsConstructor public class profileController { + private final ProfileService profileService; + + @Operation(summary = "프로필 정보 불러오기") + @ResponseStatus(OK) + @GetMapping("/profile-info") + public Response getProfileInfo(@RequestParam("memberId") Long memberId) { + List profileInfo = profileService.getProfileInfo(memberId); + return success(PROFILE_INFO_SUCCESS, profileInfo); + } } diff --git a/src/main/java/com/kimgreen/backend/domain/profile/service/ProfileService.java b/src/main/java/com/kimgreen/backend/domain/profile/service/ProfileService.java index 5a8f07c..7f109b5 100644 --- a/src/main/java/com/kimgreen/backend/domain/profile/service/ProfileService.java +++ b/src/main/java/com/kimgreen/backend/domain/profile/service/ProfileService.java @@ -1,7 +1,34 @@ package com.kimgreen.backend.domain.profile.service; +import com.kimgreen.backend.domain.member.entity.Member; +import com.kimgreen.backend.domain.member.repository.MemberRepository; +import com.kimgreen.backend.domain.member.service.MemberService; +import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; +import java.util.*; @Service +@RequiredArgsConstructor public class ProfileService { + private final MemberService memberService; + private final MemberRepository memberRepository; + + public List getProfileInfo(Long memberId){ + List profileInfo = new ArrayList<>(); + Member member = memberRepository.findById(memberId).orElseThrow(); // 찾고싶은 멤버 + + profileInfo.add(member.getNickname()); + // 뱃지 관련 + profileInfo.add(memberId.equals(memberService.getCurrentMember().getMemberId())); + return profileInfo; + } + /* + "nickname": "김지은", + "profileImg": "https://gol2580bucket.s3.us-east-2.amazonaws.com/9d465cf3-b2b0-43f9-ae54-06cfa60c9ebc_.png" + "profileBadge": "새싹환경운동가", + "profileBadgeImg": "https://gol2580bucket.s3.us-east-2.amazonaws.com/wizard.png", + "badgeList": ["새싹환경운동가", "얼리버드","","",""], + "badgeImgList": ["https://gol2580bucket.s3.us-east-2.amazonaws.com/wizard.png","https://gol2580bucket.s3.us-east-2.amazonaws.com/wizard.png"] + "isMine": true + */ } diff --git a/src/main/java/com/kimgreen/backend/response/Message.java b/src/main/java/com/kimgreen/backend/response/Message.java index ac65826..43f48b7 100644 --- a/src/main/java/com/kimgreen/backend/response/Message.java +++ b/src/main/java/com/kimgreen/backend/response/Message.java @@ -18,4 +18,6 @@ public class Message { public static String SUCCESS_GET_NOTIFICATION ="푸시알림 내역 보기 성공했습니다."; public static String POST_REPORT_SUCCESS="게시글 신고하기 성공했습니다."; + public static String PROFILE_INFO_SUCCESS="프로필 불러오기 성공했습니다."; + } From 26754fe1e87a8e23f8c32cc3bf2a95102ad3b9f2 Mon Sep 17 00:00:00 2001 From: gol2580 Date: Wed, 24 Jan 2024 02:35:26 +0900 Subject: [PATCH 02/12] [feat] calendar --- .../repository/CommentRepository.java | 4 + .../community/repository/LikeRepository.java | 4 + .../repository/PostImgRepository.java | 3 +- .../community/repository/PostRepository.java | 6 +- .../controller/calendarController.java | 19 ++- .../dto/Calendar/CalendarDetailDto.java | 27 ++++ .../Calendar/CalendarDetailRequestDto.java | 15 ++ .../Calendar/CalendarDetailResponseDto.java | 25 +++ .../dto/Calendar/CalendarResponseDto.java | 15 ++ .../profile/service/CalendarService.java | 146 ++++++++++++++++++ 10 files changed, 252 insertions(+), 12 deletions(-) create mode 100644 src/main/java/com/kimgreen/backend/domain/profile/dto/Calendar/CalendarDetailDto.java create mode 100644 src/main/java/com/kimgreen/backend/domain/profile/dto/Calendar/CalendarDetailRequestDto.java create mode 100644 src/main/java/com/kimgreen/backend/domain/profile/dto/Calendar/CalendarDetailResponseDto.java create mode 100644 src/main/java/com/kimgreen/backend/domain/profile/dto/Calendar/CalendarResponseDto.java diff --git a/src/main/java/com/kimgreen/backend/domain/community/repository/CommentRepository.java b/src/main/java/com/kimgreen/backend/domain/community/repository/CommentRepository.java index 8e5d879..27dca1f 100644 --- a/src/main/java/com/kimgreen/backend/domain/community/repository/CommentRepository.java +++ b/src/main/java/com/kimgreen/backend/domain/community/repository/CommentRepository.java @@ -2,8 +2,12 @@ import com.kimgreen.backend.domain.community.entity.Comment; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; @Repository public interface CommentRepository extends JpaRepository { + @Query("select count(*) from Comment c join Post p on c.post.postId=p.postId where p.postId= :id") + public Long countComment(@Param("id") Long postId); } diff --git a/src/main/java/com/kimgreen/backend/domain/community/repository/LikeRepository.java b/src/main/java/com/kimgreen/backend/domain/community/repository/LikeRepository.java index e440914..379a54f 100644 --- a/src/main/java/com/kimgreen/backend/domain/community/repository/LikeRepository.java +++ b/src/main/java/com/kimgreen/backend/domain/community/repository/LikeRepository.java @@ -2,8 +2,12 @@ import com.kimgreen.backend.domain.community.entity.Likes; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; @Repository public interface LikeRepository extends JpaRepository { + @Query("select count(*) from Likes l join Post p on l.post.postId=p.postId where p.postId= :id") + public Long countLike(@Param("id") Long postId); } diff --git a/src/main/java/com/kimgreen/backend/domain/community/repository/PostImgRepository.java b/src/main/java/com/kimgreen/backend/domain/community/repository/PostImgRepository.java index 4e38114..1994141 100644 --- a/src/main/java/com/kimgreen/backend/domain/community/repository/PostImgRepository.java +++ b/src/main/java/com/kimgreen/backend/domain/community/repository/PostImgRepository.java @@ -1,10 +1,11 @@ package com.kimgreen.backend.domain.community.repository; +import com.kimgreen.backend.domain.community.entity.Post; import com.kimgreen.backend.domain.community.entity.PostImg; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository public interface PostImgRepository extends JpaRepository { - + public PostImg findByPost(Post post); } diff --git a/src/main/java/com/kimgreen/backend/domain/community/repository/PostRepository.java b/src/main/java/com/kimgreen/backend/domain/community/repository/PostRepository.java index 255f43a..5f435a4 100644 --- a/src/main/java/com/kimgreen/backend/domain/community/repository/PostRepository.java +++ b/src/main/java/com/kimgreen/backend/domain/community/repository/PostRepository.java @@ -2,6 +2,8 @@ import com.kimgreen.backend.domain.community.entity.Post; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; import java.time.LocalDateTime; @@ -9,5 +11,7 @@ @Repository public interface PostRepository extends JpaRepository { - + @Query("select p from Post p where member.memberId= :id and createdAt between :start and :end") + public List findAllBetweenDate(@Param("id") Long memberId, @Param("start") LocalDateTime start, @Param("end") LocalDateTime end); + public List findAllByCreatedAtBetween(LocalDateTime start, LocalDateTime end); } diff --git a/src/main/java/com/kimgreen/backend/domain/profile/controller/calendarController.java b/src/main/java/com/kimgreen/backend/domain/profile/controller/calendarController.java index ba05c8f..310c273 100644 --- a/src/main/java/com/kimgreen/backend/domain/profile/controller/calendarController.java +++ b/src/main/java/com/kimgreen/backend/domain/profile/controller/calendarController.java @@ -1,6 +1,7 @@ package com.kimgreen.backend.domain.profile.controller; import com.kimgreen.backend.domain.member.dto.Auth.SignUpRequestDto; +import com.kimgreen.backend.domain.profile.dto.Calendar.CalendarDetailRequestDto; import com.kimgreen.backend.domain.profile.service.CalendarService; import com.kimgreen.backend.response.Response; import io.swagger.v3.oas.annotations.Operation; @@ -22,19 +23,17 @@ public class calendarController { private final CalendarService calendarService; - @Operation(summary = "프로필 달력 불러오기 (농사 잘했는지 불러오는거)") + @Operation(summary = "프로필 달력 불러오기") @ResponseStatus(OK) - @GetMapping("/calender") - public Response getCalender(@RequestParam("memberId") Long memberId, @RequestParam("date")LocalDateTime localDateTime) { - - return success(CALENDAR_SUCCESS); + @GetMapping("/simple") + public Response getCalender(@RequestParam("memberId") Long memberId, @RequestParam("date")String date) { + return success(CALENDAR_SUCCESS,calendarService.getCalendar(memberId, date)); } - @Operation(summary = "프로필 달력 상세정보 불러오기 (글들 불러오는거임)") + @Operation(summary = "프로필 달력 상세정보 불러오기") @ResponseStatus(OK) - @GetMapping("/calender-details") - public Response getCalenderDetails(@RequestBody SignUpRequestDto signUpRequestDto) { - - return success(CALENDAR_DETAILS_SUCCESS); + @GetMapping() + public Response getCalenderDetails(@RequestBody CalendarDetailRequestDto calendarDetailRequestDto) { + return success(CALENDAR_DETAILS_SUCCESS,calendarService.getCalendarDetails(calendarDetailRequestDto)); } } diff --git a/src/main/java/com/kimgreen/backend/domain/profile/dto/Calendar/CalendarDetailDto.java b/src/main/java/com/kimgreen/backend/domain/profile/dto/Calendar/CalendarDetailDto.java new file mode 100644 index 0000000..8db5cf1 --- /dev/null +++ b/src/main/java/com/kimgreen/backend/domain/profile/dto/Calendar/CalendarDetailDto.java @@ -0,0 +1,27 @@ +package com.kimgreen.backend.domain.profile.dto.Calendar; + +import com.kimgreen.backend.domain.community.entity.Post; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import java.util.ArrayList; +import java.util.List; + +@Builder +@Getter +@AllArgsConstructor +@NoArgsConstructor +public class CalendarDetailDto { + private Long postId; + private String writerNickname; + private String profileImg; + private String writerBadge; + private String content; + private int likeCount; + private Long commentCount; + private String imgUrl; + private Boolean isLiked; + +} diff --git a/src/main/java/com/kimgreen/backend/domain/profile/dto/Calendar/CalendarDetailRequestDto.java b/src/main/java/com/kimgreen/backend/domain/profile/dto/Calendar/CalendarDetailRequestDto.java new file mode 100644 index 0000000..3947356 --- /dev/null +++ b/src/main/java/com/kimgreen/backend/domain/profile/dto/Calendar/CalendarDetailRequestDto.java @@ -0,0 +1,15 @@ +package com.kimgreen.backend.domain.profile.dto.Calendar; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Builder +@Getter +@AllArgsConstructor +@NoArgsConstructor +public class CalendarDetailRequestDto { + private Long memberId; + private String date; +} diff --git a/src/main/java/com/kimgreen/backend/domain/profile/dto/Calendar/CalendarDetailResponseDto.java b/src/main/java/com/kimgreen/backend/domain/profile/dto/Calendar/CalendarDetailResponseDto.java new file mode 100644 index 0000000..2aeccb6 --- /dev/null +++ b/src/main/java/com/kimgreen/backend/domain/profile/dto/Calendar/CalendarDetailResponseDto.java @@ -0,0 +1,25 @@ +package com.kimgreen.backend.domain.profile.dto.Calendar; + +import com.kimgreen.backend.domain.community.entity.Post; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import java.util.List; + +@Builder +@Getter +@AllArgsConstructor +@NoArgsConstructor +public class CalendarDetailResponseDto { + private int postCount; + private List contents; + + public static CalendarDetailResponseDto toDto(int count, List contents) { + return CalendarDetailResponseDto.builder() + .postCount(count) + .contents(contents) + .build(); + } +} diff --git a/src/main/java/com/kimgreen/backend/domain/profile/dto/Calendar/CalendarResponseDto.java b/src/main/java/com/kimgreen/backend/domain/profile/dto/Calendar/CalendarResponseDto.java new file mode 100644 index 0000000..2ee4cb3 --- /dev/null +++ b/src/main/java/com/kimgreen/backend/domain/profile/dto/Calendar/CalendarResponseDto.java @@ -0,0 +1,15 @@ +package com.kimgreen.backend.domain.profile.dto.Calendar; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Builder +@Getter +@AllArgsConstructor +@NoArgsConstructor +public class CalendarResponseDto { + private String date; + private int postCount; +} diff --git a/src/main/java/com/kimgreen/backend/domain/profile/service/CalendarService.java b/src/main/java/com/kimgreen/backend/domain/profile/service/CalendarService.java index 8c24412..b0b113b 100644 --- a/src/main/java/com/kimgreen/backend/domain/profile/service/CalendarService.java +++ b/src/main/java/com/kimgreen/backend/domain/profile/service/CalendarService.java @@ -1,9 +1,155 @@ package com.kimgreen.backend.domain.profile.service; +import com.kimgreen.backend.domain.community.entity.Comment; +import com.kimgreen.backend.domain.community.entity.Likes; +import com.kimgreen.backend.domain.community.entity.Post; +import com.kimgreen.backend.domain.community.entity.PostImg; +import com.kimgreen.backend.domain.community.repository.CommentRepository; +import com.kimgreen.backend.domain.community.repository.LikeRepository; +import com.kimgreen.backend.domain.community.repository.PostImgRepository; +import com.kimgreen.backend.domain.community.repository.PostRepository; +import com.kimgreen.backend.domain.community.service.S3Service; +import com.kimgreen.backend.domain.member.entity.Member; +import com.kimgreen.backend.domain.member.entity.MemberProfileImg; +import com.kimgreen.backend.domain.member.repository.MemberProfileImgRepository; +import com.kimgreen.backend.domain.member.repository.MemberRepository; +import com.kimgreen.backend.domain.member.service.MemberService; +import com.kimgreen.backend.domain.profile.dto.Calendar.CalendarDetailDto; +import com.kimgreen.backend.domain.profile.dto.Calendar.CalendarDetailRequestDto; +import com.kimgreen.backend.domain.profile.dto.Calendar.CalendarDetailResponseDto; +import com.kimgreen.backend.domain.profile.dto.Calendar.CalendarResponseDto; +import com.kimgreen.backend.domain.profile.entity.RepresentativeBadge; +import com.kimgreen.backend.domain.profile.repository.RepresentativeBadgeRepository; import lombok.AllArgsConstructor; import org.springframework.stereotype.Service; +import javax.swing.text.DateFormatter; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.*; + @Service @AllArgsConstructor public class CalendarService { + private final PostRepository postRepository; + private final MemberService memberService; + private final RepresentativeBadgeRepository representativeBadgeRepository; + private final PostImgRepository postImgRepository; + private final MemberProfileImgRepository memberProfileImgRepository; + private final S3Service s3Service; + private final LikeRepository likeRepository; + private final CommentRepository commentRepository; + private final MemberRepository memberRepository; + + public List getCalendar(Long memberId, String date) { + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); + LocalDateTime localDateTime = LocalDate.parse(date, formatter).withDayOfMonth(1).atStartOfDay(); + LocalDateTime start = localDateTime; + LocalDateTime end = localDateTime.withDayOfMonth(start.toLocalDate().lengthOfMonth()).withHour(23).withMinute(59).withSecond(59); + + List postList = postRepository.findAllBetweenDate(memberId,start,end); + return getDtoList(postList); + } + + public CalendarDetailResponseDto getCalendarDetails(CalendarDetailRequestDto dto) { + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); + LocalDateTime start = LocalDate.parse(dto.getDate(), formatter).atStartOfDay(); + LocalDateTime end = start.withHour(23).withMinute(59).withSecond(59); + List postList = postRepository.findAllByCreatedAtBetween(start,end); + return CalendarDetailResponseDto.toDto(postList.size(),getResult(postList)); + + } + + public List getDtoList(List postList) { + Map resultMap = new HashMap<>(); + for(Post post:postList) { + String key = post.getCreatedAt().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")); + if(!resultMap.containsKey(key)) { + resultMap.put(key,1); + } else { + Integer value =resultMap.get(key); + resultMap.put(key,++value); + } + } + + return toList(resultMap); + } + + public List toList(Map maps) { + List result = new ArrayList<>(); + for(Map.Entry map :maps.entrySet()) { + result.add(CalendarResponseDto.builder() + .date(map.getKey()) + .postCount(map.getValue()) + .build()); + } + return result; + } + + public List getResult(List postList) { + CalendarDetailDto dto; + List result = new ArrayList<>(); + for(Post post : postList) { + Long postId = post.getPostId(); + Member member = post.getMember(); + RepresentativeBadge badge = representativeBadgeRepository.findByMember(member); + MemberProfileImg profileImg = memberProfileImgRepository.findByMember(member); + PostImg postImg = postImgRepository.findByPost(post); + List likeList = post.getLikes(); + boolean isLiked = isLiked(likeList,member); + //Long likes = likeRepository.countLike(postId); + Long comments = commentRepository.countComment(postId); + + + if(postImg==null) { + dto = dtoBuilderWithNoImg(postId,member.getNickname(),s3Service.getFullUrl(profileImg.getImgUrl()),badge.getRepresentativeBadge().name + ,post.getContent(),likeList.size(),comments,isLiked); + } else { + dto = dtoBuilderWithImg(postId, member.getNickname(), s3Service.getFullUrl(profileImg.getImgUrl()), badge.getRepresentativeBadge().name + , post.getContent(), likeList.size(), comments, s3Service.getFullUrl(postImg.getImgUrl()),isLiked); + } + + result.add(dto); + } + return result; + } + + public CalendarDetailDto dtoBuilderWithImg(Long postId, String nickname, String profileImg,String name,String content + ,int likes, Long comments, String postImg, boolean isLiked) { + return CalendarDetailDto.builder() + .postId(postId) + .writerNickname(nickname) + .profileImg(profileImg) + .writerBadge(name) + .content(content) + .likeCount(likes) + .commentCount(comments) + .imgUrl(postImg) + .isLiked(isLiked) + .build(); + } + + public CalendarDetailDto dtoBuilderWithNoImg(Long postId, String nickname, String profileImg,String name,String content + ,int likes, Long comments, boolean isLiked) { + return CalendarDetailDto.builder() + .postId(postId) + .writerNickname(nickname) + .profileImg(profileImg) + .writerBadge(name) + .content(content) + .likeCount(likes) + .commentCount(comments) + .isLiked(isLiked) + .build(); + } + + public boolean isLiked(List likesList,Member member) { + for(Likes like : likesList) { + if(like.getLikeId().equals(member.getMemberId())) { + return true; + } + } + return false; + } } From 6768818718c5753e68f15106f46530e191ecef3b Mon Sep 17 00:00:00 2001 From: gol2580 <86960201+gol2580@users.noreply.github.com> Date: Wed, 24 Jan 2024 15:31:30 +0900 Subject: [PATCH 03/12] Update code-deploy.yml --- .github/workflows/code-deploy.yml | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/.github/workflows/code-deploy.yml b/.github/workflows/code-deploy.yml index eaf954c..a42b375 100644 --- a/.github/workflows/code-deploy.yml +++ b/.github/workflows/code-deploy.yml @@ -9,9 +9,8 @@ name: Java CI with Gradle on: push: - branches: [ "master" ] - pull_request: - branches: [ "master" ] + branches: [ "deploy" ] + permissions: contents: read From be737ae9259095dcafec0761ef98d560f7990809 Mon Sep 17 00:00:00 2001 From: gol2580 Date: Thu, 25 Jan 2024 16:21:07 +0900 Subject: [PATCH 04/12] [feat] setLike --- .../community/controller/LikeController.java | 22 ++++++++++-- .../community/repository/LikeRepository.java | 6 ++++ .../domain/community/service/LikeService.java | 36 +++++++++++++++++++ .../kimgreen/backend/response/Message.java | 2 ++ 4 files changed, 64 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/kimgreen/backend/domain/community/controller/LikeController.java b/src/main/java/com/kimgreen/backend/domain/community/controller/LikeController.java index 544dbf3..fbe9207 100644 --- a/src/main/java/com/kimgreen/backend/domain/community/controller/LikeController.java +++ b/src/main/java/com/kimgreen/backend/domain/community/controller/LikeController.java @@ -1,12 +1,30 @@ package com.kimgreen.backend.domain.community.controller; +import com.kimgreen.backend.domain.community.repository.PostRepository; +import com.kimgreen.backend.domain.community.service.LikeService; +import com.kimgreen.backend.response.Response; +import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; +import lombok.AllArgsConstructor; import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; + +import static com.kimgreen.backend.response.Message.*; +import static org.springframework.http.HttpStatus.*; @Tag(name = "Like") @RestController @RequestMapping(value="/post/like") +@AllArgsConstructor public class LikeController { + + private final LikeService likeService; + + @Operation(summary = "좋아요 누르기 or 취소하기") + @ResponseStatus(OK) + @PostMapping() + public Response setLike(@RequestParam("postId") Long postId) { + likeService.setLike(postId); + return Response.success(SET_LIKES_SUCCESS); + } } diff --git a/src/main/java/com/kimgreen/backend/domain/community/repository/LikeRepository.java b/src/main/java/com/kimgreen/backend/domain/community/repository/LikeRepository.java index 379a54f..851c89b 100644 --- a/src/main/java/com/kimgreen/backend/domain/community/repository/LikeRepository.java +++ b/src/main/java/com/kimgreen/backend/domain/community/repository/LikeRepository.java @@ -1,13 +1,19 @@ package com.kimgreen.backend.domain.community.repository; import com.kimgreen.backend.domain.community.entity.Likes; +import com.kimgreen.backend.domain.community.entity.Post; +import com.kimgreen.backend.domain.member.entity.Member; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; +import java.util.Optional; + @Repository public interface LikeRepository extends JpaRepository { @Query("select count(*) from Likes l join Post p on l.post.postId=p.postId where p.postId= :id") public Long countLike(@Param("id") Long postId); + + public Optional findByPostAndMember(Post post, Member member); } diff --git a/src/main/java/com/kimgreen/backend/domain/community/service/LikeService.java b/src/main/java/com/kimgreen/backend/domain/community/service/LikeService.java index 80380db..ffd4005 100644 --- a/src/main/java/com/kimgreen/backend/domain/community/service/LikeService.java +++ b/src/main/java/com/kimgreen/backend/domain/community/service/LikeService.java @@ -1,7 +1,43 @@ package com.kimgreen.backend.domain.community.service; +import com.kimgreen.backend.domain.community.entity.Likes; +import com.kimgreen.backend.domain.community.entity.Post; +import com.kimgreen.backend.domain.community.repository.LikeRepository; +import com.kimgreen.backend.domain.community.repository.PostRepository; +import com.kimgreen.backend.domain.member.entity.Member; +import com.kimgreen.backend.domain.member.service.MemberService; +import com.kimgreen.backend.exception.PostNotFound; +import lombok.AllArgsConstructor; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.Optional; @Service +@AllArgsConstructor public class LikeService { + + private final MemberService memberService; + private final PostRepository postRepository; + private final LikeRepository likeRepository; + + @Transactional + public void setLike(Long postId) { + Member member = memberService.getCurrentMember(); + Post post = postRepository.findById(postId).orElseThrow(()-> new PostNotFound()); + + Optional foundLike = likeRepository.findByPostAndMember(post,member); + if(foundLike.isEmpty()) { + postLike(member, post); + } else { + likeRepository.delete(foundLike.get()); + } + } + + public void postLike(Member member, Post post) { + likeRepository.save(Likes.builder() + .post(post) + .member(member) + .build()); + } } diff --git a/src/main/java/com/kimgreen/backend/response/Message.java b/src/main/java/com/kimgreen/backend/response/Message.java index ed44f04..c59b4b7 100644 --- a/src/main/java/com/kimgreen/backend/response/Message.java +++ b/src/main/java/com/kimgreen/backend/response/Message.java @@ -24,4 +24,6 @@ public class Message { public static String DELETE_COMMENT_SUCCESS="댓글 삭제 성공했습니다."; public static String GET_COMMENT_SUCCESS="댓글 목록을 불러오는데 성공하였습니다."; + public static String SET_LIKES_SUCCESS="게시글 좋아요 or 취소하기 성공했습니다."; + } From 5e3754d4736e30f5bcce8faaf256d554d9be23b4 Mon Sep 17 00:00:00 2001 From: gol2580 Date: Thu, 25 Jan 2024 16:50:30 +0900 Subject: [PATCH 05/12] [fix] saveMember - update sprout --- .../backend/domain/member/service/AuthService.java | 9 +++++++++ .../kimgreen/backend/domain/profile/entity/Badge.java | 6 ++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/kimgreen/backend/domain/member/service/AuthService.java b/src/main/java/com/kimgreen/backend/domain/member/service/AuthService.java index f425f33..5e48ed2 100644 --- a/src/main/java/com/kimgreen/backend/domain/member/service/AuthService.java +++ b/src/main/java/com/kimgreen/backend/domain/member/service/AuthService.java @@ -53,6 +53,7 @@ public class AuthService { private static final String EXPIRED = "expired"; private final FCMService FCMService; + @Transactional public void signUp(SignUpRequestDto signUpRequestDto) { String email = signUpRequestDto.getEmail(); String password = signUpRequestDto.getPassword(); @@ -60,6 +61,7 @@ public void signUp(SignUpRequestDto signUpRequestDto) { validateEmail(email); saveMember(signUpRequestDto,email, password, nickname); + updateSprout(memberRepository.findByEmail(email)); } @Transactional @@ -102,6 +104,7 @@ public TokenDto logIn(LogInRequestDto dto) { } + @Transactional public TokenDto tokenReissue(TokenDto tokenDto) { //어차피 accessToken 만료인 경우에 호출되기 때문에 AT는 검증할 필요X String accessToken = tokenDto.getAccessToken(); @@ -161,6 +164,7 @@ public void validateEmail(String email) { } } + @Transactional public void saveMember(SignUpRequestDto signUpRequestDto,String email, String password, String nickname) { memberRepository.save(signUpRequestDto.toMemberEntity(email, passwordEncoder.encode(password),nickname)); Member member = memberRepository.findByEmail(email); @@ -174,4 +178,9 @@ public void saveMember(SignUpRequestDto signUpRequestDto,String email, String pa .build()); } + @Transactional + public void updateSprout(Member member) { + badgeRepository.findByMember(member).setSproutIsAchieved(true); + } + } diff --git a/src/main/java/com/kimgreen/backend/domain/profile/entity/Badge.java b/src/main/java/com/kimgreen/backend/domain/profile/entity/Badge.java index f1d7557..14b4f3d 100644 --- a/src/main/java/com/kimgreen/backend/domain/profile/entity/Badge.java +++ b/src/main/java/com/kimgreen/backend/domain/profile/entity/Badge.java @@ -3,16 +3,14 @@ import com.kimgreen.backend.domain.AuditEntity; import com.kimgreen.backend.domain.member.entity.Member; import jakarta.persistence.*; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Getter; -import lombok.NoArgsConstructor; +import lombok.*; @Getter @NoArgsConstructor @AllArgsConstructor @Entity @Builder +@Setter public class Badge extends AuditEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) From 0b4a8e1ff7bed70fd251188fe55743c371293749 Mon Sep 17 00:00:00 2001 From: epdlrnldudnj Date: Sun, 28 Jan 2024 22:55:34 +0900 Subject: [PATCH 06/12] =?UTF-8?q?=ED=94=84=EB=A1=9C=ED=95=84=20=EC=A0=95?= =?UTF-8?q?=EB=B3=B4=20=EB=B6=88=EB=9F=AC=EC=98=A4=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 기본적으로 계정 생성하면 프로필뱃지를 기본적으로 BLANK로 설정함 * 기존에 생성된 계정들은 프로필 뱃지가 null이어서 프로필 정보 불러오기가 제대로 안됨 * memberId 1,12,20 만 가능 --- .../kimgreen/backend/domain/BadgeList.java | 2 +- .../domain/member/service/AuthService.java | 8 ++- .../profile/controller/profileController.java | 22 ++++++- .../domain/profile/dto/GetProfileDto.java | 45 +++++++++++++ .../profile/service/ProfileService.java | 65 ++++++++++++++----- 5 files changed, 122 insertions(+), 20 deletions(-) create mode 100644 src/main/java/com/kimgreen/backend/domain/profile/dto/GetProfileDto.java diff --git a/src/main/java/com/kimgreen/backend/domain/BadgeList.java b/src/main/java/com/kimgreen/backend/domain/BadgeList.java index 0e5d50b..7817dfd 100644 --- a/src/main/java/com/kimgreen/backend/domain/BadgeList.java +++ b/src/main/java/com/kimgreen/backend/domain/BadgeList.java @@ -1,7 +1,7 @@ package com.kimgreen.backend.domain; public enum BadgeList { - BLANK("","",1), + BLANK("","","",1), MENTOR("그린 멘토",5,"mentor.png","질문카테고리 댓글 5회 이상",2), MENTEE("그린 멘티",5,"mentee.png","질문 카테고리 글 작성 5회 이상",3), EARLYBIRD("얼리버드",3,"earlybird.png","9시 이전에 인증글 3회이상 작성",4), diff --git a/src/main/java/com/kimgreen/backend/domain/member/service/AuthService.java b/src/main/java/com/kimgreen/backend/domain/member/service/AuthService.java index 837d4d0..45b5e48 100644 --- a/src/main/java/com/kimgreen/backend/domain/member/service/AuthService.java +++ b/src/main/java/com/kimgreen/backend/domain/member/service/AuthService.java @@ -157,7 +157,13 @@ public void saveMember(SignUpRequestDto signUpRequestDto,String email, String pa Member member = memberRepository.findByEmail(email); profileImgRepository.save(signUpRequestDto.toMemberProfileImgEntity(member)); badgeRepository.save(Badge.builder().member(member).build()); - profileBadgeRepository.save(ProfileBadge.builder().member(member).build()); + profileBadgeRepository.save(ProfileBadge.builder() + .member(member) + .profileBadge_1(BadgeList.BLANK) + .profileBadge_2(BadgeList.BLANK) + .profileBadge_3(BadgeList.BLANK) + .profileBadge_4(BadgeList.BLANK) + .profileBadge_5(BadgeList.BLANK).build()); representativeBadgeRepository.save( RepresentativeBadge.builder() .representativeBadge(BadgeList.BLANK) diff --git a/src/main/java/com/kimgreen/backend/domain/profile/controller/profileController.java b/src/main/java/com/kimgreen/backend/domain/profile/controller/profileController.java index bcf6667..057cbf2 100644 --- a/src/main/java/com/kimgreen/backend/domain/profile/controller/profileController.java +++ b/src/main/java/com/kimgreen/backend/domain/profile/controller/profileController.java @@ -1,12 +1,16 @@ package com.kimgreen.backend.domain.profile.controller; +import com.kimgreen.backend.domain.profile.dto.GetProfileDto; import com.kimgreen.backend.domain.profile.service.ProfileService; import com.kimgreen.backend.response.Response; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; +import lombok.AllArgsConstructor; import lombok.RequiredArgsConstructor; +import org.springdoc.core.annotations.ParameterObject; import org.springframework.web.bind.annotation.*; +import java.awt.print.Pageable; import java.util.*; import static com.kimgreen.backend.response.Message.PROFILE_INFO_SUCCESS; @@ -17,14 +21,26 @@ @RestController @RequestMapping(value="/profile") @RequiredArgsConstructor + public class profileController { private final ProfileService profileService; + @Operation(summary = "프로필 글 목록 불러오기(특정 멤버가 쓴 글 목록 불러오기)") + @ResponseStatus(OK) + @GetMapping("/post") + public Response getProfilePosts(@RequestParam("memberId") Long memberId, + @RequestParam("page") int page, + @RequestParam("size") int size, + @ParameterObject Pageable sort) + { + profileService.getProfilePosts(); + return success(PROFILE_INFO_SUCCESS); + } @Operation(summary = "프로필 정보 불러오기") @ResponseStatus(OK) - @GetMapping("/profile-info") + @GetMapping() public Response getProfileInfo(@RequestParam("memberId") Long memberId) { - List profileInfo = profileService.getProfileInfo(memberId); - return success(PROFILE_INFO_SUCCESS, profileInfo); + return success(PROFILE_INFO_SUCCESS, profileService.getProfileInfo(memberId)); } + } diff --git a/src/main/java/com/kimgreen/backend/domain/profile/dto/GetProfileDto.java b/src/main/java/com/kimgreen/backend/domain/profile/dto/GetProfileDto.java new file mode 100644 index 0000000..f2ba63c --- /dev/null +++ b/src/main/java/com/kimgreen/backend/domain/profile/dto/GetProfileDto.java @@ -0,0 +1,45 @@ +package com.kimgreen.backend.domain.profile.dto; + +import com.kimgreen.backend.domain.BadgeList; +import com.kimgreen.backend.domain.member.entity.Member; +import com.kimgreen.backend.domain.profile.entity.Badge; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; +import org.springframework.stereotype.Component; + +import java.util.ArrayList; + +@Builder +@Getter +@AllArgsConstructor +@NoArgsConstructor +@Component +public class GetProfileDto { + private String nickname; + private String profileImg; + private String profileBadge; + private String profileBadgeImg; + private ArrayList badgeList; + private ArrayList badgeImgList; + private boolean isMine; + + public GetProfileDto from(Member member, + String profileImg, + String profileBadge, + String profileBadgeImg, + ArrayList badgeList, + ArrayList badgeImgList, + boolean isMine){ + return GetProfileDto.builder() + .nickname(member.getNickname()) + .profileImg(profileImg) + .profileBadge(profileBadge) + .profileBadgeImg(profileBadgeImg) + .badgeList(badgeList) + .badgeImgList(badgeImgList) + .isMine(isMine) + .build(); + } +} diff --git a/src/main/java/com/kimgreen/backend/domain/profile/service/ProfileService.java b/src/main/java/com/kimgreen/backend/domain/profile/service/ProfileService.java index 7f109b5..ea13eba 100644 --- a/src/main/java/com/kimgreen/backend/domain/profile/service/ProfileService.java +++ b/src/main/java/com/kimgreen/backend/domain/profile/service/ProfileService.java @@ -1,34 +1,69 @@ package com.kimgreen.backend.domain.profile.service; +import com.kimgreen.backend.domain.BadgeList; +import com.kimgreen.backend.domain.community.service.S3Service; import com.kimgreen.backend.domain.member.entity.Member; +import com.kimgreen.backend.domain.member.entity.MemberProfileImg; +import com.kimgreen.backend.domain.member.repository.MemberProfileImgRepository; import com.kimgreen.backend.domain.member.repository.MemberRepository; import com.kimgreen.backend.domain.member.service.MemberService; +import com.kimgreen.backend.domain.profile.dto.GetProfileDto; +import com.kimgreen.backend.domain.profile.entity.ProfileBadge; +import com.kimgreen.backend.domain.profile.entity.RepresentativeBadge; +import com.kimgreen.backend.domain.profile.repository.ProfileBadgeRepository; +import com.kimgreen.backend.domain.profile.repository.RepresentativeBadgeRepository; +import lombok.AllArgsConstructor; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; +import org.springframework.web.bind.annotation.RequestParam; + import java.util.*; @Service @RequiredArgsConstructor + public class ProfileService { private final MemberService memberService; private final MemberRepository memberRepository; + private final GetProfileDto getProfileDto; + private final MemberProfileImgRepository memberProfileImgRepository; + private final S3Service s3Service; + private final RepresentativeBadgeRepository representativeBadgeRepository; + private final ProfileBadgeRepository profileBadgeRepository; + + public void getProfilePosts(){ - public List getProfileInfo(Long memberId){ - List profileInfo = new ArrayList<>(); + } + public GetProfileDto getProfileInfo(@RequestParam("memberId") Long memberId){ Member member = memberRepository.findById(memberId).orElseThrow(); // 찾고싶은 멤버 + MemberProfileImg memberProfileImg = memberProfileImgRepository.findByMember(member); + RepresentativeBadge representativeBadge = representativeBadgeRepository.findByMember(member); + profileBadgeRepository.findByMember(member); + + ArrayList badgeList = new ArrayList<>(); + ArrayList badgeImgList = new ArrayList<>(); + + ProfileBadge profileBadge = profileBadgeRepository.findByMember(member); + ArrayList list = new ArrayList<>(); + list.add(profileBadge.getProfileBadge_1()); + list.add(profileBadge.getProfileBadge_2()); + list.add(profileBadge.getProfileBadge_3()); + list.add(profileBadge.getProfileBadge_4()); + list.add(profileBadge.getProfileBadge_5()); + for(BadgeList b : list){ + badgeList.add(b.name); + if(b != BadgeList.BLANK){ + badgeImgList.add(s3Service.getFullUrl(b.url)); + } + } - profileInfo.add(member.getNickname()); - // 뱃지 관련 - profileInfo.add(memberId.equals(memberService.getCurrentMember().getMemberId())); - return profileInfo; + return getProfileDto.from(member, + s3Service.getFullUrl(memberProfileImg.getImgUrl()), + representativeBadge.getRepresentativeBadge().name, + s3Service.getFullUrl(representativeBadge.getRepresentativeBadge().url), + badgeList, + badgeImgList, + memberId.equals(memberService.getCurrentMember().getMemberId()) + ); } - /* - "nickname": "김지은", - "profileImg": "https://gol2580bucket.s3.us-east-2.amazonaws.com/9d465cf3-b2b0-43f9-ae54-06cfa60c9ebc_.png" - "profileBadge": "새싹환경운동가", - "profileBadgeImg": "https://gol2580bucket.s3.us-east-2.amazonaws.com/wizard.png", - "badgeList": ["새싹환경운동가", "얼리버드","","",""], - "badgeImgList": ["https://gol2580bucket.s3.us-east-2.amazonaws.com/wizard.png","https://gol2580bucket.s3.us-east-2.amazonaws.com/wizard.png"] - "isMine": true - */ } From b027092372bb1198bacb256b4abc2ac41c13406a Mon Sep 17 00:00:00 2001 From: epdlrnldudnj Date: Mon, 29 Jan 2024 22:37:54 +0900 Subject: [PATCH 07/12] =?UTF-8?q?=ED=94=84=EB=A1=9C=ED=95=84=20=EA=B8=80?= =?UTF-8?q?=20=EB=AA=A9=EB=A1=9D=20=EB=B6=88=EB=9F=AC=EC=98=A4=EA=B8=B0(?= =?UTF-8?q?=ED=8A=B9=EC=A0=95=20=EB=A9=A4=EB=B2=84=EA=B0=80=20=EC=93=B4=20?= =?UTF-8?q?=EA=B8=80=20=EB=AA=A9=EB=A1=9D=20=EB=B6=88=EB=9F=AC=EC=98=A4?= =?UTF-8?q?=EA=B8=B0)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 완료 --- .../profile/controller/profileController.java | 14 +- .../dto/{ => Profile}/GetProfileDto.java | 4 +- .../dto/Profile/GetProfilePostDto.java | 24 ++++ .../profile/service/ProfileService.java | 121 +++++++++++++++++- .../kimgreen/backend/response/Message.java | 2 +- 5 files changed, 145 insertions(+), 20 deletions(-) rename src/main/java/com/kimgreen/backend/domain/profile/dto/{ => Profile}/GetProfileDto.java (89%) create mode 100644 src/main/java/com/kimgreen/backend/domain/profile/dto/Profile/GetProfilePostDto.java diff --git a/src/main/java/com/kimgreen/backend/domain/profile/controller/profileController.java b/src/main/java/com/kimgreen/backend/domain/profile/controller/profileController.java index 057cbf2..f5da4ac 100644 --- a/src/main/java/com/kimgreen/backend/domain/profile/controller/profileController.java +++ b/src/main/java/com/kimgreen/backend/domain/profile/controller/profileController.java @@ -1,19 +1,18 @@ package com.kimgreen.backend.domain.profile.controller; -import com.kimgreen.backend.domain.profile.dto.GetProfileDto; +import com.kimgreen.backend.domain.profile.dto.Profile.GetProfilePostDto; import com.kimgreen.backend.domain.profile.service.ProfileService; import com.kimgreen.backend.response.Response; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; -import lombok.AllArgsConstructor; import lombok.RequiredArgsConstructor; import org.springdoc.core.annotations.ParameterObject; import org.springframework.web.bind.annotation.*; import java.awt.print.Pageable; -import java.util.*; import static com.kimgreen.backend.response.Message.PROFILE_INFO_SUCCESS; +import static com.kimgreen.backend.response.Message.PROFILE_POSTS_SUCCESS; import static com.kimgreen.backend.response.Response.success; import static org.springframework.http.HttpStatus.OK; @@ -28,13 +27,8 @@ public class profileController { @Operation(summary = "프로필 글 목록 불러오기(특정 멤버가 쓴 글 목록 불러오기)") @ResponseStatus(OK) @GetMapping("/post") - public Response getProfilePosts(@RequestParam("memberId") Long memberId, - @RequestParam("page") int page, - @RequestParam("size") int size, - @ParameterObject Pageable sort) - { - profileService.getProfilePosts(); - return success(PROFILE_INFO_SUCCESS); + public Response getProfilePosts(@RequestParam("memberId") Long memberId) { + return success(PROFILE_POSTS_SUCCESS, profileService.response(memberId)); } @Operation(summary = "프로필 정보 불러오기") @ResponseStatus(OK) diff --git a/src/main/java/com/kimgreen/backend/domain/profile/dto/GetProfileDto.java b/src/main/java/com/kimgreen/backend/domain/profile/dto/Profile/GetProfileDto.java similarity index 89% rename from src/main/java/com/kimgreen/backend/domain/profile/dto/GetProfileDto.java rename to src/main/java/com/kimgreen/backend/domain/profile/dto/Profile/GetProfileDto.java index f2ba63c..e289a29 100644 --- a/src/main/java/com/kimgreen/backend/domain/profile/dto/GetProfileDto.java +++ b/src/main/java/com/kimgreen/backend/domain/profile/dto/Profile/GetProfileDto.java @@ -1,8 +1,6 @@ -package com.kimgreen.backend.domain.profile.dto; +package com.kimgreen.backend.domain.profile.dto.Profile; -import com.kimgreen.backend.domain.BadgeList; import com.kimgreen.backend.domain.member.entity.Member; -import com.kimgreen.backend.domain.profile.entity.Badge; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Getter; diff --git a/src/main/java/com/kimgreen/backend/domain/profile/dto/Profile/GetProfilePostDto.java b/src/main/java/com/kimgreen/backend/domain/profile/dto/Profile/GetProfilePostDto.java new file mode 100644 index 0000000..c68fec2 --- /dev/null +++ b/src/main/java/com/kimgreen/backend/domain/profile/dto/Profile/GetProfilePostDto.java @@ -0,0 +1,24 @@ +package com.kimgreen.backend.domain.profile.dto.Profile; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; +import org.springframework.stereotype.Component; + +@Builder +@Getter +@AllArgsConstructor +@NoArgsConstructor +@Component +public class GetProfilePostDto { + private Long postId; + private String writerNickname; + private String writerBadge; + private String writerProfileImg; + private String content; + private int likeCount; + private int commentCount; + private String imgUrl; + private boolean isLiked; +} diff --git a/src/main/java/com/kimgreen/backend/domain/profile/service/ProfileService.java b/src/main/java/com/kimgreen/backend/domain/profile/service/ProfileService.java index ea13eba..74191d4 100644 --- a/src/main/java/com/kimgreen/backend/domain/profile/service/ProfileService.java +++ b/src/main/java/com/kimgreen/backend/domain/profile/service/ProfileService.java @@ -1,26 +1,32 @@ package com.kimgreen.backend.domain.profile.service; import com.kimgreen.backend.domain.BadgeList; +import com.kimgreen.backend.domain.community.entity.Likes; +import com.kimgreen.backend.domain.community.entity.Post; +import com.kimgreen.backend.domain.community.repository.PostImgRepository; +import com.kimgreen.backend.domain.community.repository.PostRepository; import com.kimgreen.backend.domain.community.service.S3Service; import com.kimgreen.backend.domain.member.entity.Member; import com.kimgreen.backend.domain.member.entity.MemberProfileImg; import com.kimgreen.backend.domain.member.repository.MemberProfileImgRepository; import com.kimgreen.backend.domain.member.repository.MemberRepository; import com.kimgreen.backend.domain.member.service.MemberService; -import com.kimgreen.backend.domain.profile.dto.GetProfileDto; +import com.kimgreen.backend.domain.profile.dto.Profile.GetProfileDto; import com.kimgreen.backend.domain.profile.entity.ProfileBadge; import com.kimgreen.backend.domain.profile.entity.RepresentativeBadge; import com.kimgreen.backend.domain.profile.repository.ProfileBadgeRepository; import com.kimgreen.backend.domain.profile.repository.RepresentativeBadgeRepository; import lombok.AllArgsConstructor; -import lombok.RequiredArgsConstructor; +import lombok.Builder; import org.springframework.stereotype.Service; -import org.springframework.web.bind.annotation.RequestParam; +import com.kimgreen.backend.domain.profile.dto.Profile.GetProfilePostDto; import java.util.*; @Service -@RequiredArgsConstructor +//@RequiredArgsConstructor +@AllArgsConstructor + public class ProfileService { private final MemberService memberService; @@ -30,11 +36,113 @@ public class ProfileService { private final S3Service s3Service; private final RepresentativeBadgeRepository representativeBadgeRepository; private final ProfileBadgeRepository profileBadgeRepository; + private final PostRepository postRepository; + private final PostImgRepository postImgRepository; + private final GetProfilePostDto getProfilePostDto; - public void getProfilePosts(){ + public List response(Long memberId){ + List list = new ArrayList<>(); + List allPostList = postRepository.findAll(); + List postList = new ArrayList<>(); + for(Post p : allPostList){ + if(p.getMember().getMemberId().equals(memberId)){ + postList.add(p); + } + } + for(Post p : postList) { + boolean isLiked = false; + for(Likes l : p.getLikes()){ + if(l.getMember().getMemberId().equals(memberService.getCurrentMember().getMemberId())){ + isLiked = true; + break; + } + } + if(postImgRepository.findByPost(p) == null){ + list.add(GetProfilePostDto.builder() + .postId(p.getPostId()) + .writerNickname(p.getMember().getNickname()) + .writerBadge(representativeBadgeRepository.findByMember(p.getMember()).getRepresentativeBadge().name) + .writerProfileImg(s3Service.getFullUrl(memberProfileImgRepository.findByMember(p.getMember()).getImgUrl())) + .content(p.getContent()) + .likeCount(p.getLikes().size()) + .commentCount(p.getComments().size()) + .isLiked(isLiked).build()); + }else{ // 포스트이미지 있을때 + list.add(GetProfilePostDto.builder() + .postId(p.getPostId()) + .writerNickname(p.getMember().getNickname()) + .writerBadge(representativeBadgeRepository.findByMember(p.getMember()).getRepresentativeBadge().name) + .writerProfileImg(s3Service.getFullUrl(memberProfileImgRepository.findByMember(p.getMember()).getImgUrl())) + .content(p.getContent()) + .likeCount(p.getLikes().size()) + .commentCount(p.getComments().size()) + .imgUrl(s3Service.getFullUrl(postImgRepository.findByPost(p).getImgUrl())) + .isLiked(isLiked).build()); + } + } + return list; } - public GetProfileDto getProfileInfo(@RequestParam("memberId") Long memberId){ + /* + public List getProfilePosts(Long memberId){ + List posts = postRepository.findAll(); + Member member = memberRepository.findById(memberId).orElseThrow(); + for(Post p : posts){ + if(!memberId.equals(p.getMember().getMemberId())){ + posts.remove(p); + } + + } + List profilePosts = new ArrayList<>(); + for (Post p : posts){ + Long postId = p.getPostId(); + /* + String writerNickname = member.getNickname(); + String writerBadge = representativeBadgeRepository.findByMember(member).getRepresentativeBadge().name; + String content = p.getContent(); + int likeCount = p.getLikes().size(); + int commentCount = p.getComments().size(); + + boolean isLiked = isLiked(p.getLikes(),memberService.getCurrentMember()); + + for(Likes likes : p.getLikes()){ + if(likes.getMember().getMemberId().equals(memberService.getCurrentMember().getMemberId())){ + isLiked = true; + break; + } + } + + + String imgUrl = ""; + + if(postImgRepository.findByPost(p) != null){ + imgUrl = s3Service.getFullUrl(postImgRepository.findByPost(p).getImgUrl()); + } + */ + /* + String writerProfileImg = s3Service.getFullUrl(memberProfileImgRepository.findByMember(p.getMember()).getImgUrl()); + profilePosts.add(getProfilePostsDto.from( + postId, + writerNickname, + writerBadge, + writerProfileImg, + content, + likeCount, + commentCount + //imgUrl, + //isLiked + )); + + + profilePosts.add(GetProfilePostDto.builder() + .postId(postId) + .build()); +} + return profilePosts; + } + */ + + public GetProfileDto getProfileInfo(Long memberId){ Member member = memberRepository.findById(memberId).orElseThrow(); // 찾고싶은 멤버 MemberProfileImg memberProfileImg = memberProfileImgRepository.findByMember(member); RepresentativeBadge representativeBadge = representativeBadgeRepository.findByMember(member); @@ -66,4 +174,5 @@ public GetProfileDto getProfileInfo(@RequestParam("memberId") Long memberId){ memberId.equals(memberService.getCurrentMember().getMemberId()) ); } + } diff --git a/src/main/java/com/kimgreen/backend/response/Message.java b/src/main/java/com/kimgreen/backend/response/Message.java index 7725f65..8f274c6 100644 --- a/src/main/java/com/kimgreen/backend/response/Message.java +++ b/src/main/java/com/kimgreen/backend/response/Message.java @@ -25,7 +25,7 @@ public class Message { public static String GET_COMMENT_SUCCESS="댓글 목록을 불러오는데 성공하였습니다."; public static String SET_LIKES_SUCCESS="게시글 좋아요 or 취소하기 성공했습니다."; - public static String PROFILE_INFO_SUCCESS="프로필 불러오기 성공했습니다."; + public static String PROFILE_POSTS_SUCCESS="쓴 글 목록 불러오기 성공했습니다"; } From 8bb4418ca166dbd91b696c997b7f0cd6dd48d810 Mon Sep 17 00:00:00 2001 From: epdlrnldudnj Date: Mon, 29 Jan 2024 22:52:05 +0900 Subject: [PATCH 08/12] =?UTF-8?q?=EC=A3=BC=EC=84=9D=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../profile/service/ProfileService.java | 60 ------------------- 1 file changed, 60 deletions(-) diff --git a/src/main/java/com/kimgreen/backend/domain/profile/service/ProfileService.java b/src/main/java/com/kimgreen/backend/domain/profile/service/ProfileService.java index 74191d4..c7a2bca 100644 --- a/src/main/java/com/kimgreen/backend/domain/profile/service/ProfileService.java +++ b/src/main/java/com/kimgreen/backend/domain/profile/service/ProfileService.java @@ -17,14 +17,12 @@ import com.kimgreen.backend.domain.profile.repository.ProfileBadgeRepository; import com.kimgreen.backend.domain.profile.repository.RepresentativeBadgeRepository; import lombok.AllArgsConstructor; -import lombok.Builder; import org.springframework.stereotype.Service; import com.kimgreen.backend.domain.profile.dto.Profile.GetProfilePostDto; import java.util.*; @Service -//@RequiredArgsConstructor @AllArgsConstructor @@ -83,64 +81,6 @@ public List response(Long memberId){ } return list; } - /* - public List getProfilePosts(Long memberId){ - List posts = postRepository.findAll(); - Member member = memberRepository.findById(memberId).orElseThrow(); - for(Post p : posts){ - if(!memberId.equals(p.getMember().getMemberId())){ - posts.remove(p); - } - - } - List profilePosts = new ArrayList<>(); - for (Post p : posts){ - Long postId = p.getPostId(); - /* - String writerNickname = member.getNickname(); - String writerBadge = representativeBadgeRepository.findByMember(member).getRepresentativeBadge().name; - String content = p.getContent(); - int likeCount = p.getLikes().size(); - int commentCount = p.getComments().size(); - - boolean isLiked = isLiked(p.getLikes(),memberService.getCurrentMember()); - - for(Likes likes : p.getLikes()){ - if(likes.getMember().getMemberId().equals(memberService.getCurrentMember().getMemberId())){ - isLiked = true; - break; - } - } - - - String imgUrl = ""; - - if(postImgRepository.findByPost(p) != null){ - imgUrl = s3Service.getFullUrl(postImgRepository.findByPost(p).getImgUrl()); - } - */ - /* - String writerProfileImg = s3Service.getFullUrl(memberProfileImgRepository.findByMember(p.getMember()).getImgUrl()); - profilePosts.add(getProfilePostsDto.from( - postId, - writerNickname, - writerBadge, - writerProfileImg, - content, - likeCount, - commentCount - //imgUrl, - //isLiked - )); - - - profilePosts.add(GetProfilePostDto.builder() - .postId(postId) - .build()); -} - return profilePosts; - } - */ public GetProfileDto getProfileInfo(Long memberId){ Member member = memberRepository.findById(memberId).orElseThrow(); // 찾고싶은 멤버 From 9b4abf66d9062e62f2d2593fe51f39b409168015 Mon Sep 17 00:00:00 2001 From: gol2580 Date: Wed, 31 Jan 2024 21:51:33 +0900 Subject: [PATCH 09/12] [feat] getMyComment --- .../repository/CommentRepository.java | 4 +++ .../profile/controller/profileController.java | 12 ++++++-- .../dto/Profile/CommentResponseDto.java | 29 +++++++++++++++++++ .../profile/service/ProfileService.java | 21 ++++++++++++++ .../kimgreen/backend/response/Message.java | 2 ++ 5 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/kimgreen/backend/domain/profile/dto/Profile/CommentResponseDto.java diff --git a/src/main/java/com/kimgreen/backend/domain/community/repository/CommentRepository.java b/src/main/java/com/kimgreen/backend/domain/community/repository/CommentRepository.java index 27dca1f..3492f1b 100644 --- a/src/main/java/com/kimgreen/backend/domain/community/repository/CommentRepository.java +++ b/src/main/java/com/kimgreen/backend/domain/community/repository/CommentRepository.java @@ -1,13 +1,17 @@ package com.kimgreen.backend.domain.community.repository; import com.kimgreen.backend.domain.community.entity.Comment; +import com.kimgreen.backend.domain.member.entity.Member; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; +import java.util.List; + @Repository public interface CommentRepository extends JpaRepository { @Query("select count(*) from Comment c join Post p on c.post.postId=p.postId where p.postId= :id") public Long countComment(@Param("id") Long postId); + List findByMember(Member member); } diff --git a/src/main/java/com/kimgreen/backend/domain/profile/controller/profileController.java b/src/main/java/com/kimgreen/backend/domain/profile/controller/profileController.java index f5da4ac..15ff25a 100644 --- a/src/main/java/com/kimgreen/backend/domain/profile/controller/profileController.java +++ b/src/main/java/com/kimgreen/backend/domain/profile/controller/profileController.java @@ -11,8 +11,7 @@ import java.awt.print.Pageable; -import static com.kimgreen.backend.response.Message.PROFILE_INFO_SUCCESS; -import static com.kimgreen.backend.response.Message.PROFILE_POSTS_SUCCESS; +import static com.kimgreen.backend.response.Message.*; import static com.kimgreen.backend.response.Response.success; import static org.springframework.http.HttpStatus.OK; @@ -20,7 +19,6 @@ @RestController @RequestMapping(value="/profile") @RequiredArgsConstructor - public class profileController { private final ProfileService profileService; @@ -37,4 +35,12 @@ public Response getProfileInfo(@RequestParam("memberId") Long memberId) { return success(PROFILE_INFO_SUCCESS, profileService.getProfileInfo(memberId)); } + @Operation(summary = "설정창 내가 쓴 댓글 불러오기") + @ResponseStatus(OK) + @GetMapping("/setting/profile") + public Response getMyComment() { + return success(GET_MY_COMMENT_SUCCESS,profileService.getMyComment()); + } + + } diff --git a/src/main/java/com/kimgreen/backend/domain/profile/dto/Profile/CommentResponseDto.java b/src/main/java/com/kimgreen/backend/domain/profile/dto/Profile/CommentResponseDto.java new file mode 100644 index 0000000..811c06a --- /dev/null +++ b/src/main/java/com/kimgreen/backend/domain/profile/dto/Profile/CommentResponseDto.java @@ -0,0 +1,29 @@ +package com.kimgreen.backend.domain.profile.dto.Profile; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; +import org.springframework.stereotype.Component; + +@Builder +@Getter +@AllArgsConstructor +@NoArgsConstructor +public class CommentResponseDto { + private Long commentId; + private Long postId; + private String writerBadge; + private String writerNickname; + private String comment; + + public static CommentResponseDto toDto(Long commentId, Long postId, String writerBadge, String writerNickname, String comment) { + return CommentResponseDto.builder() + .commentId(commentId) + .postId(postId) + .writerBadge(writerBadge) + .writerNickname(writerNickname) + .comment(comment) + .build(); + } +} diff --git a/src/main/java/com/kimgreen/backend/domain/profile/service/ProfileService.java b/src/main/java/com/kimgreen/backend/domain/profile/service/ProfileService.java index c7a2bca..851db41 100644 --- a/src/main/java/com/kimgreen/backend/domain/profile/service/ProfileService.java +++ b/src/main/java/com/kimgreen/backend/domain/profile/service/ProfileService.java @@ -1,8 +1,10 @@ package com.kimgreen.backend.domain.profile.service; import com.kimgreen.backend.domain.BadgeList; +import com.kimgreen.backend.domain.community.entity.Comment; import com.kimgreen.backend.domain.community.entity.Likes; import com.kimgreen.backend.domain.community.entity.Post; +import com.kimgreen.backend.domain.community.repository.CommentRepository; import com.kimgreen.backend.domain.community.repository.PostImgRepository; import com.kimgreen.backend.domain.community.repository.PostRepository; import com.kimgreen.backend.domain.community.service.S3Service; @@ -11,11 +13,13 @@ import com.kimgreen.backend.domain.member.repository.MemberProfileImgRepository; import com.kimgreen.backend.domain.member.repository.MemberRepository; import com.kimgreen.backend.domain.member.service.MemberService; +import com.kimgreen.backend.domain.profile.dto.Profile.CommentResponseDto; import com.kimgreen.backend.domain.profile.dto.Profile.GetProfileDto; import com.kimgreen.backend.domain.profile.entity.ProfileBadge; import com.kimgreen.backend.domain.profile.entity.RepresentativeBadge; import com.kimgreen.backend.domain.profile.repository.ProfileBadgeRepository; import com.kimgreen.backend.domain.profile.repository.RepresentativeBadgeRepository; +import com.kimgreen.backend.exception.PostNotFound; import lombok.AllArgsConstructor; import org.springframework.stereotype.Service; import com.kimgreen.backend.domain.profile.dto.Profile.GetProfilePostDto; @@ -37,6 +41,7 @@ public class ProfileService { private final PostRepository postRepository; private final PostImgRepository postImgRepository; private final GetProfilePostDto getProfilePostDto; + private final CommentRepository commentRepository; public List response(Long memberId){ List list = new ArrayList<>(); @@ -115,4 +120,20 @@ public GetProfileDto getProfileInfo(Long memberId){ ); } + public List getMyComment() { + Member member = memberService.getCurrentMember(); + String writer = member.getNickname(); + String writerBadge = representativeBadgeRepository.findByMember(member).getRepresentativeBadge().name; + List comments = commentRepository.findByMember(member); + List dto = new ArrayList<>(); + for(Comment comment : comments) { + Post post = postRepository.findById(comment.getPost().getPostId()).orElseThrow(PostNotFound::new); + CommentResponseDto commentDto + = CommentResponseDto.toDto(comment.getCommentId(),post.getPostId(),writer,writerBadge,comment.getContent()); + dto.add(commentDto); + } + return dto; + } + + } diff --git a/src/main/java/com/kimgreen/backend/response/Message.java b/src/main/java/com/kimgreen/backend/response/Message.java index 8f274c6..eb1a580 100644 --- a/src/main/java/com/kimgreen/backend/response/Message.java +++ b/src/main/java/com/kimgreen/backend/response/Message.java @@ -28,4 +28,6 @@ public class Message { public static String PROFILE_INFO_SUCCESS="프로필 불러오기 성공했습니다."; public static String PROFILE_POSTS_SUCCESS="쓴 글 목록 불러오기 성공했습니다"; + public static String GET_MY_COMMENT_SUCCESS = "내가 쓴 댓글 보기 성공했습니다."; + } From 85a0f01444ec12aeb292e8da011cf19ecab18194 Mon Sep 17 00:00:00 2001 From: gol2580 Date: Thu, 1 Feb 2024 13:25:30 +0900 Subject: [PATCH 10/12] [feat] getMyPost --- .../community/repository/PostRepository.java | 2 + .../profile/controller/profileController.java | 7 ++- .../dto/Profile/GetSettingPostDto.java | 46 +++++++++++++++++++ .../profile/service/ProfileService.java | 30 +++++++++++- .../kimgreen/backend/response/Message.java | 1 + 5 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/kimgreen/backend/domain/profile/dto/Profile/GetSettingPostDto.java diff --git a/src/main/java/com/kimgreen/backend/domain/community/repository/PostRepository.java b/src/main/java/com/kimgreen/backend/domain/community/repository/PostRepository.java index 5f435a4..9dcafe3 100644 --- a/src/main/java/com/kimgreen/backend/domain/community/repository/PostRepository.java +++ b/src/main/java/com/kimgreen/backend/domain/community/repository/PostRepository.java @@ -1,6 +1,7 @@ package com.kimgreen.backend.domain.community.repository; import com.kimgreen.backend.domain.community.entity.Post; +import com.kimgreen.backend.domain.member.entity.Member; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; @@ -14,4 +15,5 @@ public interface PostRepository extends JpaRepository { @Query("select p from Post p where member.memberId= :id and createdAt between :start and :end") public List findAllBetweenDate(@Param("id") Long memberId, @Param("start") LocalDateTime start, @Param("end") LocalDateTime end); public List findAllByCreatedAtBetween(LocalDateTime start, LocalDateTime end); + List findByMember(Member member); } diff --git a/src/main/java/com/kimgreen/backend/domain/profile/controller/profileController.java b/src/main/java/com/kimgreen/backend/domain/profile/controller/profileController.java index 15ff25a..82b1f81 100644 --- a/src/main/java/com/kimgreen/backend/domain/profile/controller/profileController.java +++ b/src/main/java/com/kimgreen/backend/domain/profile/controller/profileController.java @@ -37,10 +37,15 @@ public Response getProfileInfo(@RequestParam("memberId") Long memberId) { @Operation(summary = "설정창 내가 쓴 댓글 불러오기") @ResponseStatus(OK) - @GetMapping("/setting/profile") + @GetMapping("/setting/comment") public Response getMyComment() { return success(GET_MY_COMMENT_SUCCESS,profileService.getMyComment()); } + @Operation(summary = "설정창 내가 쓴 글 불러오기") + @ResponseStatus(OK) + @GetMapping("/setting/post") + public Response getMyPost() {return success(GET_MY_POST_SUCCESS, profileService.getMyPost());} + } diff --git a/src/main/java/com/kimgreen/backend/domain/profile/dto/Profile/GetSettingPostDto.java b/src/main/java/com/kimgreen/backend/domain/profile/dto/Profile/GetSettingPostDto.java new file mode 100644 index 0000000..8974e87 --- /dev/null +++ b/src/main/java/com/kimgreen/backend/domain/profile/dto/Profile/GetSettingPostDto.java @@ -0,0 +1,46 @@ +package com.kimgreen.backend.domain.profile.dto.Profile; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Builder +@Getter +@AllArgsConstructor +@NoArgsConstructor +public class GetSettingPostDto { + private Long postId; + private String writerNickname; + private String writerBadge; + private String writerProfileImg; + private String content; + private Long likeCount; + private Long commentCount; + private String imgUrl; + private boolean isLiked; + + public static GetSettingPostDto toDto(Long postId, String content, String writerBadge, String writerNickname, String writerProfileImg, Long likeCount, Long commentCount, String imgUrl) { + return GetSettingPostDto.builder() + .postId(postId) + .content(content) + .writerBadge(writerBadge) + .writerNickname(writerNickname) + .writerProfileImg(writerProfileImg) + .likeCount(likeCount) + .commentCount(commentCount) + .imgUrl(imgUrl) + .build(); + } + public static GetSettingPostDto toDto(Long postId, String content, String writerBadge, String writerNickname, String writerProfileImg, Long likeCount, Long commentCount) { + return GetSettingPostDto.builder() + .postId(postId) + .content(content) + .writerBadge(writerBadge) + .writerNickname(writerNickname) + .writerProfileImg(writerProfileImg) + .likeCount(likeCount) + .commentCount(commentCount) + .build(); + } +} diff --git a/src/main/java/com/kimgreen/backend/domain/profile/service/ProfileService.java b/src/main/java/com/kimgreen/backend/domain/profile/service/ProfileService.java index 851db41..3ac65ba 100644 --- a/src/main/java/com/kimgreen/backend/domain/profile/service/ProfileService.java +++ b/src/main/java/com/kimgreen/backend/domain/profile/service/ProfileService.java @@ -4,7 +4,9 @@ import com.kimgreen.backend.domain.community.entity.Comment; import com.kimgreen.backend.domain.community.entity.Likes; import com.kimgreen.backend.domain.community.entity.Post; +import com.kimgreen.backend.domain.community.entity.PostImg; import com.kimgreen.backend.domain.community.repository.CommentRepository; +import com.kimgreen.backend.domain.community.repository.LikeRepository; import com.kimgreen.backend.domain.community.repository.PostImgRepository; import com.kimgreen.backend.domain.community.repository.PostRepository; import com.kimgreen.backend.domain.community.service.S3Service; @@ -15,6 +17,7 @@ import com.kimgreen.backend.domain.member.service.MemberService; import com.kimgreen.backend.domain.profile.dto.Profile.CommentResponseDto; import com.kimgreen.backend.domain.profile.dto.Profile.GetProfileDto; +import com.kimgreen.backend.domain.profile.dto.Profile.GetSettingPostDto; import com.kimgreen.backend.domain.profile.entity.ProfileBadge; import com.kimgreen.backend.domain.profile.entity.RepresentativeBadge; import com.kimgreen.backend.domain.profile.repository.ProfileBadgeRepository; @@ -42,6 +45,7 @@ public class ProfileService { private final PostImgRepository postImgRepository; private final GetProfilePostDto getProfilePostDto; private final CommentRepository commentRepository; + private final LikeRepository likeRepository; public List response(Long memberId){ List list = new ArrayList<>(); @@ -125,15 +129,37 @@ public List getMyComment() { String writer = member.getNickname(); String writerBadge = representativeBadgeRepository.findByMember(member).getRepresentativeBadge().name; List comments = commentRepository.findByMember(member); + List dto = new ArrayList<>(); for(Comment comment : comments) { Post post = postRepository.findById(comment.getPost().getPostId()).orElseThrow(PostNotFound::new); - CommentResponseDto commentDto - = CommentResponseDto.toDto(comment.getCommentId(),post.getPostId(),writer,writerBadge,comment.getContent()); + CommentResponseDto commentDto = CommentResponseDto.toDto(comment.getCommentId(),post.getPostId(),writerBadge,writer,comment.getContent()); dto.add(commentDto); } return dto; } + public List getMyPost() { + Member member = memberService.getCurrentMember(); + String writer = member.getNickname(); + String writerBadge = representativeBadgeRepository.findByMember(member).getRepresentativeBadge().name; + String writerProfileImg = s3Service.getFullUrl(memberProfileImgRepository.findByMember(member).getImgUrl()); + List posts = postRepository.findByMember(member); + + List dto = new ArrayList<>(); + for (Post post : posts) { + Long countLike = likeRepository.countLike(post.getPostId()); + Long countComment = commentRepository.countComment(post.getPostId()); + PostImg postImg = postImgRepository.findByPost(post); + //Long postId, String content, String writerBadge, String writerNickname, String writerProfileImg, int likeCount, int commentCount, String imgUrl + if (postImg != null) { + dto.add(GetSettingPostDto.toDto(post.getPostId(), post.getContent(), writerBadge, writer, writerProfileImg, countLike, countComment, s3Service.getFullUrl(postImg.getImgUrl()))); + } else { + dto.add(GetSettingPostDto.toDto(post.getPostId(), post.getContent(), writerBadge, writer, writerProfileImg, countLike, countComment)); + } + + } + return dto; + } } diff --git a/src/main/java/com/kimgreen/backend/response/Message.java b/src/main/java/com/kimgreen/backend/response/Message.java index eb1a580..16eeb0a 100644 --- a/src/main/java/com/kimgreen/backend/response/Message.java +++ b/src/main/java/com/kimgreen/backend/response/Message.java @@ -29,5 +29,6 @@ public class Message { public static String PROFILE_POSTS_SUCCESS="쓴 글 목록 불러오기 성공했습니다"; public static String GET_MY_COMMENT_SUCCESS = "내가 쓴 댓글 보기 성공했습니다."; + public static String GET_MY_POST_SUCCESS = "내가 쓴 글 보기 성공했습니다."; } From 039bd828b801cd095ccb1238a79747933e4e6979 Mon Sep 17 00:00:00 2001 From: gol2580 Date: Thu, 1 Feb 2024 16:01:07 +0900 Subject: [PATCH 11/12] [feat] getMyPost --- .../profile/dto/Profile/GetSettingPostDto.java | 6 ++++-- .../domain/profile/service/CalendarService.java | 2 +- .../domain/profile/service/ProfileService.java | 16 +++++++++++++--- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/kimgreen/backend/domain/profile/dto/Profile/GetSettingPostDto.java b/src/main/java/com/kimgreen/backend/domain/profile/dto/Profile/GetSettingPostDto.java index 8974e87..a5aba98 100644 --- a/src/main/java/com/kimgreen/backend/domain/profile/dto/Profile/GetSettingPostDto.java +++ b/src/main/java/com/kimgreen/backend/domain/profile/dto/Profile/GetSettingPostDto.java @@ -20,7 +20,7 @@ public class GetSettingPostDto { private String imgUrl; private boolean isLiked; - public static GetSettingPostDto toDto(Long postId, String content, String writerBadge, String writerNickname, String writerProfileImg, Long likeCount, Long commentCount, String imgUrl) { + public static GetSettingPostDto toDto(Long postId, String content, String writerBadge, String writerNickname, String writerProfileImg, Long likeCount, Long commentCount, String imgUrl, boolean isLiked) { return GetSettingPostDto.builder() .postId(postId) .content(content) @@ -30,9 +30,10 @@ public static GetSettingPostDto toDto(Long postId, String content, String writer .likeCount(likeCount) .commentCount(commentCount) .imgUrl(imgUrl) + .isLiked(isLiked) .build(); } - public static GetSettingPostDto toDto(Long postId, String content, String writerBadge, String writerNickname, String writerProfileImg, Long likeCount, Long commentCount) { + public static GetSettingPostDto toDto(Long postId, String content, String writerBadge, String writerNickname, String writerProfileImg, Long likeCount, Long commentCount, boolean isLiked) { return GetSettingPostDto.builder() .postId(postId) .content(content) @@ -41,6 +42,7 @@ public static GetSettingPostDto toDto(Long postId, String content, String writer .writerProfileImg(writerProfileImg) .likeCount(likeCount) .commentCount(commentCount) + .isLiked(isLiked) .build(); } } diff --git a/src/main/java/com/kimgreen/backend/domain/profile/service/CalendarService.java b/src/main/java/com/kimgreen/backend/domain/profile/service/CalendarService.java index b0b113b..4a0124b 100644 --- a/src/main/java/com/kimgreen/backend/domain/profile/service/CalendarService.java +++ b/src/main/java/com/kimgreen/backend/domain/profile/service/CalendarService.java @@ -146,7 +146,7 @@ public CalendarDetailDto dtoBuilderWithNoImg(Long postId, String nickname, Strin public boolean isLiked(List likesList,Member member) { for(Likes like : likesList) { - if(like.getLikeId().equals(member.getMemberId())) { + if(like.getMember().getMemberId().equals(member.getMemberId())) { return true; } } diff --git a/src/main/java/com/kimgreen/backend/domain/profile/service/ProfileService.java b/src/main/java/com/kimgreen/backend/domain/profile/service/ProfileService.java index 3ac65ba..ba9725b 100644 --- a/src/main/java/com/kimgreen/backend/domain/profile/service/ProfileService.java +++ b/src/main/java/com/kimgreen/backend/domain/profile/service/ProfileService.java @@ -149,17 +149,27 @@ public List getMyPost() { List dto = new ArrayList<>(); for (Post post : posts) { Long countLike = likeRepository.countLike(post.getPostId()); + List likes = post.getLikes(); + boolean isLiked = isLiked(likes, member); Long countComment = commentRepository.countComment(post.getPostId()); PostImg postImg = postImgRepository.findByPost(post); - //Long postId, String content, String writerBadge, String writerNickname, String writerProfileImg, int likeCount, int commentCount, String imgUrl if (postImg != null) { - dto.add(GetSettingPostDto.toDto(post.getPostId(), post.getContent(), writerBadge, writer, writerProfileImg, countLike, countComment, s3Service.getFullUrl(postImg.getImgUrl()))); + dto.add(GetSettingPostDto.toDto(post.getPostId(), post.getContent(), writerBadge, writer, writerProfileImg, countLike, countComment, s3Service.getFullUrl(postImg.getImgUrl()),isLiked)); } else { - dto.add(GetSettingPostDto.toDto(post.getPostId(), post.getContent(), writerBadge, writer, writerProfileImg, countLike, countComment)); + dto.add(GetSettingPostDto.toDto(post.getPostId(), post.getContent(), writerBadge, writer, writerProfileImg, countLike, countComment,isLiked)); } } return dto; } + public boolean isLiked(List likesList, Member member) { + for(Likes like : likesList) { + if(like.getMember().getMemberId().equals(member.getMemberId())) { + return true; + } + } + return false; + } + } From e17978be460181354d80ffed8bcfc84c39f17829 Mon Sep 17 00:00:00 2001 From: gol2580 Date: Sun, 4 Feb 2024 15:40:22 +0900 Subject: [PATCH 12/12] [fix] token exceptions --- .../JwtAuthenticationEntryPoint.java | 13 +++++++++++- .../config/Authentication/JwtFilter.java | 20 +++++++++++++++++-- .../config/Authentication/JwtProvider.java | 6 ++++++ .../config/Security/SecurityConfig.java | 2 -- .../backend/exception/BlankToken.java | 4 ++++ .../backend/exception/MalformedToken.java | 4 ++++ .../com/kimgreen/backend/response/Advice.java | 2 ++ 7 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 src/main/java/com/kimgreen/backend/exception/BlankToken.java create mode 100644 src/main/java/com/kimgreen/backend/exception/MalformedToken.java diff --git a/src/main/java/com/kimgreen/backend/config/Authentication/JwtAuthenticationEntryPoint.java b/src/main/java/com/kimgreen/backend/config/Authentication/JwtAuthenticationEntryPoint.java index ef3e37d..0bd9481 100644 --- a/src/main/java/com/kimgreen/backend/config/Authentication/JwtAuthenticationEntryPoint.java +++ b/src/main/java/com/kimgreen/backend/config/Authentication/JwtAuthenticationEntryPoint.java @@ -18,6 +18,9 @@ public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint { private static final String SUCCESS = "success"; private static final String EXPIRED = "expired"; private static final String DENIED = "denied"; + private static final String MALFORMED = "malformed"; + private static final String BLANK = "blank"; + private static final String MALFORMED_JWT = "malformed_jwt"; @Override public void commence(HttpServletRequest request, @@ -32,7 +35,15 @@ public void commence(HttpServletRequest request, setResponse(response,HttpStatus.UNAUTHORIZED.value(),"토큰이 유효하지 않습니다."); } if (exception.equals(DENIED)) { - setResponse(response,HttpStatus.NOT_FOUND.value(), "토큰이 없습니다."); + setResponse(response,HttpStatus.NOT_FOUND.value(), "잘못된 형식의 요청입니다."); + } + if (exception.equals(MALFORMED)) { + setResponse(response,HttpStatus.BAD_REQUEST.value(), "Bearer 형식이 존재하지 않습니다."); + } + if(exception.equals(BLANK)) { + setResponse(response,HttpStatus.BAD_REQUEST.value(), "토큰이 존재하지 않습니다."); + }if(exception.equals(MALFORMED_JWT)) { + setResponse(response,HttpStatus.BAD_REQUEST.value(), "잘못된 형식의 토큰입니다."); } } diff --git a/src/main/java/com/kimgreen/backend/config/Authentication/JwtFilter.java b/src/main/java/com/kimgreen/backend/config/Authentication/JwtFilter.java index 27bb482..7eedac9 100644 --- a/src/main/java/com/kimgreen/backend/config/Authentication/JwtFilter.java +++ b/src/main/java/com/kimgreen/backend/config/Authentication/JwtFilter.java @@ -1,8 +1,9 @@ package com.kimgreen.backend.config.Authentication; -import com.kimgreen.backend.exception.TokenNotFound; -import com.kimgreen.backend.exception.TokenNotValid; +import com.kimgreen.backend.exception.BlankToken; +import com.kimgreen.backend.exception.MalformedToken; import io.jsonwebtoken.ExpiredJwtException; +import io.jsonwebtoken.MalformedJwtException; import jakarta.servlet.FilterChain; import jakarta.servlet.ServletException; import jakarta.servlet.http.HttpServletRequest; @@ -22,6 +23,9 @@ public class JwtFilter extends OncePerRequestFilter { private static final String SUCCESS = "success"; private static final String EXPIRED = "expired"; private static final String DENIED = "denied"; + private static final String MALFORMED = "malformed"; + private static final String MALFORMED_JWT = "malformed_jwt"; + private static final String BLANK = "blank"; private final JwtProvider jwtProvider; private final static String[] AUTH_WHITE_LIST_IGNORE = { "/swagger-ui/index.html" @@ -45,6 +49,11 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse System.out.println("doing jwtFilter"); try { String accessToken = jwtProvider.resolveToken(request, HttpHeaders.AUTHORIZATION); + if(accessToken.equals(MALFORMED)) { + throw new MalformedToken(); + } else if(accessToken.equals(BLANK)) { + throw new BlankToken(); + } Authentication authentication = jwtProvider.getAuthentication(accessToken); // access token 검증 @@ -57,6 +66,13 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse } catch (IllegalArgumentException e) { //throw JwtException request.setAttribute("exception",DENIED); + } catch (MalformedToken e) { + request.setAttribute("exception",MALFORMED); + } catch (BlankToken e) { + System.out.println("catch blank token"); + request.setAttribute("exception",BLANK); + } catch (MalformedJwtException e) { + request.setAttribute("exception",MALFORMED_JWT); } filterChain.doFilter(request, response); } diff --git a/src/main/java/com/kimgreen/backend/config/Authentication/JwtProvider.java b/src/main/java/com/kimgreen/backend/config/Authentication/JwtProvider.java index 160cca1..222db5d 100644 --- a/src/main/java/com/kimgreen/backend/config/Authentication/JwtProvider.java +++ b/src/main/java/com/kimgreen/backend/config/Authentication/JwtProvider.java @@ -34,6 +34,8 @@ public class JwtProvider { private static final String SUCCESS = "success"; private static final String EXPIRED = "expired"; private static final String DENIED = "denied"; + private static final String MALFORMED = "malformed"; + private static final String BLANK = "blank"; private long now; private final String AUTHORITIES_KEY = "auth"; private final CustomUserDetailsService customUserDetailsService; @@ -97,6 +99,10 @@ public String resolveToken(HttpServletRequest request, String header) { String bearerToken = request.getHeader(header); if (StringUtils.hasText(bearerToken) && bearerToken.startsWith("Bearer ")) { return bearerToken.substring(7); + } else if(StringUtils.hasText(bearerToken)) { + return MALFORMED; + } else if(bearerToken==null) { + return BLANK; } return null; } diff --git a/src/main/java/com/kimgreen/backend/config/Security/SecurityConfig.java b/src/main/java/com/kimgreen/backend/config/Security/SecurityConfig.java index df1bd78..ea55099 100644 --- a/src/main/java/com/kimgreen/backend/config/Security/SecurityConfig.java +++ b/src/main/java/com/kimgreen/backend/config/Security/SecurityConfig.java @@ -38,8 +38,6 @@ public class SecurityConfig { ,"/auth/reissue" }; private final static String[] AUTH_WHITE_LIST = { - "/", - "/**", "/swagger-ui/index.html" ,"/swagger-ui.html" ,"/swagger-ui/**" diff --git a/src/main/java/com/kimgreen/backend/exception/BlankToken.java b/src/main/java/com/kimgreen/backend/exception/BlankToken.java new file mode 100644 index 0000000..0401004 --- /dev/null +++ b/src/main/java/com/kimgreen/backend/exception/BlankToken.java @@ -0,0 +1,4 @@ +package com.kimgreen.backend.exception; + +public class BlankToken extends RuntimeException{ +} diff --git a/src/main/java/com/kimgreen/backend/exception/MalformedToken.java b/src/main/java/com/kimgreen/backend/exception/MalformedToken.java new file mode 100644 index 0000000..784569a --- /dev/null +++ b/src/main/java/com/kimgreen/backend/exception/MalformedToken.java @@ -0,0 +1,4 @@ +package com.kimgreen.backend.exception; + +public class MalformedToken extends RuntimeException{ +} diff --git a/src/main/java/com/kimgreen/backend/response/Advice.java b/src/main/java/com/kimgreen/backend/response/Advice.java index 04db0db..84cb5f0 100644 --- a/src/main/java/com/kimgreen/backend/response/Advice.java +++ b/src/main/java/com/kimgreen/backend/response/Advice.java @@ -63,4 +63,6 @@ public Response LogInRequiredResponse() { return Response.failure(HttpStatus.NOT_FOUND, "토큰이 존재하지 않습니다. 로그인 후 이용해주세요."); } + + }