diff --git a/src/main/java/com/developer/wiki/oauth/User.java b/src/main/java/com/developer/wiki/oauth/User.java index 0e1d7e0..754c204 100644 --- a/src/main/java/com/developer/wiki/oauth/User.java +++ b/src/main/java/com/developer/wiki/oauth/User.java @@ -51,4 +51,9 @@ public UserResponseDto toDto(){ public void updateRefreshToken(String token){ this.refreshToken=token; } + + public void changeUserName(String newUserName){ + if(newUserName.isEmpty()) throw new BadRequestException("userName is empty"); + this.name=newUserName; + } } diff --git a/src/main/java/com/developer/wiki/oauth/UserRepository.java b/src/main/java/com/developer/wiki/oauth/UserRepository.java index e1b8ff5..a90e49f 100644 --- a/src/main/java/com/developer/wiki/oauth/UserRepository.java +++ b/src/main/java/com/developer/wiki/oauth/UserRepository.java @@ -6,4 +6,5 @@ public interface UserRepository extends JpaRepository { Optional findByEmail(String email); Optional findByRefreshToken(String token); + Boolean existsByName(String userName); } diff --git a/src/main/java/com/developer/wiki/oauth/controller/UserController.java b/src/main/java/com/developer/wiki/oauth/controller/UserController.java index 2b27f03..c571b58 100644 --- a/src/main/java/com/developer/wiki/oauth/controller/UserController.java +++ b/src/main/java/com/developer/wiki/oauth/controller/UserController.java @@ -3,18 +3,28 @@ import com.developer.wiki.common.exception.BadRequestException; import com.developer.wiki.oauth.User; +import com.developer.wiki.oauth.dto.ImageDto; +import com.developer.wiki.oauth.dto.NicknameDto; import com.developer.wiki.oauth.dto.UserResponseDto; import com.developer.wiki.oauth.service.UserService; +import com.developer.wiki.oauth.util.AwsService; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; + +import javax.validation.Valid; +import java.io.IOException; +import java.util.Objects; +import java.util.UUID; @RequiredArgsConstructor @RestController @RequestMapping("/api/v1/user") public class UserController { private final UserService userService; + private final AwsService awsService; @GetMapping public ResponseEntity getUserInfo(@AuthenticationPrincipal User currentUser){ @@ -22,9 +32,37 @@ public ResponseEntity getUserInfo(@AuthenticationPrincipal User } @DeleteMapping("/{userId}") - public ResponseEntity deleteUSer(@AuthenticationPrincipal User currentUser, @PathVariable(name = "userId")Long userid){ + public ResponseEntity deleteUser(@AuthenticationPrincipal User currentUser, @PathVariable(name = "userId")Long userid){ if(!currentUser.getId().equals(userid)) throw new BadRequestException("Not Match Userid"); userService.deleteUser(currentUser.getId()); return ResponseEntity.ok("delete"); } + + + + @PostMapping("/nickname/{userId}") + public ResponseEntity changeUserName(@AuthenticationPrincipal User currentUser, + @PathVariable(name = "userId")Long userId, + @RequestBody @Valid NicknameDto nicknameDto){ + if(!currentUser.getId().equals(userId)) throw new BadRequestException("Not Match Userid"); + String newUserName=userService.updateUserName(nicknameDto.getUserName(),userId); + return ResponseEntity.ok(new NicknameDto(newUserName)); + } + + @PostMapping("/image/{userId}") + public ResponseEntity changeImg(@AuthenticationPrincipal User currentUser, @RequestParam("image") MultipartFile file, @PathVariable Long userId) throws IOException { + if(!currentUser.getId().equals(userId)) throw new BadRequestException("Not Match Userid"); + if(file.isEmpty()) throw new BadRequestException("파일은 Null이 될수 없습니다."); + String originalName=file.getOriginalFilename(); + System.out.println("!!!!!!!!!!!!!!!오리지널"+originalName); + String[] name=originalName.split("\\."); + System.out.println(name.length); + final String ext = name[1]; + final String saveFileName = getUuid() +"."+ ext; + System.out.println(saveFileName); + return ResponseEntity.ok(awsService.upload(saveFileName,file)); + } + private static String getUuid() { + return UUID.randomUUID().toString().replaceAll("-", ""); + } } diff --git a/src/main/java/com/developer/wiki/oauth/dto/ImageDto.java b/src/main/java/com/developer/wiki/oauth/dto/ImageDto.java new file mode 100644 index 0000000..27c4be7 --- /dev/null +++ b/src/main/java/com/developer/wiki/oauth/dto/ImageDto.java @@ -0,0 +1,14 @@ +package com.developer.wiki.oauth.dto; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class ImageDto { + private String imageUrl; +} diff --git a/src/main/java/com/developer/wiki/oauth/dto/NicknameDto.java b/src/main/java/com/developer/wiki/oauth/dto/NicknameDto.java new file mode 100644 index 0000000..71eee32 --- /dev/null +++ b/src/main/java/com/developer/wiki/oauth/dto/NicknameDto.java @@ -0,0 +1,19 @@ +package com.developer.wiki.oauth.dto; + +import lombok.*; + +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.Pattern; +import javax.validation.constraints.Size; + +@Getter +@Builder +@NoArgsConstructor +public class NicknameDto { + @NotBlank(message = "닉네임은 null일 수 없습니다.") + @Pattern(regexp="^(?=.*[a-z0-9가-힣])[a-z0-9가-힣]{2,10}$", message = "닉네임 조건 불일치") + private String userName; + public NicknameDto(String userName){ + this.userName=userName; + } +} diff --git a/src/main/java/com/developer/wiki/oauth/service/UserService.java b/src/main/java/com/developer/wiki/oauth/service/UserService.java index 5f133cf..b21106d 100644 --- a/src/main/java/com/developer/wiki/oauth/service/UserService.java +++ b/src/main/java/com/developer/wiki/oauth/service/UserService.java @@ -1,10 +1,14 @@ package com.developer.wiki.oauth.service; +import com.developer.wiki.common.exception.BadRequestException; +import com.developer.wiki.common.exception.NotFoundException; +import com.developer.wiki.oauth.User; import com.developer.wiki.oauth.UserRepository; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import javax.transaction.Transactional; +import java.util.Optional; @Service @RequiredArgsConstructor @@ -15,4 +19,13 @@ public void deleteUser(Long userId){ userRepository.deleteById(userId); } + @Transactional + public String updateUserName(String userName,Long userId){ + if(userRepository.existsByName(userName)) throw new BadRequestException("닉네임 중복입니다."); + User user=userRepository.findById(userId) + .orElseThrow(()-> new NotFoundException("유저가 없습니다.")); + user.changeUserName(userName); + return user.getName(); + } + }