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

이전꺼 머지후 올리는것 (mentoring -> weekly) #24

Merged
merged 24 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
08b2cf7
Merge pull request #18 from Step3-kakao-tech-campus/weekly
choboss00 Oct 8, 2023
6852537
Merge pull request #19 from Step3-kakao-tech-campus/dev
choboss00 Oct 8, 2023
d366b26
Merge pull request #12 from choboss00/master
choboss00 Oct 8, 2023
f28f863
feat : mentorPostRest Get요청 Paging
sjmjys954646 Oct 8, 2023
fa98b88
fix : mentorPostRestController
sjmjys954646 Oct 8, 2023
f3dbb61
fix : pagination
sjmjys954646 Oct 8, 2023
2792ff2
fix : findAllMentorPost, MentorPostResponse DTO 수정
sjmjys954646 Oct 9, 2023
f9b70a7
feat : MentorPostEach Service, Response
sjmjys954646 Oct 9, 2023
8aae3dd
feat : findMentorPost Testcode
sjmjys954646 Oct 9, 2023
b29cc3f
fix : mentorPost Bug
sjmjys954646 Oct 9, 2023
3d2f97f
feat : mentorPostIdRestController
sjmjys954646 Oct 9, 2023
92a9ef3
feat : 테스트 데이터 파일 추가
choboss00 Oct 11, 2023
109fe67
feat : interest table 에 baseTime 상속
choboss00 Oct 11, 2023
957d98a
feat : state 에 에너테이션 추가
choboss00 Oct 11, 2023
decdbb9
test : 테스트 데이터가 잘 실행되는지 테스트
choboss00 Oct 11, 2023
bcadd40
Merge pull request #22 from choboss00/feature/3-branch-mentoring
sjmjys954646 Oct 11, 2023
3f2beae
feat : softdelete
sjmjys954646 Oct 12, 2023
702ad84
Merge branch 'feature/3-branch-mentoring' of https://github.com/Step3…
sjmjys954646 Oct 12, 2023
a890e94
feat : mvcTest
sjmjys954646 Oct 12, 2023
322ef18
feat : putRequest
sjmjys954646 Oct 12, 2023
c0b65cb
feat : putRequest
sjmjys954646 Oct 12, 2023
0370f6f
fix : postRequest
sjmjys954646 Oct 12, 2023
da26d8c
feat : update testcode
sjmjys954646 Oct 12, 2023
1ba6c43
Merge pull request #23 from sjmjys954646/feature/3-branch-mentoring
choboss00 Oct 12, 2023
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
8 changes: 7 additions & 1 deletion src/main/java/com/example/demo/config/utils/BaseTime.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.demo.config.utils;

import org.hibernate.annotations.ColumnDefault;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
Expand All @@ -15,8 +16,13 @@
public abstract class BaseTime {
@CreatedDate
@Column(nullable = false)
private LocalTime createdAt;
private LocalDateTime createdAt;

@LastModifiedDate
private LocalDateTime updatedAt;

private LocalDateTime deletedAt;

@ColumnDefault("false")
private boolean isDeleted;
}
3 changes: 2 additions & 1 deletion src/main/java/com/example/demo/interest/Interest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.demo.interest;

