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

Feat/142 post #143

Merged
merged 3 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import ussum.homepage.application.post.service.dto.response.TopLikedPostListResponse;
import ussum.homepage.global.ApiResponse;
import ussum.homepage.global.config.auth.UserId;
import ussum.homepage.global.config.custom.BoardRequestBody;

@RestController
@RequiredArgsConstructor
Expand Down Expand Up @@ -83,11 +84,14 @@ public ResponseEntity<ApiResponse<?>> getBoardPost(@PathVariable(name = "boardCo
이 컨트롤러에 있는 "/board/{boardCode}/files" api를 먼저 사용하여 리턴값으로 전달받는 값을 넣어주면 됩니다.
사진이나 파일이 존재하지 않을 시 빈 List로 전달해주시면 됩니다.
isNotice는 긴급공지 사항을 나타내는 필드로 맞을시 true, 틀릴시 false를 반환하면 됩니다.

인권신고게시판 경우 기존 json에 추가로 relatedPeople 리스트로 전달해주시면 됩니다. 리스트에는 name, major, studentId에 값을 String으로
넣어서 전달해주시고 personType은 신고자, 피침해자, 침해자중 선택하여 전달해주시면 됩니다.
""")
@PostMapping("/{boardCode}/posts")
public ResponseEntity<ApiResponse<?>> createBoardPost(@Parameter(hidden = true) @UserId Long userId,
@PathVariable(name = "boardCode") String boardCode,
@RequestBody PostCreateRequest postCreateRequest){
@BoardRequestBody PostCreateRequest postCreateRequest){
return ApiResponse.success(postManageService.createBoardPost(userId, boardCode, postCreateRequest));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public PostCreateResponse createBoardPost(Long userId, String boardCode, PostCre
Board board = boardReader.getBoardWithBoardCode(boardCode);
PostCreateRequest converPostCreateRequest = postFactory.convert(boardCode,postCreateRequest);
Post post = postAppender.createPost(converPostCreateRequest.toDomain(board, userId));
postAdditionalAppender.createAdditional(converPostCreateRequest);
postAdditionalAppender.createAdditional(converPostCreateRequest,post.getId());
postFileAppender.updatePostIdForIds(converPostCreateRequest.getPostFileList(), post.getId(), FileCategory.자료집아님);
return PostCreateResponse.of(post.getId(), boardCode);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@

@Getter
public class GeneralPostCreateRequest extends PostCreateRequest {
private final String categoryCode;

@Builder
public GeneralPostCreateRequest(String title, String content, String thumbNailImage, boolean isNotice, String categoryCode, List<Long> postFileList) {
super(title, content, thumbNailImage, isNotice,postFileList);
this.categoryCode = categoryCode;
this.postFileList = postFileList;
public GeneralPostCreateRequest(String title, String content, String categoryCode, String thumbNailImage, boolean isNotice, List<Long> postFileList) {
super(title, content,categoryCode, thumbNailImage, isNotice,postFileList);
}

public Post toDomain(Board board, Long userId){
Expand Down
Original file line number Diff line number Diff line change
@@ -1,47 +1,31 @@
package ussum.homepage.application.post.service.dto.request;

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import java.util.List;
import lombok.Getter;
import ussum.homepage.domain.post.Board;
import ussum.homepage.domain.post.Post;

@Getter
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "boardId",defaultImpl = GeneralPostCreateRequest.class)
@JsonSubTypes({
@JsonSubTypes.Type(value = RightsDetailRequest.class, name = "8"),
})
//@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "boardCode",defaultImpl = GeneralPostCreateRequest.class)
//@JsonSubTypes({
// @JsonSubTypes.Type(value = RightsPostCreateRequest.class, name = "인권신고게시판")
//})
public abstract class PostCreateRequest {
protected String title;
protected String content;
protected String categoryCode;
protected String thumbNailImage;
protected boolean isNotice;
protected List<Long> postFileList;

public PostCreateRequest(String title, String content, String thumbNailImage, boolean isNotice, List<Long> postFileList) {
public PostCreateRequest(String title, String content, String categoryCode, String thumbNailImage, boolean isNotice, List<Long> postFileList) {
this.title = title;
this.content = content;
this.categoryCode = categoryCode;
this.thumbNailImage = thumbNailImage;
this.isNotice = isNotice;
this.postFileList = postFileList;
}

public Post toDomain(Board board, Long userId){
String status = "새로운";
if(isNotice){
status = "긴급공지";
}

return Post.of(null,
title,
content,
1,
thumbNailImage,
status,
null,null,null,
null,
userId,
board.getId());
}
public abstract Post toDomain(Board board, Long userId);
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package ussum.homepage.application.post.service.dto.request;

import ussum.homepage.domain.post.RightsDetail;
import ussum.homepage.infra.jpa.post.entity.RightsDetailEntity.PersonType;

public record RightsDetailRequest(
String name,
String studentId,
String major,
String personType
String personType,
Long postId
) {
public RightsDetail toDomain() {
return RightsDetail.of(null, name, studentId, major, personType);
public RightsDetail toDomain(Long postId) {
PersonType enumPersonType = PersonType.getEnumPersonTypeFromStringType(personType());

return RightsDetail.of(null, name, studentId, major, enumPersonType,postId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,37 @@
import java.util.List;
import lombok.Builder;
import lombok.Getter;
import ussum.homepage.domain.post.Board;
import ussum.homepage.domain.post.Post;

@Getter
public class RightsPostCreateRequest extends PostCreateRequest {
private final List<RightsDetailRequest> relatedPeople;

@Builder
public RightsPostCreateRequest(String title, String content, String thumbNailImage, boolean isNotice,
public RightsPostCreateRequest(String title, String content, String categoryCode,String thumbNailImage, boolean isNotice,
List<Long> postFileList, List<RightsDetailRequest> relatedPeople) {
super(title, content, thumbNailImage, isNotice, postFileList);
super(title, content, categoryCode, thumbNailImage, isNotice, postFileList);
this.relatedPeople = relatedPeople;
}

@Override
public Post toDomain(Board board, Long userId) {

return Post.of(null,
title,
content,
null,
thumbNailImage,
"새로운",
null, null, null,
categoryCode,
userId,
board.getId());
}

private boolean validation(String boardCod){
return true;
}
}

7 changes: 4 additions & 3 deletions src/main/java/ussum/homepage/domain/post/RightsDetail.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ public class RightsDetail {
private String name;
private String studentId;
private String major;
private String personType;
private PersonType personType;
private Long postId;

public static RightsDetail of(Long id,
String name,String studentId,String major,String personType) {
return new RightsDetail(id, name, studentId, major, personType);
String name,String studentId,String major,PersonType personType,Long postId) {
return new RightsDetail(id, name, studentId, major, personType, postId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@

public interface RightsDetailRepository {
void saveAll(List<RightsDetail> rightsDetails);
void deleteAll(Long postId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ public class PostAdditionalAppender {
private final RightsDetailRepository rightsDetailRepository;

@Transactional
public void createAdditional(PostCreateRequest converPostCreateRequest) {
public void createAdditional(PostCreateRequest converPostCreateRequest, Long postId) {
if (converPostCreateRequest instanceof RightsPostCreateRequest){

List<RightsDetail> rightsDetailList = new ArrayList<>();

for ( RightsDetailRequest detailRequest : ((RightsPostCreateRequest) converPostCreateRequest).getRelatedPeople()){
rightsDetailList.add(detailRequest.toDomain());
for (RightsDetailRequest detailRequest : ((RightsPostCreateRequest) converPostCreateRequest).getRelatedPeople()){
rightsDetailList.add(detailRequest.toDomain(postId));
}
rightsDetailRepository.saveAll(rightsDetailList);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
@RequiredArgsConstructor
public class PostModifier {
private final PostRepository postRepository;
private final RightsDetailRepository rightsDetailRepository;
private final PostReader postReader;
private final BoardReader boardReader;

Expand All @@ -23,6 +24,7 @@ public Post updateDataPost(Post post, String category){
}

public void deletePost(String boardCode, Long postId) {
rightsDetailRepository.deleteAll(postId);
postRepository.delete(postReader.getPostWithBoardCodeForEditAndDelete(boardCode, postId));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,28 @@ public class PostFactoryImpl implements PostFactory {

@Override
public PostCreateRequest convert(String boardCode, PostCreateRequest request) {
return switch (boardCode){
case "인권신고게시판" ->
RightsPostCreateRequest.builder()
.title(request.getTitle())
.content(request.getContent())
.isNotice(request.isNotice())
.thumbNailImage(request.getThumbNailImage())
.postFileList(request.getPostFileList())

return switch (boardCode) {
case "인권신고게시판" -> {
RightsPostCreateRequest rightsPostCreateRequest = (RightsPostCreateRequest) request;
yield RightsPostCreateRequest.builder()
.title(rightsPostCreateRequest.getTitle())
.content(rightsPostCreateRequest.getContent())
.categoryCode(rightsPostCreateRequest.getCategoryCode())
.thumbNailImage(rightsPostCreateRequest.getThumbNailImage())
.isNotice(rightsPostCreateRequest.isNotice())
.postFileList(rightsPostCreateRequest.getPostFileList())
.relatedPeople(rightsPostCreateRequest.getRelatedPeople())
.build();
default ->
GeneralPostCreateRequest.builder()
.title(request.getTitle())
.content(request.getContent())
.isNotice(request.isNotice())
.thumbNailImage(request.getThumbNailImage())
.postFileList(request.getPostFileList())
.build();
}
default -> GeneralPostCreateRequest.builder()
.title(request.getTitle())
.content(request.getContent())
.categoryCode(request.getCategoryCode())
.thumbNailImage(request.getThumbNailImage())
.isNotice(request.isNotice())
.postFileList(request.getPostFileList())
.build();
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package ussum.homepage.global.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;
import org.springframework.web.servlet.HandlerMapping;
import ussum.homepage.application.post.service.dto.request.GeneralPostCreateRequest;
import ussum.homepage.application.post.service.dto.request.RightsPostCreateRequest;
import ussum.homepage.global.config.custom.BoardRequestBody;

@Component
public class BoardRequestBodyArgumentResolver implements HandlerMethodArgumentResolver {
private final ObjectMapper objectMapper;

public BoardRequestBodyArgumentResolver(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}

@Override
public boolean supportsParameter(MethodParameter parameter) {
return parameter.hasParameterAnnotation(BoardRequestBody.class);
}

@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {

Map<String, String> pathVariables = (Map<String, String>) webRequest.getAttribute(
HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE,
RequestAttributes.SCOPE_REQUEST
);

String boardCode = pathVariables.get("boardCode");

HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class);
String body = StreamUtils.copyToString(request.getInputStream(), StandardCharsets.UTF_8);

Class<?> targetClass = "인권신고게시판".equals(boardCode) ?
RightsPostCreateRequest.class : GeneralPostCreateRequest.class;

Object result = objectMapper.readValue(body, targetClass);

return result;
}
}
3 changes: 3 additions & 0 deletions src/main/java/ussum/homepage/global/config/WebConfig.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ussum.homepage.global.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
Expand All @@ -12,9 +13,11 @@
@Configuration
public class WebConfig implements WebMvcConfigurer {
private final UserIdArgumentResolver userIdArgumentResolver;
private final ObjectMapper objectMapper;

@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers){
resolvers.add(userIdArgumentResolver);
resolvers.add(new BoardRequestBodyArgumentResolver(objectMapper));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ussum.homepage.global.config.custom;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface BoardRequestBody {
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ public enum ErrorStatus implements BaseErrorCode {
INVALID_GROUP_CODE(HttpStatus.BAD_REQUEST,"ENUM_015","유효하지 않은 GROUPCODE입니다."),
INVALID_FILE_CATEGORY(HttpStatus.BAD_REQUEST,"ENUM_016","유효하지 않은 FILE_CATEGORY입니다."),
INVALID_FILETYPE(HttpStatus.BAD_REQUEST,"ENUM_017","유효하지 않은 FILETYPE입니다."),
PERSON_TYPE_NULL(HttpStatus.BAD_REQUEST,"ENUM_18","PERSON_TYPE이 NULL입니다."),
INVALID_PERSON_TYPE(HttpStatus.BAD_REQUEST,"ENUM_19","유효하지 않은 PERSON_TYPE입니다."),
/**
* 401 Unauthorized, Token 관련 에러
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public OpenAPI openAPI() {
.bearerFormat(JWT)
);
return new OpenAPI()
.addServersItem(new Server().url("https://sssupport.shop"))
.addServersItem(new Server().url("https://dev.sssupport.shop"))
.components(new Components())
.info(new Info())
.addSecurityItem(securityRequirement)
Expand Down
Loading