Skip to content

Commit

Permalink
Merge pull request #25 from sjmjys954646/feature/3-branch-mentoring
Browse files Browse the repository at this point in the history
mentorPost 버그 수정 및 Enum형 추가
  • Loading branch information
choboss00 authored Oct 12, 2023
2 parents 1ba6c43 + c02ca17 commit 147133a
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 2 deletions.
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);
}
}
7 changes: 7 additions & 0 deletions 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 @@ -30,6 +33,10 @@ public class MentorPost extends BaseTime {

private String content;

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

@Builder
public MentorPost(User writer, String title, String content){
this.writer = writer;
Expand Down
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 @@ -14,5 +15,7 @@ public static class CreateDTO {
private String title;

private String content;

private StateEnum state;
}
}
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,7 +36,7 @@ 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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import com.example.demo.RestDoc;
import com.example.demo.config.utils.StateEnum;
import com.example.demo.interest.Interest;
import com.example.demo.interest.InterestJPARepository;
import com.example.demo.mentoring.*;
Expand Down Expand Up @@ -90,6 +91,7 @@ void CreateMentorPostTest() {
MentorPostRequest.CreateDTO mentorPostRequest = new MentorPostRequest.CreateDTO();
mentorPostRequest.setTitle("title");
mentorPostRequest.setContent("content");
mentorPostRequest.setState(StateEnum.ACTIVE);
mentorPostService.createMentorPost(mentorPostRequest, writer);

MentorPost mentorPostFind = mentorPostJPARepostiory.findById(1);
Expand Down

0 comments on commit 147133a

Please sign in to comment.