-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from TEAM-ALOM/4-feat-마이페이지를-구현합니다
4 feat 마이페이지를 구현합니다
- Loading branch information
Showing
14 changed files
with
309 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/main/java/com/alom/dorundorunbe/domain/Achievement/domain/AchievementRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.alom.dorundorunbe.domain.Achievement.domain; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.List; | ||
|
||
public interface AchievementRepository extends JpaRepository<Achievement, Long> { | ||
List<UserAchievement> findAllByUserName(String name); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/main/java/com/alom/dorundorunbe/domain/RunningRecord/domain/RunningRecordRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.alom.dorundorunbe.domain.RunningRecord.domain; | ||
|
||
import com.alom.dorundorunbe.domain.user.domain.User; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.List; | ||
|
||
public interface RunningRecordRepository extends JpaRepository<RunningRecord, Long> { | ||
List<RunningRecord> findAllByUser(User user); | ||
} |
78 changes: 78 additions & 0 deletions
78
src/main/java/com/alom/dorundorunbe/domain/mypage/controller/MyPageController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package com.alom.dorundorunbe.domain.mypage.controller; | ||
|
||
|
||
import com.alom.dorundorunbe.domain.RunningRecord.domain.RunningRecord; | ||
import com.alom.dorundorunbe.domain.mypage.dto.*; | ||
import com.alom.dorundorunbe.domain.user.domain.User; | ||
import com.alom.dorundorunbe.domain.user.domain.UserRepository; | ||
import com.alom.dorundorunbe.domain.mypage.*; | ||
import com.alom.dorundorunbe.domain.mypage.service.MyPageService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.DeleteMapping; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PutMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
@Controller | ||
public class MyPageController { | ||
@Autowired | ||
private UserRepository userRepository; | ||
|
||
@Autowired | ||
private MyPageService myPageService; | ||
|
||
@GetMapping("/myPage") | ||
public ResponseEntity<?> myPage(){ | ||
String username = SecurityContextHolder.getContext().getAuthentication().getName(); | ||
Optional<User> optionalUser = userRepository.findByUserName(username); | ||
if(optionalUser.isPresent()){ | ||
User user = optionalUser.get(); | ||
List<AchievementResponse> achievementResponses = myPageService.getAchievements(username); | ||
String rank = user.getRanking().getRank().toString(); | ||
List<RunningRecord> runningRecords = myPageService.getRunningRecords(username); | ||
|
||
MyPageResponse myPageResponse = new MyPageResponse(user.getName(), user.getEmail(), achievementResponses, rank, runningRecords); | ||
return new ResponseEntity<>(myPageResponse, HttpStatus.OK); | ||
} | ||
else{ | ||
return ResponseEntity.status(HttpStatus.NOT_FOUND) | ||
.body("User not found"); | ||
} | ||
} | ||
|
||
@PutMapping("/myPage/updateUser") | ||
public ResponseEntity<String> updateUser(@RequestBody UserUpdateDTO userDTO){ | ||
String username = SecurityContextHolder.getContext().getAuthentication().getName(); | ||
Optional<User> optionalUser = userRepository.findByUserName(username); | ||
if(optionalUser.isPresent()) | ||
return myPageService.updateByName(userDTO, username); | ||
else return ResponseEntity.status(HttpStatus.NOT_FOUND).body("User not found"); | ||
} | ||
|
||
@PutMapping("/myPage/changePassword") | ||
public ResponseEntity<String> changePassword(@RequestBody UserPasswordChangeDTO userPasswordChangeDTO){ | ||
String username = SecurityContextHolder.getContext().getAuthentication().getName(); | ||
Optional<User> optionalUser = userRepository.findByUserName(username); | ||
if(optionalUser.isPresent()) | ||
return myPageService.updatePassword(userPasswordChangeDTO, username); | ||
else return ResponseEntity.status(HttpStatus.NOT_FOUND).body("User not found"); | ||
} | ||
|
||
@DeleteMapping("/myPage/deleteUser") | ||
public ResponseEntity<String> deleteUser(@RequestBody UserDeleteDTO userDeleteDTO){ | ||
String username = SecurityContextHolder.getContext().getAuthentication().getName(); | ||
Optional<User> optionalUser = userRepository.findByUserName(username); | ||
if(optionalUser.isPresent()) | ||
return myPageService.deleteUser(userDeleteDTO, username); | ||
else return ResponseEntity.status(HttpStatus.NOT_FOUND).body("User not found"); | ||
} | ||
|
||
|
||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/alom/dorundorunbe/domain/mypage/dto/AchievementResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.alom.dorundorunbe.domain.mypage.dto; | ||
|
||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter@Setter | ||
@AllArgsConstructor | ||
public class AchievementResponse { | ||
private Long achievementId; | ||
private String achievementName; | ||
private String achievementDescription; | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/com/alom/dorundorunbe/domain/mypage/dto/MyPageResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.alom.dorundorunbe.domain.mypage.dto; | ||
|
||
import com.alom.dorundorunbe.domain.RunningRecord.domain.RunningRecord; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
import java.util.List; | ||
|
||
@Getter | ||
@Setter | ||
public class MyPageResponse { | ||
|
||
private String name; | ||
private String email; | ||
private List<AchievementResponse> achievements; | ||
private String rank; | ||
private List<RunningRecord> runningRecords; | ||
|
||
public MyPageResponse(String name, String email, List<AchievementResponse> achievements, String rank, | ||
List<RunningRecord> runningRecords) { | ||
this.name = name; | ||
this.email = email; | ||
this.achievements = achievements; | ||
this.rank = rank; | ||
this.runningRecords = runningRecords; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/alom/dorundorunbe/domain/mypage/dto/UserDeleteDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.alom.dorundorunbe.domain.mypage.dto; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class UserDeleteDTO { | ||
private String password; | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/alom/dorundorunbe/domain/mypage/dto/UserPasswordChangeDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.alom.dorundorunbe.domain.mypage.dto; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter@Setter | ||
public class UserPasswordChangeDTO { | ||
private String oldPassword; | ||
private String newPassword; | ||
private String newPasswordConfirmation; | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/alom/dorundorunbe/domain/mypage/dto/UserUpdateDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.alom.dorundorunbe.domain.mypage.dto; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
public class UserUpdateDTO { | ||
private String nickname; | ||
private String name; | ||
private Integer age; | ||
} |
106 changes: 106 additions & 0 deletions
106
src/main/java/com/alom/dorundorunbe/domain/mypage/service/MyPageService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package com.alom.dorundorunbe.domain.mypage.service; | ||
|
||
import com.alom.dorundorunbe.domain.RunningRecord.domain.RunningRecord; | ||
import com.alom.dorundorunbe.domain.RunningRecord.domain.RunningRecordRepository; | ||
import com.alom.dorundorunbe.domain.Achievement.domain.AchievementRepository; | ||
import com.alom.dorundorunbe.domain.Achievement.domain.UserAchievement; | ||
import com.alom.dorundorunbe.domain.user.domain.User; | ||
import com.alom.dorundorunbe.domain.user.domain.UserRepository; | ||
import com.alom.dorundorunbe.domain.mypage.dto.AchievementResponse; | ||
import com.alom.dorundorunbe.domain.mypage.dto.UserDeleteDTO; | ||
import com.alom.dorundorunbe.domain.mypage.dto.UserPasswordChangeDTO; | ||
import com.alom.dorundorunbe.domain.mypage.dto.UserUpdateDTO; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Service; | ||
import java.util.Collections; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
@Service | ||
public class MyPageService { | ||
@Autowired | ||
private UserRepository userRepository; | ||
|
||
@Autowired | ||
private RunningRecordRepository runningRecordRepository; | ||
|
||
@Autowired | ||
private AchievementRepository achievementRepository; | ||
|
||
public List<RunningRecord> getRunningRecords(String username) { | ||
Optional<User> userOpt = userRepository.findByUserName(username); | ||
if (userOpt.isPresent()) { | ||
User user = userOpt.get(); | ||
List<RunningRecord> runningRecords = runningRecordRepository.findAllByUser(user); | ||
Collections.sort(runningRecords, Comparator.comparing(RunningRecord::getDate).reversed()); | ||
return runningRecords; | ||
} | ||
else return null; | ||
} | ||
public List<AchievementResponse> getAchievements(String username) { | ||
List<UserAchievement> userAchievements = achievementRepository.findAllByUserName(username); | ||
List<AchievementResponse> achievementResponses = userAchievements.stream() | ||
.map(ua->new AchievementResponse( | ||
ua.getAchievement().getId(), | ||
ua.getAchievement().getName(), | ||
ua.getAchievement().getDescription() | ||
)) | ||
.collect(Collectors.toList()); | ||
return achievementResponses; | ||
} | ||
public boolean checkNameDuplicate(String username) {return userRepository.existsByName(username);} | ||
public boolean checkNickNameDuplicate(String nickName) {return userRepository.existsByNickName(nickName);} | ||
public boolean checkPasswordDuplicate(String password){return userRepository.existsByPassword(password);} | ||
public ResponseEntity<String> updateByName(UserUpdateDTO userDTO, String username) { | ||
User existingUser = userRepository.findByUserName(username).get(); | ||
if(userDTO.getName() == null) | ||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Name is required"); | ||
if(checkNameDuplicate(userDTO.getName())) | ||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Username already exists"); | ||
if(userDTO.getAge() == null) | ||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Age is required"); | ||
if(userDTO.getAge() <= 0) | ||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Age must be greater than zero"); | ||
if(userDTO.getNickname() == null) | ||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Nickname is required"); | ||
if(checkNickNameDuplicate(userDTO.getNickname())) | ||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Nickname already exists"); | ||
existingUser.setNickname(userDTO.getNickname()); | ||
existingUser.setAge(userDTO.getAge()); | ||
existingUser.setName(userDTO.getName()); | ||
userRepository.save(existingUser); | ||
return ResponseEntity.status(HttpStatus.OK).body("User updated successfully"); | ||
} | ||
public ResponseEntity<String> updatePassword(UserPasswordChangeDTO userPasswordChangeDTO, String username){ | ||
User existingUser = userRepository.findByUserName(username).get(); | ||
if(userPasswordChangeDTO.getOldPassword() == null) | ||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Old password is required"); | ||
if(userPasswordChangeDTO.getNewPassword() == null) | ||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("New password is required"); | ||
if(userPasswordChangeDTO.getNewPassword().length() < 8) | ||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("New password must be at least 8 length"); | ||
if(userPasswordChangeDTO.getNewPasswordConfirmation() == null) | ||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("New password confirmation is required"); | ||
if(!userPasswordChangeDTO.getNewPassword().equals(userPasswordChangeDTO.getNewPasswordConfirmation())) | ||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("New password does not match"); | ||
if(!userPasswordChangeDTO.getOldPassword().equals(existingUser.getPassword())) | ||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Old password does not match"); | ||
if(checkPasswordDuplicate(userPasswordChangeDTO.getNewPassword())) | ||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("New password already exists"); | ||
existingUser.setPassword(userPasswordChangeDTO.getNewPassword()); | ||
userRepository.save(existingUser); | ||
return ResponseEntity.status(HttpStatus.OK).body("User Password updated successfully"); | ||
} | ||
|
||
public ResponseEntity<String> deleteUser(UserDeleteDTO userDeleteDTO, String username) { | ||
User existingUser = userRepository.findByUserName(username).get(); | ||
if(!userDeleteDTO.getPassword().equals(existingUser.getPassword())) | ||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Password does not match"); | ||
userRepository.delete(existingUser); | ||
return ResponseEntity.status(HttpStatus.OK).body("User deleted successfully"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/main/java/com/alom/dorundorunbe/domain/user/domain/UserRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.alom.dorundorunbe.domain.user.domain; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.Optional; | ||
|
||
public interface UserRepository extends JpaRepository<User, Long> { | ||
Optional<User> findByUserName(String username); | ||
boolean existsByName(String username); | ||
boolean existsByPassword(String password); | ||
boolean existsByNickName(String nickName); | ||
} |