Skip to content

Commit

Permalink
Merge pull request #120 from seniors-project/feature/#119
Browse files Browse the repository at this point in the history
Users 도메인에 대한 내 정보 조회, 수정 API 개발
  • Loading branch information
char-yb authored Nov 8, 2023
2 parents 51ebe6c + 78be0a1 commit 174b8b5
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/seniors/domain/resume/dto/ResumeDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public static class GetResumeRes {


@Builder
private GetResumeRes(Resume resume) {
public GetResumeRes(Resume resume) {
this.id = resume.getId();
this.introduce = resume.getIntroduce();
this.photoUrl = resume.getPhotoUrl();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package com.seniors.domain.users.controller;

import com.seniors.common.annotation.LoginUsers;
import com.seniors.common.constant.ResultCode;
import com.seniors.common.dto.DataResponseDto;
import com.seniors.common.dto.ResponseDto;
import com.seniors.common.dto.ErrorResponse;
import com.seniors.common.exception.type.BadRequestException;
import com.seniors.common.exception.type.NotAuthorizedException;
import com.seniors.common.exception.type.NotFoundException;
import com.seniors.config.security.CustomUserDetails;
import com.seniors.domain.post.dto.PostDto;
import com.seniors.domain.users.dto.UsersDto.GetUserDetailRes;
import com.seniors.domain.users.dto.UsersDto.SetUserDto;
import com.seniors.domain.users.service.UsersService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
Expand All @@ -16,10 +21,10 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Tag(name = "사용자", description = "사용자 API 명세서")
@Slf4j
Expand All @@ -28,6 +33,8 @@
@RequestMapping("/api/users")
public class UsersController {

private final UsersService usersService;

/**
* 유저 검증 API
* ex) 글쓰기 페이지 접속 시 검증 API를 사용하여
Expand All @@ -49,5 +56,46 @@ public ResponseEntity<?> userValidate(
: new ResponseEntity<>(userDetails, HttpStatus.OK);
}

@Operation(summary = "유저 정보 조회")
@ApiResponse(responseCode = "200", description = "단건 조회 성공",
content = @Content(mediaType = "application/json", schema = @Schema(implementation = GetUserDetailRes.class)))
@ApiResponse(responseCode = "401", description = "유효하지 않은 회원입니다.",
content = @Content(mediaType = "application/json", schema = @Schema(implementation = NotAuthorizedException.class)))
@ApiResponse(responseCode = "404", description = "사용자가 존재하지 않습니다.",
content = @Content(mediaType = "application/json", schema = @Schema(implementation = NotFoundException.class)))
@ApiResponse(responseCode = "500", description = "서버 에러.",
content = @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class)))
@GetMapping("")
public DataResponseDto<GetUserDetailRes> usersDetails(
@Parameter(hidden = true) @LoginUsers CustomUserDetails userDetails
) {
GetUserDetailRes getUserRes = usersService.findOneUsers(userDetails.getUserId(), userDetails.getUserSnsId(),
userDetails.getUserNickname(), userDetails.getProfileImageUrl(),
userDetails.getUserEmail(), userDetails.getGender());
return DataResponseDto.of(getUserRes);
}

@Operation(summary = "유저 정보 수정")
@io.swagger.v3.oas.annotations.parameters.RequestBody(description = "수정 요청 body",
content = @Content(mediaType = "application/json", schema = @Schema(implementation = SetUserDto.class)))
@ApiResponse(responseCode = "200", description = "단건 수정 성공",
content = @Content(mediaType = "application/json", schema = @Schema(implementation = DataResponseDto.class)))
@ApiResponse(responseCode = "400", description = "유효성 검증 실패",
content = @Content(mediaType = "application/json", schema = @Schema(implementation = BadRequestException.class)))
@ApiResponse(responseCode = "401", description = "유효하지 않은 회원입니다.",
content = @Content(mediaType = "application/json", schema = @Schema(implementation = NotAuthorizedException.class)))
@ApiResponse(responseCode = "404", description = "사용자가 존재하지 않습니다.",
content = @Content(mediaType = "application/json", schema = @Schema(implementation = NotFoundException.class)))
@ApiResponse(responseCode = "500", description = "서버 에러.",
content = @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class)))
@PatchMapping("")
public DataResponseDto<?> usersModify(
@Parameter(hidden = true) @LoginUsers CustomUserDetails userDetails,
@RequestPart(value = "data") SetUserDto setUserDto,
@RequestPart(value = "profileImage", required = false) MultipartFile profileImage
) throws IOException {
usersService.modifyUsers(userDetails.getUserId(), userDetails.getProfileImageUrl(), setUserDto, profileImage);
return DataResponseDto.of("SUCCESS");
}

}
42 changes: 42 additions & 0 deletions src/main/java/com/seniors/domain/users/dto/UsersDto.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.seniors.domain.users.dto;

import com.seniors.domain.chat.dto.ChatRoomMembersDto.GetChatRoomMembersRes;
import com.seniors.domain.resume.dto.ResumeDto.GetResumeRes;
import com.seniors.domain.resume.entity.Resume;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;

Expand Down Expand Up @@ -59,4 +61,44 @@ public GetChatUserRes(Long userId, List<GetChatRoomMembersRes> chatRoomMembers)
}


@Data
public static class GetUserDetailRes {
@Schema(description = "사용자 ID")
private Long userId;

@Schema(description = "사용자 sns ID")
private String snsId;

@Schema(description = "닉네임")
private String nickname;

@Schema(description = "프로필 이미지 url")
private String profileImageUrl;

@Schema(description = "이메일")
private String email;

@Schema(description = "성별")
private String gender;

@Schema(description = "사용자 이력서")
private GetResumeRes resume;

public GetUserDetailRes(Resume resume, Long userId, String snsId, String nickname, String profileImageUrl, String email, String gender) {
this.userId = userId;
this.snsId = snsId;
this.nickname = nickname;
this.profileImageUrl = profileImageUrl;
this.email = email;
this.gender = gender;
this.resume = new GetResumeRes(resume);
}
}

@Data
public static class SetUserDto {
@Schema(description = "닉네임")
private String nickname;
}

}
1 change: 1 addition & 0 deletions src/main/java/com/seniors/domain/users/entity/Users.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.seniors.domain.notification.entity.Notification;
import com.seniors.domain.post.entity.Post;
import com.seniors.domain.post.entity.PostLike;
import com.seniors.domain.resume.entity.Resume;
import jakarta.persistence.*;
import lombok.*;
import org.hibernate.annotations.BatchSize;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package com.seniors.domain.users.repository;


import com.seniors.domain.users.dto.UsersDto.GetUserDetailRes;
import com.seniors.domain.users.dto.UsersDto.SetUserDto;
import com.seniors.domain.users.entity.Users;


public interface UsersRepositoryCustom {

Users getOneUsers(Long userId);

GetUserDetailRes getUserDetails(Long userId, String snsId, String nickname, String profileImageUrl, String email, String gender);

void modifyUser(Long userId, SetUserDto setUserDto, String profileImageUrl);
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package com.seniors.domain.users.repository;

import com.querydsl.jpa.impl.JPAQueryFactory;
import com.seniors.common.exception.type.NotFoundException;
import com.seniors.common.repository.BasicRepoSupport;
import com.seniors.domain.resume.entity.Resume;
import com.seniors.domain.users.dto.UsersDto.GetUserDetailRes;
import com.seniors.domain.users.dto.UsersDto.SetUserDto;
import com.seniors.domain.users.entity.QUsers;
import com.seniors.domain.users.entity.Users;
import jakarta.persistence.EntityManager;

import static com.seniors.domain.resume.entity.QResume.resume;

public class UsersRepositoryImpl extends BasicRepoSupport implements UsersRepositoryCustom {

protected UsersRepositoryImpl(JPAQueryFactory jpaQueryFactory, EntityManager em) {
Expand All @@ -16,11 +22,33 @@ protected UsersRepositoryImpl(JPAQueryFactory jpaQueryFactory, EntityManager em)

@Override
public Users getOneUsers(Long userId) {
Users user = jpaQueryFactory
return jpaQueryFactory
.selectFrom(QUsers.users)
.where(QUsers.users.id.eq(userId))
.fetchOne();
return user;
}

@Override
public GetUserDetailRes getUserDetails(Long userId, String snsId, String nickname, String profileImageUrl, String email, String gender) {
Resume detailResume = jpaQueryFactory
.selectFrom(resume)
.where(resume.users.id.eq(userId).and(resume.users.snsId.eq(snsId)))
.fetchOne();

if (detailResume == null) {
throw new NotFoundException("Resume Not Found");
}

return new GetUserDetailRes(detailResume, userId, snsId, nickname, profileImageUrl, email, gender);
}

@Override
public void modifyUser(Long userId, SetUserDto setUserDto, String profileImageUrl) {
jpaQueryFactory.update(users)
.set(users.nickname, setUserDto.getNickname())
.set(users.profileImageUrl, profileImageUrl)
.where(users.id.eq(userId))
.execute();
}

}
30 changes: 30 additions & 0 deletions src/main/java/com/seniors/domain/users/service/UsersService.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,43 @@
package com.seniors.domain.users.service;

import com.seniors.config.S3Uploader;
import com.seniors.domain.users.dto.UsersDto.GetUserDetailRes;
import com.seniors.domain.users.dto.UsersDto.SetUserDto;
import com.seniors.domain.users.repository.UsersRepository;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Service
@RequiredArgsConstructor
public class UsersService {

private final UsersRepository usersRepository;
private final S3Uploader s3Uploader;

public GetUserDetailRes findOneUsers(Long userId, String snsId, String nickname, String profileImageUrl, String email, String gender) {

return usersRepository.getUserDetails(userId,
snsId,
nickname,
profileImageUrl,
email,
gender);
}

@Transactional
public void modifyUsers(Long userId, String curProfileImage, SetUserDto setUserDto, MultipartFile profileImage) throws IOException {
String dirName = "users/profileImage/" + userId.toString();
String uploadImagePath = curProfileImage;
if (profileImage != null) {
// 기존 미디어 파일 삭제
s3Uploader.deleteS3Object(dirName);
uploadImagePath = s3Uploader.upload(profileImage, dirName);
}

usersRepository.modifyUser(userId, setUserDto, uploadImagePath);
}
}

0 comments on commit 174b8b5

Please sign in to comment.