Skip to content

Commit

Permalink
#92 DAILYLIFE2-46 by dm
Browse files Browse the repository at this point in the history
  • Loading branch information
shihhhdm310 committed Aug 20, 2022
1 parent 6197869 commit 5478e79
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@ public interface FileService {

String saveImg(MultipartFile[] imgName) throws IOException;

String singleSaveImg(MultipartFile imaName) throws IOException;



}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.dailylife.domain.user.entity.User;
import com.dailylife.domain.user.repository.UserRepository;
import com.dailylife.global.fileUpload.MultiUpload;
import com.dailylife.global.fileUpload.SingleUpload;
import com.dailylife.global.jwt.service.JwtService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -26,10 +27,17 @@
public class FileServiceImpl implements FileService {
private final ImageRepository imageRepository;
private final MultiUpload multiUpload;
private final SingleUpload singleUpload;
@Override
public String saveImg(MultipartFile[] imgName) throws IOException{
List<String> images = multiUpload.FileUpload(imgName);
System.out.println(images);
return null;
}

@Override
public String singleSaveImg(MultipartFile imaName) throws IOException {
return singleUpload.FileUpload(imaName);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class UserController {

@ApiOperation(value = "회원가입", notes = "회원가입을 합니다.")
@PostMapping("/join")
public ApplicationResponse<User> saveUser(@Valid @RequestBody UserJoinRequest userJoinRequest) {
public ApplicationResponse<User> saveUser(@Valid @ModelAttribute UserJoinRequest userJoinRequest) throws IOException {
return ApplicationResponse.create("회원가입이 완료되었습니다." , 200 , userService.join(userJoinRequest));
}

Expand Down Expand Up @@ -62,7 +62,11 @@ public ApplicationResponse<List<User>> findUser(@Valid UserPagination userPagina
return ApplicationResponse.create("유저에 대한 정보입니다." , 250, userService.findUser(userPagination));
}


@ApiOperation(value = "userNum반환" , notes = "토큰값을 통해서 user를 찾아서 userNum을 반환")
@PostMapping("/getUserNum")
public ApplicationResponse<Long> getUserNum() {
return ApplicationResponse.create("userNum을 반환합니다." , 260 , userService.getUserNum());
}


@PostMapping("/post")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import org.apache.tomcat.jni.Local;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotBlank;
import java.time.LocalDateTime;
Expand Down Expand Up @@ -40,7 +41,6 @@ public class UserJoinRequest {

private LocalDateTime userJoinDate;

private String userProfileImg;

private MultipartFile userProfileImg;

}
3 changes: 2 additions & 1 deletion src/main/java/com/dailylife/domain/user/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,15 @@ public class User {



public static User toEntity(UserJoinRequest userJoinRequest) {
public static User toEntity(UserJoinRequest userJoinRequest, String imaName) {
return User.builder()
.userId(userJoinRequest.getUserId())
.userPassword(userJoinRequest.getUserPassword())
.userName(userJoinRequest.getUserName())
.userPhoneNumber(userJoinRequest.getUserPhoneNumber())
.userEmail(userJoinRequest.getUserEmail())
.userJoinDate(LocalDateTime.now())
.userProfileImg(imaName)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.dailylife.domain.user.exception;

public class AlreadyUserIdException extends UserException{
public AlreadyUserIdException() {
super(5002, "이미 존재하는 아이디 입니다.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

public interface UserService {

User join(UserJoinRequest userJoinRequest);
User join(UserJoinRequest userJoinRequest) throws IOException;

User login(UserLoginRequest userLoginRequest);

Expand All @@ -26,5 +26,5 @@ public interface UserService {

List<User> findUser(UserPagination userPagination);


Long getUserNum();
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.dailylife.domain.user.dto.UserModifyRequest;
import com.dailylife.domain.user.dto.UserPagination;
import com.dailylife.domain.user.entity.User;
import com.dailylife.domain.user.exception.AlreadyUserIdException;
import com.dailylife.domain.user.exception.NotFoundUserException;
import com.dailylife.domain.user.exception.UserException;
import com.dailylife.domain.user.repository.UserPaginationRepository;
Expand All @@ -27,21 +28,21 @@
public class UserServiceImpl implements UserService {

private final UserRepository userRepository;

private final UserPaginationRepository userPaginationRepository;
private final SpringSecurityConfig springSecurity;
private final SingleUpload singleUpload;
private final JwtService jwtService;

@Override
@Transactional
public User join(UserJoinRequest userJoinRequest) {
if(userRepository.countByUserId(userJoinRequest.getUserId()) !=0){
throw new RuntimeException("이미 존재하는 ID입니다");
}
public User join(UserJoinRequest userJoinRequest) throws IOException{
if(userRepository.countByUserId(userJoinRequest.getUserId()) !=0) throw new AlreadyUserIdException();

String enPw = springSecurity.passwordEncoder().encode(userJoinRequest.getUserPassword()); // 스프링시큐리티로 pw 암호화
userJoinRequest.setUserPassword(enPw);
User user = userRepository.save(User.toEntity(userJoinRequest));

User user = userRepository.save(User.toEntity(userJoinRequest , singleUpload.FileUpload(userJoinRequest.getUserProfileImg())));

return user;
}

Expand Down Expand Up @@ -105,6 +106,11 @@ public List<User> findUser(UserPagination userPagination) {

}

@Override
public Long getUserNum() {
return userRepository.findByUserId(jwtService.getLoginId()).getUserNum();
}


}

17 changes: 10 additions & 7 deletions src/main/java/com/dailylife/global/fileUpload/SingleUpload.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,22 @@ public class SingleUpload {

public String FileUpload(MultipartFile file) throws IOException {

String uploadPath = "/profileImg"; // 우선 절대경로 설정
String uploadPath = "/home/ubuntu/images"; // 우선 절대경로 설정

File target = new File(uploadPath);
if (!target.exists()) target.mkdirs(); // 파일 경로에 폴더 없으면 새로운 폴더 생성

String saveFileName = "default";

String orgFileName = file.getOriginalFilename(); //파일 실제이름
String orgFileExtension = orgFileName.substring(orgFileName.lastIndexOf(".")); // 파일 확장자 exe같은거
String saveFileName = UUID.randomUUID().toString().replaceAll("-", "") + orgFileExtension; // 파일 랜덤이름
Long saveFileSize = file.getSize();
if(file != null) {
String orgFileName = file.getOriginalFilename(); //파일 실제이름
String orgFileExtension = orgFileName.substring(orgFileName.lastIndexOf(".")); // 파일 확장자 exe같은거
saveFileName= UUID.randomUUID().toString().replaceAll("-", "") + orgFileExtension; // 파일 랜덤이름
Long saveFileSize = file.getSize();
target = new File(uploadPath, saveFileName);
file.transferTo(target);
}

target = new File(uploadPath, saveFileName);
file.transferTo(target);

return saveFileName;
}
Expand Down

0 comments on commit 5478e79

Please sign in to comment.