Skip to content

Commit

Permalink
Merge pull request #10 from Likelion-Inner-Join/feat/form
Browse files Browse the repository at this point in the history
[Feat] form 생성 및 조회 추가 및 로그아웃, 동아리 로그인 추가
  • Loading branch information
luna156 authored Dec 7, 2024
2 parents 3aa90a4 + bd9e4bb commit ff60b2c
Show file tree
Hide file tree
Showing 29 changed files with 517 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

@SpringBootApplication
@EnableJpaAuditing
public class InnerjoinApplication {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ public enum ErrorCode {
APPLICANT_NOT_FOUNT(false, HttpStatus.UNAUTHORIZED.value(), "이메일 또는 비밀번호가 잘못되었습니다"),

//error
INTERNAL_SERVER_ERROR(false,HttpStatus.INTERNAL_SERVER_ERROR.value(), "서버 내부에서 문제가 발생했습니다.")
INTERNAL_SERVER_ERROR(false,HttpStatus.INTERNAL_SERVER_ERROR.value(), "서버 내부에서 문제가 발생했습니다."),
WRONG_SESSION_ERROR(false, HttpStatus.UNAUTHORIZED.value(), "세션값이 잘못되었습니다."),
JSON_CONVERT_ERROR(false, HttpStatus.INTERNAL_SERVER_ERROR.value(), "JSON 변환중 오류가 발생하였습니다.")
;

private final Boolean isSuccess;
Expand Down
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));
}


}
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);
}
}
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;
}
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);
}
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.likelion.innerjoin.post.model.entity;

import com.likelion.innerjoin.common.entity.DataEntity;
import com.likelion.innerjoin.post.util.JsonConverter;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;


@Entity
@Table(name = "question")
Expand All @@ -31,4 +34,7 @@ public class Question extends DataEntity {
private Long number;

private String content;

@Convert(converter = JsonConverter.class)
private List<String> list;
}
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;
}
}
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;
}
}
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 src/main/java/com/likelion/innerjoin/post/service/FormService.java
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;
}
}
Loading

0 comments on commit ff60b2c

Please sign in to comment.