-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from TG-WinG/feature/blog
feat(blog): implement hashtag search feature
- Loading branch information
Showing
8 changed files
with
213 additions
and
12 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
src/main/java/kr/tgwing/tech/blog/controller/HashtagController.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,42 @@ | ||
package kr.tgwing.tech.blog.controller; | ||
|
||
import java.security.Principal; | ||
|
||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.web.PageableDefault; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.ModelAttribute; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import lombok.AllArgsConstructor; | ||
|
||
import kr.tgwing.tech.blog.dto.HashtagQuery; | ||
import kr.tgwing.tech.blog.dto.HashtagView; | ||
import kr.tgwing.tech.blog.service.HashtagService; | ||
|
||
/** | ||
* HashtagController | ||
*/ | ||
@RestController | ||
@RequestMapping("/hashtag") | ||
@AllArgsConstructor | ||
public class HashtagController { | ||
|
||
private final HashtagService hashtagService; | ||
|
||
@GetMapping("") | ||
public Page<HashtagView> getHashtags( | ||
@ModelAttribute HashtagQuery query, | ||
@PageableDefault Pageable pageable, | ||
Principal principal | ||
) { | ||
String studentNumber = null; | ||
if (principal != null) { | ||
studentNumber = principal.getName(); | ||
} | ||
return hashtagService.getHashtags(query, studentNumber, pageable); | ||
} | ||
|
||
} |
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,14 @@ | ||
package kr.tgwing.tech.blog.dto; | ||
|
||
import lombok.Data; | ||
|
||
/** | ||
* HashtagQuery | ||
*/ | ||
@Data | ||
public class HashtagQuery { | ||
|
||
private String keyword = ""; | ||
private boolean me = false; | ||
|
||
} |
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,25 @@ | ||
package kr.tgwing.tech.blog.dto; | ||
|
||
import lombok.Builder; | ||
import lombok.Data; | ||
|
||
import kr.tgwing.tech.blog.entity.Hashtag; | ||
|
||
/** | ||
* HashtagView | ||
*/ | ||
@Data | ||
@Builder | ||
public class HashtagView { | ||
|
||
private Long id; | ||
private String name; | ||
|
||
public static HashtagView of(Hashtag hashtag) { | ||
return HashtagView.builder() | ||
.id(hashtag.getId()) | ||
.name(hashtag.getName()) | ||
.build(); | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/kr/tgwing/tech/blog/entity/HashtagSpecifications.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,27 @@ | ||
package kr.tgwing.tech.blog.entity; | ||
|
||
import jakarta.persistence.criteria.Join; | ||
|
||
import org.springframework.data.jpa.domain.Specification; | ||
|
||
import kr.tgwing.tech.user.entity.User; | ||
|
||
/** | ||
* HashtagSpecifications | ||
*/ | ||
public class HashtagSpecifications { | ||
|
||
public static Specification<Hashtag> hasNameLike(String keyword) { | ||
return (root, query, cb) -> | ||
cb.like(root.<String>get("name"), "%" + keyword + "%"); | ||
} | ||
|
||
public static Specification<Hashtag> hasStudentWithStudentNumber(String studentNumber) { | ||
return (root, query, cb) -> { | ||
Join<Hashtag, Post> hashtagPost = root.join("post"); | ||
Join<Post, User> postHashtag = hashtagPost.join("writer"); | ||
return cb.equal(postHashtag.get("studentNumber"), studentNumber); | ||
}; | ||
} | ||
} | ||
|
13 changes: 13 additions & 0 deletions
13
src/main/java/kr/tgwing/tech/blog/repository/HashtagRepository.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,13 @@ | ||
package kr.tgwing.tech.blog.repository; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; | ||
|
||
import kr.tgwing.tech.blog.entity.Hashtag; | ||
|
||
/** | ||
* HashtagRepository | ||
*/ | ||
public interface HashtagRepository extends JpaRepository<Hashtag, Long>, JpaSpecificationExecutor<Hashtag> { | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/kr/tgwing/tech/blog/service/HashtagService.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,16 @@ | ||
package kr.tgwing.tech.blog.service; | ||
|
||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
|
||
import kr.tgwing.tech.blog.dto.HashtagQuery; | ||
import kr.tgwing.tech.blog.dto.HashtagView; | ||
|
||
/** | ||
* HashtagService | ||
*/ | ||
public interface HashtagService { | ||
|
||
Page<HashtagView> getHashtags(HashtagQuery query, String userStudentNumber, Pageable pageable); | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/kr/tgwing/tech/blog/service/HashtagServiceImpl.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,36 @@ | ||
package kr.tgwing.tech.blog.service; | ||
|
||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.jpa.domain.Specification; | ||
import org.springframework.stereotype.Service; | ||
|
||
import lombok.AllArgsConstructor; | ||
|
||
import kr.tgwing.tech.blog.dto.HashtagQuery; | ||
import kr.tgwing.tech.blog.dto.HashtagView; | ||
import kr.tgwing.tech.blog.entity.Hashtag; | ||
import kr.tgwing.tech.blog.entity.HashtagSpecifications; | ||
import kr.tgwing.tech.blog.repository.HashtagRepository; | ||
|
||
/** | ||
* HashtagServiceImpl | ||
*/ | ||
@Service | ||
@AllArgsConstructor | ||
public class HashtagServiceImpl implements HashtagService { | ||
|
||
private final HashtagRepository hashtagRepository; | ||
|
||
@Override | ||
public Page<HashtagView> getHashtags(HashtagQuery query, String userStudentNumber, Pageable pageable) { | ||
Specification<Hashtag> spec = HashtagSpecifications.hasNameLike(query.getKeyword()); | ||
|
||
if (query.isMe()) { | ||
spec = spec.and(HashtagSpecifications.hasStudentWithStudentNumber(userStudentNumber)); | ||
} | ||
Page<Hashtag> result = hashtagRepository.findAll(spec, pageable); | ||
return result.map(HashtagView::of); | ||
} | ||
|
||
} |
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