Skip to content

Commit

Permalink
[feat] calendar
Browse files Browse the repository at this point in the history
  • Loading branch information
gol2580 committed Jan 23, 2024
1 parent 76d4cdc commit 26754fe
Show file tree
Hide file tree
Showing 10 changed files with 252 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Comment, Long> {
@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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Likes, Long> {
@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);
}
Original file line number Diff line number Diff line change
@@ -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<PostImg, Long> {

public PostImg findByPost(Post post);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

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;
import java.util.List;

@Repository
public interface PostRepository extends JpaRepository<Post, Long> {

@Query("select p from Post p where member.memberId= :id and createdAt between :start and :end")
public List<Post> findAllBetweenDate(@Param("id") Long memberId, @Param("start") LocalDateTime start, @Param("end") LocalDateTime end);
public List<Post> findAllByCreatedAtBetween(LocalDateTime start, LocalDateTime end);
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -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;

}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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<CalendarDetailDto> contents;

public static CalendarDetailResponseDto toDto(int count, List<CalendarDetailDto> contents) {
return CalendarDetailResponseDto.builder()
.postCount(count)
.contents(contents)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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<CalendarResponseDto> 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<Post> 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<Post> postList = postRepository.findAllByCreatedAtBetween(start,end);
return CalendarDetailResponseDto.toDto(postList.size(),getResult(postList));

}

public List<CalendarResponseDto> getDtoList(List<Post> postList) {
Map<String,Integer> 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<CalendarResponseDto> toList(Map<String, Integer> maps) {
List<CalendarResponseDto> result = new ArrayList<>();
for(Map.Entry<String, Integer> map :maps.entrySet()) {
result.add(CalendarResponseDto.builder()
.date(map.getKey())
.postCount(map.getValue())
.build());
}
return result;
}

public List<CalendarDetailDto> getResult(List<Post> postList) {
CalendarDetailDto dto;
List<CalendarDetailDto> 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<Likes> 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<Likes> likesList,Member member) {
for(Likes like : likesList) {
if(like.getLikeId().equals(member.getMemberId())) {
return true;
}
}
return false;
}
}

0 comments on commit 26754fe

Please sign in to comment.