Skip to content

Commit

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

Feature/3 branch mentoring -> weekly
  • Loading branch information
choboss00 authored Oct 14, 2023
2 parents cc8c076 + 936a019 commit e681c36
Show file tree
Hide file tree
Showing 18 changed files with 432 additions and 213 deletions.
4 changes: 4 additions & 0 deletions src/main/java/com/example/demo/config/utils/BaseTime.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.example.demo.config.utils;

import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.ColumnDefault;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
Expand All @@ -11,6 +13,8 @@
import java.time.LocalDateTime;
import java.time.LocalTime;

@Getter
@Setter
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class BaseTime {
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/com/example/demo/config/utils/StateConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.example.demo.config.utils;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.persistence.AttributeConverter;
import java.util.Optional;

@RequiredArgsConstructor
@Component
public class StateConverter implements AttributeConverter<StateEnum, String> {

@Override
public String convertToDatabaseColumn(StateEnum attribute) {
return Optional.ofNullable(attribute).orElse(StateEnum.NULL).getValue();
}

@Override
public StateEnum convertToEntityAttribute(String dbData) {
if (dbData == null || dbData.length() == 0) {
return StateEnum.NULL;
}
return StateEnum.findOf(dbData);
}
}
32 changes: 32 additions & 0 deletions src/main/java/com/example/demo/config/utils/StateEnum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.example.demo.config.utils;

import lombok.Getter;

import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Getter
public enum StateEnum {
NULL("NULL", ""),
ACTIVE("ACTIVE", "ACTIVE"),
DONE("DONE", "DONE"),
DELETED("DELETED", "DELETED");

private final String desc;
private final String value;

StateEnum(String desc, String value) {
this.desc = desc;
this.value = value;
}

private static final Map<String, StateEnum> descriptions = Collections.unmodifiableMap(Stream.of(values()).collect(Collectors.toMap(StateEnum::getValue, Function.identity())));

public static StateEnum findOf(String findValue) {
return Optional.ofNullable(descriptions.get(findValue)).orElse(NULL);
}
}
13 changes: 12 additions & 1 deletion src/main/java/com/example/demo/mentoring/MentorPost.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.example.demo.mentoring;

import com.example.demo.config.utils.BaseTime;
import com.example.demo.config.utils.StateConverter;
import com.example.demo.config.utils.StateEnum;
import com.example.demo.user.User;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

Expand All @@ -15,7 +18,7 @@
@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 = ?")
@SQLDelete(sql = "UPDATE mentor_post_tb SET deleted_at = CURRENT_TIMESTAMP, is_deleted = TRUE where id = ?")
@Table(name = "mentorPost_tb")
public class MentorPost extends BaseTime {
@Id
Expand All @@ -30,6 +33,10 @@ public class MentorPost extends BaseTime {

private String content;

@Convert(converter = StateConverter.class)
@Column(name = "state", nullable = false)
private StateEnum state = StateEnum.ACTIVE;

@Builder
public MentorPost(User writer, String title, String content){
this.writer = writer;
Expand All @@ -43,4 +50,8 @@ public void update(String title, String content)
this.content = content;
}

public void changeStatus(StateEnum stateEnum)
{
this.state = stateEnum;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,24 @@

import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface MentorPostJPARepostiory extends JpaRepository<MentorPost, Integer> {

@Query("select m from MentorPost m where m.writer.id = :writer")
@Query("select m from MentorPost m where m.writer.id = :writer and m.state = 'ACTIVE'")
List<MentorPost> findAllByWriter(@Param("writer") int writer);

MentorPost findById(int id);

@Query("select count(*) from MentorPost m where m.writer.id = :userId and m.state = 'ACTIVE'")
int countContactByMentorId(int userId);

@Query("select count(*) from MentorPost m where m.writer.id = :userId and m.state = 'DONE'")
int countDoneByMentorId(int userId);

@Query("SELECT m FROM MentorPost m INNER JOIN NotConnectedRegisterUser ncru ON m.id = ncru.mentorPost.id WHERE ncru.menteeUser.id = :menteeUserId")
List<MentorPost> findAllByMenteeUserId(@Param("menteeUserId") int menteeUserId);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.demo.mentoring;

import com.example.demo.config.utils.StateEnum;
import com.example.demo.user.User;
import lombok.Getter;
import lombok.Setter;
Expand All @@ -15,4 +16,10 @@ public static class CreateDTO {

private String content;
}

@Getter
@Setter
public static class StateDTO {
private StateEnum stateEnum;
}
}
52 changes: 52 additions & 0 deletions src/main/java/com/example/demo/mentoring/MentorPostResponse.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.example.demo.mentoring;


import com.example.demo.config.utils.StateEnum;
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;
import lombok.Getter;
import lombok.Setter;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

Expand All @@ -25,12 +27,14 @@ public static class MentorPostAllDTO {
private int postId;
private String title;
private String content;
private StateEnum stateEnum;
private WriterDTO writerDTO;

public MentorPostAllDTO(MentorPost mentorPost, List<UserInterest> userInterests) {
this.postId = mentorPost.getId();
this.title = mentorPost.getTitle();
this.content = mentorPost.getContent();
this.stateEnum = mentorPost.getState();
WriterDTO writerDTO = new MentorPostAllDTO.WriterDTO(mentorPost.getWriter(), userInterests);
this.writerDTO = writerDTO;
}
Expand Down Expand Up @@ -75,12 +79,14 @@ public static class MentorPostDTO {
private String title;
private String content;
private WriterDTO writerDTO;
private StateEnum stateEnum;
private List<MenteeDTO> menteeDTOList;

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();
this.stateEnum = mentorPost.getState();
MentorPostDTO.WriterDTO writerDTO = new MentorPostDTO.WriterDTO(mentorPost.getWriter(), mentorFavorites);
this.writerDTO = writerDTO;
List<MentorPostDTO.MenteeDTO> menteeDTOList = mentees.stream()
Expand Down Expand Up @@ -140,4 +146,50 @@ public MenteeDTO(User user, List<UserInterest> userInterests) {
}
}
}

@Getter
@Setter
public static class MentorPostAllWithTimeStampDTO {
private int postId;
private String title;
private String content;
private StateEnum stateEnum;
private WriterDTO writerDTO;
private LocalDateTime createdAt;
private LocalDateTime deletedAt;
private boolean isDeleted;

public MentorPostAllWithTimeStampDTO(MentorPost mentorPost, List<UserInterest> userInterests) {
this.postId = mentorPost.getId();
this.title = mentorPost.getTitle();
this.content = mentorPost.getContent();
this.stateEnum = mentorPost.getState();
WriterDTO writerDTO = new MentorPostAllWithTimeStampDTO.WriterDTO(mentorPost.getWriter(), userInterests);
this.writerDTO = writerDTO;
this.createdAt = mentorPost.getCreatedAt();
this.deletedAt = mentorPost.getDeletedAt();
this.isDeleted = mentorPost.isDeleted();
}

@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());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RequiredArgsConstructor
Expand All @@ -17,7 +18,7 @@ public class MentorPostRestController {
private final MentorPostService mentorPostService;

@PostMapping(value = "/mentorings/post")
public ResponseEntity<?> createMentorPost(@RequestPart MentorPostRequest.CreateDTO requestDTO, Errors errors, @AuthenticationPrincipal CustomUserDetails userDetails) {
public ResponseEntity<?> createMentorPost(@RequestBody @Valid MentorPostRequest.CreateDTO requestDTO, Errors errors, @AuthenticationPrincipal CustomUserDetails userDetails) {
mentorPostService.createMentorPost(requestDTO, userDetails.getUser());
return ResponseEntity.status(HttpStatus.OK).body(ApiUtils.successWithNoContent());
}
Expand All @@ -35,8 +36,21 @@ public ResponseEntity<?> getMentorPostId(@PathVariable int id, @AuthenticationPr
}

@PutMapping(value = "/mentorings/post/{id}")
public ResponseEntity<?> updateMentorPost(@PathVariable int id, @RequestPart MentorPostRequest.CreateDTO requestDTO, Errors errors, @AuthenticationPrincipal CustomUserDetails userDetails) {
public ResponseEntity<?> updateMentorPost(@PathVariable int id, @RequestBody @Valid MentorPostRequest.CreateDTO requestDTO, Errors errors, @AuthenticationPrincipal CustomUserDetails userDetails) {
mentorPostService.updateMentorPost(requestDTO, id);
return ResponseEntity.status(HttpStatus.OK).body(ApiUtils.successWithNoContent());
}

@DeleteMapping(value = "/mentorings/post/{id}")
public ResponseEntity<?> deleteMentorPost(@PathVariable int id, Errors errors, @AuthenticationPrincipal CustomUserDetails userDetails) {
mentorPostService.deleteMentorPost(id);
return ResponseEntity.status(HttpStatus.OK).body(ApiUtils.successWithNoContent());
}

@PatchMapping(value = "/mentorings/post/{id}/done")
public ResponseEntity<?> changeMentorPostStatus(@PathVariable int id,@RequestBody @Valid MentorPostRequest.StateDTO requestDTO, Errors errors, @AuthenticationPrincipal CustomUserDetails userDetails) {
mentorPostService.changeMentorPostStatus(requestDTO, id);
return ResponseEntity.status(HttpStatus.OK).body(ApiUtils.successWithNoContent());
}

}
33 changes: 33 additions & 0 deletions src/main/java/com/example/demo/mentoring/MentorPostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,39 @@ public void updateMentorPost(MentorPostRequest.CreateDTO createDTO, int id)

}
}

public void deleteMentorPost(int id) {
mentorPostJPARepository.deleteById(id);
}

//생성 시간까지 조회하는 test service 코드 입니다
public List<MentorPostResponse.MentorPostAllWithTimeStampDTO> findAllMentorPostWithTimeStamp() {
List<MentorPost> pageContent = mentorPostJPARepository.findAll();
//List<MentorPost> mentorPostList = mentorPostJPARepostiory.findAll();
List<MentorPostResponse.MentorPostAllWithTimeStampDTO> mentorPostDTOList = pageContent.stream().map(
mentorPost -> {
List<UserInterest> writerInterests = userInterestJPARepository.findAllById(mentorPost.getWriter().getId());
return new MentorPostResponse.MentorPostAllWithTimeStampDTO(mentorPost,writerInterests);
}
).collect(Collectors.toList());
return mentorPostDTOList;
}

public void changeMentorPostStatus(MentorPostRequest.StateDTO stateDTO, int id)
{
Optional<MentorPost> optionalMentorPost = Optional.ofNullable(mentorPostJPARepository.findById(id));

if(optionalMentorPost.isPresent())
{
MentorPost mentorPost = optionalMentorPost.get();
mentorPost.changeStatus(stateDTO.getStateEnum());
}
else
{
// 예외처리

}
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,30 @@
import java.util.stream.Collectors;

public class ContactResponse {

@Getter @Setter
public static class postCountDTO {
private int contactCount;
private int doneCount;

public postCountDTO(int contactCount, int doneCount) {
this.contactCount = contactCount;
this.doneCount = doneCount;
}
}

@Getter @Setter
public static class MenteeContactDTO {
private int postId;
private String title;
private MentorDTO mentor;

public MenteeContactDTO(MentorPost mentorPost, MentorDTO mentor) {
this.postId = mentorPost.getId();
this.title = mentorPost.getTitle();
this.mentor = mentor;
}
}
/**
*
* DTO 담는 순서
Expand Down
Loading

0 comments on commit e681c36

Please sign in to comment.