Skip to content

Commit

Permalink
Update: ENTITY 연관관계 및 CASCADE 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
BYEONGRYEOL committed May 28, 2024
1 parent 5d6c58e commit a24e6ad
Show file tree
Hide file tree
Showing 17 changed files with 221 additions and 102 deletions.
102 changes: 59 additions & 43 deletions sql/setup-dev.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,32 @@ set @deactivatedUserId := 7; # 비활성화된 유저의 UserId
set @deactivatedCreatorId := 8; # 비활성화된 공급자의 CreatorId

set @creatorId := 1; # 공급자의 CreatorId

set @adminCreatorId := 3; # 어드민의 공급자 id
insert ignore into user (id, created_at, modified_at, deleted_at, email, email_verified, introduction,
last_login_social_platform, login_id, nickname, password, user_role, roles, user_status,
username, profile_picture_id)
username, sex)

VALUES (1, localtime, localtime, null, 'admin@gmail.com', true, null, 'GOOGLE', null, '어드민테스트1_닉네임', null, 'ROLE_ADMIN',
'ROLE_ADMIN,ROLE_MANAGER,ROLE_CREATOR,ROLE_USER', 'ACTIVATED', '어드민이름', null),
(2, localtime, localtime, null, 'user@gmail.com', true, '유저_소개1', 'GOOGLE', null, '유저테스트1_닉네임', null,
'ROLE_USER', 'ROLE_USER', 'ACTIVATED', '유저이름1', null),
'ROLE_ADMIN,ROLE_MANAGER,ROLE_CREATOR,ROLE_USER', 'ACTIVATED', '어드민이름', 'NONE'),
(2, localtime, localtime, null, 'user@gmail.com', true, '유저_소개1', 'GOOGLE', null, '유저테스`트1_닉네임', null,
'ROLE_USER', 'ROLE_USER', 'ACTIVATED', '유저이름1', 'M'),
(3, localtime, localtime, null, 'emptyUser@gmail.com', true, '유저_소개2', 'GOOGLE', null, '유저테스트1_닉네임', null,
'ROLE_USER', 'ROLE_USER', 'ACTIVATED', '유저이름2', null),
'ROLE_USER', 'ROLE_USER', 'ACTIVATED', '유저이름2', 'W'),
(4, localtime, localtime, null, 'creator@gmail.com', true, '공급자_소개', 'GOOGLE', null, '유저테스트2_닉네임', null,
'ROLE_CREATOR', 'ROLE_CREATOR', 'ACTIVATED', '공급자이름1', null),
'ROLE_CREATOR', 'ROLE_CREATOR', 'ACTIVATED', '공급자이름1', 'M'),
(5, localtime, localtime, null, 'emptyCreator@gmail.com', true, '공급자_소개2', 'GOOGLE', null, '공급자테스트2_닉네임', null,
'ROLE_CREATOR', 'ROLE_CREATOR', 'ACTIVATED', '공급자이름2', null),
'ROLE_CREATOR', 'ROLE_CREATOR', 'ACTIVATED', '공급자이름2', 'M'),
(6, localtime, localtime, null, 'oauthFirstJoin@gmail.com', true, '최초가입자_소개', 'GOOGLE', null, '최초가입자_닉네임', null,
'ROLE_OAUTH_FIRST_JOIN', 'ROLE_OAUTH_FIRST_JOIN,ROLE_USER', 'ACTIVATED', '최초가입자이름', null),
'ROLE_OAUTH_FIRST_JOIN', 'ROLE_OAUTH_FIRST_JOIN,ROLE_USER', 'ACTIVATED', '최초가입자이름', 'M'),
(7, localtime, localtime, null, 'deactivatedUser@gmail.com', true, '최초가입자_소개', 'GOOGLE', null, '비활성화된유저_닉네임',
null, 'ROLE_USER', 'ROLE_USER', 'DEACTIVATED', '비활성화된유저이름', null),
null, 'ROLE_USER', 'ROLE_USER', 'DEACTIVATED', '비활성화된유저이름', 'M'),
(8, localtime, localtime, null, 'deactivatedCreator@gmail.com', true, '최초가입자_소개', 'GOOGLE', null, '비활성화된공급자_닉네임',
null, 'ROLE_CREATOR', 'ROLE_CREATOR', 'DEACTIVATED', '비활성화된공급자이름', null);
null, 'ROLE_CREATOR', 'ROLE_CREATOR', 'DEACTIVATED', '비활성화된공급자이름', 'M');

