diff --git a/src/main/java/com/likelion/innerjoin/InnerjoinApplication.java b/src/main/java/com/likelion/innerjoin/InnerjoinApplication.java index 65edbe3..b1d5d2d 100644 --- a/src/main/java/com/likelion/innerjoin/InnerjoinApplication.java +++ b/src/main/java/com/likelion/innerjoin/InnerjoinApplication.java @@ -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) { diff --git a/src/main/java/com/likelion/innerjoin/common/exception/ErrorCode.java b/src/main/java/com/likelion/innerjoin/common/exception/ErrorCode.java index 085499c..67411a7 100644 --- a/src/main/java/com/likelion/innerjoin/common/exception/ErrorCode.java +++ b/src/main/java/com/likelion/innerjoin/common/exception/ErrorCode.java @@ -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; diff --git a/src/main/java/com/likelion/innerjoin/post/controller/FormController.java b/src/main/java/com/likelion/innerjoin/post/controller/FormController.java new file mode 100644 index 0000000..181b8bd --- /dev/null +++ b/src/main/java/com/likelion/innerjoin/post/controller/FormController.java @@ -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 createPost(@RequestBody FormRequestDto formRequestDto, HttpSession session) { + return new CommonResponse<>(formService.createForm(formRequestDto, session)); + } + + @GetMapping("") + public CommonResponse> getPostList(HttpSession session) { + return new CommonResponse<>(formService.getPostList(session)); + } + + +} diff --git a/src/main/java/com/likelion/innerjoin/post/exception/FormExceptionHandler.java b/src/main/java/com/likelion/innerjoin/post/exception/FormExceptionHandler.java new file mode 100644 index 0000000..5edb196 --- /dev/null +++ b/src/main/java/com/likelion/innerjoin/post/exception/FormExceptionHandler.java @@ -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); + } +} diff --git a/src/main/java/com/likelion/innerjoin/post/exception/JsonConvertException.java b/src/main/java/com/likelion/innerjoin/post/exception/JsonConvertException.java new file mode 100644 index 0000000..8d6a7d3 --- /dev/null +++ b/src/main/java/com/likelion/innerjoin/post/exception/JsonConvertException.java @@ -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; +} diff --git a/src/main/java/com/likelion/innerjoin/post/exception/JsonExceptionHandler.java b/src/main/java/com/likelion/innerjoin/post/exception/JsonExceptionHandler.java new file mode 100644 index 0000000..070f648 --- /dev/null +++ b/src/main/java/com/likelion/innerjoin/post/exception/JsonExceptionHandler.java @@ -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); + } +} diff --git a/src/main/java/com/likelion/innerjoin/post/exception/UnauthorizedException.java b/src/main/java/com/likelion/innerjoin/post/exception/UnauthorizedException.java new file mode 100644 index 0000000..2cf699d --- /dev/null +++ b/src/main/java/com/likelion/innerjoin/post/exception/UnauthorizedException.java @@ -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; +} diff --git a/src/main/java/com/likelion/innerjoin/post/model/dto/request/FormRequestDto.java b/src/main/java/com/likelion/innerjoin/post/model/dto/request/FormRequestDto.java new file mode 100644 index 0000000..01261a6 --- /dev/null +++ b/src/main/java/com/likelion/innerjoin/post/model/dto/request/FormRequestDto.java @@ -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 questionList; +} diff --git a/src/main/java/com/likelion/innerjoin/post/model/dto/request/QuestionRequestDto.java b/src/main/java/com/likelion/innerjoin/post/model/dto/request/QuestionRequestDto.java new file mode 100644 index 0000000..2468a38 --- /dev/null +++ b/src/main/java/com/likelion/innerjoin/post/model/dto/request/QuestionRequestDto.java @@ -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 list; +} diff --git a/src/main/java/com/likelion/innerjoin/post/model/dto/response/FormListResponseDto.java b/src/main/java/com/likelion/innerjoin/post/model/dto/response/FormListResponseDto.java new file mode 100644 index 0000000..44a3f11 --- /dev/null +++ b/src/main/java/com/likelion/innerjoin/post/model/dto/response/FormListResponseDto.java @@ -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; +} diff --git a/src/main/java/com/likelion/innerjoin/post/model/dto/response/FormResponseDto.java b/src/main/java/com/likelion/innerjoin/post/model/dto/response/FormResponseDto.java new file mode 100644 index 0000000..f837d6a --- /dev/null +++ b/src/main/java/com/likelion/innerjoin/post/model/dto/response/FormResponseDto.java @@ -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 questionList; +} diff --git a/src/main/java/com/likelion/innerjoin/post/model/dto/response/QuestionResponseDto.java b/src/main/java/com/likelion/innerjoin/post/model/dto/response/QuestionResponseDto.java new file mode 100644 index 0000000..10b8f32 --- /dev/null +++ b/src/main/java/com/likelion/innerjoin/post/model/dto/response/QuestionResponseDto.java @@ -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 list; +} diff --git a/src/main/java/com/likelion/innerjoin/post/model/entity/Question.java b/src/main/java/com/likelion/innerjoin/post/model/entity/Question.java index 49a394b..5096e45 100644 --- a/src/main/java/com/likelion/innerjoin/post/model/entity/Question.java +++ b/src/main/java/com/likelion/innerjoin/post/model/entity/Question.java @@ -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") @@ -31,4 +34,7 @@ public class Question extends DataEntity { private Long number; private String content; + + @Convert(converter = JsonConverter.class) + private List list; } diff --git a/src/main/java/com/likelion/innerjoin/post/model/mapper/FormMapper.java b/src/main/java/com/likelion/innerjoin/post/model/mapper/FormMapper.java new file mode 100644 index 0000000..249772a --- /dev/null +++ b/src/main/java/com/likelion/innerjoin/post/model/mapper/FormMapper.java @@ -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; + } +} diff --git a/src/main/java/com/likelion/innerjoin/post/model/mapper/QuestionMapper.java b/src/main/java/com/likelion/innerjoin/post/model/mapper/QuestionMapper.java new file mode 100644 index 0000000..9331942 --- /dev/null +++ b/src/main/java/com/likelion/innerjoin/post/model/mapper/QuestionMapper.java @@ -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 toQuestionResponseDtoList(List questions) { + List questionResponseDtoList = new ArrayList<>(); + for (Question question : questions) { + questionResponseDtoList.add(toQuestionResponseDto(question)); + } + return questionResponseDtoList; + } +} diff --git a/src/main/java/com/likelion/innerjoin/post/repository/FormRepository.java b/src/main/java/com/likelion/innerjoin/post/repository/FormRepository.java new file mode 100644 index 0000000..4d2be88 --- /dev/null +++ b/src/main/java/com/likelion/innerjoin/post/repository/FormRepository.java @@ -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 { + List
findAllByClub(Club club); +} diff --git a/src/main/java/com/likelion/innerjoin/post/service/FormService.java b/src/main/java/com/likelion/innerjoin/post/service/FormService.java new file mode 100644 index 0000000..ad2c348 --- /dev/null +++ b/src/main/java/com/likelion/innerjoin/post/service/FormService.java @@ -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 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 getPostList(HttpSession session){ + User user = sessionVerifier.verifySession(session); + if(!(user instanceof Club club)){ + throw new UnauthorizedException("권한이 없습니다."); + } + List formList = formRepository.findAllByClub(club); + List 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; + } +} diff --git a/src/main/java/com/likelion/innerjoin/post/util/JsonConverter.java b/src/main/java/com/likelion/innerjoin/post/util/JsonConverter.java new file mode 100644 index 0000000..7fa8237 --- /dev/null +++ b/src/main/java/com/likelion/innerjoin/post/util/JsonConverter.java @@ -0,0 +1,34 @@ +package com.likelion.innerjoin.post.util; + + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.likelion.innerjoin.post.exception.JsonConvertException; +import jakarta.persistence.AttributeConverter; +import jakarta.persistence.Converter; + +import java.util.List; + +@Converter +public class JsonConverter implements AttributeConverter, String> { + private final ObjectMapper objectMapper = new ObjectMapper(); + + @Override + public String convertToDatabaseColumn(List attribute) { + try{ + return objectMapper.writeValueAsString(attribute); + } catch (Exception e){ + throw new JsonConvertException("Json 파싱 과정에서 오류가 발생하였습니다."); + } + } + + @Override + public List convertToEntityAttribute(String dbData) { + try{ + return objectMapper.readValue(dbData, new TypeReference<>() { + }); + } catch (Exception e){ + throw new JsonConvertException("Json 파싱 과정에서 오류가 발생하였습니다."); + } + } +} diff --git a/src/main/java/com/likelion/innerjoin/user/controller/LoginController.java b/src/main/java/com/likelion/innerjoin/user/controller/LoginController.java index 6281d7b..186c97f 100644 --- a/src/main/java/com/likelion/innerjoin/user/controller/LoginController.java +++ b/src/main/java/com/likelion/innerjoin/user/controller/LoginController.java @@ -2,26 +2,38 @@ import com.likelion.innerjoin.common.response.CommonResponse; -import com.likelion.innerjoin.user.model.dto.request.LoginRequestDto; -import com.likelion.innerjoin.user.model.dto.response.LoginResponseDto; +import com.likelion.innerjoin.user.model.dto.request.ApplicantLoginRequestDto; +import com.likelion.innerjoin.user.model.dto.request.ClubLoginRequestDto; +import com.likelion.innerjoin.user.model.dto.response.ApplicantLoginResponseDto; +import com.likelion.innerjoin.user.model.dto.response.ClubLoginResponseDto; import com.likelion.innerjoin.user.service.LoginService; import jakarta.servlet.http.HttpSession; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController -@RequestMapping("/user") @RequiredArgsConstructor @Slf4j public class LoginController { private final LoginService loginService; - @PostMapping("/login") - public CommonResponse login(@RequestBody LoginRequestDto loginRequestDto, HttpSession session) { - return new CommonResponse<>(loginService.login(loginRequestDto, session)); + @PostMapping("/user/login") + public CommonResponse applicantLogin(@RequestBody ApplicantLoginRequestDto applicantLoginRequestDto, HttpSession session) { + return new CommonResponse<>(loginService.applicantLogin(applicantLoginRequestDto, session)); + } + + @PostMapping("/club/login") + public CommonResponse clubLogin(@RequestBody ClubLoginRequestDto clubLoginRequestDto, HttpSession session) { + return new CommonResponse<>(loginService.clubLogin(clubLoginRequestDto, session)); + } + + @GetMapping("/logout") + public CommonResponse logout(HttpSession session) { + session.invalidate(); + return new CommonResponse<>("로그아웃되었습니다."); } } diff --git a/src/main/java/com/likelion/innerjoin/user/model/dto/request/LoginRequestDto.java b/src/main/java/com/likelion/innerjoin/user/model/dto/request/ApplicantLoginRequestDto.java similarity index 86% rename from src/main/java/com/likelion/innerjoin/user/model/dto/request/LoginRequestDto.java rename to src/main/java/com/likelion/innerjoin/user/model/dto/request/ApplicantLoginRequestDto.java index 25f0aee..38581ad 100644 --- a/src/main/java/com/likelion/innerjoin/user/model/dto/request/LoginRequestDto.java +++ b/src/main/java/com/likelion/innerjoin/user/model/dto/request/ApplicantLoginRequestDto.java @@ -8,7 +8,7 @@ @Getter @NoArgsConstructor @AllArgsConstructor -public class LoginRequestDto { +public class ApplicantLoginRequestDto { private String email; private String password; } diff --git a/src/main/java/com/likelion/innerjoin/user/model/dto/request/ClubLoginRequestDto.java b/src/main/java/com/likelion/innerjoin/user/model/dto/request/ClubLoginRequestDto.java new file mode 100644 index 0000000..78d1eea --- /dev/null +++ b/src/main/java/com/likelion/innerjoin/user/model/dto/request/ClubLoginRequestDto.java @@ -0,0 +1,11 @@ +package com.likelion.innerjoin.user.model.dto.request; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public class ClubLoginRequestDto { + private String id; + private String password; +} diff --git a/src/main/java/com/likelion/innerjoin/user/model/dto/response/LoginResponseDto.java b/src/main/java/com/likelion/innerjoin/user/model/dto/response/ApplicantLoginResponseDto.java similarity index 87% rename from src/main/java/com/likelion/innerjoin/user/model/dto/response/LoginResponseDto.java rename to src/main/java/com/likelion/innerjoin/user/model/dto/response/ApplicantLoginResponseDto.java index 994359f..8e5a6aa 100644 --- a/src/main/java/com/likelion/innerjoin/user/model/dto/response/LoginResponseDto.java +++ b/src/main/java/com/likelion/innerjoin/user/model/dto/response/ApplicantLoginResponseDto.java @@ -10,7 +10,7 @@ @Setter @NoArgsConstructor @AllArgsConstructor -public class LoginResponseDto { +public class ApplicantLoginResponseDto { String studentNumber; String email; String name; diff --git a/src/main/java/com/likelion/innerjoin/user/model/dto/response/ClubLoginResponseDto.java b/src/main/java/com/likelion/innerjoin/user/model/dto/response/ClubLoginResponseDto.java new file mode 100644 index 0000000..2007dae --- /dev/null +++ b/src/main/java/com/likelion/innerjoin/user/model/dto/response/ClubLoginResponseDto.java @@ -0,0 +1,13 @@ +package com.likelion.innerjoin.user.model.dto.response; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +@AllArgsConstructor +public class ClubLoginResponseDto { + private String name; + private String email; +} diff --git a/src/main/java/com/likelion/innerjoin/user/model/entity/Applicant.java b/src/main/java/com/likelion/innerjoin/user/model/entity/Applicant.java index 0a3b9b0..cadff5a 100644 --- a/src/main/java/com/likelion/innerjoin/user/model/entity/Applicant.java +++ b/src/main/java/com/likelion/innerjoin/user/model/entity/Applicant.java @@ -16,7 +16,7 @@ @Builder @AllArgsConstructor @NoArgsConstructor -public class Applicant extends DataEntity { +public class Applicant extends DataEntity implements User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "applicant_id") diff --git a/src/main/java/com/likelion/innerjoin/user/model/entity/Club.java b/src/main/java/com/likelion/innerjoin/user/model/entity/Club.java index 4bb227d..98eb5d4 100644 --- a/src/main/java/com/likelion/innerjoin/user/model/entity/Club.java +++ b/src/main/java/com/likelion/innerjoin/user/model/entity/Club.java @@ -9,6 +9,7 @@ import lombok.Data; import lombok.NoArgsConstructor; +import java.io.Serializable; import java.util.List; @@ -18,7 +19,7 @@ @Builder @AllArgsConstructor @NoArgsConstructor -public class Club extends DataEntity { +public class Club extends DataEntity implements User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "club_id") diff --git a/src/main/java/com/likelion/innerjoin/user/model/entity/User.java b/src/main/java/com/likelion/innerjoin/user/model/entity/User.java new file mode 100644 index 0000000..d3c932c --- /dev/null +++ b/src/main/java/com/likelion/innerjoin/user/model/entity/User.java @@ -0,0 +1,5 @@ +package com.likelion.innerjoin.user.model.entity; + +public interface User { + +} diff --git a/src/main/java/com/likelion/innerjoin/user/repository/ClubRepository.java b/src/main/java/com/likelion/innerjoin/user/repository/ClubRepository.java new file mode 100644 index 0000000..9820bcc --- /dev/null +++ b/src/main/java/com/likelion/innerjoin/user/repository/ClubRepository.java @@ -0,0 +1,8 @@ +package com.likelion.innerjoin.user.repository; + +import com.likelion.innerjoin.user.model.entity.Club; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface ClubRepository extends JpaRepository { + Club findByLoginIdAndPassword(String loginId, String password); +} diff --git a/src/main/java/com/likelion/innerjoin/user/service/LoginService.java b/src/main/java/com/likelion/innerjoin/user/service/LoginService.java index 788ba00..cafc06f 100644 --- a/src/main/java/com/likelion/innerjoin/user/service/LoginService.java +++ b/src/main/java/com/likelion/innerjoin/user/service/LoginService.java @@ -2,10 +2,14 @@ import com.likelion.innerjoin.user.exception.ApplicantNotFoundException; -import com.likelion.innerjoin.user.model.dto.request.LoginRequestDto; -import com.likelion.innerjoin.user.model.dto.response.LoginResponseDto; +import com.likelion.innerjoin.user.model.dto.request.ApplicantLoginRequestDto; +import com.likelion.innerjoin.user.model.dto.request.ClubLoginRequestDto; +import com.likelion.innerjoin.user.model.dto.response.ApplicantLoginResponseDto; +import com.likelion.innerjoin.user.model.dto.response.ClubLoginResponseDto; import com.likelion.innerjoin.user.model.entity.Applicant; +import com.likelion.innerjoin.user.model.entity.Club; import com.likelion.innerjoin.user.repository.ApplicantRepository; +import com.likelion.innerjoin.user.repository.ClubRepository; import jakarta.servlet.http.HttpSession; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -16,20 +20,40 @@ @RequiredArgsConstructor public class LoginService { public final ApplicantRepository applicantRepository; + public final ClubRepository clubRepository; - public LoginResponseDto login(LoginRequestDto loginRequestDto, HttpSession session) { + public ApplicantLoginResponseDto applicantLogin(ApplicantLoginRequestDto applicantLoginRequestDto, HttpSession session) { try{ Applicant applicant = applicantRepository.findByEmailAndPassword( - loginRequestDto.getEmail(), - loginRequestDto.getPassword() + applicantLoginRequestDto.getEmail(), + applicantLoginRequestDto.getPassword() ); if(applicant == null) {throw new ApplicantNotFoundException("아이디나 비밀번호가 잘못되었습니다.");} session.setAttribute("userId", applicant.getId()); session.setAttribute("role", "applicant"); - return new LoginResponseDto(applicant.getStudentNumber(), applicant.getEmail(), applicant.getName()); + return new ApplicantLoginResponseDto(applicant.getStudentNumber(), applicant.getEmail(), applicant.getName()); + }catch (NoSuchElementException e){ + throw new ApplicantNotFoundException("아이디나 비밀번호가 잘못되었습니다."); + } + } + + public ClubLoginResponseDto clubLogin(ClubLoginRequestDto clubLoginRequestDto, HttpSession session) { + try{ + Club club = + clubRepository.findByLoginIdAndPassword( + clubLoginRequestDto.getId(), + clubLoginRequestDto.getPassword() + ); + + if(club == null) {throw new ApplicantNotFoundException("아이디나 비밀번호가 잘못되었습니다.");} + + session.setAttribute("userId", club.getId()); + session.setAttribute("role", "club"); + + return new ClubLoginResponseDto(club.getLoginId(), club.getEmail()); }catch (NoSuchElementException e){ throw new ApplicantNotFoundException("아이디나 비밀번호가 잘못되었습니다."); } diff --git a/src/main/java/com/likelion/innerjoin/user/util/SessionVerifier.java b/src/main/java/com/likelion/innerjoin/user/util/SessionVerifier.java new file mode 100644 index 0000000..850ead0 --- /dev/null +++ b/src/main/java/com/likelion/innerjoin/user/util/SessionVerifier.java @@ -0,0 +1,43 @@ +package com.likelion.innerjoin.user.util; + +import com.likelion.innerjoin.post.exception.UnauthorizedException; +import com.likelion.innerjoin.user.model.entity.User; +import com.likelion.innerjoin.user.repository.ApplicantRepository; +import com.likelion.innerjoin.user.repository.ClubRepository; +import jakarta.servlet.http.HttpSession; +import lombok.AllArgsConstructor; +import org.springframework.stereotype.Component; + +@Component +@AllArgsConstructor +public class SessionVerifier { + private final ClubRepository clubRepository; + private final ApplicantRepository applicantRepository; + + public User verifySession(HttpSession session) { + if( session == null ){ + throw new UnauthorizedException("잘못된 접근입니다."); + } + + if( session.getAttribute("userId") == null || session.getAttribute("role") == null ){ + throw new UnauthorizedException("잘못된 접근입니다."); + } + + Integer userIdInteger = (Integer) session.getAttribute("userId"); // Integer로 가져오기 + Long userId = userIdInteger != null ? userIdInteger.longValue() : null; // Long으로 변환 + + String role = (String) session.getAttribute("role"); + + if( userId == null ){ + throw new UnauthorizedException("잘못된 유저입니다."); + } + + if( role.equals("club") ){ + return clubRepository.findById(userId).orElseThrow(() -> new UnauthorizedException("잘못된 유저입니다.")); + }else if ( role.equals("applicant") ){ + return applicantRepository.findById(userId).orElseThrow(() -> new UnauthorizedException("잘못된 유저입니다.")); + }else { + throw new UnauthorizedException("잘못된 유저입니다."); + } + } +}