Skip to content

Commit

Permalink
Feat : 유저 닉네임 수정하는 api 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
jhdl0157 committed Nov 25, 2022
1 parent 1cd810e commit 2faa0d5
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/main/java/com/developer/wiki/oauth/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
1 change: 1 addition & 0 deletions src/main/java/com/developer/wiki/oauth/UserRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
public interface UserRepository extends JpaRepository<User,Long> {
Optional<User> findByEmail(String email);
Optional<User> findByRefreshToken(String token);
Boolean existsByName(String userName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,66 @@

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<UserResponseDto> getUserInfo(@AuthenticationPrincipal User currentUser){
return ResponseEntity.ok().body(currentUser.toDto());
}

@DeleteMapping("/{userId}")
public ResponseEntity<String> deleteUSer(@AuthenticationPrincipal User currentUser, @PathVariable(name = "userId")Long userid){
public ResponseEntity<String> 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<NicknameDto> 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<String> 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("-", "");
}
}
14 changes: 14 additions & 0 deletions src/main/java/com/developer/wiki/oauth/dto/ImageDto.java
Original file line number Diff line number Diff line change
@@ -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;
}
19 changes: 19 additions & 0 deletions src/main/java/com/developer/wiki/oauth/dto/NicknameDto.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
13 changes: 13 additions & 0 deletions src/main/java/com/developer/wiki/oauth/service/UserService.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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();
}

}

0 comments on commit 2faa0d5

Please sign in to comment.