Skip to content

Commit

Permalink
docs: 알림 조회 restdocs 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
big-cir committed Dec 30, 2023
1 parent b493011 commit 02716c4
Show file tree
Hide file tree
Showing 12 changed files with 156 additions and 159 deletions.
8 changes: 8 additions & 0 deletions perfume-api/src/docs/asciidoc/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,14 @@ include::{snippets}/connect-sse/request-headers.adoc[]
=== 응답
include::{snippets}/connect-sse/http-response.adoc[]

== 알림 조회
=== 요청
include::{snippets}/get-notifications/http-request.adoc[]

=== 응답
include::{snippets}/get-notifications/http-response.adoc[]
include::{snippets}/get-notifications/response-fields.adoc[]

== 알림 확인
=== 요청
include::{snippets}/read-notification/http-request.adoc[]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
//package io.perfume.api.common.config;
//
//import lombok.RequiredArgsConstructor;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.data.redis.connection.RedisConnectionFactory;
//import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
//import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
//import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
//
//@Configuration
//@EnableRedisRepositories
//@RequiredArgsConstructor
//public class RedisConfiguration {
//
// @Value("${spring.redis.host}")
// private String host;
//
// @Value("${spring.redis.port}")
// private int port;
//
// @Value("${spring.redis.password}")
// private String password;
//
// @Bean
// public RedisConnectionFactory redisConnectionFactory() {
// RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
// redisStandaloneConfiguration.setHostName(host);
// redisStandaloneConfiguration.setPort(port);
// redisStandaloneConfiguration.setPassword(password);
// return new LettuceConnectionFactory(redisStandaloneConfiguration);
// }
//}
package io.perfume.api.common.config;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;

