Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…into feat/#25/정규-세션-비지니스-로직-개발
  • Loading branch information
koreaioi committed Nov 14, 2024
2 parents 47fab62 + 1bed76c commit 7988655
Show file tree
Hide file tree
Showing 22 changed files with 476 additions and 6 deletions.
6 changes: 6 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,22 @@ dependencies {
// jpa
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'

// mail
implementation 'org.springframework.boot:spring-boot-starter-mail'

// security
//implementation 'org.springframework.boot:spring-boot-starter-security'
//implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity6'
testImplementation 'org.springframework.security:spring-security-test'


// junit
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'

// db
runtimeOnly 'org.postgresql:postgresql'
runtimeOnly 'com.h2database:h2'


// s3
implementation 'org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.tave.tavewebsite.domain.member.controller;

import com.tave.tavewebsite.domain.member.dto.request.RegisterManagerRequestDto;
import com.tave.tavewebsite.domain.member.dto.response.CheckNickNameResponseDto;
import com.tave.tavewebsite.domain.member.dto.response.UnauthorizedManagerResponseDto;
import com.tave.tavewebsite.domain.member.service.MemberService;
import com.tave.tavewebsite.global.mail.dto.MailResponseDto;
import com.tave.tavewebsite.global.success.SuccessResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/manager")
@RequiredArgsConstructor
public class ManagerController {

private final MemberService memberService;

@PostMapping
public SuccessResponse<MailResponseDto> registerManager(@RequestBody @Valid RegisterManagerRequestDto requestDto) {
MailResponseDto response = memberService.saveMember(requestDto);
return new SuccessResponse<>(response);
}

@GetMapping("/unauthorized")
public SuccessResponse<List<UnauthorizedManagerResponseDto>> getUnauthorizedManager(){
List<UnauthorizedManagerResponseDto> response = memberService.getUnauthorizedManager();
return new SuccessResponse<>(response);
}

@GetMapping("/{nickName}")
public SuccessResponse<CheckNickNameResponseDto> checkNickName(@PathVariable String nickName){
memberService.validateNickname(nickName);
CheckNickNameResponseDto response = new CheckNickNameResponseDto(nickName);
return new SuccessResponse<>(response, nickName+" 사용가능합니다.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.tave.tavewebsite.domain.member.dto.request;

import com.tave.tavewebsite.domain.member.entity.DepartmentType;
import com.tave.tavewebsite.domain.member.entity.JobType;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;

public record RegisterManagerRequestDto(

@Email(message = "이메일 형식에 맞게 작성해주세요.")
String email,
@NotNull(message = "비밀번호를 입력하세요.")
@Pattern(regexp = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\W).+$",
message = "비밀번호는 8자 이상이어야 하며, 대문자, 소문자, 특수문자를 포함해야 합니다.")
String password,
@NotNull(message ="아이디를 입력해주세요")
String nickname,
@NotNull(message ="이름을 입력해주세요")
String username,
@NotNull(message ="아지트 아이디를 입력해주세요")
String agitId,
@NotNull(message = "본인 기수를 입력해주세요")
String generation,
@NotNull(message = "부서를 선택해주세요")
DepartmentType department,
@NotNull(message = "직책을 선택해주세요")
JobType job

) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.tave.tavewebsite.domain.member.dto.response;

public record CheckNickNameResponseDto(
String email
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.tave.tavewebsite.domain.member.dto.response;

import com.tave.tavewebsite.domain.member.entity.DepartmentType;
import com.tave.tavewebsite.domain.member.entity.JobType;
import com.tave.tavewebsite.domain.member.entity.Member;

public record UnauthorizedManagerResponseDto(

String username,
DepartmentType department,
JobType job,
String generation,
String agitId

) {
public static UnauthorizedManagerResponseDto fromEntity(Member member){
return new UnauthorizedManagerResponseDto(
member.getUsername(),
member.getDepartment(),
member.getJob(),
member.getGeneration(),
member.getAgitId()
);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.tave.tavewebsite.domain.member.entity;

import com.tave.tavewebsite.domain.member.dto.request.RegisterManagerRequestDto;
import com.tave.tavewebsite.global.common.BaseEntity;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.*;

import static com.tave.tavewebsite.domain.member.entity.RoleType.UNAUTHORIZED_MANAGER;

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
Expand All @@ -26,16 +29,19 @@ public class Member extends BaseEntity {
@Column(length = 100, nullable = false)
private String password;

@NotNull
@Size(min = 4, max = 10)
@Column(length = 10, nullable = false)
private String role;
@Enumerated(EnumType.STRING)
private RoleType role;

@NotNull
@Size(min = 2, max = 20)
@Column(length = 20, nullable = false)
private String nickname;

@NotNull
@Size(min = 2, max = 20)
@Column(length = 20, nullable = false)
private String username;

@Size(min = 1, max = 30)
@Column(length = 30)
private String agitId;
Expand All @@ -50,15 +56,32 @@ public class Member extends BaseEntity {
@Enumerated(EnumType.STRING)
private JobType job;


@Builder
public Member(String email, String password, String role, String nickname, String agitId, String generation, DepartmentType department, JobType job) {
public Member(String email, String password, RoleType role, String nickname, String username, String agitId, String generation, DepartmentType department, JobType job) {
this.email = email;
this.password = password;
this.role = role;
this.nickname = nickname;
this.username = username;
this.agitId = agitId;
this.generation = generation;
this.department = department;
this.job = job;
}

// 패스워드 인코딩 필요
public static Member toMember(RegisterManagerRequestDto registerManagerRequestDto) {
return Member.builder()
.email(registerManagerRequestDto.email())
.password(registerManagerRequestDto.password())
.agitId(registerManagerRequestDto.agitId())
.nickname(registerManagerRequestDto.nickname())
.username(registerManagerRequestDto.username())
.generation(registerManagerRequestDto.generation())
.role(UNAUTHORIZED_MANAGER)
.job(registerManagerRequestDto.job())
.department(registerManagerRequestDto.department())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.tave.tavewebsite.domain.member.entity;

public enum RoleType {
MEMBER,
MANAGER,
ADMIN,
UNAUTHORIZED_MANAGER
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.tave.tavewebsite.domain.member.exception;

import com.tave.tavewebsite.global.exception.BaseErrorException;

import static com.tave.tavewebsite.domain.member.exception.ErrorMessage.DUPLICATE_EMAIL;

public class DuplicateEmailException extends BaseErrorException {
public DuplicateEmailException() {
super(DUPLICATE_EMAIL.getCode(), DUPLICATE_EMAIL.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.tave.tavewebsite.domain.member.exception;

import com.tave.tavewebsite.global.exception.BaseErrorException;

import static com.tave.tavewebsite.domain.member.exception.ErrorMessage.DUPLICATE_NICKNAME;

public class DuplicateNicknameException extends BaseErrorException {
public DuplicateNicknameException() {
super(DUPLICATE_NICKNAME.getCode(), DUPLICATE_NICKNAME.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.tave.tavewebsite.domain.member.exception;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public enum ErrorMessage {

NOT_FOUNT_USER(400, "유저를 찾을 수 없습니다."),
DUPLICATE_EMAIL(400, "중복되는 이메일입니다."),
DUPLICATE_NICKNAME(400, "이미 사용중인 아이디입니다. 다른 아이디로 가입해주세요.");

final int code;
final String message;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.tave.tavewebsite.domain.member.exception;

import com.tave.tavewebsite.global.exception.BaseErrorException;

import static com.tave.tavewebsite.domain.member.exception.ErrorMessage.NOT_FOUNT_USER;

public class NotFoundMemberException extends BaseErrorException {
public NotFoundMemberException() {
super(NOT_FOUNT_USER.getCode(), NOT_FOUNT_USER.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.tave.tavewebsite.domain.member.memberRepository;

import com.tave.tavewebsite.domain.member.entity.Member;
import com.tave.tavewebsite.domain.member.entity.RoleType;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
import java.util.Optional;

public interface MemberRepository extends JpaRepository<Member, Long> {

Optional<Member> findByEmail(String email);

Optional<Member> findByNickname(String Nickname);

List<Member> findByRole(RoleType role);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.tave.tavewebsite.domain.member.service;

import com.tave.tavewebsite.domain.member.dto.request.RegisterManagerRequestDto;
import com.tave.tavewebsite.domain.member.dto.response.UnauthorizedManagerResponseDto;
import com.tave.tavewebsite.domain.member.entity.Member;
import com.tave.tavewebsite.domain.member.entity.RoleType;
import com.tave.tavewebsite.domain.member.exception.DuplicateEmailException;
import com.tave.tavewebsite.domain.member.exception.DuplicateNicknameException;
import com.tave.tavewebsite.domain.member.memberRepository.MemberRepository;
import com.tave.tavewebsite.global.mail.service.MailService;
import com.tave.tavewebsite.global.mail.dto.MailResponseDto;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static com.tave.tavewebsite.domain.member.entity.RoleType.UNAUTHORIZED_MANAGER;

@Service
@Transactional
@RequiredArgsConstructor
public class MemberService {

private final MemberRepository memberRepository;
private final MailService mailService;

public MailResponseDto saveMember(RegisterManagerRequestDto requestDto){

validateNickname(requestDto.nickname());
validateEmail(requestDto.email());

// PasswordEncoder 추가되면
// toMember에서 encode(requestDto.getPassword()) 추가하기
// 추가 후 위 주석은 삭제
Member saveMember = memberRepository.save(Member.toMember(requestDto));
return mailService.sendManagerRegisterMessage(saveMember.getEmail());
}

@Transactional(readOnly = true)
public List<UnauthorizedManagerResponseDto> getUnauthorizedManager(){
return memberRepository.findByRole(UNAUTHORIZED_MANAGER).stream()
.map(UnauthorizedManagerResponseDto::fromEntity)
.toList();
}

private void validateEmail(String email){
memberRepository.findByEmail(email).ifPresent(
member -> {throw new DuplicateEmailException();}
);
}

public void validateNickname(String nickname){
memberRepository.findByNickname(nickname).ifPresent(
member -> {throw new DuplicateNicknameException();}
);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.tave.tavewebsite.domain.session.entity.repository;


import com.tave.tavewebsite.domain.session.entity.Session;
import org.springframework.data.jpa.repository.JpaRepository;

public interface SessionRepository extends JpaRepository<Session, Long> {

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@


import com.tave.tavewebsite.global.exception.Response.ExceptionResponse;
import com.tave.tavewebsite.global.mail.exception.FailMailSendException;
import jakarta.mail.MessagingException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import java.io.UnsupportedEncodingException;

@Slf4j
@RestControllerAdvice
Expand All @@ -30,9 +33,21 @@ public ResponseEntity<ExceptionResponse<Void>> handle(BaseErrorException e) {
}

// @Valid 예외 처리 (@NotNull, @Size, etc...) or IllegalArgumentException
@ExceptionHandler({MethodArgumentNotValidException.class, IllegalArgumentException.class})
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<ExceptionResponse<Void>> handle(MethodArgumentNotValidException e) {

logWarning(e, ERROR_CODE);
ExceptionResponse<Void> response = ExceptionResponse.fail(ERROR_CODE, e.getBindingResult().getAllErrors().get(0).getDefaultMessage());

return ResponseEntity
.status(ERROR_CODE)
.body(response);
}

@ExceptionHandler({FailMailSendException.class, MessagingException.class, UnsupportedEncodingException.class})
public ResponseEntity<ExceptionResponse<Void>> handle(FailMailSendException e) {


logWarning(e, ERROR_CODE);
ExceptionResponse<Void> response = ExceptionResponse.fail(ERROR_CODE, e.getMessage());

Expand Down
Loading

0 comments on commit 7988655

Please sign in to comment.