Skip to content

Commit

Permalink
✨ 느린 우편함 조회(달력 조회, 전제 리스트) 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
euunrud committed Feb 11, 2024
1 parent 20c0a1a commit 628ddb3
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 10 deletions.
2 changes: 2 additions & 0 deletions src/main/java/aromanticcat/umcproject/entity/SlowLetter.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package aromanticcat.umcproject.entity;

import aromanticcat.umcproject.web.dto.Letterbox.LetterResponse;
import aromanticcat.umcproject.web.dto.slowLetter.SlowLetterGetResponse;
import lombok.*;

import javax.persistence.*;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
package aromanticcat.umcproject.repository;

import aromanticcat.umcproject.entity.Letter;
import aromanticcat.umcproject.entity.Member;
import aromanticcat.umcproject.entity.SlowLetter;
import org.springframework.data.jpa.repository.JpaRepository;

import java.time.LocalDate;
import java.util.List;
import java.util.Optional;

public interface SlowLetterRepository extends JpaRepository<SlowLetter, Long> {
Optional<List<SlowLetter>> findSlowLettersByMember(Member member);
Optional<SlowLetter> findBySlowLetterId(Long slowLetterId);

Optional<List<SlowLetter>> findSlowLettersByDateBetweenAndMemberId(LocalDate startDate, LocalDate endDate, Long memberId);
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package aromanticcat.umcproject.service.slowLetter;

import aromanticcat.umcproject.entity.Letterbox;
import aromanticcat.umcproject.entity.Member;
import aromanticcat.umcproject.entity.SlowLetter;
import aromanticcat.umcproject.repository.LetterBoxRepository;
import aromanticcat.umcproject.repository.LetterRepository;
import aromanticcat.umcproject.repository.MemberRepository;
import aromanticcat.umcproject.repository.SlowLetterRepository;
import aromanticcat.umcproject.web.dto.Letterbox.LetterboxRequest;
import aromanticcat.umcproject.web.dto.SlowLetterRequest;
import aromanticcat.umcproject.web.dto.slowLetter.SlowLetterCalResponse;
import aromanticcat.umcproject.web.dto.slowLetter.SlowLetterGetResponse;
import aromanticcat.umcproject.web.dto.slowLetter.SlowLetterRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@Transactional(readOnly = true)
@RequiredArgsConstructor
@Service
Expand All @@ -27,4 +29,35 @@ public SlowLetter save(Long memberId, SlowLetterRequest request) {
slowLetter.updateMember(member);
return slowLetterRepository.save(slowLetter);
}

public List<SlowLetterGetResponse> getSlowLetters(Long memberId) {
Member findMember = memberRepository.findById(memberId).orElse(null);
List<SlowLetter> letters = slowLetterRepository.findSlowLettersByMember(findMember).orElse(null);
List<SlowLetterGetResponse> responses = new ArrayList<>();
for (SlowLetter slowLetter : letters) {
responses.add(SlowLetterGetResponse.from(slowLetter));
}
return responses;
}

public SlowLetter getSlowLetter(Long letterId) {
SlowLetter findLetter = slowLetterRepository.findBySlowLetterId(letterId).orElse(null);

return findLetter;
}

public List<SlowLetterCalResponse> getWriteSlowLetters(Long memberId, int month) {
int year = LocalDate.now().getYear();
int selectMonth = month;

LocalDate startOfMonth = LocalDate.of(year, month, 1);
LocalDate endOfMonth = startOfMonth.plusMonths(1).minusDays(1);

List<SlowLetter> letters = slowLetterRepository.findSlowLettersByDateBetweenAndMemberId(startOfMonth, endOfMonth, memberId).orElse(null);
List<SlowLetterCalResponse> responses = new ArrayList<>();
for (SlowLetter slowLetter : letters) {
responses.add(SlowLetterCalResponse.from(slowLetter));
}
return responses;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@

import aromanticcat.umcproject.entity.SlowLetter;
import aromanticcat.umcproject.service.slowLetter.SlowLetterService;
import aromanticcat.umcproject.web.dto.SlowLetterRequest;
import aromanticcat.umcproject.web.dto.SlowLetterResponse;
import aromanticcat.umcproject.web.dto.Letterbox.LetterResponse;
import aromanticcat.umcproject.web.dto.slowLetter.SlowLetterCalResponse;
import aromanticcat.umcproject.web.dto.slowLetter.SlowLetterGetResponse;
import aromanticcat.umcproject.web.dto.slowLetter.SlowLetterRequest;
import aromanticcat.umcproject.web.dto.slowLetter.SlowLetterResponse;
import io.swagger.annotations.ApiOperation;
import aromanticcat.umcproject.apiPayload.ApiResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/slow_letters")
Expand All @@ -26,4 +32,35 @@ public ApiResponse<SlowLetterResponse> createApplicant(@PathVariable Long member
return ApiResponse.onFailure(HttpStatus.INTERNAL_SERVER_ERROR.toString(), e.getMessage(), null);
}
}

@GetMapping("/{memberId}/list")
@ApiOperation(value = "느린 우편함 리스트 조회 - 연말 확인 시")
public ApiResponse<List<SlowLetterGetResponse>> getSlowLetters(@PathVariable Long memberId) {
List<SlowLetterGetResponse> letters = slowLetterService.getSlowLetters(memberId);
try {
return ApiResponse.onSuccess(letters);
} catch (Exception e){
return ApiResponse.onFailure(HttpStatus.INTERNAL_SERVER_ERROR.toString(), e.getMessage(), null);
}
}

@GetMapping("/{letterId}")
@ApiOperation(value = "느린 우편함 조회")
public ApiResponse<SlowLetterGetResponse> getSlowLetter(@PathVariable Long letterId) {
try {
return ApiResponse.onSuccess(SlowLetterGetResponse.from(slowLetterService.getSlowLetter(letterId)));
} catch (Exception e){
return ApiResponse.onFailure(HttpStatus.INTERNAL_SERVER_ERROR.toString(), e.getMessage(), null);
}
}

@GetMapping("/{memberId}/{month}")
public ApiResponse<List<SlowLetterCalResponse>> findDataByMonthAndMemberId(@PathVariable Long memberId, @PathVariable int month) {
List<SlowLetterCalResponse> days = slowLetterService.getWriteSlowLetters(memberId, month);
try {
return ApiResponse.onSuccess(days);
} catch (Exception e){
return ApiResponse.onFailure(HttpStatus.INTERNAL_SERVER_ERROR.toString(), e.getMessage(), null);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package aromanticcat.umcproject.web.dto.slowLetter;

import aromanticcat.umcproject.entity.SlowLetter;
import lombok.Builder;

@Builder
public record SlowLetterCalResponse(
String date
) {
public static SlowLetterCalResponse from(SlowLetter slowLetter) {
return SlowLetterCalResponse.builder()
.date(slowLetter.getDate().toString())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package aromanticcat.umcproject.web.dto.slowLetter;

import aromanticcat.umcproject.entity.SlowLetter;
import lombok.Builder;

@Builder
public record SlowLetterGetResponse(
Long slowLetterId,
String date,
String content
) {
public static SlowLetterGetResponse from(SlowLetter slowLetter) {
return SlowLetterGetResponse.builder()
.slowLetterId(slowLetter.getSlowLetterId())
.date(slowLetter.getDate().toString())
.content(slowLetter.getContent())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package aromanticcat.umcproject.web.dto;
package aromanticcat.umcproject.web.dto.slowLetter;

import aromanticcat.umcproject.entity.SlowLetter;
import lombok.Getter;
import org.springframework.format.annotation.DateTimeFormat;

import javax.validation.constraints.NotNull;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package aromanticcat.umcproject.web.dto;
package aromanticcat.umcproject.web.dto.slowLetter;

import aromanticcat.umcproject.entity.SlowLetter;
import lombok.Builder;
Expand Down

0 comments on commit 628ddb3

Please sign in to comment.