diff --git a/src/main/java/com/gapple/weeingback/domain/auth/service/impl/AuthServiceImpl.java b/src/main/java/com/gapple/weeingback/domain/auth/service/implementation/AuthServiceImpl.java similarity index 63% rename from src/main/java/com/gapple/weeingback/domain/auth/service/impl/AuthServiceImpl.java rename to src/main/java/com/gapple/weeingback/domain/auth/service/implementation/AuthServiceImpl.java index 66d540f..00a0a11 100644 --- a/src/main/java/com/gapple/weeingback/domain/auth/service/impl/AuthServiceImpl.java +++ b/src/main/java/com/gapple/weeingback/domain/auth/service/implementation/AuthServiceImpl.java @@ -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; @@ -15,7 +18,6 @@ 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; @@ -23,7 +25,7 @@ @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; @@ -41,31 +43,33 @@ public ResponseEntity 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 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 roles = new ArrayList<>(); - roles.add(AccessRole.valueOf(member.getRole())); + String id = member.getId().toString(); + String password = member.getPassword(); - Authentication authentication = + List 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 sendAuth(EmailCertifyRequest request) { - return ResponseEntity.ok(emailService.sendMail(request.getEmail())); + return ResponseEntity.ok(emailService.sendAuth(request.getEmail())); } } diff --git a/src/main/java/com/gapple/weeingback/domain/boardgame/repository/BoardgameRepository.java b/src/main/java/com/gapple/weeingback/domain/boardgame/repository/BoardgameRepository.java index 2a3fdcf..211215d 100644 --- a/src/main/java/com/gapple/weeingback/domain/boardgame/repository/BoardgameRepository.java +++ b/src/main/java/com/gapple/weeingback/domain/boardgame/repository/BoardgameRepository.java @@ -8,5 +8,7 @@ @Repository public interface BoardgameRepository extends JpaRepository { + boolean existsById(UUID id); + Boardgame findBoardgameById(UUID id); } diff --git a/src/main/java/com/gapple/weeingback/domain/boardgame/service/implmentation/BoardgameServiceImpl.java b/src/main/java/com/gapple/weeingback/domain/boardgame/service/implmentation/BoardgameServiceImpl.java index f5b5f8b..020550a 100644 --- a/src/main/java/com/gapple/weeingback/domain/boardgame/service/implmentation/BoardgameServiceImpl.java +++ b/src/main/java/com/gapple/weeingback/domain/boardgame/service/implmentation/BoardgameServiceImpl.java @@ -41,7 +41,9 @@ public ResponseEntity 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")); } diff --git a/src/main/java/com/gapple/weeingback/domain/diary/controller/DiaryController.java b/src/main/java/com/gapple/weeingback/domain/diary/controller/DiaryController.java new file mode 100644 index 0000000..21b05d7 --- /dev/null +++ b/src/main/java/com/gapple/weeingback/domain/diary/controller/DiaryController.java @@ -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); + } +} diff --git a/src/main/java/com/gapple/weeingback/domain/diary/entity/Diary.java b/src/main/java/com/gapple/weeingback/domain/diary/entity/Diary.java new file mode 100644 index 0000000..cd67221 --- /dev/null +++ b/src/main/java/com/gapple/weeingback/domain/diary/entity/Diary.java @@ -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; +} diff --git a/src/main/java/com/gapple/weeingback/domain/diary/entity/dto/request/DiarySubmitRequest.java b/src/main/java/com/gapple/weeingback/domain/diary/entity/dto/request/DiarySubmitRequest.java new file mode 100644 index 0000000..a7f540e --- /dev/null +++ b/src/main/java/com/gapple/weeingback/domain/diary/entity/dto/request/DiarySubmitRequest.java @@ -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; +} diff --git a/src/main/java/com/gapple/weeingback/domain/diary/entity/dto/response/DiarySubmitResponse.java b/src/main/java/com/gapple/weeingback/domain/diary/entity/dto/response/DiarySubmitResponse.java new file mode 100644 index 0000000..aec179f --- /dev/null +++ b/src/main/java/com/gapple/weeingback/domain/diary/entity/dto/response/DiarySubmitResponse.java @@ -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; +} diff --git a/src/main/java/com/gapple/weeingback/domain/diary/repository/DiaryRepository.java b/src/main/java/com/gapple/weeingback/domain/diary/repository/DiaryRepository.java new file mode 100644 index 0000000..d4953a8 --- /dev/null +++ b/src/main/java/com/gapple/weeingback/domain/diary/repository/DiaryRepository.java @@ -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 { +} diff --git a/src/main/java/com/gapple/weeingback/domain/diary/service/DiaryService.java b/src/main/java/com/gapple/weeingback/domain/diary/service/DiaryService.java new file mode 100644 index 0000000..9a3225d --- /dev/null +++ b/src/main/java/com/gapple/weeingback/domain/diary/service/DiaryService.java @@ -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); +} diff --git a/src/main/java/com/gapple/weeingback/domain/diary/service/implementation/DiaryServiceImpl.java b/src/main/java/com/gapple/weeingback/domain/diary/service/implementation/DiaryServiceImpl.java new file mode 100644 index 0000000..a400c7d --- /dev/null +++ b/src/main/java/com/gapple/weeingback/domain/diary/service/implementation/DiaryServiceImpl.java @@ -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 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")); + } +} diff --git a/src/main/java/com/gapple/weeingback/domain/member/entity/Member.java b/src/main/java/com/gapple/weeingback/domain/member/entity/Member.java index 26eca5b..7b27381 100644 --- a/src/main/java/com/gapple/weeingback/domain/member/entity/Member.java +++ b/src/main/java/com/gapple/weeingback/domain/member/entity/Member.java @@ -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; @@ -48,10 +49,18 @@ public class Member implements GrantedAuthority { @JoinColumn(name = "boardgame_id") private Boardgame boardgame; + @OneToMany + @JoinColumn(name = "diary_id") + private List 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; diff --git a/src/main/java/com/gapple/weeingback/global/email/service/EmailService.java b/src/main/java/com/gapple/weeingback/global/email/service/EmailService.java index 5c37c15..55c50c7 100644 --- a/src/main/java/com/gapple/weeingback/global/email/service/EmailService.java +++ b/src/main/java/com/gapple/weeingback/global/email/service/EmailService.java @@ -1,6 +1,6 @@ package com.gapple.weeingback.global.email.service; public interface EmailService { - String sendMail(String to); + String sendAuth(String to); String createCode(); } diff --git a/src/main/java/com/gapple/weeingback/global/email/service/impl/EmailServiceImpl.java b/src/main/java/com/gapple/weeingback/global/email/service/impl/EmailServiceImpl.java index e816130..30c5ef6 100644 --- a/src/main/java/com/gapple/weeingback/global/email/service/impl/EmailServiceImpl.java +++ b/src/main/java/com/gapple/weeingback/global/email/service/impl/EmailServiceImpl.java @@ -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(); diff --git a/src/main/java/com/gapple/weeingback/global/exception/MemberExistsException.java b/src/main/java/com/gapple/weeingback/global/exception/MemberExistsException.java new file mode 100644 index 0000000..1a68f4a --- /dev/null +++ b/src/main/java/com/gapple/weeingback/global/exception/MemberExistsException.java @@ -0,0 +1,4 @@ +package com.gapple.weeingback.global.exception; + +public class MemberExistsException extends RuntimeException { +} diff --git a/src/main/java/com/gapple/weeingback/global/exception/MemberNotFoundException.java b/src/main/java/com/gapple/weeingback/global/exception/MemberNotFoundException.java new file mode 100644 index 0000000..d1eaff9 --- /dev/null +++ b/src/main/java/com/gapple/weeingback/global/exception/MemberNotFoundException.java @@ -0,0 +1,4 @@ +package com.gapple.weeingback.global.exception; + +public class MemberNotFoundException extends RuntimeException { +} diff --git a/src/main/java/com/gapple/weeingback/global/exception/PasswordNotMatchException.java b/src/main/java/com/gapple/weeingback/global/exception/PasswordNotMatchException.java new file mode 100644 index 0000000..738c534 --- /dev/null +++ b/src/main/java/com/gapple/weeingback/global/exception/PasswordNotMatchException.java @@ -0,0 +1,4 @@ +package com.gapple.weeingback.global.exception; + +public class PasswordNotMatchException extends RuntimeException { +} diff --git a/src/main/java/com/gapple/weeingback/global/handler/GlobalExceptionHandler.java b/src/main/java/com/gapple/weeingback/global/handler/GlobalExceptionHandler.java new file mode 100644 index 0000000..a4b3a09 --- /dev/null +++ b/src/main/java/com/gapple/weeingback/global/handler/GlobalExceptionHandler.java @@ -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 handleMethodPasswordNotMatchException() { + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + } + + @ExceptionHandler(MemberNotFoundException.class) + public ResponseEntity handleMethodMemberNotFound() { + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + + @ExceptionHandler(MemberExistsException.class) + public ResponseEntity handleMethodMemberExistsException(){ + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + } +} \ No newline at end of file