Skip to content

Commit

Permalink
#56 chore: 구독 API url 계층 구조 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
Haeun-Y committed Oct 1, 2023
1 parent da4f2aa commit 7c0e33c
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 94 deletions.
53 changes: 0 additions & 53 deletions src/main/java/com/ewhatever/qna/mail/MailController.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.ewhatever.qna.mail.controller;

import com.ewhatever.qna.common.Base.BaseException;
import com.ewhatever.qna.common.Base.BaseResponse;
import com.ewhatever.qna.mail.dto.PostMailReq;
import com.ewhatever.qna.mail.service.MailService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import static com.ewhatever.qna.common.Base.BaseResponseStatus.SUCCESS;

@RequestMapping("/letter")
@RestController
@RequiredArgsConstructor
public class MailController {

private final MailService mailService;

@ResponseBody
@PostMapping
public BaseResponse<String> sendMail(@RequestBody PostMailReq postMailReq) throws BaseException {
mailService.sendMail(postMailReq);
return new BaseResponse<>(SUCCESS);
}

}
33 changes: 0 additions & 33 deletions src/main/java/com/ewhatever/qna/mail/service/MailService.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
package com.ewhatever.qna.mail.service;

import com.ewhatever.qna.common.Base.BaseException;
import com.ewhatever.qna.login.CustomUnauthorizedException;
import com.ewhatever.qna.login.JwtIssuer;
import com.ewhatever.qna.login.dto.AuthService;
import com.ewhatever.qna.mail.dto.GetSubscriptionRes;
import com.ewhatever.qna.mail.dto.PostMailReq;
import com.ewhatever.qna.mail.dto.PostSubscriptionReq;
import com.ewhatever.qna.user.entity.User;
import com.ewhatever.qna.user.repository.UserRepository;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
Expand All @@ -17,8 +11,6 @@
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.thymeleaf.context.Context;
import org.thymeleaf.spring6.SpringTemplateEngine;

