Skip to content

Commit

Permalink
Merge pull request #34 from GApple-T/handle-exception
Browse files Browse the repository at this point in the history
πŸ”€::νšŒμ›κ°€μž… 였λ₯˜ 핸듀링 μΆ”κ°€
  • Loading branch information
enbraining authored Dec 19, 2023
2 parents 84537a6 + 51d7f8f commit 899b2a6
Show file tree
Hide file tree
Showing 17 changed files with 212 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.gapple.weeingback.domain.auth.service.impl;
package com.gapple.weeingback.domain.auth.service.implementation;

import com.gapple.weeingback.domain.auth.dto.*;
import com.gapple.weeingback.domain.auth.service.AuthService;
import com.gapple.weeingback.domain.member.entity.AccessRole;
import com.gapple.weeingback.domain.member.entity.Member;
import com.gapple.weeingback.domain.member.repository.MemberRepository;
import com.gapple.weeingback.global.email.service.impl.EmailServiceImpl;
import com.gapple.weeingback.global.email.service.EmailService;
import com.gapple.weeingback.global.exception.MemberExistsException;
import com.gapple.weeingback.global.exception.MemberNotFoundException;
import com.gapple.weeingback.global.exception.PasswordNotMatchException;
import com.gapple.weeingback.global.jwt.JwtProvider;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -15,15 +18,14 @@
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
@Slf4j
public class AuthServiceImpl implements AuthService {
private final EmailServiceImpl emailService;
private final EmailService emailService;
private final MemberRepository memberRepository;
private final PasswordEncoder passwordEncoder;
private final JwtProvider jwtProvider;
Expand All @@ -41,31 +43,33 @@ public ResponseEntity<AuthJoinResponse> join(AuthJoinRequest req){

memberRepository.save(member);
return ResponseEntity.ok(new AuthJoinResponse("ok"));
} else throw new RuntimeException();
} else throw new MemberExistsException();
}

@Transactional(rollbackFor = RuntimeException.class)
@Transactional(readOnly = true)
public ResponseEntity<AuthLoginResponse> login(AuthLoginRequest request){
Member member = memberRepository.findMemberByEmail(request.getEmail())
.orElseThrow(RuntimeException::new);
.orElseThrow(MemberNotFoundException::new);

if(passwordEncoder.matches(request.getPassword(), member.getPassword())){
String id = member.getId().toString();
String password = member.getPassword();
if(!passwordEncoder.matches(request.getPassword(), member.getPassword()))
throw new PasswordNotMatchException();

List<AccessRole> roles = new ArrayList<>();
roles.add(AccessRole.valueOf(member.getRole()));
String id = member.getId().toString();
String password = member.getPassword();

Authentication authentication =
List<AccessRole> roles = new ArrayList<>();
roles.add(AccessRole.valueOf(member.getRole()));

Authentication authentication =
new UsernamePasswordAuthenticationToken(id, password, roles);
String access = jwtProvider.generateAccessToken(authentication);
String refresh = jwtProvider.generateRefreshToken(authentication);
return ResponseEntity.ok(new AuthLoginResponse(access, refresh, "ok"));
} else throw new IllegalArgumentException();
String access = jwtProvider.generateAccessToken(authentication);
String refresh = jwtProvider.generateRefreshToken(authentication);

return ResponseEntity.ok(new AuthLoginResponse(access, refresh, "ok"));
}

