From 2bb163ae739080b33715fca486b0c7b168cbd13f Mon Sep 17 00:00:00 2001 From: Seonrae Kim <134375389+raeraeraee@users.noreply.github.com> Date: Tue, 26 Sep 2023 12:05:25 +0900 Subject: [PATCH] =?UTF-8?q?[FEATURES]=20=EB=A7=88=EC=9D=B4=20=ED=8E=98?= =?UTF-8?q?=EC=9D=B4=EC=A7=80=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84=20?= =?UTF-8?q?(#66)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat : MyPage 개발 - 마이페이구현 * Refactor : 코드 Build되도록 수정 --- .../api/user/controller/MypageController.java | 48 +++++----- .../api/user/service/MypageService.java | 44 +++++++++- .../api/user/vo/request/MyPageRequestDto.java | 20 +++++ Api/src/main/resources/static/css/mypage.css | 22 +++++ Api/src/main/resources/static/js/mypage.js | 19 ++++ .../main/resources/templates/user/mypage.html | 88 +++++++++++++++---- .../domain/domains/user/entity/User.java | 1 + .../user/repository/MypageRepository.java | 7 -- 8 files changed, 202 insertions(+), 47 deletions(-) create mode 100644 Api/src/main/java/picasso/server/api/user/vo/request/MyPageRequestDto.java create mode 100644 Api/src/main/resources/static/css/mypage.css create mode 100644 Api/src/main/resources/static/js/mypage.js delete mode 100644 Domain/src/main/java/picasso/server/domain/domains/user/repository/MypageRepository.java diff --git a/Api/src/main/java/picasso/server/api/user/controller/MypageController.java b/Api/src/main/java/picasso/server/api/user/controller/MypageController.java index 410e184b..4df44856 100644 --- a/Api/src/main/java/picasso/server/api/user/controller/MypageController.java +++ b/Api/src/main/java/picasso/server/api/user/controller/MypageController.java @@ -8,47 +8,51 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.multipart.MultipartFile; import picasso.server.api.user.service.MypageService; import picasso.server.api.user.service.UserService; +import picasso.server.api.user.vo.request.MyPageRequestDto; import picasso.server.domain.domains.user.entity.User; -import java.util.HashMap; -import java.util.Map; -import java.util.Optional; - @Slf4j @Controller @RequestMapping("/user") @RequiredArgsConstructor public class MypageController { - private final UserService userService; private final MypageService mypageService; - // 마이페이지 수정 전 + @GetMapping("/mypage") - public String getUserByEmail(HttpSession session, Model model) { + public String showMyPage(HttpSession session, Model model) { User loginUser = (User) session.getAttribute("loginUser"); + if (loginUser == null) { - // TODO : 사용자 정보 없는 경우 Exception 처리 필요 (로그인을 하지 않은 상태이기 떄문) + return "redirect:/auth/login"; } - Optional optionalUser = userService.findUserById(loginUser.getId()); - if (optionalUser.isEmpty()) { - // TODO : 로그인 사용자(세션)는 존재하더라도, DB에서 현재 사용자 정보가 존재하지 않는 경우이기 떄문에 Exception처리가 필요 - } - model.addAttribute("user", optionalUser.get()); + User userDetail = mypageService.getUserDetail(loginUser.getId()); + model.addAttribute("user", userDetail); + return "user/mypage"; } - // TODO: 테스트용 Controller 입니다. 추후 삭제 혹은 변경이 필요합니다. - @ResponseBody - @PostMapping("/session-info") - public Map paymentSessionInfoRtn(HttpSession session) { - User temp = (User)session.getAttribute("loginUser"); - return new HashMap(){{ - put("userId", 1L); - }}; + + @PostMapping("/update") + public String handleEdit(MyPageRequestDto requestDto, HttpSession session, Model model) { + User loginUser = (User) session.getAttribute("loginUser"); + if (loginUser == null) { + return "redirect:/auth/login"; + } + + MultipartFile profileImage = requestDto.getProfile(); + + User user = mypageService.updateUserInfo(session, requestDto); + if (user == null) { + return "redirect:/error"; + } + session.setAttribute("loginUser", user); + model.addAttribute("user", user); + return "redirect:/user/mypage"; } } diff --git a/Api/src/main/java/picasso/server/api/user/service/MypageService.java b/Api/src/main/java/picasso/server/api/user/service/MypageService.java index 0df17e47..0823b7a1 100644 --- a/Api/src/main/java/picasso/server/api/user/service/MypageService.java +++ b/Api/src/main/java/picasso/server/api/user/service/MypageService.java @@ -1,17 +1,55 @@ package picasso.server.api.user.service; +import jakarta.servlet.http.HttpSession; import jakarta.transaction.Transactional; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; -import picasso.server.domain.domains.user.repository.MypageRepository; +import picasso.server.api.user.vo.request.MyPageRequestDto; +import picasso.server.common.exception.NotFoundException; +import picasso.server.common.util.NaverObjectStorageUsageType; +import picasso.server.common.util.NaverObjectStorageUtil; +import picasso.server.domain.domains.user.entity.User; +import picasso.server.domain.domains.user.repository.UserRepository; @Service @Transactional @RequiredArgsConstructor public class MypageService { - private final MypageRepository mypageRepository; + private final UserRepository userRepository; + private final NaverObjectStorageUtil naverObjectStorageUtil; + public User getUserDetail(Long id) { + return userRepository.findById(id).orElse(null); + } -} + /** + * + * @param session 현재 존재하는 Session + * @param updateRequestDto 수정해야 하는 정보 + * @return + */ + public User updateUserInfo(HttpSession session, MyPageRequestDto updateRequestDto) { + User sessionLoginUser = (User) session.getAttribute("loginUser"); + User databaseUserInfo = userRepository.findById(sessionLoginUser.getId()).orElseThrow(()-> NotFoundException.EXCEPTION); + + if(sessionLoginUser.getEmail().equals(databaseUserInfo.getEmail())) { + if(!databaseUserInfo.getNickName().equals(updateRequestDto.getChgNickName())) { + databaseUserInfo.setNickName(updateRequestDto.getChgNickName()); + } + + + if(!updateRequestDto.getProfile().isEmpty()) { + databaseUserInfo.setProfile( + naverObjectStorageUtil.storageFileUpload( + NaverObjectStorageUsageType.PROFILE, updateRequestDto.getProfile() + ) + ); + } + return databaseUserInfo; + } + + return null; + } +} \ No newline at end of file diff --git a/Api/src/main/java/picasso/server/api/user/vo/request/MyPageRequestDto.java b/Api/src/main/java/picasso/server/api/user/vo/request/MyPageRequestDto.java new file mode 100644 index 00000000..63507bda --- /dev/null +++ b/Api/src/main/java/picasso/server/api/user/vo/request/MyPageRequestDto.java @@ -0,0 +1,20 @@ +package picasso.server.api.user.vo.request; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import lombok.ToString; +import org.springframework.web.multipart.MultipartFile; + +@Getter +@Setter +@ToString +@AllArgsConstructor +@NoArgsConstructor +public class MyPageRequestDto { + private String orgNickName; + private String chgNickName; + private String isNewProfile; + private MultipartFile profile; +} diff --git a/Api/src/main/resources/static/css/mypage.css b/Api/src/main/resources/static/css/mypage.css new file mode 100644 index 00000000..5d181a7d --- /dev/null +++ b/Api/src/main/resources/static/css/mypage.css @@ -0,0 +1,22 @@ +div.container > div.mypage-container { + border-radius: 2%; + padding: 40px; + margin: 100px auto; + width: 80vh; + min-width: 800px; + background-color: rgba(220, 220, 220, 0.857); +} + +form#mypage-form > div.mypage-title { + text-align: center; + margin-bottom: 30px; +} + +.img-thumbnail { + width: 100%; + height: 300px; +} + +.col > button { + width: 100%; +} \ No newline at end of file diff --git a/Api/src/main/resources/static/js/mypage.js b/Api/src/main/resources/static/js/mypage.js new file mode 100644 index 00000000..5bc65cab --- /dev/null +++ b/Api/src/main/resources/static/js/mypage.js @@ -0,0 +1,19 @@ +function validateFileExtension(fileInput) { + const allowedExtensions = ['jpg', 'jpeg', 'png']; + const filenameDisplayElement = document.getElementById('filename'); + + if (fileInput.files.length > 0) { + const fileName = fileInput.files[0].name; + const fileExtension = fileName.split('.').pop().toLowerCase(); + + if (!allowedExtensions.includes(fileExtension)) { + alert("프로필 사진은 .jpg, .jpeg 또는 .png 확장자만 허용됩니다."); + fileInput.value = ''; // 파일 입력을 초기화합니다. + filenameDisplayElement.textContent = ''; + } else { + filenameDisplayElement.textContent = fileName; + } + } else { + filenameDisplayElement.textContent = ''; + } +} \ No newline at end of file diff --git a/Api/src/main/resources/templates/user/mypage.html b/Api/src/main/resources/templates/user/mypage.html index 45762e27..1fc874f9 100644 --- a/Api/src/main/resources/templates/user/mypage.html +++ b/Api/src/main/resources/templates/user/mypage.html @@ -1,20 +1,78 @@ - - - My Page - - -
- -
+ +
+ +
+
+ +
+

마이페이지

+
- -
+
+
+ ... +
- -
> - +
+
+ +
+ + +
+
- - +
+ +
+ +
+
+ +
+ +
+ + +
+
+
+
+ + +
+
+ +
+
+ +
+
+ +
+
+
+ +
+
+ +
+
+ +
+
+
+
+
+ + + + \ No newline at end of file diff --git a/Domain/src/main/java/picasso/server/domain/domains/user/entity/User.java b/Domain/src/main/java/picasso/server/domain/domains/user/entity/User.java index 076b1cb0..9e448662 100644 --- a/Domain/src/main/java/picasso/server/domain/domains/user/entity/User.java +++ b/Domain/src/main/java/picasso/server/domain/domains/user/entity/User.java @@ -95,6 +95,7 @@ public class User { private LocalDateTime loginAt = LocalDateTime.now(); @OneToMany(mappedBy = "user", fetch = FetchType.LAZY) + @Builder.Default private List pictures = new ArrayList<>(); public void updatePoint(Long point) { diff --git a/Domain/src/main/java/picasso/server/domain/domains/user/repository/MypageRepository.java b/Domain/src/main/java/picasso/server/domain/domains/user/repository/MypageRepository.java deleted file mode 100644 index 72707ff8..00000000 --- a/Domain/src/main/java/picasso/server/domain/domains/user/repository/MypageRepository.java +++ /dev/null @@ -1,7 +0,0 @@ -package picasso.server.domain.domains.user.repository; - -import org.springframework.data.jpa.repository.JpaRepository; -import picasso.server.domain.domains.user.entity.User; - -public interface MypageRepository extends JpaRepository { -}