Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;

@Tag(name = "Bookmark API", description = "북마크 API")
@RestController
@RequestMapping("/bookmarks")
Expand All @@ -29,8 +30,9 @@ public class BookmarkController {
@PostMapping("/{storeId}")
@Operation(summary = "북마크 생성", description = "특정 주점에 대한 북마크 생성")
@ApiResponse(responseCode = "201", description = "북마크 생성")
public ResponseEntity<?> createBookmark(@PathVariable Long storeId,@AuthenticationPrincipal CustomOAuth2User customOAuth2User) {
BookmarkCreateResponse response = bookmarkService.createBookmark(storeId,customOAuth2User);
public ResponseEntity<?> createBookmark(@PathVariable Long storeId,
@AuthenticationPrincipal CustomOAuth2User customOAuth2User) {
BookmarkCreateResponse response = bookmarkService.createBookmark(storeId, customOAuth2User);

return ResponseEntity
.status(HttpStatus.CREATED)
Expand All @@ -40,6 +42,7 @@ public ResponseEntity<?> createBookmark(@PathVariable Long storeId,@Authenticati
)
);
}

@GetMapping
@Operation(summary = "북마크 조회", description = "내가 북마크한 주점 조회")
@ApiResponse(responseCode = "200", description = "북마크 조회")
Expand All @@ -52,15 +55,17 @@ public ResponseEntity<?> getAllBookmarks(@AuthenticationPrincipal CustomOAuth2Us
)
);
}

