-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 사용자용 요청 주제(토픽) 생성/조회 API 구현 (#318)
* feat: 사용자용 요청 주제(토픽) 생성/조회 API 구현 (#306) * chore: 충돌 해결 * feat: 사용자용 요청 주제 목록 조회 API URI를 WhiteList에 추가 (#306) * test: 토픽 생성 테스트 검증 수정 (#306)
- Loading branch information
Showing
15 changed files
with
434 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
src/main/java/com/listywave/list/application/domain/category/CategoryTypeConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/main/java/com/listywave/topic/application/domain/Topic.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
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; | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/com/listywave/topic/application/service/TopicService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.listywave.topic.application.service; | ||
|
||
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.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); | ||
} | ||
|
||
public ExposedTopicFindResponse findAllExposed(@Nullable Long cursorId, int size) { | ||
List<Topic> result = topicRepository.findAllExposed(cursorId, size); | ||
return ExposedTopicFindResponse.of(result, size); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
src/main/java/com/listywave/topic/application/service/dto/ExposedTopicFindResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/com/listywave/topic/application/service/dto/TopicCreateRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/com/listywave/topic/presentation/TopicController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.listywave.topic.presentation; | ||
|
||
import com.listywave.common.auth.Auth; | ||
import com.listywave.topic.application.service.TopicService; | ||
import com.listywave.topic.application.service.dto.ExposedTopicFindResponse; | ||
import com.listywave.topic.application.service.dto.TopicCreateRequest; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
public class TopicController { | ||
|
||
private final TopicService topicService; | ||
|
||
@PostMapping("/topics") | ||
ResponseEntity<Void> create(@RequestBody TopicCreateRequest request, @Auth Long userId) { | ||
topicService.create(request, userId); | ||
return ResponseEntity.noContent().build(); | ||
} | ||
|
||
@GetMapping("/topics") | ||
ResponseEntity<ExposedTopicFindResponse> findAllExposed( | ||
@RequestParam(required = false) Long cursorId, | ||
@RequestParam(defaultValue = "10") int size | ||
) { | ||
ExposedTopicFindResponse result = topicService.findAllExposed(cursorId, size); | ||
return ResponseEntity.ok(result); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/listywave/topic/repository/CustomTopicRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.listywave.topic.repository; | ||
|
||
import com.listywave.topic.application.domain.Topic; | ||
import java.util.List; | ||
|
||
public interface CustomTopicRepository { | ||
|
||
List<Topic> findAllExposed(Long cursorId, int size); | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/com/listywave/topic/repository/CustomTopicRepositoryImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.listywave.topic.repository; | ||
|
||
import static com.listywave.topic.application.domain.QTopic.topic; | ||
import static com.listywave.user.application.domain.QUser.user; | ||
|
||
import com.listywave.topic.application.domain.Topic; | ||
import com.querydsl.core.types.dsl.BooleanExpression; | ||
import com.querydsl.jpa.impl.JPAQueryFactory; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@RequiredArgsConstructor | ||
public class CustomTopicRepositoryImpl implements CustomTopicRepository { | ||
|
||
private final JPAQueryFactory queryFactory; | ||
|
||
@Override | ||
public List<Topic> findAllExposed(Long cursorId, int size) { | ||
return queryFactory | ||
.selectFrom(topic) | ||
.join(user).on(topic.user.id.eq(user.id)) | ||
.where( | ||
cursorIdLowerThan(cursorId), | ||
topic.isExposed.isTrue() | ||
) | ||
.limit(size + 1) | ||
.orderBy(topic.id.desc()) | ||
.fetch(); | ||
} | ||
|
||
private static BooleanExpression cursorIdLowerThan(Long cursorId) { | ||
return cursorId == null ? null : topic.id.lt(cursorId); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/com/listywave/topic/repository/TopicRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.listywave.topic.repository; | ||
|
||
import com.listywave.topic.application.domain.Topic; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface TopicRepository extends JpaRepository<Topic, Long>, CustomTopicRepository { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.