-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
29 changed files
with
516 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/main/java/com/likelion/innerjoin/post/controller/FormController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.likelion.innerjoin.post.controller; | ||
|
||
import com.likelion.innerjoin.common.response.CommonResponse; | ||
import com.likelion.innerjoin.post.model.dto.request.FormRequestDto; | ||
import com.likelion.innerjoin.post.model.dto.response.FormListResponseDto; | ||
import com.likelion.innerjoin.post.model.dto.response.FormResponseDto; | ||
import com.likelion.innerjoin.post.service.FormService; | ||
import jakarta.servlet.http.HttpSession; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.List; | ||
|
||
@RestController | ||
@RequestMapping("/form") | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class FormController { | ||
private final FormService formService; | ||
|
||
@PostMapping("") | ||
public CommonResponse<FormResponseDto> createPost(@RequestBody FormRequestDto formRequestDto, HttpSession session) { | ||
return new CommonResponse<>(formService.createForm(formRequestDto, session)); | ||
} | ||
|
||
@GetMapping("") | ||
public CommonResponse<List<FormListResponseDto>> getPostList(HttpSession session) { | ||
return new CommonResponse<>(formService.getPostList(session)); | ||
} | ||
|
||
|
||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/likelion/innerjoin/post/exception/FormExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.likelion.innerjoin.post.exception; | ||
|
||
import com.likelion.innerjoin.common.exception.ErrorCode; | ||
import com.likelion.innerjoin.common.response.CommonResponse; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
@Slf4j | ||
@RestControllerAdvice | ||
public class FormExceptionHandler { | ||
@ExceptionHandler(UnauthorizedException.class) | ||
@ResponseStatus(HttpStatus.UNAUTHORIZED) | ||
public CommonResponse<?> wrongSession(UnauthorizedException e, HttpServletRequest request) { | ||
log.warn("FORM-001> 요청 URI: " + request.getRequestURI() + ", 에러 메세지: " + e.getMessage()); | ||
return new CommonResponse<>(ErrorCode.WRONG_SESSION_ERROR); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/likelion/innerjoin/post/exception/JsonConvertException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.likelion.innerjoin.post.exception; | ||
|
||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public class JsonConvertException extends RuntimeException { | ||
private String message; | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/likelion/innerjoin/post/exception/JsonExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.likelion.innerjoin.post.exception; | ||
|
||
import com.likelion.innerjoin.common.exception.ErrorCode; | ||
import com.likelion.innerjoin.common.response.CommonResponse; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
@Slf4j | ||
@RestControllerAdvice | ||
public class JsonExceptionHandler { | ||
@ExceptionHandler(JsonConvertException.class) | ||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) | ||
public CommonResponse<?> jsonConvertError(JsonConvertException e, HttpServletRequest request) { | ||
log.warn("JSON-001> 요청 URI: " + request.getRequestURI() + ", 에러 메세지: " + e.getMessage()); | ||
return new CommonResponse<>(ErrorCode.JSON_CONVERT_ERROR); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/likelion/innerjoin/post/exception/UnauthorizedException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.likelion.innerjoin.post.exception; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public class UnauthorizedException extends RuntimeException { | ||
private String message; | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/com/likelion/innerjoin/post/model/dto/request/FormRequestDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.likelion.innerjoin.post.model.dto.request; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
import java.util.List; | ||
|
||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class FormRequestDto { | ||
private String title; | ||
private String description; | ||
private List<QuestionRequestDto> questionList; | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/com/likelion/innerjoin/post/model/dto/request/QuestionRequestDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.likelion.innerjoin.post.model.dto.request; | ||
|
||
import com.likelion.innerjoin.post.model.entity.QuestionType; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
import java.util.List; | ||
|
||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class QuestionRequestDto { | ||
private Long number; | ||
private String question; | ||
private QuestionType type; | ||
private List<String> list; | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/likelion/innerjoin/post/model/dto/response/FormListResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.likelion.innerjoin.post.model.dto.response; | ||
|
||
import com.fasterxml.jackson.annotation.JsonFormat; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
@Getter | ||
@Setter | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class FormListResponseDto { | ||
private Long id; | ||
private String title; | ||
private String description; | ||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") | ||
private LocalDateTime modifiedAt; | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/likelion/innerjoin/post/model/dto/response/FormResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.likelion.innerjoin.post.model.dto.response; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
import java.util.List; | ||
|
||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class FormResponseDto { | ||
private Long id; | ||
private String title; | ||
private String description; | ||
private List<QuestionResponseDto> questionList; | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/likelion/innerjoin/post/model/dto/response/QuestionResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.likelion.innerjoin.post.model.dto.response; | ||
|
||
import com.likelion.innerjoin.post.model.entity.QuestionType; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
import java.util.List; | ||
|
||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class QuestionResponseDto { | ||
private Long questionId; | ||
private Long number; | ||
private String question; | ||
private QuestionType type; | ||
private List<String> list; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/main/java/com/likelion/innerjoin/post/model/mapper/FormMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.likelion.innerjoin.post.model.mapper; | ||
|
||
import com.likelion.innerjoin.post.model.dto.response.FormResponseDto; | ||
import com.likelion.innerjoin.post.model.entity.Form; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class FormMapper { | ||
private final QuestionMapper questionMapper; | ||
|
||
public FormResponseDto toFormResponseDto(Form form) { | ||
FormResponseDto formResponseDto = new FormResponseDto(); | ||
formResponseDto.setId(form.getId()); | ||
formResponseDto.setTitle(form.getTitle()); | ||
formResponseDto.setDescription(form.getDescription()); | ||
formResponseDto.setQuestionList(questionMapper.toQuestionResponseDtoList(form.getQuestionList())); | ||
return formResponseDto; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/com/likelion/innerjoin/post/model/mapper/QuestionMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.likelion.innerjoin.post.model.mapper; | ||
|
||
import com.likelion.innerjoin.post.model.dto.response.QuestionResponseDto; | ||
import com.likelion.innerjoin.post.model.entity.Question; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Component | ||
public class QuestionMapper { | ||
public QuestionResponseDto toQuestionResponseDto(Question question) { | ||
QuestionResponseDto questionResponseDto = new QuestionResponseDto(); | ||
questionResponseDto.setQuestionId(question.getId()); | ||
questionResponseDto.setNumber(question.getNumber()); | ||
questionResponseDto.setQuestion(question.getContent()); | ||
questionResponseDto.setType(question.getQuestionType()); | ||
questionResponseDto.setList(question.getList()); | ||
return questionResponseDto; | ||
} | ||
|
||
public List<QuestionResponseDto> toQuestionResponseDtoList(List<Question> questions) { | ||
List<QuestionResponseDto> questionResponseDtoList = new ArrayList<>(); | ||
for (Question question : questions) { | ||
questionResponseDtoList.add(toQuestionResponseDto(question)); | ||
} | ||
return questionResponseDtoList; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/likelion/innerjoin/post/repository/FormRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.likelion.innerjoin.post.repository; | ||
|
||
import com.likelion.innerjoin.post.model.entity.Form; | ||
import com.likelion.innerjoin.user.model.entity.Club; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.List; | ||
|
||
public interface FormRepository extends JpaRepository<Form, Long> { | ||
List<Form> findAllByClub(Club club); | ||
} |
82 changes: 82 additions & 0 deletions
82
src/main/java/com/likelion/innerjoin/post/service/FormService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package com.likelion.innerjoin.post.service; | ||
|
||
import com.likelion.innerjoin.post.exception.UnauthorizedException; | ||
import com.likelion.innerjoin.post.model.dto.request.FormRequestDto; | ||
import com.likelion.innerjoin.post.model.dto.request.QuestionRequestDto; | ||
import com.likelion.innerjoin.post.model.dto.response.FormListResponseDto; | ||
import com.likelion.innerjoin.post.model.dto.response.FormResponseDto; | ||
import com.likelion.innerjoin.post.model.entity.Form; | ||
import com.likelion.innerjoin.post.model.entity.Question; | ||
import com.likelion.innerjoin.post.model.mapper.FormMapper; | ||
import com.likelion.innerjoin.post.repository.FormRepository; | ||
import com.likelion.innerjoin.user.model.entity.Club; | ||
import com.likelion.innerjoin.user.model.entity.User; | ||
import com.likelion.innerjoin.user.repository.ClubRepository; | ||
import com.likelion.innerjoin.user.util.SessionVerifier; | ||
import jakarta.servlet.http.HttpSession; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class FormService { | ||
private final ClubRepository clubRepository; | ||
private final FormRepository formRepository; | ||
private final FormMapper formMapper; | ||
private final SessionVerifier sessionVerifier; | ||
|
||
public FormResponseDto createForm(FormRequestDto formRequestDto, HttpSession session) { | ||
|
||
User user = sessionVerifier.verifySession(session); | ||
if(!(user instanceof Club club)){ | ||
throw new UnauthorizedException("권한이 없습니다."); | ||
} | ||
|
||
List<Question> questionList = new ArrayList<>(); | ||
|
||
if(formRequestDto.getQuestionList() != null){ | ||
for( QuestionRequestDto question : formRequestDto.getQuestionList() ){ | ||
questionList.add( | ||
Question.builder() | ||
.number(question.getNumber()) | ||
.content(question.getQuestion()) | ||
.questionType(question.getType()) | ||
.list(question.getList()) | ||
.build() | ||
); | ||
} | ||
} | ||
|
||
Form form = formRepository.save( | ||
Form.builder() | ||
.club(club) | ||
.questionList(questionList) | ||
.title(formRequestDto.getTitle()) | ||
.description(formRequestDto.getDescription()) | ||
.build() | ||
); | ||
|
||
return formMapper.toFormResponseDto(form); | ||
} | ||
|
||
public List<FormListResponseDto> getPostList(HttpSession session){ | ||
User user = sessionVerifier.verifySession(session); | ||
if(!(user instanceof Club club)){ | ||
throw new UnauthorizedException("권한이 없습니다."); | ||
} | ||
List<Form> formList = formRepository.findAllByClub(club); | ||
List<FormListResponseDto> formListResponseDtoList = new ArrayList<>(); | ||
for( Form form : formList ){ | ||
FormListResponseDto formListResponseDto = new FormListResponseDto(); | ||
formListResponseDto.setId(form.getId()); | ||
formListResponseDto.setTitle(form.getTitle()); | ||
formListResponseDto.setDescription(form.getDescription()); | ||
formListResponseDto.setModifiedAt(form.getModifiedAt()); | ||
formListResponseDtoList.add(formListResponseDto); | ||
} | ||
return formListResponseDtoList; | ||
} | ||
} |
Oops, something went wrong.