Skip to content

Commit

Permalink
Merge pull request #102 from Recipe-Project/feature/add_aop
Browse files Browse the repository at this point in the history
Feature/add aop
  • Loading branch information
joona95 authored Aug 10, 2024
2 parents f8966af + 3b8ad1f commit a4a3875
Show file tree
Hide file tree
Showing 12 changed files with 196 additions and 311 deletions.
26 changes: 0 additions & 26 deletions scripts/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,6 @@ DEPLOY_LOG="$PROJECT_ROOT/deploy.log"

TIME_NOW=$(date +%c)

# Installing docker engine if not exists
if ! type docker > /dev/null #docker를 깔아주는 코드, EC2 인스턴스에는 아무것도 없기 때문에 직접 깔아줘야 한다.
then
echo "docker does not exist"
echo "Start installing docker"
sudo yum -y update
sudo yum install -y docker
sudo systemctl start docker
fi

# Installing docker-compose if not exists
if ! type docker-compose > /dev/null #docker-compose를 깔아주는 코드
then
echo "docker-compose does not exist"
echo "Start installing docker-compose"
sudo curl -L "https://github.com/docker/compose/releases/download/1.27.3/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
fi

# docker 구동
DOCKER_PID=$(pgrep -f $PROJECT_ROOT/docker-compose.yml)
if [ -z $CURRENT_PID ]; then
echo "start docker-compose up"
sudo docker-compose -f $PROJECT_ROOT/docker-compose.yml up --build -d
fi