@DeleteMapping("/{bookmarkId}")
@Operation(summary = "북마크 삭제", description = "특정 주점에 대한 북마크 삭제")
@ApiResponse(responseCode = "200", description = "북마크 삭제")
public ResponseEntity<?> deleteBookmark(@PathVariable Long bookmarkId, @AuthenticationPrincipal CustomOAuth2User customOAuth2User) {
public ResponseEntity<?> deleteBookmark(@PathVariable Long bookmarkId,
@AuthenticationPrincipal CustomOAuth2User customOAuth2User) {
return ResponseEntity
.ok()
.body(
ApiUtils.success(
bookmarkService.deleteBookmark(bookmarkId,customOAuth2User)
bookmarkService.deleteBookmark(bookmarkId, customOAuth2User)
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;

import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -33,19 +34,29 @@ public class BookmarkService {

@Transactional
public BookmarkCreateResponse createBookmark(Long storeId, CustomOAuth2User customOAuth2User) {

parameterValidation(storeId, customOAuth2User);
Store store = storeRepository.findById(storeId)
.orElseThrow(() -> new EntityNotFoundException(storeId + " store not found."));
User user = userRepository.findById(customOAuth2User.getUserId())
.orElseThrow(() -> new EntityNotFoundException("User not found"));

if (bookmarkRepository.existsByUserAndStore(user, store)) {
throw new IllegalArgumentException("already bookmarked");
Optional<Bookmark> isBookmark = bookmarkRepository.findRawByUserAndStoreAndDeletedFalse(user, store);

if (isBookmark.isPresent()) {
Bookmark bookmark = isBookmark.get();
if (bookmark.isDeleted()) {
bookmark.restore();
return BookmarkCreateResponse.fromEntity(bookmarkRepository.save(bookmark));
} else {
throw new IllegalArgumentException("already bookmarked");
}
}

Bookmark bookmark = Bookmark.builder()
.store(store)
.user(user)
.deleted(false)
.build();

return BookmarkCreateResponse.fromEntity(bookmarkRepository.save(bookmark));
Expand All @@ -56,7 +67,7 @@ public List<StorePageReadResponse> getBookmarks(CustomOAuth2User customOAuth2Use
User user = userRepository.findById(customOAuth2User.getUserId())
.orElseThrow(UserNotFoundException::new);

List<Long> storeIds = bookmarkRepository.findAllByUser(user)
List<Long> storeIds = bookmarkRepository.findAllByUserAndDeletedFalse(user)
.stream()
.map(Bookmark::getStore)
.map(Store::getStoreId)
Expand All @@ -68,15 +79,15 @@ public List<StorePageReadResponse> getBookmarks(CustomOAuth2User customOAuth2Use
}

@Transactional
public String deleteBookmark(Long bookmarkId, CustomOAuth2User customOAuth2User) {
parameterValidation(bookmarkId, customOAuth2User);
Bookmark bookmark = bookmarkRepository.findById(bookmarkId)
.orElseThrow(() -> new EntityNotFoundException(bookmarkId + " bookmark not found."));
public String deleteBookmark(Long storeId, CustomOAuth2User customOAuth2User) {
parameterValidation(storeId, customOAuth2User);
Bookmark bookmark = bookmarkRepository.findActiveByUserIdAndStoreId(storeId, customOAuth2User.getUserId())
.orElseThrow(() -> new EntityNotFoundException(storeId + " bookmark not found."));
if (!Objects.equals(bookmark.getUser().getId(), customOAuth2User.getUserId())) {
throw new IllegalArgumentException("you can only delete your own bookmark");
}
bookmarkRepository.delete(bookmark);
return "Bookmark ID " + bookmarkId + " deleted.";
bookmark.softDelete();
return "Bookmark ID " + storeId + " deleted.";
}

private static void parameterValidation(Long storeId, CustomOAuth2User customOAuth2User) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public StoreDepartmentReadResponse getAllStoresByPageAndDeparments(Pageable page
.toList();

// 2) 사용자 북마크된 storeId 집합 조회
List<Long> storeBookmarkIds = bookmarkRepository.findAllByUser(user)
List<Long> storeBookmarkIds = bookmarkRepository.findAllByUserAndDeletedFalse(user)
.stream()
.map(Bookmark::getStore)
.map(Store::getStoreId)
Expand Down Expand Up @@ -118,7 +118,7 @@ public StoreDepartmentReadResponse getAllStoresByPageAndDeparments(Pageable page
Department::getName
));

List<Bookmark> allBookmarks = bookmarkRepository.findStoreIdByUser(user);
List<Bookmark> allBookmarks = bookmarkRepository.findStoreIdByUserAndDeletedFalse(user);
Map<Long, Boolean> bookmarkMap = allBookmarks.stream()
.collect(Collectors.toMap(
bookmark -> bookmark.getStore().getStoreId(),
Expand Down Expand Up @@ -155,7 +155,7 @@ public StoreDetailReadResponse getStoreByStoreId(Long storeId, CustomOAuth2User
.map(Department::getName)
.orElse("Unknown Department");

boolean isBookmark = bookmarkRepository.existsByUserAndStore(user, store);
boolean isBookmark = bookmarkRepository.existsByUserAndStoreAndDeletedFalse(user, store);

// 2-1) Redis에서 각 Store의 웨이팅 사이즈 조회
String key = "waiting:" + storeId;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.nowait.domainuserrdb.bookmark.entity;

import java.time.LocalDateTime;

import com.nowait.domaincorerdb.base.entity.BaseTimeEntity;
import com.nowait.domaincorerdb.store.entity.Store;
import com.nowait.domaincorerdb.user.entity.User;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
Expand Down Expand Up @@ -43,4 +46,14 @@ public class Bookmark extends BaseTimeEntity {
@JoinColumn(name = "store_id")
private Store store;

@Column(nullable = false)
private boolean deleted = false;

public void softDelete() {
this.deleted = true;
}

public void restore() {
this.deleted = false;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.nowait.domainuserrdb.bookmark.repository;

import java.awt.print.Book;
import java.util.Collection;
import java.util.List;
import java.util.Optional;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import com.nowait.domaincorerdb.store.entity.Store;
Expand All @@ -13,9 +15,23 @@

@Repository
public interface BookmarkRepository extends JpaRepository<Bookmark,Long> {
boolean existsByUserAndStore(User user, Store store);
boolean existsByUserAndStoreAndDeletedFalse(User user, Store store);

Collection<Bookmark> findAllByUser(User user);
Optional<Bookmark> findRawByUserAndStoreAndDeletedFalse(User user, Store store);

List<Bookmark> findStoreIdByUser(User user);
Collection<Bookmark> findAllByUserAndDeletedFalse(User user);

List<Bookmark> findStoreIdByUserAndDeletedFalse(User user);

@Query("""
select b
from Bookmark b
where b.user.id = :userId
and b.store.storeId = :storeId
and b.deleted = false
""")
Optional<Bookmark> findActiveByUserIdAndStoreId(
@Param("storeId") Long storeId,
@Param("userId") Long userId
);
}