import com.example.demo.config.utils.BaseTime;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -11,7 +12,7 @@
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Entity
@Table(name = "interest_tb")
public class Interest {
public class Interest extends BaseTime {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/example/demo/mentoring/MentorPost.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import javax.persistence.*;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Entity
@Where(clause = "deleted_at IS NULL")
@SQLDelete(sql = "UPDATE mentorPost_tb SET deleted_at = CURRENT_TIMESTAMP, isDeleted = TRUE where id = ?")
@Table(name = "mentorPost_tb")
public class MentorPost extends BaseTime {
@Id
Expand All @@ -32,4 +36,11 @@ public MentorPost(User writer, String title, String content){
this.title = title;
this.content = content;
}

public void update(String title, String content)
{
this.title = title;
this.content = content;
}

}
10 changes: 1 addition & 9 deletions src/main/java/com/example/demo/mentoring/MentorPostRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,10 @@ public class MentorPostRequest {

@Getter
@Setter
public class CreateDTO {
public static class CreateDTO {
@NotNull
private String title;

private String content;

public MentorPost toEntity(User writer) {
return MentorPost.builder()
.writer(writer)
.title(title)
.content(content)
.build();
}
}
}
99 changes: 95 additions & 4 deletions src/main/java/com/example/demo/mentoring/MentorPostResponse.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.demo.mentoring;


import com.example.demo.mentoring.contact.NotConnectedRegisterUser;
import com.example.demo.user.Role;
import com.example.demo.user.User;
import com.example.demo.user.userInterest.UserInterest;
Expand All @@ -12,19 +13,87 @@

public class MentorPostResponse {

/*
페이지에서 멘토post 전체를 조회
MentorPostAllDTO
클릭시 들어간 페이지에서 보여지는 Post
MentorPostDTO
*/
@Getter
@Setter
public static class MentorPostAllDTO {
private int postId;
private String title;
private String content;
private WriterDTO writerDTO;

public MentorPostAllDTO(MentorPost mentorPost, List<UserInterest> userInterests) {
this.postId = mentorPost.getId();
this.title = mentorPost.getTitle();
this.content = mentorPost.getContent();
WriterDTO writerDTO = new MentorPostAllDTO.WriterDTO(mentorPost.getWriter(), userInterests);
this.writerDTO = writerDTO;
}

@Getter @Setter
public static class WriterDTO {
private int mentorId;
private String profileImage;
private String name;
private String country;
private Role role;
private List<String> interests;

public WriterDTO(User user, List<UserInterest> userInterests) {
this.mentorId = user.getId();
this.profileImage = user.getProfileImage();
this.name = user.getFirstName() + " " + user.getLastName();
this.country = user.getCountry();
this.role = user.getRole();
this.interests = userInterests.stream()
.map(userInterest -> userInterest.getInterest().getCategory())
.collect(Collectors.toList());
}
}
}

/*
param :
MentorPost mentorPost 멘토 데이터
List<UserInterest> mentorInterests 멘토의 Interest
List<NotConnectedRegisterUser> mentees 멘티들 데이터
List<UserInterest> menteeInterests : 멘티들 각각의 Interest 리스트 전체

dto :
writerDTO : 작성자인 멘토의 DTO
menteeDTO : 멘티들의 DTO
*/
@Getter
@Setter
public static class MentorPostDTO {
private int postId;
private String title;
private String content;
private WriterDTO writerDTO;
private List<MenteeDTO> menteeDTOList;

public MentorPostDTO(MentorPost mentorPost, WriterDTO writerDTO) {
public MentorPostDTO(MentorPost mentorPost, List<UserInterest> mentorFavorites, List<NotConnectedRegisterUser> mentees, List<UserInterest> menteeInterest) {
this.postId = mentorPost.getId();
this.title = mentorPost.getTitle();
this.content = mentorPost.getContent();
MentorPostDTO.WriterDTO writerDTO = new MentorPostDTO.WriterDTO(mentorPost.getWriter(), mentorFavorites);
this.writerDTO = writerDTO;
List<MentorPostDTO.MenteeDTO> menteeDTOList = mentees.stream()
.map(mentee -> {
List<UserInterest> eachMenteeFavorite = menteeInterest.stream().filter(
userInterest -> mentee.getMenteeUser().getId() == userInterest.getUser().getId()
).collect(Collectors.toList());

MentorPostDTO.MenteeDTO menteeDTO = new MentorPostDTO.MenteeDTO(mentee.getMenteeUser(), eachMenteeFavorite);
return menteeDTO;
})
.collect(Collectors.toList());
this.menteeDTOList = menteeDTOList;
}

@Getter @Setter
Expand All @@ -34,16 +103,38 @@ public static class WriterDTO {
private String name;
private String country;
private Role role;
private List<String> favorites;
private List<String> interests;

public WriterDTO(User user, List<UserInterest> userInterests) {
this.mentorId = user.getId();
this.profileImage = user.getProfileImage();
this.name = user.getFirstName() + " " + user.getLastName();
this.country = user.getCountry();
this.role = user.getRole();
this.favorites = userInterests.stream()
.filter(userInterest -> userInterest.getUser().getId() == user.getId())
this.interests = userInterests.stream()
.map(userInterest -> userInterest.getInterest().getCategory())
.collect(Collectors.toList());
}
}

@Getter @Setter
public static class MenteeDTO{
private int menteeId;
private String profileImage;
private String name;
private String country;
private Role role;
private int age;
private List<String> interests;

public MenteeDTO(User user, List<UserInterest> userInterests) {
this.menteeId = user.getId();
this.profileImage = user.getProfileImage();
this.name = user.getFirstName() + " " + user.getLastName();
this.country = user.getCountry();
this.role = user.getRole();
this.age = user.getAge();
this.interests = userInterests.stream()
.map(userInterest -> userInterest.getInterest().getCategory())
.collect(Collectors.toList());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,40 @@
import com.example.demo.config.auth.CustomUserDetails;
import com.example.demo.config.utils.ApiUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequiredArgsConstructor
@RestController
public class MentorPostRestController {
private final MentorPostService mentorPostService;

@PostMapping(value = "/mentorings")
@PostMapping(value = "/mentorings/post")
public ResponseEntity<?> createMentorPost(@RequestPart MentorPostRequest.CreateDTO requestDTO, Errors errors, @AuthenticationPrincipal CustomUserDetails userDetails) {
mentorPostService.createMentorPost(requestDTO, userDetails.getUser());
return ResponseEntity.ok().body(ApiUtils.success(true));
return ResponseEntity.status(HttpStatus.OK).body(ApiUtils.successWithNoContent());
}

@GetMapping("/mentorings/post")
public ResponseEntity<?> getMentorPost(@RequestParam(value = "page", defaultValue = "0") Integer page, @AuthenticationPrincipal CustomUserDetails userDetails) {
List<MentorPostResponse.MentorPostAllDTO> responseDTOs = mentorPostService.findAllMentorPost(page);
return ResponseEntity.ok(ApiUtils.success(responseDTOs));
}

@GetMapping("/mentorings/post/{id}")
public ResponseEntity<?> getMentorPostId(@PathVariable int id, @AuthenticationPrincipal CustomUserDetails userDetails) {
MentorPostResponse.MentorPostDTO responseDTO = mentorPostService.findMentorPost(id);
return ResponseEntity.ok(ApiUtils.success(responseDTO));
}

@PutMapping(value = "/mentorings/post/{id}")
public ResponseEntity<?> updateMentorPost(@PathVariable int id, @RequestPart MentorPostRequest.CreateDTO requestDTO, Errors errors, @AuthenticationPrincipal CustomUserDetails userDetails) {
mentorPostService.updateMentorPost(requestDTO, id);
return ResponseEntity.status(HttpStatus.OK).body(ApiUtils.successWithNoContent());
}
}
61 changes: 54 additions & 7 deletions src/main/java/com/example/demo/mentoring/MentorPostService.java
Original file line number Diff line number Diff line change
@@ -1,40 +1,87 @@
package com.example.demo.mentoring;

import com.example.demo.mentoring.contact.ContactJPARepository;
import com.example.demo.mentoring.contact.NotConnectedRegisterUser;
import com.example.demo.user.User;
import com.example.demo.user.userInterest.UserInterest;
import com.example.demo.user.userInterest.UserInterestJPARepository;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Transactional
@RequiredArgsConstructor
@Service
public class MentorPostService {
private final MentorPostJPARepostiory mentorPostJPARepostiory;
private final MentorPostJPARepostiory mentorPostJPARepository;
private final UserInterestJPARepository userInterestJPARepository;
private final ContactJPARepository contactJPARepository;

//mentorPost생성
@Transactional
public void createMentorPost(MentorPostRequest.CreateDTO createDTO, User writer) {
mentorPostJPARepostiory.save(createDTO.toEntity(writer));
MentorPost mentorPost = new MentorPost( writer, createDTO.getTitle(), createDTO.getContent());
mentorPostJPARepository.save(mentorPost);
}

/* 1. mentorPostList를 조회
2. 각 List당 writer별 writerInterests를 조회
3. MentorPostDTO 생성*/
public List<MentorPostResponse.MentorPostDTO> findAllMentorPost() {
List<MentorPost> mentorPostList = mentorPostJPARepostiory.findAll();
List<MentorPostResponse.MentorPostDTO> mentorPostDTOList = mentorPostList.stream().map(
public List<MentorPostResponse.MentorPostAllDTO> findAllMentorPost(int page) {
Pageable pageable = PageRequest.of(page,5);

Page<MentorPost> pageContent = mentorPostJPARepository.findAll(pageable);
//List<MentorPost> mentorPostList = mentorPostJPARepostiory.findAll();
List<MentorPostResponse.MentorPostAllDTO> mentorPostDTOList = pageContent.getContent().stream().map(
mentorPost -> {
List<UserInterest> writerInterests = userInterestJPARepository.findAllById(mentorPost.getWriter().getId());
MentorPostResponse.MentorPostDTO.WriterDTO writerDTO = new MentorPostResponse.MentorPostDTO.WriterDTO(mentorPost.getWriter(), writerInterests);
return new MentorPostResponse.MentorPostDTO(mentorPost,writerDTO);
return new MentorPostResponse.MentorPostAllDTO(mentorPost,writerInterests);
}
).collect(Collectors.toList());
return mentorPostDTOList;
}

public MentorPostResponse.MentorPostDTO findMentorPost(int id){
MentorPost mentorPost = mentorPostJPARepository.findById(id);

//writer 데이터
User mentor = mentorPost.getWriter();
//mentee들 데이터
List<NotConnectedRegisterUser> menteeList = contactJPARepository.findAllByMentorPostId(id);
//writer Interest데이터
List<UserInterest> mentorInterests = userInterestJPARepository.findAllById(mentor.getId());
//mentee들 Interest데이터
List<UserInterest> menteeInterests = menteeList.stream()
.flatMap(mentee -> userInterestJPARepository.findAllById(mentee.getMenteeUser().getId()).stream())
.collect(Collectors.toList());

MentorPostResponse.MentorPostDTO mentorPostDTO = new MentorPostResponse.MentorPostDTO(mentorPost, mentorInterests, menteeList, menteeInterests);

return mentorPostDTO;
}
@Transactional
public void updateMentorPost(MentorPostRequest.CreateDTO createDTO, int id)
{
Optional<MentorPost> optionalMentorPost = Optional.ofNullable(mentorPostJPARepository.findById(id));

if(optionalMentorPost.isPresent())
{
MentorPost mentorPost = optionalMentorPost.get();
mentorPost.update(createDTO.getTitle(), createDTO.getContent());
}
else
{
// 예외처리

}
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public class NotConnectedRegisterUser extends BaseTime {
@ManyToOne(fetch = FetchType.LAZY)
private User menteeUser;

@Column(nullable = false)
@Enumerated(value = EnumType.STRING)
private State state;

public enum State {
Expand Down
Loading
Loading