-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Feat/#36 backend news api (요약된 뉴스 조회)
- Loading branch information
Showing
13 changed files
with
195 additions
and
9 deletions.
There are no files selected for viewing
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
27 changes: 27 additions & 0 deletions
27
.../src/main/java/com/rollthedice/backend/domain/bookmark/controller/BookmarkController.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 com.rollthedice.backend.domain.bookmark.controller; | ||
|
||
import com.rollthedice.backend.domain.bookmark.service.BookmarkService; | ||
import com.rollthedice.backend.domain.news.dto.response.NewsResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.List; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("bookmark") | ||
public class BookmarkController { | ||
private final BookmarkService bookmarkService; | ||
|
||
@ResponseStatus(HttpStatus.OK) | ||
@GetMapping("") | ||
public List<NewsResponse> getBookmarked(final Pageable pageable) { | ||
return bookmarkService.getBookmarkedNews(pageable); | ||
} | ||
|
||
} |
3 changes: 2 additions & 1 deletion
3
.../backend/domain/news/entity/Bookmark.java → ...kend/domain/bookmark/entity/Bookmark.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
17 changes: 17 additions & 0 deletions
17
.../src/main/java/com/rollthedice/backend/domain/bookmark/repository/BookmarkRepository.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,17 @@ | ||
package com.rollthedice.backend.domain.bookmark.repository; | ||
|
||
import com.rollthedice.backend.domain.member.entity.Member; | ||
import com.rollthedice.backend.domain.bookmark.entity.Bookmark; | ||
import com.rollthedice.backend.domain.news.entity.News; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.List; | ||
|
||
@Repository | ||
public interface BookmarkRepository extends JpaRepository<Bookmark, Long> { | ||
Boolean existsBookmarkByMemberAndNews(Member member, News news); | ||
|
||
List<Bookmark> findAllByMemberOrderByCreatedAt(Member member, Pageable pageable); | ||
} |
39 changes: 39 additions & 0 deletions
39
...d/core/src/main/java/com/rollthedice/backend/domain/bookmark/service/BookmarkService.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,39 @@ | ||
package com.rollthedice.backend.domain.bookmark.service; | ||
|
||
import com.rollthedice.backend.domain.bookmark.repository.BookmarkRepository; | ||
import com.rollthedice.backend.domain.member.entity.Member; | ||
import com.rollthedice.backend.domain.member.query.AuthService; | ||
import com.rollthedice.backend.domain.news.dto.response.NewsResponse; | ||
import com.rollthedice.backend.domain.news.entity.News; | ||
import com.rollthedice.backend.domain.news.mapper.NewsMapper; | ||
import com.rollthedice.backend.domain.news.service.NewsService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
public class BookmarkService { | ||
private final AuthService authService; | ||
private final BookmarkRepository bookmarkRepository; | ||
private final NewsMapper newsMapper; | ||
|
||
|
||
public boolean isBookmarked(Member member, News news) { | ||
return bookmarkRepository.existsBookmarkByMemberAndNews(member, news); | ||
} | ||
|
||
@Transactional(readOnly = true) | ||
public List<NewsResponse> getBookmarkedNews(Pageable pageable) { | ||
Member member = authService.getMember(); | ||
return bookmarkRepository.findAllByMemberOrderByCreatedAt(member, pageable).stream() | ||
.map(bookmark -> newsMapper.toResponse(bookmark.getNews(), true)) | ||
.collect(Collectors.toList()); | ||
|
||
} | ||
} | ||
|
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
24 changes: 24 additions & 0 deletions
24
...end/core/src/main/java/com/rollthedice/backend/domain/news/controller/NewsController.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,24 @@ | ||
package com.rollthedice.backend.domain.news.controller; | ||
|
||
import com.rollthedice.backend.domain.crawling.NewsCrawlingService; | ||
import com.rollthedice.backend.domain.news.dto.response.NewsResponse; | ||
import com.rollthedice.backend.domain.news.service.NewsService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.List; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("news") | ||
public class NewsController { | ||
private final NewsService newsService; | ||
|
||
@ResponseStatus(HttpStatus.OK) | ||
@GetMapping("") | ||
public List<NewsResponse> getNews(final Pageable pageable) { | ||
return newsService.getNews(pageable); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...end/core/src/main/java/com/rollthedice/backend/domain/news/dto/response/NewsResponse.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,23 @@ | ||
package com.rollthedice.backend.domain.news.dto.response; | ||
|
||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.Lob; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class NewsResponse { | ||
private Long id; | ||
private String title; | ||
private String content; | ||
private String thumbnail; | ||
private String postDate; | ||
private Boolean isBookmarked; | ||
} |
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
12 changes: 12 additions & 0 deletions
12
backend/core/src/main/java/com/rollthedice/backend/domain/news/mapper/NewsMapper.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,12 @@ | ||
package com.rollthedice.backend.domain.news.mapper; | ||
|
||
import com.rollthedice.backend.domain.news.dto.response.NewsResponse; | ||
import com.rollthedice.backend.domain.news.entity.News; | ||
import org.mapstruct.Mapper; | ||
import org.mapstruct.MappingConstants.ComponentModel; | ||
|
||
@Mapper(componentModel = ComponentModel.SPRING) | ||
public interface NewsMapper { | ||
|
||
NewsResponse toResponse(final News news, boolean isBookmarked); | ||
} |
8 changes: 8 additions & 0 deletions
8
...end/core/src/main/java/com/rollthedice/backend/domain/news/repository/NewsRepository.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 |
---|---|---|
@@ -1,7 +1,15 @@ | ||
package com.rollthedice.backend.domain.news.repository; | ||
|
||
import com.rollthedice.backend.domain.news.entity.News; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.List; | ||
|
||
@Repository | ||
public interface NewsRepository extends JpaRepository<News, Long> { | ||
List<News> findAllByContentIsNull(); | ||
List<News> findAllByOrderByViewsDescCreatedAtDesc( | ||
final Pageable 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
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