From f8c517f2b304f6735ec0fa2f9a212a0eb0bd07fe Mon Sep 17 00:00:00 2001
From: Kimtaewan <65708461+ktykty0722@users.noreply.github.com>
Date: Wed, 22 Feb 2023 11:49:46 +0900
Subject: [PATCH] =?UTF-8?q?BE=20-=20=EB=A1=9C=EA=B7=B8=EC=9D=B8=20?=
 =?UTF-8?q?=EC=8B=9C=EC=97=90=EB=A7=8C=20=EB=8F=99=EC=9E=91=ED=95=A0=20?=
 =?UTF-8?q?=EC=88=98=20=EC=9E=88=EB=8F=84=EB=A1=9D=20=EC=88=98=EC=A0=95,?=
 =?UTF-8?q?=20=EA=B8=B0=ED=94=84=ED=8B=B0=EC=BD=98=20=EB=93=B1=EB=A1=9D?=
 =?UTF-8?q?=EC=8B=9C=20=EC=82=AC=EC=A7=84=20s3=EC=97=90=20=EB=93=B1?=
 =?UTF-8?q?=EB=A1=9D=ED=95=98=EB=8A=94=20=EB=B6=80=EB=B6=84=20=EB=A9=94?=
 =?UTF-8?q?=EC=86=8C=EB=93=9C=20=EB=B6=84=EB=A6=AC=20(#187)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* feat(BE) : s3에 사진 저장하는 코드 메소드 분리 및 세션으로 user 정보 불러오는 코드 수정(#186)

* feat(BE) : AOP 부분 주석 해제(#186)

* feat(BE) : 마이페이지 하드코딩 부분 수정(#186)

* feat(BE) : 사용한 기프티콘 리스트 반환부분 수중(#186)
---
 .../amatta_server/aop/AuthorizationAop.java   |  5 +--
 .../controller/GifticonController.java        |  2 +-
 .../gifticon/service/GifticonService.java     | 43 +++++++++----------
 .../user/controller/UserController.java       | 13 +++---
 4 files changed, 28 insertions(+), 35 deletions(-)

diff --git a/amatta_server/src/main/java/com/amatta/amatta_server/aop/AuthorizationAop.java b/amatta_server/src/main/java/com/amatta/amatta_server/aop/AuthorizationAop.java
index d655033..7eea8c0 100644
--- a/amatta_server/src/main/java/com/amatta/amatta_server/aop/AuthorizationAop.java
+++ b/amatta_server/src/main/java/com/amatta/amatta_server/aop/AuthorizationAop.java
@@ -22,8 +22,7 @@ public class AuthorizationAop {
     @Before("within(@com.amatta.amatta_server.aop.ClassRequiresAuth *)")
     public void authorizeClass() throws NotAuthenticatedException, NullPointerException {
         logger.info("AuthorizationAop.authorize");
-        //TODO: 테스트 후 복구!!!
-        /*HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
+        HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
         HttpSession session = request.getSession(false);
         if(session == null) {
             throw new NotAuthenticatedException();
@@ -31,7 +30,7 @@ public void authorizeClass() throws NotAuthenticatedException, NullPointerExcept
         Users user = (Users) session.getAttribute("User");
         if(user == null) {
             throw new NotAuthenticatedException();
-        }*/
+        }
     }
 
     @Before("@annotation(com.amatta.amatta_server.aop.MethodRequiresAuth)")
diff --git a/amatta_server/src/main/java/com/amatta/amatta_server/gifticon/controller/GifticonController.java b/amatta_server/src/main/java/com/amatta/amatta_server/gifticon/controller/GifticonController.java
index ad3c52f..ce7ed95 100644
--- a/amatta_server/src/main/java/com/amatta/amatta_server/gifticon/controller/GifticonController.java
+++ b/amatta_server/src/main/java/com/amatta/amatta_server/gifticon/controller/GifticonController.java
@@ -60,7 +60,7 @@ public ResponseEntity<?> gifticonList(@RequestParam(name = "keyword", required =
 
     @GetMapping("/used")
     public ResponseEntity<?> usedGifticonList() {
-        return new ResponseEntity<>(gifticonService.usedTest(), HttpStatus.OK);
+        return new ResponseEntity<>(gifticonService.usedGifticonList(), HttpStatus.OK);
     }
 
     @PutMapping("/used")
diff --git a/amatta_server/src/main/java/com/amatta/amatta_server/gifticon/service/GifticonService.java b/amatta_server/src/main/java/com/amatta/amatta_server/gifticon/service/GifticonService.java
index f0798cb..440e8a9 100644
--- a/amatta_server/src/main/java/com/amatta/amatta_server/gifticon/service/GifticonService.java
+++ b/amatta_server/src/main/java/com/amatta/amatta_server/gifticon/service/GifticonService.java
@@ -73,7 +73,11 @@ public Gifticon mapTextToGifticon(GifticonTextDto dto) throws GifticonParseExcep
 
     @Transactional
     public void addGifticon(GifticonDto dto, MultipartFile image, MultipartFile thumbnail) throws DuplicateGifticonException, IOException {
-        long uid = 2;
+        Users user = getUserBySessionId();
+        if(user == null) {
+            throw new NotAuthenticatedException();
+        }
+        long uid = user.getId();
         String brandName = dto.getBrandName();
         String itemName = dto.getItemName();
         String barcode = dto.getBarcode();
@@ -84,32 +88,13 @@ public void addGifticon(GifticonDto dto, MultipartFile image, MultipartFile thum
         if(gifticonRepository.findByBarcode(barcode).isPresent()) {
             throw new DuplicateGifticonException();
         }
-        String originalName = String.valueOf(System.currentTimeMillis()); // 파일 이름
-        long size = image.getSize(); // 파일 크기
-        ObjectMetadata objectMetaData = new ObjectMetadata();
-        objectMetaData.setContentType(image.getContentType());
-        objectMetaData.setContentLength(size);
-        amazonS3Client.putObject(
-                new PutObjectRequest(S3Bucket, originalName, image.getInputStream(), objectMetaData)
-                        .withCannedAcl(CannedAccessControlList.PublicRead)
-        );
-        String imagePath = amazonS3Client.getUrl(S3Bucket, originalName).toString();
-
-        String originalName2 = String.valueOf(System.currentTimeMillis()); // 파일 이름
-        long size2 = thumbnail.getSize(); // 파일 크기
-        ObjectMetadata objectMetaData2 = new ObjectMetadata();
-        objectMetaData2.setContentType(thumbnail.getContentType());
-        objectMetaData2.setContentLength(size2);
-        amazonS3Client.putObject(
-                new PutObjectRequest(S3Bucket, originalName2, thumbnail.getInputStream(), objectMetaData2)
-                        .withCannedAcl(CannedAccessControlList.PublicRead)
-        );
-        String imagePath2 = amazonS3Client.getUrl(S3Bucket, originalName2).toString();
+        String imagePath = sendToS3(image, String.valueOf(System.currentTimeMillis()));
+        String thumbnailPath = sendToS3(thumbnail, String.valueOf(System.currentTimeMillis()));
 
         gifticonRepository.addGifticon(
                 uid,
                 imagePath,
-                imagePath2,
+                thumbnailPath,
                 brandName,
                 itemName,
                 barcode,
@@ -156,6 +141,18 @@ private Users getUserBySessionId() {
         return (Users) session.getAttribute("User");
     }
 
+    private String sendToS3(MultipartFile image, String name) throws IOException {
+        long size = image.getSize();
+        ObjectMetadata objectMetaData = new ObjectMetadata();
+        objectMetaData.setContentType(image.getContentType());
+        objectMetaData.setContentLength(size);
+        amazonS3Client.putObject(
+                new PutObjectRequest(S3Bucket, name, image.getInputStream(), objectMetaData)
+                        .withCannedAcl(CannedAccessControlList.PublicRead)
+        );
+        return amazonS3Client.getUrl(S3Bucket, name).toString();
+    }
+
     public List<Gifticon> test(String keyword) {
         return gifticonRepository.test(keyword);
     }
diff --git a/amatta_server/src/main/java/com/amatta/amatta_server/user/controller/UserController.java b/amatta_server/src/main/java/com/amatta/amatta_server/user/controller/UserController.java
index 00b5ea9..ffa72b2 100644
--- a/amatta_server/src/main/java/com/amatta/amatta_server/user/controller/UserController.java
+++ b/amatta_server/src/main/java/com/amatta/amatta_server/user/controller/UserController.java
@@ -9,8 +9,6 @@
 import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.MethodArgumentNotValidException;
 import org.springframework.web.bind.annotation.*;
-import org.springframework.web.context.request.RequestContextHolder;
-import org.springframework.web.context.request.ServletRequestAttributes;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpSession;
@@ -85,12 +83,11 @@ public ResponseEntity<?> findPasswordByEmail(@RequestBody UserFindPasswordByEmai
     }
 
     @GetMapping("/mypage")
-    public ResponseEntity<?> mypage(/*@SessionAttribute(value = "User", required = false) Users user*/) {
-//        if (Objects.isNull(user)) {
-//            return new ResponseEntity<>(new UserMypageRes(false), HttpStatus.UNAUTHORIZED);
-//        }
-        return new ResponseEntity<>(new UserMypageRes(true, "test@test.com", "testPassword", "test", "010-0000-0000"), HttpStatus.OK);
-//        return new ResponseEntity<>(new UserMypageRes(true, user.getEmail(), user.getPassword(), user.getName(), user.getPhoneNumber()), HttpStatus.OK);
+    public ResponseEntity<?> mypage(@SessionAttribute(value = "User", required = false) Users user) {
+        if (Objects.isNull(user)) {
+            return new ResponseEntity<>(new UserMypageRes(false), HttpStatus.UNAUTHORIZED);
+        }
+        return new ResponseEntity<>(new UserMypageRes(true, user.getEmail(), user.getPassword(), user.getName(), user.getPhoneNumber()), HttpStatus.OK);
     }
 
     @PutMapping("/password")