Skip to content
This repository has been archived by the owner on Aug 13, 2022. It is now read-only.

Commit

Permalink
Merge pull request #57 from f-lab-edu/푸시_서비스_적용
Browse files Browse the repository at this point in the history
[#55] 푸시 서비스 적용
  • Loading branch information
yyy9942 authored Jan 16, 2020
2 parents 3da2fd6 + 6de3866 commit 9520e3c
Show file tree
Hide file tree
Showing 55 changed files with 3,005 additions and 151 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>

<!-- FCM -->
<dependency>
<groupId>com.google.firebase</groupId>
Expand Down
106 changes: 91 additions & 15 deletions src/main/java/com/delfood/aop/AuthCheckAspect.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package com.delfood.aop;

import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.Objects;
import javax.servlet.http.HttpSession;
import org.apache.commons.codec.binary.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.codehaus.commons.compiler.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
Expand All @@ -27,9 +34,8 @@ public class AuthCheckAspect {
* 로그인되어있지 않을 시 해당 메서드 로직을 중지시킨 후 리턴한다.
* @OwnerLoginCheck 해당 어노테이션이 적용된 메서드를 검사한다.
* @author jun
* @param pjp
* @return 로그인시 SUCCESS, 비로그인시 NO_LOGIN
* @throws Throwable
* @param jp 조인포인트
* @throws Throwable 발생 가능한 예외
*/
@Before("@annotation(com.delfood.aop.OwnerLoginCheck)")
public void ownerLoginCheck(JoinPoint jp) throws Throwable {
Expand All @@ -49,25 +55,46 @@ public void ownerLoginCheck(JoinPoint jp) throws Throwable {
* 세션에서 사장님 로그인을 체크 한다.
* 그 후 입력받은 파라미터 값 중 매장 id를 검색하여 해당 매장이 접속한 사장님의 것인지 검사한다.
* @author jun
* @param pjp
* @return 비로그인시 NO_LOGIN, 해당 매장의 사장이 아닐 시 UNAUTHORIZED, 권한이 있을 시 SUCCESS
* @throws Throwable
* @param jp 조인포인트
* @throws Throwable 발새 가능한 예외
*/
@Before("@annotation(com.delfood.aop.OwnerShopCheck)")
public void ownerShopCheck(JoinPoint jp) throws Throwable {
@Before("@annotation(com.delfood.aop.OwnerShopCheck) && @annotation(ownerShopCheck)")
public void ownerShopCheck(JoinPoint jp, OwnerShopCheck ownerShopCheck) throws Throwable {
log.debug("AOP - Owner Shop Check Started");


HttpSession session = ((ServletRequestAttributes)(RequestContextHolder.currentRequestAttributes())).getRequest().getSession();
HttpSession session =
((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest()
.getSession();
String ownerId = SessionUtil.getLoginOwnerId(session);
if(ownerId == null) {

if (ownerId == null) {
log.debug("AOP - Owner Shop Check Result - NO_LOGIN");
throw new HttpStatusCodeException(HttpStatus.UNAUTHORIZED, "NO_LOGIN") {};
}

Object[] args = jp.getArgs();
Long shopId = (Long) args[0];

// 메소드 파라미터 추출
MethodSignature signature = (MethodSignature) jp.getSignature();
Method method = signature.getMethod();
Parameter[] parameters = method.getParameters();

Long shopId = null;

// 파라미터의 이름과 어노테이션의 value를 비교하여 검사
for (int i = 0; i < parameters.length; i++) {
String parameterName = parameters[i].getName();
if (StringUtils.equals(ownerShopCheck.value(), parameterName)) {
shopId = (Long) args[i];
}
}

// 어노테이션 value로 설정된 값과 같은 변수 이름이 없을 경우 예외처리
if (Objects.isNull(shopId)) {
throw new IllegalArgumentException("OwnerShopCheck 어노테이션 설정이 잘못되었습니다. value와 변수 명을 일치시켜주세요.");
}


if (!shopService.isShopOwner(shopId, ownerId)) {
log.debug("AOP - Owner Shop Check Result - UNAUTHORIZED");
Expand All @@ -78,9 +105,8 @@ public void ownerShopCheck(JoinPoint jp) throws Throwable {
/**
* 고객의 로그인을 체크한다.
* @author jun
* @param pjp
* @return
* @throws Throwable
* @param jp 조인포인튼
* @throws Throwable 발생 가능한 예외
*/
@Before("@annotation(com.delfood.aop.MemberLoginCheck)")
public void memberLoginCheck(JoinPoint jp) throws Throwable {
Expand All @@ -93,4 +119,54 @@ public void memberLoginCheck(JoinPoint jp) throws Throwable {
throw new HttpStatusCodeException(HttpStatus.UNAUTHORIZED, "NO_LOGIN") {};
}
}

/**
* 라이더 로그인을 체크한다.
* @author jun
* @param jp 조인포인트
* @throws Throwable 발생 가능한 예외 설정
*/
@Before("@annotation(com.delfood.aop.RiderLoginCheck)")
public void riderLoginCheck(JoinPoint jp) throws Throwable {
log.debug("AOP - Rider Login Check Started");

HttpSession session =
((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest()
.getSession();
String riderId = SessionUtil.getLoginRiderId(session);

if (Objects.isNull(riderId)) {
throw new HttpStatusCodeException(HttpStatus.UNAUTHORIZED, "RIDER_NO_LOGIN") {};
}
}

/**
* 공통 로그인 체크 AOP.
* 고객, 사장님, 라이더의 로그인 체크 기능을 하나로 모아두었다.
* @param jp 조인포인트
* @throws Throwable 발생 가능한 예외
*/
@Before("@annotation(com.delfood.aop.LoginCheck) && @ annotation(loginCheck)")
public void loginCheck(JoinPoint jp, LoginCheck loginCheck) throws Throwable {
log.debug("AOP - Login Check Started");

HttpSession session =
((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest()
.getSession();

if (LoginCheck.UserType.MEMBER.equals(loginCheck.type())) {
memberLoginCheck(jp);
}

if (LoginCheck.UserType.OWNER.equals(loginCheck.type())) {
ownerLoginCheck(jp);
}

if (LoginCheck.UserType.RIDER.equals(loginCheck.type())) {
riderLoginCheck(jp);
}


}

}
28 changes: 28 additions & 0 deletions src/main/java/com/delfood/aop/LoginCheck.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.delfood.aop;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* 로그인의 상태를 확인한다.
* 회원, 사장님, 라이더의 로그인 상태를 확인하여 로그인 되지 않았다면 예외를 발생시킨다.
* @author jun
*
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface LoginCheck {

/**
* 로그인을 체크하고 싶은 유저의 로그인 타입.
* 회원(MEMBER), 사장님(OWNER), 라이더(RIDER)중 선택할 수 있다.
* @return
*/
UserType type();

public static enum UserType {
MEMBER, OWNER, RIDER
}
}
11 changes: 9 additions & 2 deletions src/main/java/com/delfood/aop/OwnerShopCheck.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
package com.delfood.aop;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* <b>매장 id가 첫 번째 파라미터로 와야한다.</b>
* <b>매장 id를 파라미터로 주어야 한다.</b>
* 접속한 사장님이 해당 매장의 주인인지 확인한다.
* @author yyy99
*
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface OwnerShopCheck {

/**
* 해당 변수의 이름.
* @return
*/
String value();
}
9 changes: 9 additions & 0 deletions src/main/java/com/delfood/aop/RiderLoginCheck.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.delfood.aop;

import java.lang.annotation.ElementType;
import java.lang.annotation.Target;

@Target(ElementType.METHOD)
public @interface RiderLoginCheck {

}
12 changes: 7 additions & 5 deletions src/main/java/com/delfood/controller/CartControllelr.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.delfood.controller;

import com.delfood.aop.LoginCheck;
import com.delfood.aop.LoginCheck.UserType;
import com.delfood.aop.MemberLoginCheck;
import com.delfood.dto.ItemDTO;
import com.delfood.service.CartService;
Expand All @@ -23,31 +25,31 @@ public class CartControllelr {
private CartService cartService;

@PostMapping("/members/cart/menus")
@MemberLoginCheck
@LoginCheck(type = UserType.MEMBER)
public void addMenu(@RequestBody ItemDTO item, HttpSession session) {
cartService.addOrdersItem(item, SessionUtil.getLoginMemberId(session));
}

@GetMapping("/members/cart/menus")
@MemberLoginCheck
@LoginCheck(type = UserType.MEMBER)
public List<ItemDTO> getCart(HttpSession session) {
return cartService.getItems(SessionUtil.getLoginMemberId(session));
}

@DeleteMapping("/members/cart/menus")
@MemberLoginCheck
@LoginCheck(type = UserType.MEMBER)
public void clearCart(HttpSession session) {
cartService.claer(SessionUtil.getLoginMemberId(session));
}

@DeleteMapping("/members/cart/menus/{index}")
@MemberLoginCheck
@LoginCheck(type = UserType.MEMBER)
public void deleteCartMenu(HttpSession session, @PathVariable long index) {
cartService.deleteCartMenu(SessionUtil.getLoginMemberId(session), index);
}

@GetMapping("/members/cart/price")
@MemberLoginCheck
@LoginCheck(type = UserType.MEMBER)
public CartPriceResponse cartPrice(HttpSession session) {
String memberId = SessionUtil.getLoginMemberId(session);
return new CartPriceResponse(cartService.getItems(memberId), cartService.allPrice(memberId));
Expand Down
78 changes: 78 additions & 0 deletions src/main/java/com/delfood/controller/CouponController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.delfood.controller;

import com.delfood.dto.CouponDTO;
import com.delfood.service.CouponService;
import lombok.extern.log4j.Log4j2;
import java.time.LocalDateTime;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
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;

@Log4j2
@RestController
@RequestMapping("/coupons/")
public class CouponController {

@Autowired
CouponService couponService;

/**
* 쿠폰을 추가한다.
* @param couponInfo 쿠폰 정보
* @return
*
* @author jinyoung
*/
@PostMapping
public void addCoupon(@RequestBody CouponDTO couponInfo) {

if (CouponDTO.hasNullData(couponInfo)) {
log.error("insufficient coupon information! {}", couponInfo.toString());
throw new NullPointerException("insufficient coupon information! " + couponInfo.toString());
}

couponService.addCoupon(couponInfo);
}

/**
* 쿠폰 이름과 만료일을 수정한다.
*
* @param id 쿠폰 아이디
* @param name 수정할 쿠폰 이름
* @param endAt 수정할 만료일
*
* @author jinyoung
*/
@PatchMapping
public void updateCouponNameAndEndAt(Long id, String name, LocalDateTime endAt) {
couponService.updateCouponNameAndEndAt(id, name, endAt);
}

/**
* 쿠폰을 삭제한다.
*
* @param id 쿠폰 아이디
*/
public void deleteCoupon(Long id) {
couponService.deleteCoupon(id);
}


/**
* 만료일이 지나지 않은 쿠폰들을 조회한다.
* @return 쿠폰리스트
*
* @author jinyoung
*/
@GetMapping
public List<CouponDTO> getAvailableCoupons() {
return couponService.getAvaliableCoupons();
}

}
Loading

0 comments on commit 9520e3c

Please sign in to comment.