Skip to content

Commit

Permalink
Merge pull request #74 from tukcomCD2024/feat/#71-backend-bookmark
Browse files Browse the repository at this point in the history
Feat/#71 Bookmark 생성 및 삭제
  • Loading branch information
yeonjy authored Apr 20, 2024
2 parents b39fd07 + 8c5f3d8 commit 0652055
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@
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 org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("bookmark")
@RequestMapping("bookmarks")
public class BookmarkController {
private final BookmarkService bookmarkService;

Expand All @@ -24,4 +21,15 @@ public List<NewsResponse> getBookmarked(final Pageable pageable) {
return bookmarkService.getBookmarkedNews(pageable);
}

@ResponseStatus(HttpStatus.CREATED)
@PostMapping("/{newsId}")
public void saveBookmark(@PathVariable final Long newsId) {
bookmarkService.saveBookmark(newsId);
}

@ResponseStatus(HttpStatus.NO_CONTENT)
@DeleteMapping("/{newsId}")
public void deleteBookmark(@PathVariable final Long newsId) {
bookmarkService.deleteBookmark(newsId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import com.rollthedice.backend.global.config.BaseTimeEntity;
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;


@Getter
@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
Expand All @@ -23,4 +25,10 @@ public class Bookmark extends BaseTimeEntity {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "news_id")
private News news;

@Builder
public Bookmark(Member member, News news) {
this.member = member;
this.news = news;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ public interface BookmarkRepository extends JpaRepository<Bookmark, Long> {
Boolean existsBookmarkByMemberAndNews(Member member, News news);

List<Bookmark> findAllByMemberOrderByCreatedAt(Member member, Pageable pageable);

void deleteByNewsId(Long newsId);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.rollthedice.backend.domain.bookmark.service;

import com.rollthedice.backend.domain.bookmark.entity.Bookmark;
import com.rollthedice.backend.domain.bookmark.repository.BookmarkRepository;
import com.rollthedice.backend.domain.member.entity.Member;
import com.rollthedice.backend.domain.member.query.AuthService;
Expand All @@ -19,6 +20,7 @@
@Service
public class BookmarkService {
private final AuthService authService;
private final NewsService newsService;
private final BookmarkRepository bookmarkRepository;
private final NewsMapper newsMapper;

Expand All @@ -35,5 +37,19 @@ public List<NewsResponse> getBookmarkedNews(Pageable pageable) {
.collect(Collectors.toList());

}

@Transactional
public void saveBookmark(Long newsId) {
Member member = authService.getMember();
bookmarkRepository.save(Bookmark.builder()
.member(member)
.news(newsService.getOneNews(newsId))
.build());
}

@Transactional
public void deleteBookmark(Long newsId) {
bookmarkRepository.deleteByNewsId(newsId);
}
}

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.rollthedice.backend.domain.news.service;

import com.rollthedice.backend.domain.bookmark.entity.Bookmark;
import com.rollthedice.backend.domain.bookmark.service.BookmarkService;
import com.rollthedice.backend.domain.member.entity.Member;
import com.rollthedice.backend.domain.member.query.AuthService;
Expand Down Expand Up @@ -66,4 +65,8 @@ public List<NewsResponse> getNews(final Pageable pageable) {
news, bookmarkService.isBookmarked(member, news)))
.collect(Collectors.toList());
}

public News getOneNews(Long newsId) {
return newsRepository.findById(newsId).orElseThrow(EntityNotFoundException::new);
}
}

0 comments on commit 0652055

Please sign in to comment.