# build 파일 복사
echo "$TIME_NOW > $WAR_FILE 파일 복사" >> $DEPLOY_LOG
cp $PROJECT_ROOT/*.war $WAR_FILE
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/recipe/app/src/common/aop/LoginCheck.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.recipe.app.src.common.aop;

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

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface LoginCheck {

}
40 changes: 40 additions & 0 deletions src/main/java/com/recipe/app/src/common/aop/LoginCheckAspect.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.recipe.app.src.common.aop;

import com.recipe.app.src.user.domain.SecurityUser;
import com.recipe.app.src.user.domain.User;
import com.recipe.app.src.user.exception.UserTokenNotExistException;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
@Slf4j
@Aspect
public class LoginCheckAspect {

@Around("@annotation(com.recipe.app.src.common.aop.LoginCheck)")
public Object loginCheck(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {

Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

if (authentication == null) {
throw new UserTokenNotExistException();
}

User user = ((SecurityUser) authentication.getPrincipal()).getUser();

log.info("Login User Id : " + user.getUserId());

Object[] args = new Object[]{user};

System.out.println(Arrays.toString(args));

return proceedingJoinPoint.proceed(args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public DialogController(DialogService dialogService) {
this.dialogService = dialogService;
}

@GetMapping("")
@GetMapping
public DialogResponse getDialog() {

return dialogService.findDialog();
Expand Down
58 changes: 22 additions & 36 deletions src/main/java/com/recipe/app/src/fridge/api/FridgeController.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
package com.recipe.app.src.fridge.api;

import com.recipe.app.src.common.aop.LoginCheck;
import com.recipe.app.src.fridge.application.FridgeService;
import com.recipe.app.src.fridge.application.dto.FridgeRequest;
import com.recipe.app.src.fridge.application.dto.FridgeResponse;
import com.recipe.app.src.fridge.application.dto.FridgesResponse;
import com.recipe.app.src.user.domain.SecurityUser;
import com.recipe.app.src.user.domain.User;
import com.recipe.app.src.user.exception.UserTokenNotExistException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
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;
import springfox.documentation.annotations.ApiIgnore;

@Api(tags = {"냉장고 Controller"})
Expand All @@ -27,62 +32,43 @@ public FridgeController(FridgeService fridgeService) {

@ApiOperation(value = "냉장고 채우기 API")
@PostMapping
public void postFridges(@ApiIgnore final Authentication authentication) {

if (authentication == null)
throw new UserTokenNotExistException();

User user = ((SecurityUser) authentication.getPrincipal()).getUser();
@LoginCheck
public void postFridges(@ApiIgnore User user) {

fridgeService.create(user);
}

@ApiOperation(value = "냉장고 목록 조회 API")
@GetMapping
public FridgesResponse getFridges(@ApiIgnore final Authentication authentication) {

if (authentication == null)
throw new UserTokenNotExistException();

User user = ((SecurityUser) authentication.getPrincipal()).getUser();
@LoginCheck
public FridgesResponse getFridges(@ApiIgnore User user) {

return fridgeService.getFridges(user);
}

@ApiOperation(value = "냉장고 상세 조회 API")
@GetMapping("/{fridgeId}")
public FridgeResponse getFridge(@ApiIgnore final Authentication authentication, @PathVariable Long fridgeId) {

if (authentication == null)
throw new UserTokenNotExistException();

User user = ((SecurityUser) authentication.getPrincipal()).getUser();
@LoginCheck
public FridgeResponse getFridge(@ApiIgnore User user, @PathVariable Long fridgeId) {

return fridgeService.getFridge(user, fridgeId);
}

@ApiOperation(value = "냉장고 삭제 API")
@DeleteMapping("/{fridgeId}")
public void deleteFridge(@ApiIgnore final Authentication authentication, @PathVariable Long fridgeId) {

if (authentication == null)
throw new UserTokenNotExistException();

User user = ((SecurityUser) authentication.getPrincipal()).getUser();
@LoginCheck
public void deleteFridge(@ApiIgnore User user, @PathVariable Long fridgeId) {

fridgeService.delete(user, fridgeId);
}

@ApiOperation(value = "냉장고 수정 API")
@PatchMapping("/{fridgeId}")
public void patchFridge(@ApiIgnore final Authentication authentication, @PathVariable Long fridgeId,
@ApiParam(value = "냉장고 수정 입력 정보", required = true)
@RequestBody FridgeRequest request) {

if (authentication == null)
throw new UserTokenNotExistException();

User user = ((SecurityUser) authentication.getPrincipal()).getUser();
@LoginCheck
public void patchFridge(@ApiIgnore User user,
@PathVariable Long fridgeId,
@ApiParam(value = "냉장고 수정 입력 정보", required = true)
@RequestBody FridgeRequest request) {

fridgeService.update(user, fridgeId, request);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package com.recipe.app.src.fridgeBasket.api;

import com.recipe.app.src.common.aop.LoginCheck;
import com.recipe.app.src.fridgeBasket.application.FridgeBasketService;
import com.recipe.app.src.fridgeBasket.application.dto.FridgeBasketCountResponse;
import com.recipe.app.src.fridgeBasket.application.dto.FridgeBasketsRequest;
import com.recipe.app.src.fridgeBasket.application.dto.FridgeBasketRequest;
import com.recipe.app.src.fridgeBasket.application.dto.FridgeBasketsRequest;
import com.recipe.app.src.fridgeBasket.application.dto.FridgeBasketsResponse;
import com.recipe.app.src.user.domain.SecurityUser;
import com.recipe.app.src.user.domain.User;
import com.recipe.app.src.user.exception.UserTokenNotExistException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
Expand All @@ -35,64 +33,44 @@ public FridgeBasketController(FridgeBasketService fridgeBasketService) {

@ApiOperation(value = "재료 선택하여 냉장고 바구니 채우기 API")
@PostMapping
public void postFridgeBasketByIngredientId(@ApiIgnore final Authentication authentication,
@LoginCheck
public void postFridgeBasketByIngredientId(@ApiIgnore User user,
@ApiParam(value = "재료 선택 목록", required = true)
@RequestBody FridgeBasketsRequest request) {

if (authentication == null)
throw new UserTokenNotExistException();

User user = ((SecurityUser) authentication.getPrincipal()).getUser();

fridgeBasketService.create(user, request);
}

@ApiOperation(value = "냉장고 바구니 조회 API")
@GetMapping
public FridgeBasketsResponse getFridgeBaskets(@ApiIgnore final Authentication authentication) {

if (authentication == null)
throw new UserTokenNotExistException();

User user = ((SecurityUser) authentication.getPrincipal()).getUser();
@LoginCheck
public FridgeBasketsResponse getFridgeBaskets(@ApiIgnore User user) {

return fridgeBasketService.findAllByUser(user);
}

@ApiOperation(value = "냉장고 바구니 삭제 API")
@DeleteMapping("/{fridgeBasketId}")
public void deleteFridgeBasket(@ApiIgnore final Authentication authentication, @PathVariable Long fridgeBasketId) {

if (authentication == null)
throw new UserTokenNotExistException();

User user = ((SecurityUser) authentication.getPrincipal()).getUser();
@LoginCheck
public void deleteFridgeBasket(@ApiIgnore User user, @PathVariable Long fridgeBasketId) {

fridgeBasketService.delete(user, fridgeBasketId);
}

@ApiOperation(value = "냉장고 바구니 수정 API")
@PatchMapping("/{fridgeBasketId}")
public void patchFridgeBasket(@ApiIgnore final Authentication authentication, @PathVariable Long fridgeBasketId,
@LoginCheck
public void patchFridgeBasket(@ApiIgnore User user, @PathVariable Long fridgeBasketId,
@ApiParam(value = "냉장고 바구니 수정 정보", required = true)
@RequestBody FridgeBasketRequest request) {

if (authentication == null)
throw new UserTokenNotExistException();

User user = ((SecurityUser) authentication.getPrincipal()).getUser();

fridgeBasketService.update(user, fridgeBasketId, request);
}

@ApiOperation(value = "냉장고 바구니 갯수 조회 API")
@GetMapping("/count")
public FridgeBasketCountResponse getFridgeBasketCount(@ApiIgnore final Authentication authentication) {

if (authentication == null)
throw new UserTokenNotExistException();

User user = ((SecurityUser) authentication.getPrincipal()).getUser();
@LoginCheck
public FridgeBasketCountResponse getFridgeBasketCount(@ApiIgnore User user) {

return fridgeBasketService.countFridgeBasketByUser(user);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
package com.recipe.app.src.ingredient.api;

import com.recipe.app.src.common.aop.LoginCheck;
import com.recipe.app.src.ingredient.application.IngredientFacadeService;
import com.recipe.app.src.ingredient.application.IngredientService;
import com.recipe.app.src.ingredient.application.dto.IngredientCreateResponse;
import com.recipe.app.src.ingredient.application.dto.IngredientRequest;
import com.recipe.app.src.ingredient.application.dto.IngredientsResponse;
import com.recipe.app.src.user.domain.SecurityUser;
import com.recipe.app.src.user.domain.User;
import com.recipe.app.src.user.exception.UserTokenNotExistException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.lang.Nullable;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
Expand All @@ -38,52 +36,36 @@ public IngredientController(IngredientFacadeService ingredientFacadeService, Ing

@ApiOperation(value = "재료 목록 조회 API")
@GetMapping("")
public IngredientsResponse getIngredients(@ApiIgnore final Authentication authentication,
@LoginCheck
public IngredientsResponse getIngredients(@ApiIgnore User user,
@ApiParam(name = "keyword", type = "String", example = "감자", value = "검색어")
@RequestParam(value = "keyword", required = false) @Nullable String keyword) {

if (authentication == null)
throw new UserTokenNotExistException();

User user = ((SecurityUser) authentication.getPrincipal()).getUser();

return ingredientFacadeService.findIngredientsByKeyword(user, keyword);
}

@ApiOperation(value = "나만의 재료 목록 조회 API")
@GetMapping("/my")
public IngredientsResponse getMyIngredients(@ApiIgnore final Authentication authentication) {

if (authentication == null)
throw new UserTokenNotExistException();

User user = ((SecurityUser) authentication.getPrincipal()).getUser();
@LoginCheck
public IngredientsResponse getMyIngredients(@ApiIgnore User user) {

return ingredientFacadeService.findIngredientsByUser(user);
}

@ApiOperation(value = "나만의 재료 등록 API")
@PostMapping("")
public IngredientCreateResponse postIngredient(@ApiIgnore final Authentication authentication,
@LoginCheck
public IngredientCreateResponse postIngredient(@ApiIgnore User user,
@ApiParam(value = "재료 추가 정보", required = true)
@RequestBody IngredientRequest request) {

if (authentication == null)
throw new UserTokenNotExistException();

User user = ((SecurityUser) authentication.getPrincipal()).getUser();
@RequestBody IngredientRequest request) {

return ingredientService.create(user.getUserId(), request);
}

@ApiOperation(value = "나만의 재료 삭제 API")
@DeleteMapping("/{ingredientId}")
public void deleteIngredient(@ApiIgnore final Authentication authentication, @PathVariable Long ingredientId) {

if (authentication == null)
throw new UserTokenNotExistException();

User user = ((SecurityUser) authentication.getPrincipal()).getUser();
@LoginCheck
public void deleteIngredient(@ApiIgnore User user, @PathVariable Long ingredientId) {

ingredientFacadeService.deleteIngredient(user, ingredientId);
}
Expand Down
Loading

0 comments on commit a4a3875

Please sign in to comment.