Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[중간점검] 03/21 스터디 전 중간점검 PR #1

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions howoldareu_clone/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ dependencies {
runtimeOnly 'com.mysql:mysql-connector-j'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.restdocs:spring-restdocs-mockmvc'
testImplementation 'org.springframework.restdocs:spring-restdocs-asciidoctor'
}

tasks.named('test') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import static com.sopt.Server.exception.Success.CREATE_MEMBER_SUCCESS;

@RestController
@RequiredArgsConstructor
public class MemberController {

private final MemberService memberService;

@PostMapping("/member")
public ApiResponse<MemberGetResponse> saveMember(@RequestBody MemberPostRequest request) {

ApiResponse<MemberGetResponse> response = memberService.saveMember(request.nickName(), request.age());
return response;
return ApiResponse.success(CREATE_MEMBER_SUCCESS, memberService.saveMember(request));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class Answer {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long answerId;

@ManyToOne
@ManyToOne(fetch = FetchType.LAZY)
private Question question;

private boolean answerType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,8 @@ public Result(Long id, Member member, int resultAge, LocalDateTime testedDate) {
this.resultAge = resultAge;
this.testedDate = testedDate;
}

public String getTestedDateToString(){
return testedDate.getMonthValue() + "월 " + testedDate.getDayOfMonth() + "일";
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.sopt.Server.common;
package com.sopt.Server.domain.enums;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
Expand All @@ -18,5 +18,18 @@ public enum AgeEnum {
private final String imageUrl1;
private final String imageUrl2;

public static AgeEnum getAgeEnum(int age){
if(age < 20)
return AgeEnum.TEENAGER;
else if(age < 30)
return AgeEnum.TWENTIES;
else if(age < 40)
return AgeEnum.THIRTIES;
else if(age < 50)
return AgeEnum.FORTIES;
else
return AgeEnum.FIFTIES;
}

}

Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package com.sopt.Server.repository;

import com.sopt.Server.domain.Member;
import jakarta.persistence.EntityNotFoundException;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface MemberJpaRepository extends JpaRepository<Member, Long> {
Optional<Member> findByName(String name);
default Member findByNameOrThrow(String name) {
return findByName(name)
.orElseThrow(() ->
new EntityNotFoundException("해당 이름의 회원이 존재하지 않습니다."));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,4 @@
import java.util.Optional;

public interface QuestionJpaRepository extends JpaRepository<Question, Long>{
List<Question> findAll();
Optional<Question> findById(Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,5 @@
import java.util.List;

public interface ResultJpaRepository extends JpaRepository<Result, Long>{
Result save(Result result);

List<Result> findAllByMemberIdOrderByIdDesc(Long memberId);
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package com.sopt.Server.service;

import com.sopt.Server.common.ApiResponse;
import com.sopt.Server.controller.request.MemberPostRequest;
import com.sopt.Server.controller.response.MemberGetResponse;
import com.sopt.Server.domain.Member;
import com.sopt.Server.exception.Success;
import com.sopt.Server.repository.MemberJpaRepository;
import jakarta.persistence.EntityNotFoundException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
Expand All @@ -19,14 +17,17 @@ public class MemberService {
private final MemberJpaRepository memberJpaRepository;

@Transactional
public ApiResponse<MemberGetResponse> saveMember(String nickName, int age) {
Member newMember = Member.builder().name(nickName).realAge(age).build();
Member member = memberJpaRepository.findByName(nickName).orElse(null);//null이면
if(member != null)//있다면
return ApiResponse.success(Success.GET_MEMBER_SUCCESS, MemberGetResponse.of(member));
else {
public MemberGetResponse saveMember(MemberPostRequest request) {
try {
Member member = memberJpaRepository.findByNameOrThrow(request.nickName());
return MemberGetResponse.of(member);
} catch (EntityNotFoundException e) {
Member newMember = Member.builder()
.name(request.nickName())
.realAge(request.age())
.build();
memberJpaRepository.save(newMember);
return MemberGetResponse.of(newMember);
}
return ApiResponse.success(Success.CREATE_MEMBER_SUCCESS, MemberGetResponse.of(newMember));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.sopt.Server.service;

import com.sopt.Server.common.AgeEnum;
import com.sopt.Server.domain.enums.AgeEnum;
import com.sopt.Server.controller.request.AnswerListRequestDTO;
import com.sopt.Server.controller.request.AnswerRequestDTO;
import com.sopt.Server.controller.response.AllResultsResponseDTO;
Expand All @@ -19,7 +19,6 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

Expand All @@ -41,7 +40,7 @@ public ResultResponseDTO saveResult(AnswerListRequestDTO request) {
Answer answer = answerJpaRepository.findByQuestionAndAnswerType(question, result.answerType()).orElseThrow(()->new CustomException(Error.NOT_FOUND_ANSWER_EXCEPTION,Error.NOT_FOUND_ANSWER_EXCEPTION.getMessage()));
memberAge += answer.getAnswerScore();
}
AgeEnum ageEnum = getAgeEnum(memberAge);
AgeEnum ageEnum = AgeEnum.getAgeEnum(memberAge);
resultJpaRepository.save(Result.builder().member(member).resultAge(memberAge).build());
return ResultResponseDTO.of(request.nickname(),memberAge,ageEnum.getTitle(),ageEnum.getContent(),ageEnum.getImageUrl1(), ageEnum.getImageUrl2());
}
Expand All @@ -50,10 +49,10 @@ public List<AllResultsResponseDTO> getAllResults(Long memberId) {

List<Result> resultList = resultJpaRepository.findAllByMemberIdOrderByIdDesc(memberId);
List<AllResultsResponseDTO> answer = new ArrayList<>();
//멤버의 result 모두 갖고옴 이것을 각각의 result마다 allresultsresponsedto만들어야 해

for(Result result : resultList) {
AgeEnum ageEnum = getAgeEnum(result.getResultAge());
String time = getStringDate(result.getTestedDate());
AgeEnum ageEnum = AgeEnum.getAgeEnum(result.getResultAge());
String time = result.getTestedDateToString();
AllResultsResponseDTO dto = AllResultsResponseDTO.of(result, ageEnum.getTitle(), ageEnum.getContent(), time, ageEnum.getImageUrl1(), ageEnum.getImageUrl2());
answer.add(dto);
}
Expand All @@ -62,22 +61,4 @@ public List<AllResultsResponseDTO> getAllResults(Long memberId) {

}

private String getStringDate(LocalDateTime time) {
String answer = time.getMonthValue() + "월 " + time.getDayOfMonth() + "일";
return answer;
}

private AgeEnum getAgeEnum(int age){
if(age < 20)
return AgeEnum.TEENAGER;
else if(age < 30)
return AgeEnum.TWENTIES;
else if(age < 40)
return AgeEnum.THIRTIES;
else if(age < 50)
return AgeEnum.FORTIES;
else
return AgeEnum.FIFTIES;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.sopt.Server.domain;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.time.LocalDateTime;

import static org.junit.jupiter.api.Assertions.*;

public class ResultTest {
@Test
@DisplayName("결과가 생성된 일자 정보를 반환한다")
void getTestDateToString() {
// given
LocalDateTime testedDate = LocalDateTime.of(2023, 3, 21, 12, 30);
Result result = Result.builder()
.testedDate(testedDate)
.build();
String expected = "3월 21일";

// when
String resultString = result.getTestedDateToString();

// then
assertEquals(expected, resultString);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.sopt.Server.domain.enums;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

public class AgeEnumTest {
private static final int eighteen = 18;
private static final int twentyFive = 25;
private static final int thirtyOne = 31;
private static final int fortyThree = 43;
private static final int fiftyFive = 55;

@Test
@DisplayName("18세는 10대이다")
void getAgeEnumTeenager() {
assertEquals(AgeEnum.TEENAGER, AgeEnum.getAgeEnum(eighteen));
}

@Test
@DisplayName("25세는 20대이다")
void getAgeEnumTwenties() {
assertEquals(AgeEnum.TWENTIES, AgeEnum.getAgeEnum(twentyFive));
}

@Test
@DisplayName("31세는 30대이다")
void getAgeEnumThirties() {
assertEquals(AgeEnum.THIRTIES, AgeEnum.getAgeEnum(thirtyOne));
}

@Test
@DisplayName("43세는 40대이다")
void getAgeEnumForties() {
assertEquals(AgeEnum.FORTIES, AgeEnum.getAgeEnum(fortyThree));
}

@Test
@DisplayName("55세는 50대이다")
void getAgeEnumFifties() {
assertEquals(AgeEnum.FIFTIES, AgeEnum.getAgeEnum(fiftyFive));
}

}