Skip to content

Commit

Permalink
Merge pull request #24 from Step3-kakao-tech-campus/feature/3-branch-…
Browse files Browse the repository at this point in the history
…mentoring

이전꺼 머지후 올리는것 (mentoring -> weekly)
  • Loading branch information
choboss00 authored Oct 12, 2023
2 parents 2eba3df + 1ba6c43 commit cc8c076
Show file tree
Hide file tree
Showing 12 changed files with 536 additions and 40 deletions.
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

0 comments on commit cc8c076

Please sign in to comment.