Skip to content

Commit

Permalink
fix: 작성 편지 리스트 조회 기능, 사용자 정보 같이 받아오는 부분 삭제 및 패키지 이동 #3
Browse files Browse the repository at this point in the history
  • Loading branch information
623nana committed Jul 29, 2021
1 parent 5ec15db commit 649e662
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.nexters.covid.letter.api;

import com.nexters.covid.base.BaseResponse;
import com.nexters.covid.letter.api.dto.LetterResponse;
import com.nexters.covid.letter.api.dto.OptionResponse;
import com.nexters.covid.letter.service.LetterService;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

Expand All @@ -14,9 +16,15 @@ public class LetterController {

private final LetterService letterService;

@GetMapping("/letter/option")
@GetMapping("/letters/options")
public BaseResponse<List<OptionResponse>> options() {
List<OptionResponse> options = letterService.options();
return new BaseResponse<>(200, 0, "", options);
}

@GetMapping("/letters")
public BaseResponse<List<LetterResponse>> letters(Authentication authentication) {
List<LetterResponse> letters = letterService.letters(authentication.getName());;
return new BaseResponse<>(200, 0, "", letters);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.nexters.covid.letter.domain;

import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;

public interface LetterRepository extends JpaRepository<Letter, Long> {

List<Letter> findLettersByEmail(String email);
}
13 changes: 13 additions & 0 deletions src/main/java/com/nexters/covid/letter/service/LetterService.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package com.nexters.covid.letter.service;

import com.nexters.covid.letter.api.dto.LetterResponse;
import com.nexters.covid.letter.api.dto.OptionResponse;
import com.nexters.covid.letter.domain.LetterRepository;
import com.nexters.covid.letter.domain.SendOptionRepository;
import com.nexters.covid.user.domain.UserRepository;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@RequiredArgsConstructor
@Service
public class LetterService {

private final LetterRepository letterRepository;
private final SendOptionRepository sendOptionRepository;

public List<OptionResponse> options() {
Expand All @@ -19,4 +24,12 @@ public List<OptionResponse> options() {
.map(OptionResponse::new)
.collect(Collectors.toList());
}

@Transactional(readOnly = true)
public List<LetterResponse> letters(String email) {
return letterRepository.findLettersByEmail(email)
.stream()
.map(LetterResponse::new)
.collect(Collectors.toList());
}
}
7 changes: 0 additions & 7 deletions src/main/java/com/nexters/covid/user/api/UserController.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.nexters.covid.user.api;

import com.nexters.covid.base.BaseResponse;
import com.nexters.covid.letter.api.dto.LetterResponse;
import com.nexters.covid.user.api.dto.LoginRequest;
import com.nexters.covid.user.api.dto.LoginResponse;
import com.nexters.covid.user.api.dto.UserResponse;
Expand Down Expand Up @@ -29,10 +28,4 @@ public BaseResponse<UserResponse> findUser(Authentication authentication) {
UserResponse user = userService.findUserEmail(authentication.getName());
return new BaseResponse<>(200, 0, "", user);
}

@GetMapping("/user/letter")
public BaseResponse<LetterResponse> letters(Authentication authentication) {
LetterResponse letters = userService.letters(authentication.getName());;
return new BaseResponse<>(200, 0, "", letters);
}
}
7 changes: 0 additions & 7 deletions src/main/java/com/nexters/covid/user/service/UserService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.nexters.covid.user.service;

import com.nexters.covid.letter.api.dto.LetterResponse;
import com.nexters.covid.user.api.dto.LoginRequest;
import com.nexters.covid.user.api.dto.LoginResponse;
import com.nexters.covid.user.api.dto.UserResponse;
Expand Down Expand Up @@ -45,10 +44,4 @@ public UserResponse findUserEmail(String email) {
.orElseThrow(() -> new RuntimeException("사용자가 없습니다."));
return new UserResponse(user);
}

@Transactional(readOnly = true)
public LetterResponse letters(String email) {
User user = userRepository.findUserAndLettersByEmail(email);
return new LetterResponse(user);
}
}
4 changes: 2 additions & 2 deletions src/main/resources/data-h2.sql
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
insert into user (email, identifier, name) values ('email', 'identifier', 'name');

insert into letter (answer, contents, encrypted_id, letter_to, email, sticker, question_id, state, title, user_id)
values ('ANSWER', 'CONTENTS', 'ENCRYPTED', 'LETTER_TO', 'email', 'A', 'HAPPY', 'PENDING', 'TITLE', 1);
values ('ANSWER', 'CONTENTS', 'ENCRYPTED', 'LETTER_TO', 'email', 'HAPPY', 1 , 'PENDING', 'TITLE', 1);
insert into letter (answer, contents, encrypted_id, letter_to, email, sticker, question_id, state, title, user_id)
values ('ANSWER1', 'CONTENTS1', 'ENCRYPTED1', 'LETTER_TO1', 'email', 'A', 'BLUE', 'PENDING', 'TITLE1', 1);
values ('ANSWER1', 'CONTENTS1', 'ENCRYPTED1', 'LETTER_TO1', 'email', 'BLUE', 2, 'PENDING', 'TITLE1', 1);

insert into send_option (text, covid_stat) values ('테스트', 1000);
insert into send_option (text, covid_stat) values ('테스트2', 2000);
Expand Down

0 comments on commit 649e662

Please sign in to comment.