Skip to content

Commit

Permalink
[#108] feat: 태그 검색과 많이 기록된 태그 조회 기능 (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdomo authored Oct 16, 2023
1 parent c488d52 commit ecac410
Show file tree
Hide file tree
Showing 14 changed files with 154 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@
import com.todaysfail.domains.like.domain.FailureLike;
import com.todaysfail.domains.like.port.FailureLikeCommandPort;
import com.todaysfail.domains.like.port.FailureLikeQueryPort;
import com.todaysfail.domains.tag.domain.Tag;
import com.todaysfail.domains.tag.exception.TagCountExceedException;
import com.todaysfail.domains.tag.port.TagCommandPort;
import com.todaysfail.domains.tag.service.TagDomainService;
import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -21,11 +25,17 @@ public class FailureDomainService {
private final FailureCommandPort failureCommandPort;
private final FailureLikeCommandPort failureLikeCommandPort;
private final FailureLikeQueryPort failureLikeQueryPort;
private final TagCommandPort tagCommandPort;
private final TagDomainService tagDomainService;

public Failure register(final Failure failure, Category category) {
@Transactional
public Failure register(final Failure failure, Category category, List<Tag> tags) {
validateFailureTagSize(failure.getTags());
validateFailureDate(failure.getFailureDate());
category.validateOwnership(failure.getUserId());
List<Tag> incrementedTags =
tags.stream().map(tagDomainService::increaseUsedCount).collect(Collectors.toList());
tagCommandPort.saveAll(incrementedTags);
return failureCommandPort.save(failure);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ public List<Tag> saveAndRetrieveAllTags(Set<String> tagSet) {
tagName ->
tagRepository
.findByTagName(tagName)
.orElseGet(
() ->
tagRepository.save(
Tag.builder()
.tagName(tagName)
.build())))
.orElseGet(() -> tagRepository.save(Tag.register(tagName))))
.collect(Collectors.toList());
}

@Override
public List<Tag> saveAll(List<Tag> tags) {
return tagRepository.saveAll(tags);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,14 @@ public List<Tag> queryAllByNames(Set<String> tags) {
public List<Tag> queryAllByIds(List<Long> tags) {
return tagRepository.findAllById(tags);
}

@Override
public List<Tag> querySearch(String searchKeyword) {
return tagRepository.findTop5ByTagNameContainsIgnoreCase(searchKeyword);
}

@Override
public List<Tag> queryPopular() {
return tagRepository.findTop5ByOrderByUsedCountDesc();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,14 @@ public class Tag extends BaseTimeEntity {
private Long id;

private String tagName;

private Long usedCount;

public static Tag register(String tagName) {
return Tag.builder().tagName(tagName).usedCount(0L).build();
}

public void increaseUsedCount() {
this.usedCount++;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@

public interface TagCommandPort {
List<Tag> saveAndRetrieveAllTags(Set<String> tagNameSet);

List<Tag> saveAll(List<Tag> tags);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ public interface TagQueryPort {
List<Tag> queryAllByNames(Set<String> tags);

List<Tag> queryAllByIds(List<Long> tags);

List<Tag> querySearch(String searchKeyword);

List<Tag> queryPopular();
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package com.todaysfail.domains.tag.repository;

import com.todaysfail.domains.tag.domain.Tag;
import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;

public interface TagRepository extends JpaRepository<Tag, Long> {
Optional<Tag> findByTagName(String tagName);

List<Tag> findTop5ByTagNameContainsIgnoreCase(String searchKeyword);

List<Tag> findTop5ByOrderByUsedCountDesc();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.todaysfail.domains.tag.service;

import com.todaysfail.aop.lock.RedissonLock;
import com.todaysfail.domains.tag.domain.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class TagDomainService {

@RedissonLock(lockName = "태그사용수증가", identifier = "tag")
public Tag increaseUsedCount(Tag tag) {
tag.increaseUsedCount();
return tag;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public FailureResponse execute(FailureRegisterRequest request) {
.tags(tags.stream().map(Tag::getId).collect(Collectors.toList()))
.secret(request.secret())
.build();
Failure registeredFailure = failureDomainService.register(failure, category);
Failure registeredFailure = failureDomainService.register(failure, category, tags);
return failureMapper.toFailureResponse(registeredFailure);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.todaysfail.api.web.tag;

import com.todaysfail.api.web.tag.dto.response.TagResponse;
import com.todaysfail.api.web.tag.usecase.TagPopularUseCase;
import com.todaysfail.api.web.tag.usecase.TagSearchUseCase;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "7. [태그]")
@RestController
@RequestMapping("/api/v1/tags")
@SecurityRequirement(name = "access-token")
@RequiredArgsConstructor
public class TagController {
private final TagSearchUseCase tagSearchUseCase;
private final TagPopularUseCase tagPopularUseCase;

@Operation(summary = "태그를 검색합니다. (5개)")
@GetMapping("/search")
public List<TagResponse> search(@RequestParam String searchKeyword) {
return tagSearchUseCase.execute(searchKeyword);
}

@Operation(summary = "많이 사용된 태그를 조회합니다. (5개)")
@GetMapping("/popular")
public List<TagResponse> popular() {
return tagPopularUseCase.execute();
}

// @Operation(summary = "추천 태그를 조회합니다.")
// @GetMapping("/recommend")
// TODO: 추천 태그 조회 API 구현
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import com.todaysfail.domains.tag.domain.Tag;

public record TagResponse(Long tagId, String tagName) {
public record TagResponse(Long tagId, String tagName, Long usedCount) {
public static TagResponse from(Tag tag) {
return new TagResponse(tag.getId(), tag.getTagName());
return new TagResponse(tag.getId(), tag.getTagName(), tag.getUsedCount());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
import com.todaysfail.api.web.tag.dto.response.TagResponse;
import com.todaysfail.common.annotation.Mapper;
import com.todaysfail.domains.tag.domain.Tag;
import java.util.Set;
import java.util.List;
import java.util.stream.Collectors;

@Mapper
public class TagMapper {
public TagResponse toTagResponse(Tag tag) {
return new TagResponse(tag.getId(), tag.getTagName());
return TagResponse.from(tag);
}

public Set<TagResponse> toTagResponseSet(Set<Tag> tagSet) {
return tagSet.stream().map(this::toTagResponse).collect(Collectors.toSet());
public List<TagResponse> toTagResponseList(List<Tag> tagList) {
return tagList.stream().map(this::toTagResponse).collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.todaysfail.api.web.tag.usecase;

import com.todaysfail.api.web.tag.dto.response.TagResponse;
import com.todaysfail.api.web.tag.mapper.TagMapper;
import com.todaysfail.common.annotation.UseCase;
import com.todaysfail.domains.tag.domain.Tag;
import com.todaysfail.domains.tag.port.TagQueryPort;
import java.util.List;
import lombok.RequiredArgsConstructor;

@UseCase
@RequiredArgsConstructor
public class TagPopularUseCase {
private final TagMapper tagMapper;
private final TagQueryPort tagQueryPort;

public List<TagResponse> execute() {
List<Tag> popularTagList = tagQueryPort.queryPopular();
return tagMapper.toTagResponseList(popularTagList);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.todaysfail.api.web.tag.usecase;

import com.todaysfail.api.web.tag.dto.response.TagResponse;
import com.todaysfail.api.web.tag.mapper.TagMapper;
import com.todaysfail.common.annotation.UseCase;
import com.todaysfail.domains.tag.domain.Tag;
import com.todaysfail.domains.tag.port.TagQueryPort;
import java.util.List;
import lombok.RequiredArgsConstructor;

@UseCase
@RequiredArgsConstructor
public class TagSearchUseCase {
private final TagMapper tagMapper;
private final TagQueryPort tagQueryPort;

public List<TagResponse> execute(String searchKeyword) {
List<Tag> tagList = tagQueryPort.querySearch(searchKeyword);
return tagMapper.toTagResponseList(tagList);
}
}

0 comments on commit ecac410

Please sign in to comment.