Open pjs1710 opened 6 months ago
package com.backend.Holo.entity;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import lombok.Getter;
import lombok.Setter;
@Entity
@Getter @Setter
@Table(name = "post")
public class SearchPost {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "postId")
private Long postId;
private String title;
@NotNull
private Long categoryId;
}
package com.backend.Holo.repository;
import com.backend.Holo.entity.SearchPost;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import java.util.List;
public interface SearchPostRepository extends JpaRepository<SearchPost, Long> {
@Query("SELECT p FROM SearchPost p WHERE p.title LIKE %:keyword%")
List<SearchPost> findByTitleContaining(@Param("keyword") String keyword);
@Query("SELECT p FROM SearchPost p WHERE p.title LIKE %:keyword% AND p.categoryId = :categoryId")
List<SearchPost> findByTitleContainingAndCategoryId(@Param("keyword") String keyword, @Param("categoryId") int categoryId);
}
package com.backend.Holo.service;
import com.backend.Holo.entity.SearchPost;
import com.backend.Holo.repository.SearchPostRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
@RequiredArgsConstructor
public class SearchPostService {
private final SearchPostRepository searchPostRepository;
public List<SearchPost> searchPosts(String keyword) {
return searchPostRepository.findByTitleContaining(keyword);
}
public List<SearchPost> searchPostsByCategory(String keyword, int categoryId) {
return searchPostRepository.findByTitleContainingAndCategoryId(keyword, categoryId);
}
}
SearchPost Logic Class들은 추후에 FrontEnd 작업 구체화되면 코드 수정 필요.
BackEnd
SearchPostController