-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
235 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.petqa.api; | ||
|
||
import com.petqa.apiPayload.apiPayload.ApiResponse; | ||
import com.petqa.dto.mainpage.MainPageDTO; | ||
import com.petqa.service.mainPage.MainPageService; | ||
import jakarta.transaction.Transactional; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/MainPage") | ||
public class MainPageAPIController { | ||
private final MainPageService mainPageService; | ||
|
||
@GetMapping("/")//Parameter의 userId값으로 조회 | ||
public ApiResponse<MainPageDTO.MainPageResponseDTO> getMainPage(@RequestParam Long userId) { | ||
MainPageDTO.MainPageResponseDTO mainPageInfo = mainPageService.getMainPageInfo(userId); | ||
//MainPageService의 getMainPageInfo로 정보 가져옴 | ||
return ApiResponse.onSuccess(mainPageInfo); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.petqa.converter; | ||
|
||
import com.petqa.domain.Mapping.UserQuestion; | ||
import com.petqa.domain.Pet; | ||
import com.petqa.domain.Question; | ||
import com.petqa.domain.User; | ||
import com.petqa.dto.mainpage.MainPageDTO; | ||
import com.petqa.repository.PetRepository; | ||
import com.petqa.repository.QuestionRepository; | ||
import com.petqa.repository.UserQuestionRepository; | ||
import com.petqa.repository.UserRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.time.LocalDate; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class MainPageConverter { | ||
private final UserQuestionRepository userQuestionRepository; | ||
private final PetRepository petRepository; | ||
private final QuestionRepository questionRepository; | ||
private final UserRepository userRepository; | ||
|
||
public MainPageDTO.MainPageResponseDTO tomainPageResponseDTO(Long userId){ | ||
User user = userRepository.findById(userId) | ||
.orElseThrow(()->new IllegalArgumentException("User not found")); | ||
Pet pet = petRepository.findByUserId(userId); | ||
if (pet == null) {//조회했는데 pet이 null일 때, | ||
throw new IllegalArgumentException("Pet not found"); | ||
} | ||
|
||
UserQuestion userQuestion = userQuestionRepository.findTopByUserIdOrderByCreatedAtDesc(userId) | ||
.orElseThrow(() -> new IllegalArgumentException("UserQuestion not found")); | ||
|
||
Question todayQuestion = questionRepository.findById(Long.valueOf(userQuestion.getUser().getQuestionCount())) | ||
.orElseThrow(() -> new IllegalArgumentException("Question not found")); | ||
LocalDate today = LocalDate.now(); | ||
|
||
boolean questionStatus = user.getUserQuestionsList().stream() | ||
.anyMatch(mq -> mq.getCreatedAt().toLocalDate().equals(today)); | ||
|
||
return MainPageDTO.MainPageResponseDTO.builder() | ||
.petName(pet.getName()) | ||
.petCategory(pet.getPetType().name()) | ||
.point(Long.valueOf((user.getPoints()))) | ||
.todayQuestion(todayQuestion.getContent()) | ||
.questionStatus(questionStatus) | ||
.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.petqa.domain; | ||
|
||
import com.petqa.domain.Mapping.UserQuestion; | ||
import com.petqa.domain.common.BaseEntity; | ||
import jakarta.persistence.*; | ||
import lombok.*; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Entity | ||
@Getter | ||
@Builder | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@AllArgsConstructor | ||
public class Answer extends BaseEntity { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
private String answer; | ||
|
||
@OneToMany(mappedBy = "answer", cascade = CascadeType.ALL) | ||
private List<UserQuestion> userQuestionsList = new ArrayList<>(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.petqa.domain.Mapping; | ||
|
||
import com.petqa.domain.Answer; | ||
import com.petqa.domain.Question; | ||
import com.petqa.domain.User; | ||
import com.petqa.domain.common.BaseEntity; | ||
import jakarta.persistence.*; | ||
import lombok.*; | ||
|
||
@Entity | ||
@Getter | ||
@Builder | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@AllArgsConstructor | ||
public class UserQuestion extends BaseEntity { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "user_id") | ||
private User user; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "question_id") | ||
private Question question; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "answer_id") | ||
private Answer answer; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.petqa.domain; | ||
|
||
import com.petqa.domain.Mapping.UserQuestion; | ||
import com.petqa.domain.common.BaseEntity; | ||
import jakarta.persistence.*; | ||
import lombok.*; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Entity | ||
@Getter | ||
@Builder | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@AllArgsConstructor | ||
public class Question extends BaseEntity { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
private String content; | ||
|
||
@OneToMany(mappedBy = "question", cascade = CascadeType.ALL) | ||
private List<UserQuestion> userQuestionsList = new ArrayList<>(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.petqa.dto.mainpage; | ||
|
||
import lombok.*; | ||
|
||
public class MainPageDTO { | ||
|
||
@Getter | ||
@Setter | ||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public static class MainPageResponseDTO{ | ||
String petName; | ||
|
||
String petCategory; | ||
|
||
Long point; | ||
|
||
String todayQuestion; | ||
|
||
Boolean questionStatus; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.petqa.repository; | ||
|
||
import com.petqa.domain.Question; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface QuestionRepository extends JpaRepository<Question, Long> { | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/petqa/repository/UserQuestionRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.petqa.repository; | ||
|
||
import com.petqa.domain.Mapping.UserQuestion; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.Optional; | ||
|
||
public interface UserQuestionRepository extends JpaRepository<UserQuestion, Long> { | ||
Optional<UserQuestion> findTopByUserIdOrderByCreatedAtDesc(Long userId); | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/petqa/service/mainPage/MainPageService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.petqa.service.mainPage; | ||
|
||
import com.petqa.dto.mainpage.MainPageDTO; | ||
|
||
public interface MainPageService { | ||
MainPageDTO.MainPageResponseDTO getMainPageInfo(Long userId); | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/com/petqa/service/mainPage/MainPageServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.petqa.service.mainPage; | ||
|
||
import com.petqa.converter.MainPageConverter; | ||
import com.petqa.dto.mainpage.MainPageDTO; | ||
import jakarta.transaction.Transactional; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class MainPageServiceImpl implements MainPageService { | ||
private final MainPageConverter mainPageConverter; | ||
|
||
@Override | ||
@Transactional | ||
public MainPageDTO.MainPageResponseDTO getMainPageInfo(Long userId) { | ||
return mainPageConverter.tomainPageResponseDTO(userId); | ||
//유저 정보를 넣으면 MainPageConverter를 통해 원하는 DTO로 만들어줌 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters