Skip to content

Commit

Permalink
add endpoint to retrieve bookmark details and other minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
angelodpadron committed Oct 11, 2024
1 parent eda3b6a commit fa52e57
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package org.meliapp.backend.controller

import org.meliapp.backend.dto.ApiResponse
import org.meliapp.backend.dto.bookmark.BookmarkDetails
import org.meliapp.backend.dto.bookmark.BookmarkRequestBody
import org.meliapp.backend.dto.bookmark.BookmarkResponse
import org.meliapp.backend.dto.bookmark.BookmarkSummary
import org.meliapp.backend.service.BookmarkService
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
Expand All @@ -15,12 +16,17 @@ class BookmarkController(
) {

@GetMapping
fun getBookmarks(): ResponseEntity<ApiResponse<List<BookmarkResponse>>> {
fun getBookmarks(): ResponseEntity<ApiResponse<List<BookmarkSummary>>> {
return ResponseEntity.ok(ApiResponse(bookmarkService.getUserBookmarks()))
}

@GetMapping("/{bookmarkId}")
fun getBookmarkDetails(@PathVariable bookmarkId: Long): ResponseEntity<ApiResponse<BookmarkDetails>> {
return ResponseEntity.ok(ApiResponse(bookmarkService.getBookmarkDetails(bookmarkId)))
}

@PostMapping
fun bookmarkProduct(@RequestBody request: BookmarkRequestBody): ResponseEntity<ApiResponse<BookmarkResponse>> {
fun bookmarkProduct(@RequestBody request: BookmarkRequestBody): ResponseEntity<ApiResponse<BookmarkDetails>> {
return ResponseEntity
.status(HttpStatus.CREATED)
.body(ApiResponse(bookmarkService.bookmarkProduct(request)))
Expand All @@ -30,7 +36,7 @@ class BookmarkController(
fun editBookmark(
@PathVariable bookmarkId: Long,
@RequestBody request: BookmarkRequestBody
): ResponseEntity<ApiResponse<BookmarkResponse>> {
): ResponseEntity<ApiResponse<BookmarkDetails>> {
return ResponseEntity.ok(ApiResponse(bookmarkService.editBookmark(bookmarkId, request)))
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.meliapp.backend.dto.bookmark

import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.annotation.JsonProperty

data class BookmarkDetails @JsonCreator constructor(
val id: Long,
@JsonProperty(value = "product_title")
val productTitle: String,
@JsonProperty(value = "post_id")
val postId: String,
val thumbnail: String,
val stars: Int,
val comment: String,

)

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.meliapp.backend.dto.bookmark

import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.annotation.JsonProperty

data class BookmarkSummary @JsonCreator constructor(
val id: Long,
@JsonProperty(value = "product_title")
val productTitle: String,
val stars: Int,
val thumbnail: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class GlobalExceptionHandler {
}

@ExceptionHandler(ProductNotFoundException::class, BookmarkNotFoundException::class)
fun handleNotFoundException(e: ProductNotFoundException): ResponseEntity<ApiResponse<Any>> {
fun handleNotFoundException(e: RuntimeException): ResponseEntity<ApiResponse<Any>> {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(ApiResponse(null, e.localizedMessage))
}

Expand Down
1 change: 1 addition & 0 deletions src/main/kotlin/org/meliapp/backend/model/Product.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ class Product {
@Column(unique = true)
lateinit var meliId: String
lateinit var title: String
lateinit var thumbnail: String
lateinit var price: BigDecimal
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ interface BookmarkRepository : JpaRepository<Bookmark, Long> {
@Query("SELECT b FROM Bookmark b WHERE b.user.id = :userId")
fun findByUserId(@Param("userId") userId: Long): List<Bookmark>

@Query("SELECT b FROM Bookmark b WHERE b.user.id = :userId AND b.id = :bookmarkId")
fun findByIdAndUserId(@Param("userId") userId: Long, @Param("bookmarkId") id: Long): Optional<Bookmark>
@Query("SELECT b FROM Bookmark b WHERE b.id = :bookmarkId AND b.user.id = :userId")
fun findByIdAndUserId(@Param("bookmarkId") id: Long, @Param("userId") userId: Long): Optional<Bookmark>
}
44 changes: 31 additions & 13 deletions src/main/kotlin/org/meliapp/backend/service/BookmarkService.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package org.meliapp.backend.service

import jakarta.transaction.Transactional
import org.meliapp.backend.dto.bookmark.BookmarkDetails
import org.meliapp.backend.dto.bookmark.BookmarkRequestBody
import org.meliapp.backend.dto.bookmark.BookmarkResponse
import org.meliapp.backend.dto.bookmark.BookmarkSummary
import org.meliapp.backend.exception.apc.BookmarkNotFoundException
import org.meliapp.backend.model.Bookmark
import org.meliapp.backend.model.Product
Expand All @@ -18,16 +19,33 @@ class BookmarkService(
private val productRepository: ProductRepository,
) {

fun getUserBookmarks(): List<BookmarkResponse> {
fun getUserBookmarks(): List<BookmarkSummary> {
val currentUser = authService.getUserAuthenticated()

return bookmarkRepository
.findByUserId(currentUser.id)
.map { toBookmarkResponse(it) }
.map { toBookmarkSummary(it) }
}

fun getBookmarkDetails(bookmarkId: Long): BookmarkDetails {
val currentUser = authService.getUserAuthenticated()
return toBookmarkDetails(getBookmark(bookmarkId, currentUser.id))
}

private fun toBookmarkDetails(bookmark: Bookmark): BookmarkDetails {
return BookmarkDetails(
id = bookmark.id,
productTitle = bookmark.product.title,
postId = bookmark.product.meliId,
thumbnail = bookmark.product.thumbnail,
stars = bookmark.stars,
comment = bookmark.comment,

)
}

@Transactional
fun bookmarkProduct(request: BookmarkRequestBody): BookmarkResponse {
fun bookmarkProduct(request: BookmarkRequestBody): BookmarkDetails {
val currentUser = authService.getUserAuthenticated()
val productResponse = meliSearchService.findById(request.meliId)

Expand All @@ -37,6 +55,7 @@ class BookmarkService(
productRepository.save(Product().apply {
meliId = productResponse.id
title = productResponse.title
thumbnail = productResponse.thumbnail
price = productResponse.price
})
}
Expand All @@ -50,12 +69,12 @@ class BookmarkService(

bookmarkRepository.save(bookmark)

return toBookmarkResponse(bookmark)
return toBookmarkDetails(bookmark)

}

@Transactional
fun editBookmark(bookmarkId: Long, request: BookmarkRequestBody): BookmarkResponse {
fun editBookmark(bookmarkId: Long, request: BookmarkRequestBody): BookmarkDetails {
val currentUser = authService.getUserAuthenticated()
val bookmark = getBookmark(bookmarkId, currentUser.id)

Expand All @@ -66,7 +85,7 @@ class BookmarkService(

bookmarkRepository.save(bookmark)

return toBookmarkResponse(bookmark)
return toBookmarkDetails(bookmark)
}

@Transactional
Expand All @@ -81,13 +100,12 @@ class BookmarkService(
.findByIdAndUserId(bookmarkId, userId)
.orElseThrow { BookmarkNotFoundException(bookmarkId) }

private fun toBookmarkResponse(bookmark: Bookmark): BookmarkResponse =
BookmarkResponse(
private fun toBookmarkSummary(bookmark: Bookmark) =
BookmarkSummary(
id = bookmark.id,
stars = bookmark.stars,
comment = bookmark.comment,
meliId = bookmark.product.meliId,
userId = bookmark.user.id
productTitle = bookmark.product.title,
thumbnail = bookmark.product.thumbnail,
stars = bookmark.stars
)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ class BookmarkServiceUnitTest {

whenever(mockUser.id).thenReturn(1L)
whenever(mockProduct.meliId).thenReturn("meli_id")
whenever(mockProduct.title).thenReturn("product_title")
whenever(mockProduct.thumbnail).thenReturn("product_thumbnail")

whenever(meliSearchService.findById(bookmarkRequest.meliId)).thenReturn(productResponse)
whenever(productRepository.findByMeliId(bookmarkRequest.meliId)).thenReturn(Optional.of(mockProduct))
Expand All @@ -69,10 +71,9 @@ class BookmarkServiceUnitTest {
val response = bookmarkService.bookmarkProduct(bookmarkRequest)

// Assert
assertEquals(bookmarkRequest.meliId, response.meliId)
assertEquals(bookmarkRequest.meliId, response.postId)
assertEquals(bookmarkRequest.stars, response.stars)
assertEquals(bookmarkRequest.comment, response.comment)
assertEquals(mockUser.id, response.userId)

// Verify
verify(bookmarkRepository, times(1)).save(any())
Expand All @@ -98,6 +99,8 @@ class BookmarkServiceUnitTest {

whenever(mockUser.id).thenReturn(1L)
whenever(mockProduct.meliId).thenReturn("meli_id")
whenever(mockProduct.title).thenReturn("product_title")
whenever(mockProduct.thumbnail).thenReturn("product_thumbnail")

whenever(meliSearchService.findById(bookmarkRequest.meliId)).thenReturn(productResponse)
whenever(productRepository.findByMeliId(bookmarkRequest.meliId)).thenReturn(Optional.empty())
Expand Down Expand Up @@ -192,6 +195,8 @@ class BookmarkServiceUnitTest {
whenever(bookmarkRepository.findByIdAndUserId(anyLong(), anyLong())).thenReturn(Optional.of(bookmark))
whenever(authService.getUserAuthenticated()).thenReturn(mockUser)
whenever(mockProduct.meliId).thenReturn("meli_id")
whenever(mockProduct.title).thenReturn("product_title")
whenever(mockProduct.thumbnail).thenReturn("product_thumbnail")
whenever(mockUser.id).thenReturn(1L)

// Act
Expand All @@ -201,8 +206,7 @@ class BookmarkServiceUnitTest {
assertEquals(1L, response.id)
assertEquals("updated comment", response.comment)
assertEquals(4, response.stars)
assertEquals("meli_id", response.meliId)
assertEquals(1L, response.userId)
assertEquals("meli_id", response.postId)

// Verify
verify(bookmarkRepository, times(1)).save(bookmark)
Expand Down

0 comments on commit fa52e57

Please sign in to comment.