Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.neighbors.tohero.application.letter.dto;

import com.neighbors.tohero.domain.domain.mainPage.model.Letter;

import java.text.Format;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

public record GetMyLettersResponse (
List<MyLetterInfo> myLetterInfos
){
public record MyLetterInfo(
long letterId,
String to,
String from,
String createdAt,
boolean isOpened,
String content
){
public static MyLetterInfo from(Letter letter){
return new MyLetterInfo(
letter.getLetterId(),
letter.getTargetName(),
letter.getWriter(),
letter.getCreatedDate().format(DateTimeFormatter.ofPattern("YY.MM.dd")),
letter.isOpened(),
letter.getLetterContent());
}
}

public static GetMyLettersResponse from(List<Letter> myLetters){
List<MyLetterInfo> myLetterInfos = myLetters.stream()
.map(MyLetterInfo::from)
.toList();
return new GetMyLettersResponse(myLetterInfos);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
import com.neighbors.tohero.application.baseResponse.BaseResponse;
import com.neighbors.tohero.application.baseResponse.BaseResponseMessage;
import com.neighbors.tohero.application.baseResponse.BaseResponseStatus;
import com.neighbors.tohero.application.letter.dto.CreateLetterRequest;
import com.neighbors.tohero.application.letter.dto.CreateLetterResponse;
import com.neighbors.tohero.application.letter.dto.GetLetterDetailRequest;
import com.neighbors.tohero.application.letter.dto.GetLetterDetailResponse;
import com.neighbors.tohero.application.letter.dto.*;
import com.neighbors.tohero.common.enums.Role;
import com.neighbors.tohero.common.exception.address.AddressException;
import com.neighbors.tohero.common.exception.letter.LetterException;
Expand Down Expand Up @@ -61,6 +58,16 @@ public BaseResponse<GetLetterDetailResponse> getLetterDetail(GetLetterDetailRequ
);
}

public BaseResponse<GetMyLettersResponse> getMyLetters(long userId){
List<Letter> myLetters = getLetter.getMyLetters(userId);

return new BaseResponse<>(
BaseResponseStatus.OK,
BaseResponseMessage.편지가_성공적으로_조회되었습니다.getMessage(),
GetMyLettersResponse.from(myLetters)
);
}

private BaseResponse<CreateLetterResponse> createGuestLetter(final String nickname, final CreateLetterRequest createLetterRequest) {
long createdLetterId = createLetter.createGuestLetter(
nickname,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import lombok.Builder;
import lombok.Getter;

import java.time.LocalDateTime;

@Getter
@Builder
@AllArgsConstructor
Expand All @@ -23,6 +25,8 @@ public class Letter {
private String writer;
private User user;

private LocalDateTime createdDate;

public String getTargetName(){
if(targetName == null) {
return "익명의";
Expand All @@ -38,7 +42,8 @@ public static Letter createNonUserAndAddress(LetterEntity letterEntity) {
letterEntity.getTargetName(),
letterEntity.getIsPublic(),
letterEntity.getReadingAlarm(),
null,letterEntity.getWriter(),null
null,letterEntity.getWriter(),null,
letterEntity.created_at
);
}

Expand All @@ -52,7 +57,8 @@ public static Letter createNonUser(LetterEntity letterEntity, Address address) {
letterEntity.getReadingAlarm(),
address
,letterEntity.getWriter(),
null
null,
letterEntity.created_at
);
}

Expand All @@ -66,7 +72,8 @@ public static Letter createNonAddress(LetterEntity letterEntity, User user) {
letterEntity.getReadingAlarm(),
null,
letterEntity.getUser().getNickName(),
user
user,
letterEntity.created_at
);
}

Expand All @@ -80,7 +87,8 @@ public static Letter from(LetterEntity letterEntity, Address address, User user)
letterEntity.getReadingAlarm(),
address,
letterEntity.getUser().getNickName(),
user
user,
letterEntity.created_at
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ public long getTotalLetterNumber(){
}

public List<Letter> getPageableLetter(Pageable pageable){
return letterRepository.getPageableLetter(pageable);
return letterRepository.getLetters(repo -> repo.findPagedLetterEntity(pageable));
}

public Letter getLetterById(long letterId){
return letterRepository.getLetter(repo -> repo.findByIdAndPublic(letterId));
}

public List<Letter> getMyLetters(long userId){
return letterRepository.getLetters(repo -> repo.findAllByUserId(userId));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

public interface LetterRepository {
long getTotalLetterNumber();
List<Letter> getPageableLetter(Pageable pageable);
List<Letter> getLetters(Function<LetterEntityRepository, Optional<List<LetterEntity>>> function);
Letter createLetter(Letter letter);
void remainLetterWithoutUser(long userId);
Letter getLetter(Function<LetterEntityRepository, Optional<LetterEntity>> function);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
Expand All @@ -29,8 +30,12 @@ public long getTotalLetterNumber() {
}

@Override
public List<Letter> getPageableLetter(Pageable pageable) {
List<LetterEntity> letterEntities = letterEntityRepository.findPagedLetterEntity(pageable).getContent();
public List<Letter> getLetters(Function<LetterEntityRepository, Optional<List<LetterEntity>>> function) {
List<LetterEntity> letterEntities = function.apply(letterEntityRepository)
.orElseThrow(()-> new LetterException(
BaseResponseStatus.NO_RESULT,
BaseResponseMessage.일치하는_편지가_없습니다.getMessage()
));

return letterEntities.stream()
.map(letterMapper::toDomain)
Expand All @@ -47,6 +52,7 @@ public Letter createLetter(Letter letter) {
@Override
public void remainLetterWithoutUser(long userId) {
letterEntityRepository.findAllByUserId(userId)
.orElse(new ArrayList<>())
.forEach(letter -> {
letter.remainLetterWithoutUser();
letterEntityRepository.save(letter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
public interface LetterEntityRepository extends JpaRepository<LetterEntity, Long> {

@Query("SELECT le FROM LetterEntity le WHERE le.isPublic = true")
Slice<LetterEntity> findPagedLetterEntity(Pageable pageable);
Optional<List<LetterEntity>> findPagedLetterEntity(Pageable pageable);

@Query("SELECT COUNT(le) FROM LetterEntity le WHERE le.isPublic = true")
long countPublicLetter();

@Query("SELECT le FROM LetterEntity le WHERE le.user.userId = :userId")
List<LetterEntity> findAllByUserId(@Param("userId") Long userId);
Optional<List<LetterEntity>> findAllByUserId(@Param("userId") Long userId);

@Query("SELECT le FROM LetterEntity le WHERE le.letterId = :letterId AND le.isPublic = true")
Optional<LetterEntity> findByIdAndPublic(@Param("letterId") long letterId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,10 @@ public ResponseEntity<BaseResponse> getLetterDetail(@ParameterObject GetLetterDe
return ResponseEntity.ok()
.body(letterService.getLetterDetail(getLetterDetailRequest));
}

@GetMapping("")
public ResponseEntity<BaseResponse> getMyLetters(@Parameter(hidden = true) @AuthenticationPrincipal JwtUserDetails jwtUserDetail){
return ResponseEntity.ok()
.body(letterService.getMyLetters(jwtUserDetail.getUserId()));
}
}
Loading