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: 태그 개수 추가 #207

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
23 changes: 14 additions & 9 deletions module-api/src/main/java/inspiration/domain/tag/TagService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package inspiration.domain.tag;

import inspiration.RestPage;
import inspiration.domain.inspiration_tag.InspirationTagRepository;
import inspiration.domain.tag.response.TagCountResponse;
import inspiration.exception.ConflictRequestException;
import inspiration.exception.NoAccessAuthorizationException;
import inspiration.exception.ResourceNotFoundException;
Expand All @@ -9,8 +11,6 @@
import inspiration.domain.tag.request.TagAddRequest;
import inspiration.domain.tag.response.TagResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
Expand All @@ -25,14 +25,19 @@ public class TagService {

private final TagRepository tagRepository;
private final MemberService memberService;
private final InspirationTagRepository inspirationTagRepository;

@Transactional(readOnly = true)
public RestPage<TagResponse> findTags(Pageable pageable, Long memberId) {

public RestPage<TagCountResponse> findTags(Pageable pageable, Long memberId) {
Member member = memberService.findById(memberId);

Page<Tag> tagPage = tagRepository.findAllByMember(member, pageable);
return new RestPage<>(tagPage.map(TagResponse::from));
return new RestPage<>(
tagRepository.findAllByMember(member, pageable)
.map(TagResponse::from)
.map(it -> {
long count = inspirationTagRepository.countByTag_id(it.getId());
return it.toTagCountResponse(count);
})
);
}

@Transactional(readOnly = true)
Expand Down Expand Up @@ -69,12 +74,12 @@ public TagResponse addTag(TagAddRequest request, Long memberId) {

public Tag getTag(Long id) {
return tagRepository.findById(id)
.orElseThrow(ResourceNotFoundException::new);
.orElseThrow(ResourceNotFoundException::new);
}

public void removeTag(Long id, Long memberId) {
Tag tag = tagRepository.findById(id)
.orElseThrow(ResourceNotFoundException::new);
.orElseThrow(ResourceNotFoundException::new);

if (!tag.getMember().isSameMember(memberId)) {
throw new NoAccessAuthorizationException();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package inspiration.domain.tag.response;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import inspiration.domain.member.response.MemberResponse;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;

import java.time.LocalDateTime;

@Data
@AllArgsConstructor
public final class TagCountResponse {
private Long id;
private MemberResponse memberResponse;
private String content;
private Long count;
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "Asia/Seoul")
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
private LocalDateTime createdDatetime;
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "Asia/Seoul")
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
private LocalDateTime updatedDatetime;
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,15 @@ public static TagResponse from(Tag tag) {
.build();
}

public TagCountResponse toTagCountResponse(long tagCount) {
return new TagCountResponse(
this.id,
this.memberResponse,
this.content,
tagCount,
this.createdDatetime,
this.updatedDatetime
);
}

}
2 changes: 1 addition & 1 deletion module-domain/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ dependencies {
annotationProcessor "com.querydsl:querydsl-apt:${dependencyManagement.importedProperties['querydsl.version']}:jpa"
annotationProcessor 'jakarta.persistence:jakarta.persistence-api'
annotationProcessor 'jakarta.annotation:jakarta.annotation-api'
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public interface InspirationTagRepository extends JpaRepository<InspirationTag,

Optional<InspirationTag> findByInspirationAndTag(Inspiration inspiration, Tag tag);

long countByTag_id(Long tagId);

void deleteAllByInspiration(Inspiration inspiration);

@Modifying
Expand Down
2 changes: 1 addition & 1 deletion module-web/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ bootJar {
jar {
enabled = false
archivesBaseName = 'inspiration'
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package inspiration.v1.tag;

import inspiration.domain.inspiration_tag.InspirationTagRepository;
import inspiration.domain.tag.response.TagCountResponse;
import inspiration.infrastructure.AuthenticationPrincipal;
import inspiration.domain.inspiration.InspirationService;
import inspiration.domain.tag.TagService;
Expand Down Expand Up @@ -30,6 +32,7 @@ public class TagController {

private final TagService tagService;
private final InspirationService inspirationService;
private final InspirationTagRepository inspirationTagRepository;


@GetMapping("/list")
Expand All @@ -40,7 +43,7 @@ public class TagController {
})
public ResponseEntity<ResultResponse> tagList(Pageable pageable,
@ApiIgnore @AuthenticationPrincipal Long memberId ) {
Page<TagResponse> tagResponsePage = tagService.findTags(pageable, memberId);
Page<TagCountResponse> tagResponsePage = tagService.findTags(pageable, memberId);
return ResponseEntity.ok().body(ResultResponse.from(tagResponsePage));
}

Expand Down