Skip to content

Commit

Permalink
feat: 참고 링크를 사람별로 관리하도록 변경 (#157)
Browse files Browse the repository at this point in the history
* [#155] feat: 참고 링크를 사람별로 관리하도록 변경

* [#148] fix: 카테고리에서 블로그 의존성을 제거함에 따라 예외 메세지 변경

* test: 계층화 카테고리 테스트코드 추가
  • Loading branch information
shin-mallang authored Dec 12, 2023
1 parent 948c945 commit 4412fbe
Show file tree
Hide file tree
Showing 15 changed files with 118 additions and 203 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/mallang/category/TieredCategory.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ private void validateNoChildrenInParent(T parent) {
if (equals(root) && root.getPreviousSibling() == null && root.getNextSibling() == null) {
return;
}
throw new CategoryHierarchyViolationException("블로드에 존재하는 다른 최상위 카테고리와의 관계가 명시되지 않았습니다.");
throw new CategoryHierarchyViolationException("존재하는 다른 최상위 카테고리와의 관계가 명시되지 않았습니다.");
} else {
if (!parent.getChildren().isEmpty()) {
throw new CategoryHierarchyViolationException("주어진 부모의 자식 카테고리와의 관계가 명시되지 않았습니다.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import com.mallang.auth.domain.Member;
import com.mallang.auth.domain.MemberRepository;
import com.mallang.blog.domain.Blog;
import com.mallang.blog.domain.BlogRepository;
import com.mallang.reference.application.command.SaveReferenceLinkCommand;
import com.mallang.reference.application.command.UpdateReferenceLinkCommand;
import com.mallang.reference.domain.ReferenceLink;
Expand All @@ -17,14 +15,12 @@
@Service
public class ReferenceLinkService {

private final BlogRepository blogRepository;
private final MemberRepository memberRepository;
private final ReferenceLinkRepository referenceLinkRepository;

public Long save(SaveReferenceLinkCommand command) {
Blog blog = blogRepository.getByName(command.blogName());
Member member = memberRepository.getById(command.memberId());
ReferenceLink referenceLink = command.toReferenceLink(member, blog);
ReferenceLink referenceLink = command.toReferenceLink(member);
return referenceLinkRepository.save(referenceLink).getId();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
package com.mallang.reference.application.command;

import com.mallang.auth.domain.Member;
import com.mallang.blog.domain.Blog;
import com.mallang.reference.domain.ReferenceLink;
import jakarta.annotation.Nullable;

public record SaveReferenceLinkCommand(
Long memberId,
String blogName,
String url,
String title,
@Nullable String memo
) {
public ReferenceLink toReferenceLink(Member member, Blog blog) {
return new ReferenceLink(url, title, memo, member, blog);
public ReferenceLink toReferenceLink(Member member) {
return new ReferenceLink(url, title, memo, member);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import static lombok.AccessLevel.PROTECTED;

import com.mallang.auth.domain.Member;
import com.mallang.blog.domain.Blog;
import com.mallang.common.domain.CommonRootEntity;
import com.mallang.reference.exception.NoAuthorityReferenceLinkException;
import jakarta.persistence.Embedded;
Expand Down Expand Up @@ -39,17 +38,11 @@ public class ReferenceLink extends CommonRootEntity<Long> {
@JoinColumn(name = "owner_id", nullable = false)
private Member member;

@ManyToOne(fetch = LAZY)
@JoinColumn(name = "blog_id", nullable = false)
private Blog blog;

public ReferenceLink(String url, String title, String memo, Member member, Blog blog) {
public ReferenceLink(String url, String title, String memo, Member member) {
this.url = new ReferenceLinkUrl(url);
this.title = new ReferenceLinkTitle(title);
this.memo = new ReferenceLinkMemo(memo);
this.blog = blog;
this.member = member;
blog.validateOwner(member);
}

public void update(String url, String title, String memo) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,12 @@ public ResponseEntity<String> fetchTitleInfo(
return ResponseEntity.ok(fetchReferenceLinkTitleService.fetchTitleMetaInfo(url));
}

@PostMapping("/{blogName}")
@PostMapping
public ResponseEntity<Void> save(
@Auth Long memberId,
@PathVariable(name = "blogName") String blogName,
@Valid @RequestBody SaveReferenceLinkRequest request
) {
SaveReferenceLinkCommand command = request.toCommand(memberId, blogName);
SaveReferenceLinkCommand command = request.toCommand(memberId);
Long referenceLinkId = referenceLinkService.save(command);
return ResponseEntity.created(URI.create("/reference-title/" + referenceLinkId)).build();
}
Expand All @@ -73,23 +72,21 @@ public ResponseEntity<Void> delete(
return ResponseEntity.noContent().build();
}

@GetMapping("/{blogName}/exists")
@GetMapping("/exists")
public ResponseEntity<Boolean> checkExistsUrl(
@Auth Long memberId,
@PathVariable("blogName") String blogName,
@RequestParam("url") String url
) {
boolean isExists = referenceLinkQueryService.existsReferenceLinkByUrl(memberId, blogName, url.strip());
boolean isExists = referenceLinkQueryService.existsReferenceLinkByUrl(memberId, url.strip());
return ResponseEntity.ok(isExists);
}

@GetMapping("/{blogName}")
@GetMapping
public ResponseEntity<List<ReferenceLinkSearchResponse>> search(
@Auth Long memberId,
@PathVariable("blogName") String blogName,
@ModelAttribute ReferenceLinkSearchDaoCond cond
) {
List<ReferenceLinkSearchResponse> result = referenceLinkQueryService.search(memberId, blogName, cond);
List<ReferenceLinkSearchResponse> result = referenceLinkQueryService.search(memberId, cond);
return ResponseEntity.ok(result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public record SaveReferenceLinkRequest(
@NotBlank String title,
@Nullable String memo
) {
public SaveReferenceLinkCommand toCommand(Long memberId, String blogName) {
return new SaveReferenceLinkCommand(memberId, blogName, url, title, memo);
public SaveReferenceLinkCommand toCommand(Long memberId) {
return new SaveReferenceLinkCommand(memberId, url, title, memo);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import com.mallang.auth.domain.Member;
import com.mallang.auth.query.repository.MemberQueryRepository;
import com.mallang.blog.domain.Blog;
import com.mallang.blog.query.repository.BlogQueryRepository;
import com.mallang.reference.query.repository.ReferenceLinkQueryRepository;
import com.mallang.reference.query.repository.ReferenceLinkSearchDao.ReferenceLinkSearchDaoCond;
import com.mallang.reference.query.response.ReferenceLinkSearchResponse;
Expand All @@ -17,21 +15,16 @@
@Service
public class ReferenceLinkQueryService {

private final BlogQueryRepository blogQueryRepository;
private final MemberQueryRepository memberQueryRepository;
private final ReferenceLinkQueryRepository referenceLinkQueryRepository;

public boolean existsReferenceLinkByUrl(Long memberId, String blogName, String url) {
Blog blog = blogQueryRepository.getByName(blogName);
public boolean existsReferenceLinkByUrl(Long memberId, String url) {
Member member = memberQueryRepository.getById(memberId);
blog.validateOwner(member);
return referenceLinkQueryRepository.existsByBlogAndUrl(blog, url);
return referenceLinkQueryRepository.existsByMemberAndUrl(member, url);
}

public List<ReferenceLinkSearchResponse> search(Long memberId, String blogName, ReferenceLinkSearchDaoCond cond) {
Blog blog = blogQueryRepository.getByName(blogName);
public List<ReferenceLinkSearchResponse> search(Long memberId, ReferenceLinkSearchDaoCond cond) {
Member member = memberQueryRepository.getById(memberId);
blog.validateOwner(member);
return ReferenceLinkSearchResponse.from(referenceLinkQueryRepository.search(blog, cond));
return ReferenceLinkSearchResponse.from(referenceLinkQueryRepository.search(member, cond));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.mallang.reference.query.repository;

import com.mallang.blog.domain.Blog;
import com.mallang.auth.domain.Member;
import com.mallang.reference.domain.ReferenceLink;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
Expand All @@ -10,6 +10,6 @@ public interface ReferenceLinkQueryRepository extends
JpaRepository<ReferenceLink, Long>,
ReferenceLinkSearchDao {

@Query("SELECT COUNT(rl) > 0 FROM ReferenceLink rl WHERE rl.blog = :blog AND rl.url.url = :url")
boolean existsByBlogAndUrl(@Param("blog") Blog blog, @Param("url") String url);
@Query("SELECT COUNT(rl) > 0 FROM ReferenceLink rl WHERE rl.member = :member AND rl.url.url = :url")
boolean existsByMemberAndUrl(@Param("member") Member member, @Param("url") String url);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import static com.mallang.reference.domain.QReferenceLink.referenceLink;

import com.mallang.blog.domain.Blog;
import com.mallang.auth.domain.Member;
import com.mallang.reference.domain.ReferenceLink;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.impl.JPAQueryFactory;
Expand All @@ -14,7 +14,7 @@

public interface ReferenceLinkSearchDao {

List<ReferenceLink> search(Blog blog, ReferenceLinkSearchDaoCond cond);
List<ReferenceLink> search(Member member, ReferenceLinkSearchDaoCond cond);

record ReferenceLinkSearchDaoCond(
@Nullable String url,
Expand All @@ -30,10 +30,10 @@ class ReferenceLinkSearchDaoImpl implements ReferenceLinkSearchDao {
private final JPAQueryFactory query;

@Override
public List<ReferenceLink> search(Blog blog, ReferenceLinkSearchDaoCond cond) {
public List<ReferenceLink> search(Member member, ReferenceLinkSearchDaoCond cond) {
return query.selectFrom(referenceLink)
.where(
blogEq(blog),
memberEq(member),
urlContains(cond.url()),
titleContains(cond.title()),
memoContains(cond.memo())
Expand All @@ -42,8 +42,8 @@ public List<ReferenceLink> search(Blog blog, ReferenceLinkSearchDaoCond cond) {
.fetch();
}

private BooleanExpression blogEq(Blog blog) {
return referenceLink.blog.eq(blog);
private BooleanExpression memberEq(Member member) {
return referenceLink.member.eq(member);
}

private BooleanExpression urlContains(String url) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ public class ReferenceLinkAcceptanceSteps {

public static ExtractableResponse<Response> 참고_링크_저장_요청(
String 세션_ID,
String 블로그_이름,
SaveReferenceLinkRequest 참고_링크_저장_요청
) {
return given(세션_ID)
.body(참고_링크_저장_요청)
.post("/reference-links/{blogName}", 블로그_이름)
.post("/reference-links")
.then()
.extract();
}
Expand Down Expand Up @@ -47,26 +46,24 @@ public class ReferenceLinkAcceptanceSteps {

public static ExtractableResponse<Response> 주어진_URL_로_이미_등록된_링크_존재여부_확인_요청(
String 세션_ID,
String 블로그_이름,
String URL
) {
return given(세션_ID)
.queryParam("url", URL)
.get("/reference-links/{blogName}/exists", 블로그_이름)
.get("/reference-links/exists")
.then()
.extract();
}

public static ExtractableResponse<Response> 참고_링크_검색_요청(
String 세션_ID,
String 블로그_이름,
ReferenceLinkSearchDaoCond 검색_조건
) {
return given(세션_ID)
.queryParam("url", 검색_조건.url())
.queryParam("title", 검색_조건.title())
.queryParam("memo", 검색_조건.memo())
.get("/reference-links/{blogName}", 블로그_이름)
.get("/reference-links")
.then()
.extract();
}
Expand Down
Loading

0 comments on commit 4412fbe

Please sign in to comment.