@Override
public ResponseEntity<String> sendAuth(EmailCertifyRequest request) {
return ResponseEntity.ok(emailService.sendMail(request.getEmail()));
return ResponseEntity.ok(emailService.sendAuth(request.getEmail()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@

@Repository
public interface BoardgameRepository extends JpaRepository<Boardgame, Long> {
boolean existsById(UUID id);

Boardgame findBoardgameById(UUID id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ public ResponseEntity<BoardgameCreateResponse> createBoardgame(Long maxOf) {
.joined(0L)
.build();

boardgameRepository.save(boardgame);
if(member.getBoardgame() == null){
boardgameRepository.save(boardgame);
} else throw new IllegalArgumentException();

return ResponseEntity.ok().body(new BoardgameCreateResponse("ok"));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.gapple.weeingback.domain.diary.controller;

import com.gapple.weeingback.domain.diary.entity.dto.request.DiarySubmitRequest;
import com.gapple.weeingback.domain.diary.service.DiaryService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
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("/api/diary")
@RequiredArgsConstructor
public class DiaryController {
private final DiaryService diaryService;
@PostMapping
public ResponseEntity submitDiary(@Valid @RequestBody DiarySubmitRequest request){
return diaryService.submitDiary(request);
}
}
30 changes: 30 additions & 0 deletions src/main/java/com/gapple/weeingback/domain/diary/entity/Diary.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.gapple.weeingback.domain.diary.entity;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.GenericGenerator;

import java.util.UUID;

@Table
@Entity
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Diary {
@Id
@GeneratedValue(generator = "uuid2", strategy = GenerationType.IDENTITY)
@GenericGenerator(name="uuid2", strategy = "uuid2")
private UUID id;

@Column
private String title;

@Column
private String description;

@Column
private Long accessRange;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.gapple.weeingback.domain.diary.entity.dto.request;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
@AllArgsConstructor
public class DiarySubmitRequest {
private Long accessRange;

private String title;

private String description;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.gapple.weeingback.domain.diary.entity.dto.response;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
@AllArgsConstructor
public class DiarySubmitResponse {
private String success;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.gapple.weeingback.domain.diary.repository;

import com.gapple.weeingback.domain.diary.entity.Diary;
import org.springframework.data.jpa.repository.JpaRepository;

public interface DiaryRepository extends JpaRepository<Diary, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.gapple.weeingback.domain.diary.service;

import com.gapple.weeingback.domain.diary.entity.dto.request.DiarySubmitRequest;
import org.springframework.http.ResponseEntity;

public interface DiaryService {
ResponseEntity submitDiary(DiarySubmitRequest request);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.gapple.weeingback.domain.diary.service.implementation;

import com.gapple.weeingback.domain.diary.entity.Diary;
import com.gapple.weeingback.domain.diary.entity.dto.request.DiarySubmitRequest;
import com.gapple.weeingback.domain.diary.entity.dto.response.DiarySubmitResponse;
import com.gapple.weeingback.domain.diary.repository.DiaryRepository;
import com.gapple.weeingback.domain.diary.service.DiaryService;
import com.gapple.weeingback.domain.member.entity.Member;
import com.gapple.weeingback.domain.member.repository.MemberRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
@RequiredArgsConstructor
public class DiaryServiceImpl implements DiaryService {
private final DiaryRepository diaryRepository;
private final MemberRepository memberRepository;
@Override
public ResponseEntity<DiarySubmitResponse> submitDiary(DiarySubmitRequest request) {
String id = SecurityContextHolder.getContext().getAuthentication().getName();
Member member = memberRepository.findMemberById(UUID.fromString(id));

Diary diary = Diary.builder()
.title(request.getTitle())
.description(request.getDescription())
.accessRange(request.getAccessRange())
.build();

member.addDiary(diary);

diaryRepository.save(diary);
memberRepository.save(member);

return ResponseEntity.ok().body(new DiarySubmitResponse("ok"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.gapple.weeingback.domain.boardgame.entity.Boardgame;
import com.gapple.weeingback.domain.consulting.entity.Consulting;

import com.gapple.weeingback.domain.diary.entity.Diary;
import jakarta.persistence.*;
import lombok.*;
import org.hibernate.annotations.GenericGenerator;
Expand Down Expand Up @@ -48,10 +49,18 @@ public class Member implements GrantedAuthority {
@JoinColumn(name = "boardgame_id")
private Boardgame boardgame;

@OneToMany
@JoinColumn(name = "diary_id")
private List<Diary> diaries;

public void addConsulting(Consulting consulting){
this.consulting.add(consulting);
}

public void addDiary(Diary diary){
this.diaries.add(diary);
}

@Override
public String getAuthority() {
return role;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.gapple.weeingback.global.email.service;

public interface EmailService {
String sendMail(String to);
String sendAuth(String to);
String createCode();
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
@RequiredArgsConstructor
public class EmailServiceImpl implements EmailService {
private final JavaMailSender javaMailSender;
public String sendMail(String to){
public String sendAuth(String to){
String authNumber = createCode();
MimeMessage mimeMessage = javaMailSender.createMimeMessage();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.gapple.weeingback.global.exception;

public class MemberExistsException extends RuntimeException {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.gapple.weeingback.global.exception;

public class MemberNotFoundException extends RuntimeException {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.gapple.weeingback.global.exception;

public class PasswordNotMatchException extends RuntimeException {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.gapple.weeingback.global.handler;

import com.gapple.weeingback.global.exception.MemberExistsException;
import com.gapple.weeingback.global.exception.MemberNotFoundException;
import com.gapple.weeingback.global.exception.PasswordNotMatchException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(PasswordNotMatchException.class)
public ResponseEntity<HttpStatus> handleMethodPasswordNotMatchException() {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}

@ExceptionHandler(MemberNotFoundException.class)
public ResponseEntity<HttpStatus> handleMethodMemberNotFound() {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}

@ExceptionHandler(MemberExistsException.class)
public ResponseEntity<HttpStatus> handleMethodMemberExistsException(){
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
}

0 comments on commit 899b2a6

Please sign in to comment.