Skip to content

Commit

Permalink
[merge] API 완성 전 마무리 작업
Browse files Browse the repository at this point in the history
  • Loading branch information
jinkonu authored Jul 15, 2024
2 parents 276de1b + 356a94c commit 6599f0b
Show file tree
Hide file tree
Showing 28 changed files with 308 additions and 68 deletions.
48 changes: 29 additions & 19 deletions src/main/java/org/recordy/server/keyword/domain/Keyword.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,42 @@
package org.recordy.server.keyword.domain;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Base64;
import java.util.List;

public enum Keyword {

EXOTIC("이색적인"),
QUITE("조용한"),
TRENDY("트렌디한"),
SENSUAL("감각적인"),
CUTE("귀여운"),
DUCKUMORI("덕후몰이"),
FUNNY("재밌는"),
COZY("아늑한"),
CLASSIC("클래식한"),
GOOD_TO_FOCUS("집중하기 좋은"),
CLEAN("깔끔한"),
SCARY("무서운"),
VIBRANT("강렬한"),
감각적인,
강렬한,
귀여운,
깔끔한,
덕후몰이,
아늑한,
이색적인,
재밌는,
조용한,
집중하기_좋은,
클래식한,
트렌디한,
;

private final String name;

Keyword(String name) {
this.name = name;
public static String encode() {
return Base64.getEncoder().encodeToString(
Arrays.stream(Keyword.values())
.map(Enum::name)
.reduce((a, b) -> a + "," + b)
.orElseThrow()
.getBytes(StandardCharsets.UTF_8)
);
}

public static List<Keyword> from(List<String> keywords) {
return keywords.stream()
public static List<Keyword> decode(String utf8Bytes) {
String[] keywords = new String(Base64.getDecoder().decode(utf8Bytes), StandardCharsets.UTF_8).split(",");

System.out.println(Arrays.toString(keywords));

return Arrays.stream(keywords)
.map(Keyword::valueOf)
.toList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,12 @@ public class KeywordEntity extends JpaMetaInfoEntity {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String name;

@Column(unique = true, nullable = false)
@Enumerated(EnumType.STRING)
private Keyword keyword;

public KeywordEntity(Keyword keyword) {
this.keyword = keyword;
this.name = keyword.name();
}

public static KeywordEntity from(Keyword keyword) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package org.recordy.server.record.controller.dto.request;

import java.util.List;

public record RecordCreateRequest(
String location,
String content,
List<String> keywords) {
String keywords) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ public record RecordCreate(
List<Keyword> keywords
) {

public static RecordCreate from(Long uploaderId, RecordCreateRequest recordCreateRequest) {
public static RecordCreate of(Long uploaderId, RecordCreateRequest recordCreateRequest) {
return new RecordCreate(
uploaderId,
recordCreateRequest.location(),
recordCreateRequest.content(),
Keyword.from(recordCreateRequest.keywords()));
Keyword.decode(recordCreateRequest.keywords())
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public interface RecordRepository {
Slice<Record> findAllByIdAfterOrderByIdDesc(long cursor, Pageable pageable);
Slice<Record> findAllByIdAfterAndKeywordsOrderByIdDesc(List<Keyword> keywords, long cursor, Pageable pageable);
Slice<Record> findAllByUserIdOrderByIdDesc(long userId, long cursor, Pageable pageable);
Map<Keyword, Long> countAllByUserIdGroupByKeyword(long userId);
Slice<Record> findAllBySubscribingUserIdOrderByIdDesc(long userId, long cursor, Pageable pageable);
long countAllByUserId(long userId);
Optional<Long> findMaxId();
Long count();
}
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,14 @@ public Slice<RecordEntity> findAllBySubscribingUserIdOrderByIdDesc(long userId,
return new SliceImpl<>(recordEntities, pageable, QueryDslUtils.hasNext(pageable, recordEntities));
}

public long countAllByUserId(long userId) {
return jpaQueryFactory
.select(recordEntity.id.count())
.from(recordEntity)
.where(recordEntity.user.id.eq(userId))
.fetchOne();
}

public Optional<Long> findMaxId() {
return Optional.ofNullable(jpaQueryFactory
.select(recordEntity.id.max())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ public Slice<Record> findAllByUserIdOrderByIdDesc(long userId, long cursor, Page
.map(RecordEntity::toDomain);
}

@Override
public Slice<Record> findAllBySubscribingUserIdOrderByIdDesc(long userId, long cursor, Pageable pageable) {
return recordQueryDslRepository.findAllBySubscribingUserIdOrderByIdDesc(userId, cursor, pageable)
.map(RecordEntity::toDomain);
}

@Override
public Map<Keyword, Long> countAllByUserIdGroupByKeyword(long userId) {
Map<KeywordEntity, Long> preference = recordQueryDslRepository.countAllByUserIdGroupByKeyword(userId);
Expand All @@ -111,9 +117,8 @@ public Map<Keyword, Long> countAllByUserIdGroupByKeyword(long userId) {
}

@Override
public Slice<Record> findAllBySubscribingUserIdOrderByIdDesc(long userId, long cursor, Pageable pageable) {
return recordQueryDslRepository.findAllBySubscribingUserIdOrderByIdDesc(userId, cursor, pageable)
.map(RecordEntity::toDomain);
public long countAllByUserId(long userId) {
return recordQueryDslRepository.countAllByUserId(userId);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.recordy.server.record.service;

import org.recordy.server.record.controller.dto.response.RecordInfoWithBookmark;
import org.recordy.server.keyword.domain.Keyword;
import org.recordy.server.record.domain.File;
import org.recordy.server.record.domain.Record;
import org.recordy.server.record.domain.usecase.RecordCreate;
Expand All @@ -16,9 +16,9 @@ public interface RecordService {

// query
void watch(long userId, long recordId);
Slice<Record> getFamousRecords(List<String> keywords, int pageNumber, int size);
Slice<Record> getFamousRecords(List<Keyword> keywords, int pageNumber, int size);
Slice<Record> getRecentRecords(List<Keyword> keywords, Long cursorId, int size);
Slice<Record> getRecentRecordsByUser(long userId, long cursorId, int size);
Slice<Record> getRecentRecords(List<String> keywords, Long cursorId, int size);
Slice<Record> getSubscribingRecords(long userId, long cursorId, int size);
List<Record> getTotalRecords(int size);
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ public void watch(long userId, long recordId) {
}

@Override
public Slice<Record> getFamousRecords(List<String> keywords, int pageNumber, int size) {
public Slice<Record> getFamousRecords(List<Keyword> keywords, int pageNumber, int size) {
if (Objects.isNull(keywords) || keywords.isEmpty()) {
return getFamousRecords(pageNumber, size);
}

return getFamousRecordsWithKeywords(Keyword.from(keywords), pageNumber, size);
return getFamousRecordsWithKeywords(keywords, pageNumber, size);
}

private Slice<Record> getFamousRecords(int pageNumber, int size) {
Expand All @@ -100,12 +100,12 @@ public Slice<Record> getRecentRecordsByUser(long userId, long cursorId, int size
}

@Override
public Slice<Record> getRecentRecords(List<String> keywords, Long cursorId, int size) {
if (Objects.isNull(keywords) || keywords.isEmpty()) {
public Slice<Record> getRecentRecords(List<Keyword> keywords, Long cursorId, int size) {
if (Objects.isNull(keywords)) {
return getRecentRecords(cursorId, size);
}

return getRecentRecordsWithKeywords(Keyword.from(keywords), cursorId, size);
return getRecentRecordsWithKeywords(keywords, cursorId, size);
}

private Slice<Record> getRecentRecords(long cursorId, int size) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
import org.springframework.data.domain.Slice;
import org.springframework.data.domain.SliceImpl;

public record UserInfo (
public record UserInfo(
Long id,
String nickname
//todo
// 유저 프로필 추가
String nickname,
String profileImageUrl
){
public static UserInfo from(User user) {
return new UserInfo(
user.getId(),
user.getNickname()
user.getNickname(),
user.getProfileImageUrl()
);
}

Expand Down
9 changes: 8 additions & 1 deletion src/main/java/org/recordy/server/user/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
public class User {

private static final Pattern NICKNAME_PATTERN = Pattern.compile("^[가-힣0-9_.]+$");
private static final String DEFAULT_PROFILE_IMAGE_URL = "https://recordy-bucket.s3.ap-northeast-2.amazonaws.com/profile_";

private Long id;
private AuthPlatform authPlatform;
private UserStatus status;
private String profileImageUrl;
private String nickname;
private TermsAgreement termsAgreement;
private LocalDateTime createdAt;
Expand All @@ -36,8 +38,9 @@ public User activate(UserSignUp userSignUp) {
return User.builder()
.id(this.id)
.authPlatform(this.authPlatform)
.nickname(userSignUp.nickname())
.status(ACTIVE)
.profileImageUrl(getImage(id))
.nickname(userSignUp.nickname())
.termsAgreement(userSignUp.termsAgreement())
.createdAt(createdAt)
.build();
Expand All @@ -54,4 +57,8 @@ private void validateTermsAgreed(TermsAgreement termsAgreement) {
throw new UserException(ErrorMessage.INVALID_REQUEST_TERM);
}
}

private String getImage(Long id) {
return DEFAULT_PROFILE_IMAGE_URL + (id % 3);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.recordy.server.user.domain.usecase;

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

public record UserProfile(
Long id,
String nickname,
String profileImageUrl,
long recordCount,
long followerCount,
long followingCount
) {
public static UserProfile of(User user, long recordCount, long followerCount, long followingCount) {
return new UserProfile(
user.getId(),
user.getNickname(),
user.getProfileImageUrl(),
recordCount,
followerCount,
followingCount
);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.recordy.server.user.service;

import org.recordy.server.auth.domain.Auth;
import org.recordy.server.user.controller.dto.request.UserSignUpRequest;
import org.recordy.server.user.domain.usecase.UserProfile;
import org.recordy.server.user.domain.usecase.UserSignIn;
import org.recordy.server.user.domain.User;
import org.recordy.server.user.domain.usecase.UserSignUp;
Expand All @@ -18,6 +18,7 @@ public interface UserService {
void delete(long userId);

// query
UserProfile getProfile(long id);
Optional<User> getByPlatformId(String platformId);
Optional<User> getById(long id);
void validateDuplicateNickname(String nickname);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,54 @@
package org.recordy.server.user.service.impl;

import lombok.RequiredArgsConstructor;
import org.recordy.server.auth.domain.Auth;
import org.recordy.server.auth.domain.AuthPlatform;
import org.recordy.server.auth.service.AuthService;
import org.recordy.server.auth.service.AuthTokenService;
import org.recordy.server.common.message.ErrorMessage;
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.usecase.UserProfile;
import org.recordy.server.user.domain.User;
import org.recordy.server.user.domain.UserStatus;
import org.recordy.server.user.domain.usecase.UserSignIn;
import org.recordy.server.user.domain.usecase.UserSignUp;
import org.recordy.server.user.exception.UserException;
import org.recordy.server.user.repository.UserRepository;
import org.recordy.server.user.service.UserService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Optional;

@RequiredArgsConstructor
@Service
public class UserServiceImpl implements UserService {

private final Long rootUserId;

private final UserRepository userRepository;
private final SubscribeRepository subscribeRepository;
private final RecordRepository recordRepository;
private final AuthService authService;
private final AuthTokenService authTokenService;

public UserServiceImpl(
@Value("${user.root.id}") Long rootUserId,
UserRepository userRepository,
SubscribeRepository subscribeRepository,
RecordRepository recordRepository,
AuthService authService,
AuthTokenService authTokenService
) {
this.rootUserId = rootUserId;
this.userRepository = userRepository;
this.subscribeRepository = subscribeRepository;
this.recordRepository = recordRepository;
this.authService = authService;
this.authTokenService = authTokenService;
}

@Override
public Auth signIn(UserSignIn userSignIn) {
AuthPlatform platform = authService.getPlatform(userSignIn);
Expand Down Expand Up @@ -54,10 +77,23 @@ public User signUp(UserSignUp userSignUp) {
User pendingUser = userRepository.findById(userSignUp.userId())
.orElseThrow(() -> new UserException(ErrorMessage.USER_NOT_FOUND));
User updatedUser = pendingUser.activate(userSignUp);
followRoot(updatedUser);

return userRepository.save(updatedUser);
}

private void followRoot(User user) {
if (!user.getId().equals(rootUserId)) {
userRepository.findById(rootUserId)
.ifPresent(rootUser ->
subscribeRepository.save(Subscribe.builder()
.subscribingUser(user)
.subscribedUser(rootUser)
.build())
);
}
}

@Override
public String reissueToken(String refreshToken) {
String platformId = authTokenService.getPlatformIdFromRefreshToken(refreshToken);
Expand Down Expand Up @@ -86,6 +122,17 @@ public void delete(long userId) {
userRepository.deleteById(userId);
}

@Override
public UserProfile getProfile(long id) {
User user = userRepository.findById(id)
.orElseThrow(() -> new UserException(ErrorMessage.USER_NOT_FOUND));
long records = recordRepository.countAllByUserId(user.getId());
long followers = subscribeRepository.countSubscribingUsers(user.getId());
long followings = subscribeRepository.countSubscribedUsers(user.getId());

return UserProfile.of(user, records, followers, followings);
}

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

0 comments on commit 6599f0b

Please sign in to comment.