Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 관리자용 요청 주제(토픽) 조회 및 수정 API 구현 #319

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class AuthorizationInterceptor implements HandlerInterceptor {
new UriAndMethod("/categories", GET),
new UriAndMethod("/users/basic-profile-image", GET),
new UriAndMethod("/users/basic-background-image", GET),
new UriAndMethod("/topics", GET)
};

private final JwtManager jwtManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static com.listywave.common.exception.ErrorCode.RESOURCE_NOT_FOUND;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.listywave.common.exception.CustomException;
import java.util.Arrays;
import lombok.AllArgsConstructor;
Expand All @@ -15,31 +14,39 @@ public enum CategoryType {
ENTIRE("0", "전체"),
MUSIC("1", "음악"),
MOVIE_DRAMA("2", "영화&드라마"),
ENTERTAINMENT_ARTS("3","엔터&예술"),
ENTERTAINMENT_ARTS("3", "엔터&예술"),
TRAVEL("4", "여행"),
RESTAURANT_CAFE("5", "맛집&카페"),
FOOD_RECIPES("6", "음식&레시피"),
PLACE("7", "공간"),
DAILYLIFE_THOUGHTS("8", "일상&생각"),
HOBBY_LEISURE("9", "취미&레저"),
ETC("10", "기타")
ETC("10", "기타"),
;

private static final String ERROR_MESSAGE = "해당 카테고리는 존재하지 않습니다. 입력값: ";

private final String code;
private final String viewName;

public static CategoryType codeOf(String code) {
return Arrays.stream(CategoryType.values())
.filter(t -> t.getCode().equals(code))
.findAny()
.orElseThrow(() -> new CustomException(RESOURCE_NOT_FOUND, "해당 카테고리코드는 존재하지 않습니다."));
.orElseThrow(() -> new CustomException(RESOURCE_NOT_FOUND, ERROR_MESSAGE + code));
}

@JsonCreator
public static CategoryType nameOf(String name) {
return Arrays.stream(CategoryType.values())
.filter(categoryType -> categoryType.name().equalsIgnoreCase(name))
.findFirst()
.orElseThrow(() -> new CustomException(RESOURCE_NOT_FOUND, "해당 카테고리는 존재하지 않습니다."));
.orElseThrow(() -> new CustomException(RESOURCE_NOT_FOUND, ERROR_MESSAGE + name));
}

public static CategoryType viewNameOf(String viewName) {
return Arrays.stream(CategoryType.values())
.filter(categoryType -> categoryType.getViewName().equals(viewName))
.findFirst()
.orElseThrow(() -> new CustomException(RESOURCE_NOT_FOUND, ERROR_MESSAGE + viewName));
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.listywave.list.application.domain.category;

import jakarta.persistence.AttributeConverter;
import jakarta.persistence.Converter;

@Converter(autoApply = true)
public class CategoryTypeConverter implements AttributeConverter<CategoryType, String> {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@
import com.listywave.collaborator.application.domain.Collaborators;
import com.listywave.common.exception.CustomException;
import com.listywave.list.application.domain.category.CategoryType;
import com.listywave.list.application.domain.category.CategoryTypeConverter;
import com.listywave.list.application.domain.item.Item;
import com.listywave.list.application.domain.item.Items;
import com.listywave.list.application.domain.label.Labels;
import com.listywave.user.application.domain.User;
import jakarta.persistence.Column;
import jakarta.persistence.Convert;
import jakarta.persistence.Embedded;
import jakarta.persistence.Entity;
import jakarta.persistence.EntityListeners;
Expand Down Expand Up @@ -56,7 +54,6 @@ public class ListEntity {
private User user;

@Column(name = "category_code", length = 10, nullable = false)
@Convert(converter = CategoryTypeConverter.class)
private CategoryType category;

@Embedded
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
import java.util.List;
import java.util.Set;
import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.stereotype.Service;
Expand All @@ -75,7 +74,6 @@ public class ListService {
private final AlarmRepository alarmRepository;
private final CollaboratorService collaboratorService;
private final HistoryService historyService;
private final ApplicationEventPublisher applicationEventPublisher;

public ListCreateResponse listCreate(ListCreateRequest request, Long loginUserId) {
User user = userRepository.getById(loginUserId);
Expand Down
52 changes: 52 additions & 0 deletions src/main/java/com/listywave/topic/application/domain/Topic.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.listywave.topic.application.domain;

import static lombok.AccessLevel.PROTECTED;

import com.listywave.common.BaseEntity;
import com.listywave.list.application.domain.category.CategoryType;
import com.listywave.list.application.domain.list.ListDescription;
import com.listywave.list.application.domain.list.ListTitle;
import com.listywave.user.application.domain.User;
import jakarta.persistence.Column;
import jakarta.persistence.Embedded;
import jakarta.persistence.Entity;
import jakarta.persistence.ForeignKey;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Getter
@Builder
@NoArgsConstructor(access = PROTECTED)
@AllArgsConstructor
public class Topic extends BaseEntity {

@ManyToOne
@JoinColumn(name = "user_id", nullable = false, foreignKey = @ForeignKey(name = "topic_user_fk"))
private User user;

@Column(name = "category_code", length = 10, nullable = false)
private CategoryType category;

@Embedded
private ListTitle title;

@Embedded
private ListDescription description;

@Column(nullable = false)
private boolean isAnonymous;

@Column(nullable = false)
private boolean isExposed;

public void update(boolean isExposed, CategoryType categoryType, String title) {
this.isExposed = isExposed;
this.category = categoryType;
this.title = new ListTitle(title);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.listywave.topic.application.service;

import com.listywave.list.application.domain.category.CategoryType;
import com.listywave.topic.application.domain.Topic;
import com.listywave.topic.application.service.dto.ExposedTopicFindResponse;
import com.listywave.topic.application.service.dto.TopicCreateRequest;
import com.listywave.topic.application.service.dto.TopicFindResponse;
import com.listywave.topic.repository.TopicRepository;
import com.listywave.user.application.domain.User;
import com.listywave.user.repository.user.UserRepository;
import jakarta.annotation.Nullable;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
@RequiredArgsConstructor
public class TopicService {

private final UserRepository userRepository;
private final TopicRepository topicRepository;

public void create(TopicCreateRequest request, Long userId) {
User user = userRepository.getById(userId);
Topic topic = request.toEntity(user);
topicRepository.save(topic);
}

@Transactional(readOnly = true)
public ExposedTopicFindResponse findAllExposed(@Nullable Long cursorId, int size) {
List<Topic> result = topicRepository.findAllExposed(cursorId, size);
return ExposedTopicFindResponse.of(result, size);
}

@Transactional(readOnly = true)
public TopicFindResponse findAll(@Nullable Long cursorId, int size) {
List<Topic> result = topicRepository.findAll(cursorId, size);
long totalCount = (topicRepository.count() / size) + 1;
return TopicFindResponse.from(result, size, totalCount);
}

public void update(Long topicId, boolean isExposed, String categoryCode, String title) {
Topic topic = topicRepository.getById(topicId);
topic.update(isExposed, CategoryType.codeOf(categoryCode), title);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.listywave.topic.application.service.dto;

import com.listywave.topic.application.domain.Topic;
import java.util.List;
import lombok.Builder;

public record ExposedTopicFindResponse(
Long cursorId,
boolean hasNext,
List<TopicDto> topics
) {

public static ExposedTopicFindResponse of(List<Topic> topics, int size) {
if (topics.isEmpty()) {
return new ExposedTopicFindResponse(null, false, List.of());
}

boolean hasNext = false;
if (topics.size() > size) {
hasNext = true;
topics.remove(topics.size() - 1);
}
Long cursorId = topics.get(topics.size() - 1).getId();
List<TopicDto> topicDtos = TopicDto.toList(topics);

return new ExposedTopicFindResponse(cursorId, hasNext, topicDtos);
}

@Builder
public record TopicDto(
Long id,
String categoryEngName,
String categoryKorName,
String title,
String description,
Long ownerId,
String ownerNickname,
boolean isAnonymous
) {

public static List<TopicDto> toList(List<Topic> topics) {
return topics.stream()
.map(TopicDto::of)
.toList();
}

public static TopicDto of(Topic topic) {
return TopicDto.builder()
.id(topic.getId())
.categoryEngName(topic.getCategory().name())
.categoryKorName(topic.getCategory().getViewName())
.title(topic.getTitle().getValue())
.description(topic.getDescription().getValue())
.ownerId(topic.getUser().getId())
.ownerNickname(topic.getUser().getNickname())
.isAnonymous(topic.isAnonymous())
.build();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.listywave.topic.application.service.dto;

import com.listywave.list.application.domain.category.CategoryType;
import com.listywave.list.application.domain.list.ListDescription;
import com.listywave.list.application.domain.list.ListTitle;
import com.listywave.topic.application.domain.Topic;
import com.listywave.user.application.domain.User;

public record TopicCreateRequest(
String categoryKorName,
String title,
String description,
boolean isAnonymous
) {

public Topic toEntity(User user) {
return Topic.builder()
.user(user)
.category(CategoryType.viewNameOf(categoryKorName))
.title(new ListTitle(title))
.description(new ListDescription(description))
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.listywave.topic.application.service.dto;

import com.listywave.topic.application.domain.Topic;
import java.time.LocalDateTime;
import java.util.List;
import lombok.Builder;

@Builder
public record TopicFindResponse(
boolean hasNext,
long totalCount,
Long cursorId,
List<TopicDto> topics
) {

public static TopicFindResponse from(List<Topic> topics, int size, long totalCount) {
if (topics.isEmpty()) {
return new TopicFindResponse(false, totalCount, null, List.of());
}

boolean hasNext = false;
if (topics.size() > size) {
hasNext = true;
topics.remove(topics.size() - 1);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이거또한 앞서 페이징유틸에서 사용하는 메서드 이용하실 수 있으면 하면 좋고 동호님 판단하에 해주시길 바랍니다 !!

long cursorId = topics.get(topics.size() - 1).getId();

return TopicFindResponse.builder()
.hasNext(hasNext)
.totalCount(totalCount)
.cursorId(cursorId)
.topics(TopicDto.toList(topics))
.build();
}

@Builder
public record TopicDto(
String categoryEngName,
String categoryKorName,
String title,
String description,
LocalDateTime createdDate,
Long ownerId,
String ownerNickname,
boolean isAnonymous,
boolean isExposed
) {

public static List<TopicDto> toList(List<Topic> topics) {
return topics.stream()
.map(TopicDto::of)
.toList();
}

private static TopicDto of(Topic topic) {
return TopicDto.builder()
.categoryEngName(topic.getCategory().name())
.categoryKorName(topic.getCategory().getViewName())
.title(topic.getTitle().getValue())
.description(topic.getDescription().getValue())
.createdDate(topic.getCreatedDate())
.ownerId(topic.getUser().getId())
.ownerNickname(topic.getUser().getNickname())
.isAnonymous(topic.isAnonymous())
.isExposed(topic.isExposed())
.build();
}
}
}
Loading
Loading