Skip to content

Commit

Permalink
[merge] User 도메인 리팩토링 및 테스트 보강 (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
jinkonu authored Jul 29, 2024
2 parents 3539591 + 70eeb3e commit c7cb927
Show file tree
Hide file tree
Showing 32 changed files with 395 additions and 201 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class UploadEntity {

@Builder
public UploadEntity(Long id, RecordEntity record, KeywordEntity keyword) {
this.id = id;
this.record = record;
this.keyword = keyword;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ public void deleteById(long recordId) {
.ifPresent(recordJpaRepository::delete);
}

@Transactional
@Override
public void deleteByUserId(long userId) {
recordJpaRepository.deleteAllByUserId(userId);
}

@Override
public Optional<Record> findById(long recordId) {
return recordJpaRepository.findById(recordId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import org.recordy.server.view.repository.ViewRepository;
import org.recordy.server.user.domain.User;
import org.recordy.server.user.exception.UserException;
import org.recordy.server.user.service.UserService;
import org.recordy.server.user.repository.UserRepository;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Slice;
import org.springframework.stereotype.Service;
Expand All @@ -35,11 +35,13 @@ public class RecordServiceImpl implements RecordService {
private final RecordRepository recordRepository;
private final ViewRepository viewRepository;
private final BookmarkRepository bookmarkRepository;
private final UserService userService;
private final FileService fileService;
private final UserRepository userRepository;

@Override
public Record create(RecordCreate recordCreate) {
User user = userService.getById(recordCreate.uploaderId())
public Record create(RecordCreate recordCreate, File file) {
FileUrl fileUrl = fileService.save(file);
User user = userRepository.findById(recordCreate.uploaderId())
.orElseThrow(() -> new UserException(ErrorMessage.USER_NOT_FOUND));

return recordRepository.save(Record.builder()
Expand All @@ -63,7 +65,7 @@ public void delete(long userId, long recordId) {

@Override
public void watch(long userId, long recordId) {
User user = userService.getById(userId)
User user = userRepository.findById(userId)
.orElseThrow(() -> new UserException(ErrorMessage.USER_NOT_FOUND));
Record record = recordRepository.findById(recordId)
.orElseThrow(() -> new RecordException(ErrorMessage.RECORD_NOT_FOUND));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ public ResponseEntity<Void> signUp(
) {
userService.signUp(UserSignUp.of(userId, request.nickname(), request.termsAgreement()));

return ResponseEntity.
status(HttpStatus.CREATED).
build();
return ResponseEntity
.status(HttpStatus.CREATED)
.build();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import lombok.RequiredArgsConstructor;
import org.recordy.server.common.dto.response.CursorBasePaginatedResponse;
import org.recordy.server.subscribe.repository.SubscribeRepository;
import org.recordy.server.auth.security.resolver.UserId;
import org.recordy.server.user.controller.dto.response.UserInfoWithFollowing;
import org.recordy.server.subscribe.domain.usecase.SubscribeCreate;
Expand All @@ -30,7 +29,6 @@ public class UserController implements UserApi {

private final SubscribeService subscribeService;
private final UserService userService;
private final SubscribeRepository subscribeRepository;

@Override
@PostMapping("/follow/{followingId}")
Expand All @@ -53,7 +51,10 @@ public ResponseEntity<CursorBasePaginatedResponse<UserInfo>> getSubscribedUserIn

return ResponseEntity
.status(HttpStatus.OK)
.body(CursorBasePaginatedResponse.of(UserInfo.of(subscribeService.getSubscribedUsers(userId, cursorId, size)), userInfo -> userInfo.id()));
.body(CursorBasePaginatedResponse.of(
UserInfo.from(subscribeService.getSubscribedUsers(userId, cursorId, size)),
UserInfo::id
));
}

@Override
Expand All @@ -68,16 +69,19 @@ public ResponseEntity<CursorBasePaginatedResponse<UserInfoWithFollowing>> getSub

return ResponseEntity
.status(HttpStatus.OK)
.body(CursorBasePaginatedResponse.of(UserInfoWithFollowing.of(users, following), userInfoWithFollowing -> userInfoWithFollowing.userInfo().id()));
}
.body(CursorBasePaginatedResponse.of(
UserInfoWithFollowing.of(users, following),
userInfoWithFollowing -> userInfoWithFollowing.userInfo().id()
));
}


@Override
@GetMapping("/profile/{otherUserId}")
public ResponseEntity<UserProfile> getUserInfosWithFollowing(
@UserId Long userId,
@PathVariable Long otherUserId
) {
) {
return ResponseEntity
.status(HttpStatus.OK)
.body(userService.getProfile(userId, otherUserId));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.recordy.server.user.controller.dto.request;

import org.recordy.server.user.domain.TermsAgreement;

public record UserSignUpRequest(
String nickname,
TermsAgreement termsAgreement
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public record UserInfo(
String nickname,
String profileImageUrl
){

public static UserInfo from(User user) {
return new UserInfo(
user.getId(),
Expand All @@ -18,7 +19,7 @@ public static UserInfo from(User user) {
);
}

public static Slice<UserInfo> of (Slice<User> users) {
public static Slice<UserInfo> from(Slice<User> users) {
return new SliceImpl<>(
users.getContent().stream()
.map(UserInfo::from)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ public record UserInfoWithFollowing(
UserInfo userInfo,
Boolean following
) {
public static Slice<UserInfoWithFollowing> of (Slice<User> users, List<Boolean> following) {
public static Slice<UserInfoWithFollowing> of(Slice<User> users, List<Boolean> following) {
return users.map(user -> {
int index = users.getContent().indexOf(user);
Boolean isFollowing = following.get(index);
UserInfo userInfo = UserInfo.from(user);
return new UserInfoWithFollowing(userInfo,isFollowing);

return new UserInfoWithFollowing(userInfo, isFollowing);
});
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.recordy.server.user.controller.dto.request;
package org.recordy.server.user.domain;

public record TermsAgreement(
boolean useTerm,
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/org/recordy/server/user/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import org.recordy.server.auth.domain.AuthPlatform;
import org.recordy.server.common.message.ErrorMessage;
import org.recordy.server.user.controller.dto.request.TermsAgreement;
import org.recordy.server.user.domain.usecase.UserSignUp;
import org.recordy.server.user.exception.UserException;

Expand Down
12 changes: 10 additions & 2 deletions src/main/java/org/recordy/server/user/domain/UserEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.recordy.server.common.domain.JpaMetaInfoEntity;
import org.recordy.server.record.domain.RecordEntity;
import org.recordy.server.subscribe.domain.SubscribeEntity;
import org.recordy.server.user.controller.dto.request.TermsAgreement;

import java.time.LocalDateTime;
import java.util.ArrayList;
Expand Down Expand Up @@ -41,7 +40,16 @@ public class UserEntity extends JpaMetaInfoEntity {
@OneToMany(mappedBy = "subscribedUser", cascade = CascadeType.ALL, orphanRemoval = true)
private List<SubscribeEntity> subscribers = new ArrayList<>();

public UserEntity(Long id, String platformId, AuthPlatform.Type platformType, UserStatus status, String profileImageUrl, String nickname, TermsAgreement termsAgreement, LocalDateTime createdAt) {
public UserEntity(
Long id,
String platformId,
AuthPlatform.Type platformType,
UserStatus status,
String profileImageUrl,
String nickname,
TermsAgreement termsAgreement,
LocalDateTime createdAt
) {
this.id = id;
this.platformId = platformId;
this.platformType = platformType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ public enum UserStatus {

PENDING,
ACTIVE,
DELETED;
;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public record UserProfile(
long bookmarkCount,
boolean isFollowing
) {

public static UserProfile of(User user, long recordCount, long followerCount, long followingCount, long bookmarkCount, boolean isFollowing) {
return new UserProfile(
user.getId(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.recordy.server.user.domain.usecase;

import org.recordy.server.user.controller.dto.request.TermsAgreement;
import org.recordy.server.user.domain.TermsAgreement;

public record UserSignUp(
Long userId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@ public interface UserRepository {

// command
User save(User user);

void deleteById(long userId);

// query
Optional<User> findById(long userId);
Optional<User> findByPlatformId(String platformId);

boolean existsByNickname(String nickname);

Optional<User> findById(long userId);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.recordy.server.user.repository.impl;

import org.recordy.server.user.domain.User;
import org.recordy.server.user.domain.UserEntity;
import org.springframework.data.jpa.repository.JpaRepository;

Expand All @@ -9,6 +8,5 @@
public interface UserJpaRepository extends JpaRepository<UserEntity, Long> {

Optional<UserEntity> findByPlatformId(String platformId);

boolean existsByNickname(String nickname);
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,24 @@ private void followRootUser(UserEntity userEntity) {
}

@Override
public Optional<User> findByPlatformId(String platformId) {
return userJpaRepository.findByPlatformId(platformId)
.map(UserEntity::toDomain);
public void deleteById(long userId) {
userJpaRepository.deleteById(userId);
}

@Override
public boolean existsByNickname(String nickname) {
return userJpaRepository.existsByNickname(nickname);
public Optional<User> findById(long userId) {
return userJpaRepository.findById(userId)
.map(UserEntity::toDomain);
}

@Override
public void deleteById(long userId) {
userJpaRepository.deleteById(userId);
public Optional<User> findByPlatformId(String platformId) {
return userJpaRepository.findByPlatformId(platformId)
.map(UserEntity::toDomain);
}

@Override
public Optional<User> findById(long userId) {
return userJpaRepository.findById(userId)
.map(UserEntity::toDomain);
public boolean existsByNickname(String nickname) {
return userJpaRepository.existsByNickname(nickname);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import org.recordy.server.user.domain.User;
import org.recordy.server.user.domain.usecase.UserSignUp;

import java.util.Optional;

public interface UserService {

// command
Expand All @@ -19,7 +17,5 @@ public interface UserService {

// query
UserProfile getProfile(long userId, long otherUserId);
Optional<User> getByPlatformId(String platformId);
Optional<User> getById(long id);
void validateDuplicateNickname(String nickname);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import org.recordy.server.record.repository.RecordRepository;
import org.recordy.server.subscribe.domain.Subscribe;
import org.recordy.server.subscribe.repository.SubscribeRepository;
import org.recordy.server.user.controller.dto.request.TermsAgreement;
import org.recordy.server.user.domain.TermsAgreement;
import org.recordy.server.user.domain.usecase.UserProfile;
import org.recordy.server.user.domain.User;
import org.recordy.server.user.domain.UserStatus;
Expand All @@ -22,8 +22,6 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class UserServiceImpl implements UserService {

Expand Down Expand Up @@ -63,7 +61,7 @@ public Auth signIn(UserSignIn userSignIn) {
}

private User getOrCreateUser(AuthPlatform platform) {
return getByPlatformId(platform.getId())
return userRepository.findByPlatformId(platform.getId())
.orElseGet(() -> create(platform));
}

Expand All @@ -89,11 +87,10 @@ public User signUp(UserSignUp userSignUp) {
@Override
public String reissueToken(String refreshToken) {
String platformId = authTokenService.getPlatformIdFromRefreshToken(refreshToken);
Long userId = getByPlatformId(platformId)
.orElseThrow(() -> new UserException(ErrorMessage.USER_NOT_FOUND))
.getId();
User user = userRepository.findByPlatformId(platformId)
.orElseThrow(() -> new UserException(ErrorMessage.USER_NOT_FOUND));

return authTokenService.issueAccessToken(userId);
return authTokenService.issueAccessToken(user.getId());
}

@Override
Expand All @@ -116,6 +113,7 @@ public void delete(long userId) {
recordRepository.deleteByUserId(userId);
authService.signOut(user.getAuthPlatform().getId());
userRepository.deleteById(userId);
recordRepository.deleteByUserId(userId);
}

@Override
Expand All @@ -131,16 +129,6 @@ public UserProfile getProfile(long userId, long otherUserId) {
return UserProfile.of(user, records, followers, followings, bookmarks, isFollowing);
}

@Override
public Optional<User> getByPlatformId(String platformId) {
return userRepository.findByPlatformId(platformId);
}

@Override
public Optional<User> getById(long id) {
return userRepository.findById(id);
}

@Override
public void validateDuplicateNickname(String nickname) {
if (userRepository.existsByNickname(nickname)) {
Expand Down
Loading

0 comments on commit c7cb927

Please sign in to comment.