Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion .rules/checkstyle-rules.xml
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,13 @@ The following rules in the Naver coding convention cannot be checked by this con
value="[space-after-comma-semicolon]: ''{0}'' is not followed by whitespace."/>
</module>
<module name="NoWhitespaceBefore">
<property name="tokens" value="COMMA,SEMI"/>
<property name="tokens" value="COMMA"/>
<message key="ws.preceded"
value="[space-after-comma-semicolon] ''{0}'' is preceded with whitespace."/>
</module>
<module name="NoWhitespaceBefore">
<property name="tokens" value="SEMI"/>
<property name="allowLineBreaks" value="true"/>
<message key="ws.preceded"
value="[space-after-comma-semicolon] ''{0}'' is preceded with whitespace."/>
</module>
Expand Down
8 changes: 3 additions & 5 deletions src/main/java/queuing/core/CoreApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
@SpringBootApplication
@ConfigurationPropertiesScan
public class CoreApplication {

public static void main(String[] args) {
SpringApplication.run(CoreApplication.class, args);
}

public static void main(String[] args) {
SpringApplication.run(CoreApplication.class, args);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package queuing.core.friend.application.query;

public record GetFriendListQuery(
String userSlug,
Long lastId,
int size
) {
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package queuing.core.friend.application.dto;
package queuing.core.friend.application.result;

import queuing.core.user.domain.entity.User;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
import org.springframework.transaction.annotation.Transactional;

import lombok.RequiredArgsConstructor;
import queuing.core.friend.application.dto.FriendSummary;
import queuing.core.friend.application.dto.GetFriendListCommand;

import queuing.core.friend.application.query.GetFriendListQuery;
import queuing.core.friend.application.result.FriendSummary;
import queuing.core.friend.application.usecase.GetFriendListUseCase;
import queuing.core.friend.domain.entity.Friend;
import queuing.core.friend.domain.repository.FriendRepository;
Expand All @@ -21,21 +22,24 @@
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class FriendReadService implements GetFriendListUseCase {

private final UserRepository userRepository;
private final FriendRepository friendRepository;
private final UserRepository userRepository;

@Override
public SliceResult<FriendSummary> getFriendList(GetFriendListCommand command) {
User user = userRepository.findBySlug(command.userSlug())
public SliceResult<FriendSummary> getFriendList(GetFriendListQuery query) {
User user = userRepository.findBySlug(query.userSlug())
.orElseThrow(() -> new BusinessException(ErrorCode.USER_NOT_FOUND));

SliceResult<Friend> friends = friendRepository.findFriendsByUserId(user.getId(), command.lastId(), command.size());
SliceResult<Friend> result = friendRepository.findFriendsByUserId(
user.getId(),
query.lastId(),
query.size()
);

List<FriendSummary> summaries = friends.items().stream()
List<FriendSummary> summaries = result.items().stream()
.map(friend -> FriendSummary.from(friend.getCounterpart(user)))
.toList();

return SliceResult.of(summaries, friends.hasNext());
return SliceResult.of(summaries, result.hasNext());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,11 @@ public void sendRequest(String requesterSlug, String targetSlug) {
User target = userRepository.findBySlug(targetSlug)
.orElseThrow(() -> new BusinessException(ErrorCode.USER_NOT_FOUND));

if (requester.equals(target)) {
throw new BusinessException(ErrorCode.FRIEND_INVALID_REQUEST);
}

if (friendRepository.findRelationship(requester, target).isPresent()) {
throw new BusinessException(ErrorCode.FRIEND_ALREADY_EXISTS);
}

Friend friend = Friend.builder()
.requester(requester)
.receiver(target)
.status(FriendStatus.PENDING)
.build();
Friend friend = Friend.createRequest(requester, target);

friendRepository.save(friend);
}
Expand All @@ -58,15 +50,7 @@ public void acceptRequest(String userSlug, Long requestId) {
Friend friend = friendRepository.findById(requestId)
.orElseThrow(() -> new BusinessException(ErrorCode.FRIEND_REQUEST_NOT_FOUND));

if (!friend.getReceiver().equals(user)) {
throw new BusinessException(ErrorCode.FRIEND_INVALID_REQUEST);
}

if (friend.getStatus() != FriendStatus.PENDING) {
throw new BusinessException(ErrorCode.FRIEND_INVALID_REQUEST);
}

friend.updateStatus(FriendStatus.ACCEPTED);
friend.accept(user);
}

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

public interface DeleteFriendUseCase {
void deleteFriend(String userSlug, String targetSlug);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package queuing.core.friend.application.usecase;

import queuing.core.friend.application.dto.FriendSummary;
import queuing.core.friend.application.dto.GetFriendListCommand;
import queuing.core.friend.application.query.GetFriendListQuery;
import queuing.core.friend.application.result.FriendSummary;
import queuing.core.global.dto.SliceResult;

public interface GetFriendListUseCase {
SliceResult<FriendSummary> getFriendList(GetFriendListCommand command);
}
SliceResult<FriendSummary> getFriendList(GetFriendListQuery query);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

public interface SendFriendRequestUseCase {
void sendRequest(String requesterSlug, String targetSlug);
}
}
26 changes: 26 additions & 0 deletions src/main/java/queuing/core/friend/domain/entity/Friend.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import queuing.core.global.exception.BusinessException;
import queuing.core.global.exception.ErrorCode;
import queuing.core.user.domain.entity.User;

@Entity
Expand Down Expand Up @@ -76,4 +79,27 @@ public User getCounterpart(User user) {
}
return requester;
}

public static Friend createRequest(User requester, User receiver) {
if (requester.equals(receiver)) {
throw new BusinessException(ErrorCode.FRIEND_INVALID_REQUEST);
}
return Friend.builder()
.requester(requester)
.receiver(receiver)
.status(FriendStatus.PENDING)
.build();
}

public void accept(User user) {
if (!this.receiver.equals(user)) {
throw new BusinessException(ErrorCode.FRIEND_INVALID_REQUEST);
}

if (this.status != FriendStatus.PENDING) {
throw new BusinessException(ErrorCode.FRIEND_INVALID_REQUEST);
}

this.status = FriendStatus.ACCEPTED;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ public interface FriendRepository extends JpaRepository<Friend, Long>, FriendRep

Optional<Friend> findByRequesterAndReceiver(User requester, User receiver);

// Find a relationship in either direction
@Query("SELECT f FROM Friend f WHERE (f.requester = :user1 AND f.receiver = :user2) OR (f.requester = :user2 AND f.receiver = :user1)")
@Query("SELECT f FROM Friend f WHERE (f.requester = :user1 AND f.receiver = :user2) "
+ "OR (f.requester = :user2 AND f.receiver = :user1)")
Optional<Friend> findRelationship(@Param("user1") User user1, @Param("user2") User user2);

// Find pending requests received by user
Page<Friend> findByReceiverAndStatus(User receiver, FriendStatus status, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.querydsl.jpa.impl.JPAQueryFactory;

import lombok.RequiredArgsConstructor;

import queuing.core.friend.domain.entity.Friend;
import queuing.core.friend.domain.entity.FriendStatus;
import queuing.core.friend.domain.entity.QFriend;
Expand All @@ -25,7 +26,7 @@ public SliceResult<Friend> findFriendsByUserId(Long userId, Long lastId, int siz

BooleanExpression isFriend = (friend.requester.id.eq(userId).or(friend.receiver.id.eq(userId)))
.and(friend.status.eq(FriendStatus.ACCEPTED));

BooleanExpression lessThanId = (lastId != null) ? friend.id.lt(lastId) : null;

List<Friend> friends = query.selectFrom(friend)
Expand All @@ -44,4 +45,4 @@ public SliceResult<Friend> findFriendsByUserId(Long userId, Long lastId, int siz

return SliceResult.of(friends, hasNext);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

import lombok.RequiredArgsConstructor;

import queuing.core.friend.application.dto.FriendSummary;
import queuing.core.friend.application.dto.GetFriendListCommand;
import queuing.core.friend.application.query.GetFriendListQuery;
import queuing.core.friend.application.result.FriendSummary;
import queuing.core.friend.application.usecase.DeleteFriendUseCase;
import queuing.core.friend.application.usecase.GetFriendListUseCase;
import queuing.core.friend.presentation.response.FriendResponse;
Expand Down Expand Up @@ -50,7 +50,7 @@ public ResponseEntity<ResponseBody<ListFriendResponse>> getFriends(
@RequestParam(defaultValue = "20") int size
) {
SliceResult<FriendSummary> result = getFriendListUseCase.getFriendList(
new GetFriendListCommand(principal.getUsername(), lastId, size)
new GetFriendListQuery(principal.getUsername(), lastId, size)
);

List<FriendResponse> items = result.items().stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
public record SendFriendRequest(
@NotBlank(message = "친구 추가할 대상을 입력해주세요.")
String targetSlug
) {}
) {
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package queuing.core.friend.presentation.response;

import queuing.core.friend.application.dto.FriendSummary;
import queuing.core.friend.application.result.FriendSummary;

public record FriendResponse(
Long id,
Expand Down
55 changes: 55 additions & 0 deletions src/main/java/queuing/core/global/dto/PageCondition.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package queuing.core.global.dto;

/**
* 페이징 조회를 위한 요청 조건을 담습니다.
*
* @param page 현재 페이지 번호 (1부터 시작)
* @param size 한 페이지당 보여줄 아이템 개수
* @param sort 정렬 조건
*/
public record PageCondition(
int page,
int size,
String sort
) {
public PageCondition {
if (page < 1) {
throw new IllegalArgumentException("Page number must be 1 or greater.");
}
if (size < 1) {
throw new IllegalArgumentException("Page size must be 1 or greater.");
}
}

/**
* 정렬 조건 없는 {@code PageCondition} 객체를 생성합니다.
*
* @param page 현재 페이지 번호 (1부터 시작)
* @param size 한 페이지당 보여줄 아이템 개수
* @return PageCondition 객체
*/
public static PageCondition of(int page, int size) {
return new PageCondition(page, size, null);
}

/**
* 정렬 조건이 있는 {@code PageCondition} 객체를 생성합니다.
*
* @param page 현재 페이지 번호 (1부터 시작)
* @param size 한 페이지당 보여줄 아이템 개수
* @param sort 정렬 조건
* @return PageCondition 객체
*/
public static PageCondition of(int page, int size, String sort) {
return new PageCondition(page, size, sort);
}

/**
* 데이터베이스 조회 시작 지점을 반환합니다.

* @return offset
*/
public long offset() {
return (long) (page - 1) * size;
}
}
18 changes: 0 additions & 18 deletions src/main/java/queuing/core/global/dto/PageData.java

This file was deleted.

Loading