@Configuration
@EnableRedisRepositories
@RequiredArgsConstructor
public class RedisConfiguration {

@Value("${spring.redis.host}")
private String host;

@Value("${spring.redis.port}")
private int port;

@Value("${spring.redis.password}")
private String password;

@Bean
public RedisConnectionFactory redisConnectionFactory() {
RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
redisStandaloneConfiguration.setHostName(host);
redisStandaloneConfiguration.setPort(port);
redisStandaloneConfiguration.setPassword(password);
return new LettuceConnectionFactory(redisStandaloneConfiguration);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.perfume.api.notification.application.port.in.DeleteNotificationUseCase;
import io.perfume.api.notification.application.port.in.GetNotificationUseCase;
import io.perfume.api.notification.application.port.in.ReadNotificationUseCase;
import java.time.LocalDateTime;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
Expand All @@ -17,8 +18,6 @@
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/v1/notification")
public class NotificationController {
Expand All @@ -32,31 +31,31 @@ public class NotificationController {
private final GetNotificationUseCase getNotificationUseCase;

public NotificationController(
NotificationFacadeService notificationService,
ReadNotificationUseCase readNotificationUseCase,
DeleteNotificationUseCase deleteNotificationUseCase, GetNotificationUseCase getNotificationUseCase) {
NotificationFacadeService notificationService,
ReadNotificationUseCase readNotificationUseCase,
DeleteNotificationUseCase deleteNotificationUseCase,
GetNotificationUseCase getNotificationUseCase) {
this.notificationService = notificationService;
this.readNotificationUseCase = readNotificationUseCase;
this.deleteNotificationUseCase = deleteNotificationUseCase;
this.getNotificationUseCase = getNotificationUseCase;
}


@PreAuthorize("isAuthenticated()")
@GetMapping()
public ResponseEntity<CursorResponse<GetNotificationResponseDto>> getNotifications(
@AuthenticationPrincipal User user, GetNotificationRequestDto request) {
@AuthenticationPrincipal User user, GetNotificationRequestDto request) {
var userId = Long.parseLong(user.getUsername());
var notifications = getNotificationUseCase.getNotifications(userId, request.toCommand());
final var responseItems =
notifications.getItems().stream().map(GetNotificationResponseDto::from).toList();
notifications.getItems().stream().map(GetNotificationResponseDto::from).toList();
return ResponseEntity.ok(
CursorResponse.of(
responseItems,
notifications.hasNext(),
notifications.hasPrevious(),
notifications.getNextCursor(),
notifications.getPreviousCursor()));
CursorResponse.of(
responseItems,
notifications.hasNext(),
notifications.hasPrevious(),
notifications.getNextCursor(),
notifications.getPreviousCursor()));
}

@PreAuthorize("isAuthenticated()")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import io.perfume.api.notification.application.port.in.dto.GetNotificationCommand;

public record GetNotificationRequestDto(Integer pageSize, String before, String after) {
public GetNotificationCommand toCommand() {
return new GetNotificationCommand(getSizeOrDefault(), before, after);
}
public GetNotificationCommand toCommand() {
return new GetNotificationCommand(getSizeOrDefault(), before, after);
}

private int getSizeOrDefault() {
return pageSize == null ? 10 : pageSize;
}
private int getSizeOrDefault() {
return pageSize == null ? 10 : pageSize;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@

public record GetNotificationResponseDto(Long id, String content, String notificationType) {

public static GetNotificationResponseDto from(GetNotificationResult getNotificationResult) {
return new GetNotificationResponseDto(
getNotificationResult.id(),
getNotificationResult.content(),
getNotificationResult.notificationType()
);
}
public static GetNotificationResponseDto from(GetNotificationResult getNotificationResult) {
return new GetNotificationResponseDto(
getNotificationResult.id(),
getNotificationResult.content(),
getNotificationResult.notificationType());
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package io.perfume.api.notification.adapter.port.out;

import static io.perfume.api.notification.adapter.port.out.QNotificationEntity.notificationEntity;

import com.querydsl.jpa.impl.JPAQueryFactory;
import dto.repository.CursorPageable;
import dto.repository.CursorPagination;
import io.perfume.api.base.PersistenceAdapter;
import io.perfume.api.notification.application.port.out.notification.NotificationQueryRepository;
import io.perfume.api.notification.domain.Notification;

import java.util.List;
import java.util.Optional;

import static io.perfume.api.notification.adapter.port.out.QNotificationEntity.notificationEntity;

@PersistenceAdapter
public class NotificationQueryPersistenceAdapter implements NotificationQueryRepository {

Expand Down Expand Up @@ -41,9 +40,13 @@ public Optional<Notification> findById(Long id) {

@Override
public CursorPagination<Notification> findByreceiveUserId(CursorPageable pageable, long userId) {
var qb = jpaQueryFactory
var qb =
jpaQueryFactory
.selectFrom(notificationEntity)
.where(notificationEntity.receiveUserId.eq(userId)
.where(
notificationEntity
.receiveUserId
.eq(userId)
.and(notificationEntity.deletedAt.isNull()))
.orderBy(notificationEntity.id.desc());

Expand All @@ -58,10 +61,11 @@ public CursorPagination<Notification> findByreceiveUserId(CursorPageable pageabl
}

final List<Notification> notifications =
qb.limit(pageable.getFetchSize()).fetch().stream()
qb.limit(pageable.getFetchSize()).fetch().stream()
.map(notificationMapper::toDomain)
.toList();

return CursorPagination.of(notifications, pageable, notification -> notification.getReceiveUserId().toString());
return CursorPagination.of(
notifications, pageable, notification -> notification.getReceiveUserId().toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@

public interface GetNotificationUseCase {

CursorPagination<GetNotificationResult> getNotifications(long userId, GetNotificationCommand command);
CursorPagination<GetNotificationResult> getNotifications(
long userId, GetNotificationCommand command);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
import dto.repository.CursorDirection;

public record GetNotificationCommand(int pageSize, String before, String after) {
public String getCursor() {
if (before != null) return before;
return after;
}
public String getCursor() {
if (before != null) return before;
return after;
}

public boolean hasNext() {
return after != null;
}
public boolean hasNext() {
return after != null;
}

public boolean hasPrevious() {
return before != null;
}
public boolean hasPrevious() {
return before != null;
}

public CursorDirection getDirection() {
if (before != null) return CursorDirection.PREVIOUS;
return CursorDirection.NEXT;
}
public CursorDirection getDirection() {
if (before != null) return CursorDirection.PREVIOUS;
return CursorDirection.NEXT;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
package io.perfume.api.notification.application.port.in.dto;

public record GetNotificationResult(Long id, String content, String notificationType) {
}
public record GetNotificationResult(Long id, String content, String notificationType) {}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import dto.repository.CursorPageable;
import dto.repository.CursorPagination;
import io.perfume.api.notification.application.port.in.dto.NotificationResult;
import io.perfume.api.notification.domain.Notification;

import java.util.Optional;

public interface NotificationQueryRepository {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,26 @@
@Service
public class GetNotificationService implements GetNotificationUseCase {

private final NotificationQueryRepository notificationQueryRepository;
private final NotificationQueryRepository notificationQueryRepository;

public GetNotificationService(NotificationQueryRepository notificationQueryRepository) {
this.notificationQueryRepository = notificationQueryRepository;
}
public GetNotificationService(NotificationQueryRepository notificationQueryRepository) {
this.notificationQueryRepository = notificationQueryRepository;
}

@Override
public CursorPagination<GetNotificationResult> getNotifications(long userId, GetNotificationCommand command) {
var pageable =
new CursorPageable(command.pageSize(), command.getDirection(), command.getCursor());
var notifications = notificationQueryRepository.findByreceiveUserId(pageable, userId);
var result =
notifications.getItems().stream()
.map(
item ->
new GetNotificationResult(
item.getId(),
item.getContent(),
item.getNotificationType().getType()))
.toList();
@Override
public CursorPagination<GetNotificationResult> getNotifications(
long userId, GetNotificationCommand command) {
var pageable =
new CursorPageable(command.pageSize(), command.getDirection(), command.getCursor());
var notifications = notificationQueryRepository.findByreceiveUserId(pageable, userId);
var result =
notifications.getItems().stream()
.map(
item ->
new GetNotificationResult(
item.getId(), item.getContent(), item.getNotificationType().getType()))
.toList();

return CursorPagination.of(result, notifications);
}
return CursorPagination.of(result, notifications);
}
}
Loading

0 comments on commit 02716c4

Please sign in to comment.