insert ignore into creator (id, user_id, workable, created_at, modified_at)
values (1, 4, true, localtime, localtime),
(2, 5, false, localtime, localtime);
insert ignore into creator (id, created_at, modified_at, account_holder, account_number, bank_type, workable, user_id)
values (1, localtime, localtime, '서병렬', '123-4142-2523331-123', '국민은행', true, 4),
(2, localtime, localtime, null, null, null, false, 5),
(3, localtime, localtime, null, null, null, true, 1);

insert ignore into picture_pose (id, created_at, modified_at, url, uploaded_by)
values (1, LOCALTIME, LOCALTIME, 'pose_picture_url1', 2),
Expand All @@ -54,17 +55,27 @@ values (1, LOCALTIME, LOCALTIME, 'user_face_picture_url1', 2),
(9, LOCALTIME, LOCALTIME, 'user_face_picture_url3', 2);



insert ignore into picture_generate_request (id, creator_id, picture_pose_id, requester_id, prompt, camera_angle,
insert ignore into picture_generate_request (id, creator_id, picture_pose_id, requester_id, prompt, prompt_advanced,
camera_angle,
request_status, shot_coverage, created_at, modified_at)
VALUES (1, null, 4, 2, 'prompt_test_1', '위에서 촬영', 'CREATED', '얼굴만 클로즈업', '2000-01-01 07:00:00', localtime),
(2, null, 3, 2, 'prompt_test_2', '같은 높이에서 촬영', 'CANCELED', '허리 위로 촬영', '2020-01-01 07:00:00', localtime),
(3, 1, 2, 2, 'prompt_test_3', '아래에서 촬영', 'ASSIGNING', '무릎 위로 촬영', '2000-01-01 07:00:00', localtime),
(4, 1, 2, 2, 'prompt_test_4', '아래에서 촬영', 'IN_PROGRESS', '무릎 위로 촬영', '2000-01-01 07:00:00', localtime),
(5, 1, 2, 2, 'prompt_test_5', '아래에서 촬영', 'IN_PROGRESS', '무릎 위로 촬영', '2000-01-01 07:00:00', localtime),
(6, 1, 2, 2, 'prompt_test_6', '아래에서 촬영', 'REPORTED', '무릎 위로 촬영', '2000-01-01 07:00:00', localtime),
(7, 1, 1, 2, 'prompt_test_7', '위에서 촬영', 'COMPLETED', '전신 촬영', '2020-01-01 07:00:00', localtime),
(8, 1, 1, 2, 'prompt_test_7', '위에서 촬영', 'REPORTED', '전신 촬영', '2020-01-01 07:00:00', localtime);
VALUES (1, null, 4, 2, '생성된요청', 'prompt_advanced_test_1', '위에서 촬영', 'CREATED', '얼굴만 클로즈업',
'2000-01-01 07:00:00', localtime),
(2, null, 3, 2, '취소된요청', 'prompt_advanced_test_2', '같은 높이에서 촬영', 'CANCELED', '허리 위로 촬영',
'2020-01-01 07:00:00', localtime),
(3, :creatorId, 2, 2, '특정 공급자와 매칭중인요청', 'prompt_advanced_test_3', '아래에서 촬영', 'ASSIGNING', '무릎 위로 촬영',
'2000-01-01 07:00:00', localtime),
(4, :creatorId, 2, 2, '매칭 후 진행중인 요청', 'prompt_advanced_test_4', '아래에서 촬영', 'IN_PROGRESS', '무릎 위로 촬영',
'2000-01-01 07:00:00', localtime),
(5, :creatorId, 2, 2, '매칭 후 진행중인 요청', 'prompt_advanced_test_5', '아래에서 촬영', 'IN_PROGRESS', '무릎 위로 촬영',
'2000-01-01 07:00:00', localtime),
(6, :creatorId, 2, 2, '신고된 요청', 'prompt_advanced_test_6', '아래에서 촬영', 'REPORTED', '무릎 위로 촬영', '2000-01-01 07:00:00',
localtime),
(7, :creatorId, 1, 2, '완료된 요청', 'prompt_advanced_test_7', '위에서 촬영', 'COMPLETED', '전신 촬영', '2020-01-01 07:00:00',
localtime),
(8, :creatorId, 1, 2, '신고된 요청', 'prompt_advanced_test_8', '위에서 촬영', 'REPORTED', '전신 촬영', '2020-01-01 07:00:00',
localtime),
(9, :adminCreatorId, 3, 2, 'prompt_test_9', 'prompt_advanced_test_9', '위에서 촬영', 'MATCH_TO_ADMIN', '전신 촬영', '2020-01-01 07:00:00',
localtime);

insert ignore into picture_generate_request_picture_user_face (picture_generate_request_id, user_face_picture_list_id)

Expand All @@ -91,15 +102,19 @@ values (1, 1),
(7, 6),
(8, 7),
(8, 8),
(8, 9);
(8, 9),
(9, 7),
(9, 8),
(9, 9);

insert ignore into picture_generate_response (id, created_at, modified_at, status, creator_id, request_id)
values (1, localtime, localtime, 'BEFORE_WORK', 1, 3),
(2, localtime, localtime, 'SUBMITTED_FIRST', 1, 4),
(3, localtime, localtime, 'SUBMITTED_FINAL', 1, 5),
(4, localtime, localtime, 'REPORTED', 1, 6),
(5, localtime, localtime, 'COMPLETED', 1, 7),
(6, localtime, localtime, 'REPORTED', 1, 8);

insert ignore into picture_generate_response (id, created_at, modified_at, memo, status, creator_id, request_id)
values (1, localtime, localtime, 'memo', 'BEFORE_WORK', 1, 3),
(2, localtime, localtime, 'memo', 'SUBMITTED_FIRST', 1, 4),
(3, localtime, localtime, 'memo', 'SUBMITTED_FINAL', 1, 5),
(4, localtime, localtime, 'memo', 'REPORTED', 1, 6),
(5, localtime, localtime, 'memo', 'COMPLETED', 1, 7),
(6, localtime, localtime, 'memo', 'REPORTED', 1, 8);

insert ignore into report (id, created_at, modified_at, content, report_status, picture_generate_response_id)
values (1, localtime, localtime, '변태자식이 본인 발가락 사진을 보낸 것 같습니다.', 'NOT_RESOLVED', 4),
Expand All @@ -111,24 +126,25 @@ values (1, localtime, localtime, '얼굴 완성 전 url 1', 2, 4),
(2, localtime, localtime, '얼굴 완성 전 url 2', 3, 4),
(3, localtime, localtime, '얼굴 완성 전 url 3', 4, 4);

insert ignore into picture_completed (id, created_at, modified_at, url, picture_generate_response_id, user_id,
uploaded_by)
values (1, localtime, localtime, '얼굴 완성 사진 url 1', 3, 2, 1),
(2, localtime, localtime, '얼굴 완성 사진 url 2', 4, 2, 1),
(3, localtime, localtime, '얼굴 완성 사진 url 3', 5, 2, 1),
(4, localtime, localtime, '얼굴 완성 사진 url 4', 6, 2, 1);
insert ignore into picture_completed (id, created_at, modified_at, url, uploaded_by, picture_generate_response_id,
requester_id)
values (1, localtime, localtime, '얼굴 완성 사진 url 1', 1, 3, 2),
(2, localtime, localtime, '얼굴 완성 사진 url 2', 1, 4, 2),
(3, localtime, localtime, '얼굴 완성 사진 url 3', 1, 5, 2),
(4, localtime, localtime, '얼굴 완성 사진 url 4', 1, 6, 2);

insert ignore into response_example (id, created_at, modified_at, example_picture_url, example_prompt, prompt_only,
uploaded_by)
values (1, localtime, localtime, '/EXAMPLE/벚꽃벤치여자.png', '벚꽃 벤치 여자', false, 1),
(2, localtime, localtime, '/EXAMPLE/한강잠수부.png', '한강잠수부', false, 1),
(3, localtime, localtime, null, '프롬프트만있음 사진은 없고', true, 1);


insert ignore into settlement (id, created_at, modified_at, elapsed_minutes, reward, picture_generate_response_id)
values (1, localtime, localtime, 80, 2000, 2),
(2, localtime, localtime, 50, 2500, 3),
(3, localtime, localtime, 230, 1000, 5);
insert ignore into settlement (id, created_at, modified_at, elapsed_minutes, reward, picture_generate_response_id,
settlement_status)
values (1, localtime, localtime, 80, 2000, 2, 'WITHDRAWN'),
(2, localtime, localtime, 50, 2500, 3, 'CREATED'),
(3, localtime, localtime, 10, 2500, 4, 'CANCELLED'),
(4, localtime, localtime, 230, 1000, 5, 'CREATED');

insert ignore into deposit (id, created_at, modified_at, deposit_amount, user_id)
values (1, localtime, localtime, 0, 1),
Expand Down
24 changes: 17 additions & 7 deletions src/main/java/com/gt/genti/adapter/in/web/AdminUserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static com.gt.genti.other.util.ApiUtils.*;

import java.util.List;
import java.util.Objects;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -14,9 +15,9 @@

import com.gt.genti.application.service.UserService;
import com.gt.genti.domain.enums.UserRole;
import com.gt.genti.domain.enums.converter.EnumUtil;
import com.gt.genti.dto.ChangeUserRoleDto;
import com.gt.genti.dto.ChangeUserStatusDto;
import com.gt.genti.dto.UserInfoResponseDto;
import com.gt.genti.dto.UserInfoResponseDtoForAdmin;

import jakarta.websocket.server.PathParam;
Expand All @@ -29,23 +30,32 @@ public class AdminUserController {
private final UserService userService;

@PostMapping("/users/{userId}/status")
public ResponseEntity<ApiResult<Boolean>> changeUserStatus(@PathVariable Long userId, @RequestBody ChangeUserStatusDto changeUserStatusDto){
public ResponseEntity<ApiResult<Boolean>> changeUserStatus(@PathVariable Long userId,
@RequestBody ChangeUserStatusDto changeUserStatusDto) {
return success(userService.updateUserStatus(userId, changeUserStatusDto));
}

@PostMapping("/users/{userId}/role")
public ResponseEntity<ApiResult<Boolean>> changeUserRole(@PathVariable Long userId, @RequestBody ChangeUserRoleDto changeUserRoleDto){
public ResponseEntity<ApiResult<Boolean>> changeUserRole(@PathVariable Long userId,
@RequestBody ChangeUserRoleDto changeUserRoleDto) {
return success(userService.updateUserRole(userId, changeUserRoleDto));
}

@GetMapping("/users/all")
@GetMapping("/users")
public ResponseEntity<ApiResult<List<UserInfoResponseDtoForAdmin>>> getAllUserInfo(
){
@PathParam(value = "role") String role
) {
//TODO Role에 따라 분기
// edited at 2024-05-24
// author 서병렬

return success(userService.getAllUserInfo());
List<UserInfoResponseDtoForAdmin> result;
if (Objects.equals(role, "ALL")) {
result = userService.getAllUserInfo();
} else {
UserRole userRole = EnumUtil.stringToEnum(UserRole.class, role);
result = userService.getAllUserInfo(userRole);
}
return success(result);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.springframework.data.domain.Slice;
import org.springframework.stereotype.Service;

import com.gt.genti.domain.PicturePost;
import com.gt.genti.domain.Post;
import com.gt.genti.dto.PostBriefResponseDto;
import com.gt.genti.dto.PostDetailResponseDto;
Expand Down Expand Up @@ -48,12 +47,11 @@ private static List<PostDetailResponseDto> mapToPostResponseDto(Slice<Post> post
return postList.stream()
.map(post -> PostDetailResponseDto.builder()
.postId(post.getId())
.postImageUrl(
post.getPictureList().stream().map(PicturePost::getUrl).toList())
.picturePostList(post.getPictureList())
.likes(post.getLikes())
.userId(post.getUser().getId())
.content(post.getContent())
.profileImageUrl(post.getUser().getPictureProfileList().getUrl())
.pictureProfile(post.getUser().getPictureProfileList().get(0))
.createdAt(post.getCreatedAt())
.build()).toList();
}
Expand Down
19 changes: 8 additions & 11 deletions src/main/java/com/gt/genti/application/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.gt.genti.domain.PictureCompleted;
import com.gt.genti.domain.PictureProfile;
import com.gt.genti.domain.User;
import com.gt.genti.domain.common.PictureEntity;
import com.gt.genti.domain.enums.UserRole;
import com.gt.genti.dto.ChangeUserRoleDto;
import com.gt.genti.dto.ChangeUserStatusDto;
Expand Down Expand Up @@ -39,11 +40,7 @@ public class UserService {
@Transactional
public UserInfoResponseDto getUserInfo(Long userId) {
User foundUser = findActivateUserByUserId(userId);
List<PictureProfile> pictureProfileList = foundUser.getPictureProfileList();
return UserInfoResponseDto.builder()
.user(foundUser)
.pictureProfileList(pictureProfileList)
.build();
return new UserInfoResponseDto(foundUser);
}

@Transactional
Expand All @@ -57,10 +54,7 @@ public UserInfoResponseDto updateUserInfo(Long userId, UserInfoUpdateRequestDto
foundUser.addProfilePicture(foundPictureProfile);
}

return UserInfoResponseDto.builder()
.user(foundUser)
.pictureProfileList(foundUser.getPictureProfileList())
.build();
return new UserInfoResponseDto(foundUser);
}

@Transactional
Expand Down Expand Up @@ -105,7 +99,7 @@ public List<CommonPictureResponseDto> getAllMyGeneratedPicture(Long userId) {
log.info("userId" + userId);
List<PictureCompleted> pictureCompletedList = pictureCompletedRepository.findAllByUser(foundUser);
return pictureCompletedList.stream()
.map(entity -> new CommonPictureResponseDto(entity.getId(), entity.getUrl()))
.map(PictureEntity::mapToCommonResponse)
.toList();
}

Expand All @@ -130,7 +124,10 @@ private User findDeactivatedUserByUserId(Long userId) {
}

public List<UserInfoResponseDtoForAdmin> getAllUserInfo() {
// userRepository
return userRepository.findAll().stream().map(UserInfoResponseDtoForAdmin::new).toList();
}

public List<UserInfoResponseDtoForAdmin> getAllUserInfo(UserRole userRole) {
return null;
}
}
4 changes: 4 additions & 0 deletions src/main/java/com/gt/genti/domain/PictureCompleted.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ public class PictureCompleted extends PictureEntity {
@Column(name = "url", nullable = false)
String url;

@ManyToOne
@JoinColumn(name = "requester_id")
User requester;

@ManyToOne
@JoinColumn(name = "picture_generate_response_id")
PictureGenerateResponse pictureGenerateResponse;
Expand Down
8 changes: 0 additions & 8 deletions src/main/java/com/gt/genti/domain/PictureUserFace.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package com.gt.genti.domain;

import java.util.List;

import com.gt.genti.domain.common.BaseTimeEntity;
import com.gt.genti.domain.common.PictureEntity;

import jakarta.persistence.Column;
Expand All @@ -12,7 +9,6 @@
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import lombok.AccessLevel;
import lombok.Builder;
Expand All @@ -31,10 +27,6 @@ public class PictureUserFace extends PictureEntity {
@Column(name = "url", nullable = false)
String url;

@ManyToOne
@JoinColumn(name = "user_id")
User user;

@Builder
public PictureUserFace(String url, User user) {
this.url = url;
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/com/gt/genti/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@

import com.gt.genti.domain.common.BaseTimeEntity;
import com.gt.genti.domain.enums.OauthType;
import com.gt.genti.domain.enums.Sex;
import com.gt.genti.domain.enums.UserRole;
import com.gt.genti.domain.enums.UserStatus;
import com.gt.genti.domain.enums.converter.OauthTypeConverterIgnoreCase;
import com.gt.genti.domain.enums.converter.SexConverter;
import com.gt.genti.domain.enums.converter.UserRoleConverter;
import com.gt.genti.domain.enums.converter.UserStatusConverter;
import com.gt.genti.error.ErrorCode;
Expand Down Expand Up @@ -46,12 +48,16 @@ public class User extends BaseTimeEntity {
List<PictureProfile> pictureProfileList;

// PictureUserFace는 완전히 user에 종속
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
@OneToMany(mappedBy = "uploadedBy", cascade = CascadeType.ALL, orphanRemoval = true)
List<PictureUserFace> pictureUserFaceList;

@Column(name = "email")
String email;

@Column(name = "sex")
@Convert(converter = SexConverter.class)
Sex sex;

@Column(name = "introduction")
String introduction;

Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/gt/genti/domain/enums/Sex.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.gt.genti.domain.enums;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public enum Sex implements ConvertableEnum {
M("M"),
W("W"),
NONE("NONE");
private final String stringValue;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.gt.genti.domain.enums.converter;

import com.gt.genti.domain.enums.BankType;
import com.gt.genti.domain.enums.Sex;

import jakarta.persistence.Converter;

@Converter
public class SexConverter extends DefaultStringAttributeConverter<Sex> {

public SexConverter() {
super(Sex.class);
}

}
Loading

0 comments on commit a24e6ad

Please sign in to comment.