Expand All @@ -35,13 +27,7 @@ public class MailService {

private JavaMailSender emailSender;
private final SpringTemplateEngine templateEngine;

private final JwtIssuer jwtIssuer;

private final UserRepository userRepository;

private final AuthService authService;

@Async
public void sendMail(PostMailReq postMailReq) throws BaseException {

Expand Down Expand Up @@ -81,23 +67,4 @@ public void sendMail(PostMailReq postMailReq) throws BaseException {

log.info("*** 메일 전송 완료 ***");
}

@Transactional
public void subscribeLetter(String token, PostSubscriptionReq postSubscriptionReq, BindingResult bindingResult) throws BaseException {
if(!jwtIssuer.validateToken(token)) throw new CustomUnauthorizedException(INVALID_TOKEN.getMessage());
if(bindingResult.hasErrors()) throw new BaseException(INVALID_EMAIL);
User user = userRepository.findById(authService.getUserIdx(token)).orElseThrow(()-> new BaseException(INVALID_USER));
user.setEmail(postSubscriptionReq.getEmail());
}

@Transactional
public void deleteSubscriptionLetter(String token) throws BaseException {
User user = userRepository.findById(authService.getUserIdx(token)).orElseThrow(()-> new BaseException(INVALID_USER));
user.setEmail(null);
}

public GetSubscriptionRes getSubscriptionInformation(String token) throws BaseException {
User user = userRepository.findById(authService.getUserIdx(token)).orElseThrow(()-> new BaseException(INVALID_USER));
return new GetSubscriptionRes(user);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@

import com.ewhatever.qna.common.Base.BaseException;
import com.ewhatever.qna.common.Base.BaseResponse;
import com.ewhatever.qna.login.CustomUnauthorizedException;
import com.ewhatever.qna.user.dto.GetSubscriptionRes;
import com.ewhatever.qna.user.dto.PostSubscriptionReq;
import com.ewhatever.qna.user.dto.GetCommentResponse;
import com.ewhatever.qna.user.dto.GetJunyQuestionResponse;
import com.ewhatever.qna.user.dto.GetScrapResponse;
import com.ewhatever.qna.user.dto.GetSinyAnswerResponse;
import com.ewhatever.qna.user.service.UserService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import static com.ewhatever.qna.common.Base.BaseResponseStatus.INVALID_TOKEN;
import static com.ewhatever.qna.common.Base.BaseResponseStatus.SUCCESS;

@RequestMapping("/users")
@RestController
Expand Down Expand Up @@ -57,4 +57,27 @@ public BaseResponse<Page<GetScrapResponse>> getMyScraps(HttpServletRequest reque
@RequestParam(value = "requestPageNum", defaultValue = "0") int requestPageNum) throws BaseException{
return new BaseResponse<>(userService.getMyScraps(request.getHeader("Authorization"), requestPageNum));
}


@ResponseBody
@PostMapping("/subscription")
public BaseResponse<String> subscribeLetter(HttpServletRequest request,
@Valid @RequestBody PostSubscriptionReq postSubscriptionReq,
BindingResult bindingResult) throws BaseException {
userService.subscribeLetter(request.getHeader("Authorization"), postSubscriptionReq, bindingResult);
return new BaseResponse<>(SUCCESS);
}

@ResponseBody
@DeleteMapping("/subscription")
public BaseResponse<String> deleteSubscriptionLetter(HttpServletRequest request) throws BaseException {
userService.deleteSubscriptionLetter(request.getHeader("Authorization"));
return new BaseResponse<>(SUCCESS);
}

@ResponseBody
@GetMapping("/subscription")
public BaseResponse<GetSubscriptionRes> getSubscriptionInformation(HttpServletRequest request) throws BaseException {
return new BaseResponse<>(userService.getSubscriptionInformation(request.getHeader("Authorization")));
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ewhatever.qna.mail.dto;
package com.ewhatever.qna.user.dto;

import com.ewhatever.qna.user.entity.User;
import lombok.Getter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ewhatever.qna.mail.dto;
package com.ewhatever.qna.user.dto;

import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotNull;
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/com/ewhatever/qna/user/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import com.ewhatever.qna.login.CustomUnauthorizedException;
import com.ewhatever.qna.login.JwtIssuer;
import com.ewhatever.qna.login.dto.AuthService;
import com.ewhatever.qna.user.dto.GetSubscriptionRes;
import com.ewhatever.qna.user.dto.PostSubscriptionReq;
import com.ewhatever.qna.post.repository.PostRepository;
import com.ewhatever.qna.scrap.repository.ScrapRepository;
import com.ewhatever.qna.user.dto.*;
Expand All @@ -20,6 +22,8 @@
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -137,4 +141,23 @@ private void checkSinyRole(Role role) throws BaseException {
if(role.equals(Role.Cyni)) return;
else throw new BaseException(NO_SENIOR_ROLE);
}

@Transactional
public void subscribeLetter(String token, PostSubscriptionReq postSubscriptionReq, BindingResult bindingResult) throws BaseException {
if(!jwtIssuer.validateToken(token)) throw new CustomUnauthorizedException(INVALID_TOKEN.getMessage());
if(bindingResult.hasErrors()) throw new BaseException(INVALID_EMAIL);
User user = userRepository.findById(authService.getUserIdx(token)).orElseThrow(()-> new BaseException(INVALID_USER));
user.setEmail(postSubscriptionReq.getEmail());
}

@Transactional
public void deleteSubscriptionLetter(String token) throws BaseException {
User user = userRepository.findById(authService.getUserIdx(token)).orElseThrow(()-> new BaseException(INVALID_USER));
user.setEmail(null);
}

public GetSubscriptionRes getSubscriptionInformation(String token) throws BaseException {
User user = userRepository.findById(authService.getUserIdx(token)).orElseThrow(()-> new BaseException(INVALID_USER));
return new GetSubscriptionRes(user);
}
}

0 comments on commit 7c0e33c

Please sign in to comment.