-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from angelodpadron/feature/save-product
Feature: save products as bookmarks
- Loading branch information
Showing
24 changed files
with
542 additions
and
68 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
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
49 changes: 49 additions & 0 deletions
49
src/main/kotlin/org/meliapp/backend/controller/BookmarkController.kt
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,49 @@ | ||
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.BookmarkSummary | ||
import org.meliapp.backend.service.BookmarkService | ||
import org.springframework.http.HttpStatus | ||
import org.springframework.http.ResponseEntity | ||
import org.springframework.web.bind.annotation.* | ||
|
||
@RestController | ||
@RequestMapping("/api/bookmarks") | ||
class BookmarkController( | ||
private val bookmarkService: BookmarkService | ||
) { | ||
|
||
@GetMapping | ||
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<BookmarkDetails>> { | ||
return ResponseEntity | ||
.status(HttpStatus.CREATED) | ||
.body(ApiResponse(bookmarkService.bookmarkProduct(request))) | ||
} | ||
|
||
@PutMapping("/{bookmarkId}") | ||
fun editBookmark( | ||
@PathVariable bookmarkId: Long, | ||
@RequestBody request: BookmarkRequestBody | ||
): ResponseEntity<ApiResponse<BookmarkDetails>> { | ||
return ResponseEntity.ok(ApiResponse(bookmarkService.editBookmark(bookmarkId, request))) | ||
} | ||
|
||
@DeleteMapping("/{bookmarkId}") | ||
fun deleteBookmark(@PathVariable bookmarkId: Long): ResponseEntity<ApiResponse<Any>> { | ||
bookmarkService.deleteBookmark(bookmarkId) | ||
return ResponseEntity.ok(ApiResponse(null, "Bookmark deleted")) | ||
} | ||
|
||
} |
61 changes: 7 additions & 54 deletions
61
src/main/kotlin/org/meliapp/backend/controller/MeliSearchController.kt
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,71 +1,24 @@ | ||
package org.meliapp.backend.controller | ||
|
||
import io.swagger.v3.oas.annotations.Operation | ||
import io.swagger.v3.oas.annotations.Parameter | ||
import io.swagger.v3.oas.annotations.Parameters | ||
import io.swagger.v3.oas.annotations.media.Content | ||
import io.swagger.v3.oas.annotations.media.ExampleObject | ||
import io.swagger.v3.oas.annotations.media.Schema | ||
import org.meliapp.backend.dto.ApiResponse | ||
import org.meliapp.backend.dto.meli.MeliSearchResponse | ||
import org.meliapp.backend.dto.product.ProductResponse | ||
import org.meliapp.backend.service.MeliSearchService | ||
import org.springframework.http.ResponseEntity | ||
import org.springframework.web.bind.annotation.GetMapping | ||
import org.springframework.web.bind.annotation.RequestMapping | ||
import org.springframework.web.bind.annotation.RequestParam | ||
import org.springframework.web.bind.annotation.RestController | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse as SwaggerApiResponse | ||
import org.springframework.web.bind.annotation.* | ||
|
||
@RestController | ||
@RequestMapping("/api/products") | ||
class MeliSearchController(private val meliSearchService: MeliSearchService) { | ||
|
||
@GetMapping("/search") | ||
@Operation( | ||
summary = "Find items by keyword", | ||
description = "Searches for items using a keyword in the MercadoLibre API.", | ||
responses = [ | ||
SwaggerApiResponse( | ||
responseCode = "200", | ||
description = "Successful search results", | ||
content = [ | ||
Content( | ||
mediaType = "application/json", | ||
schema = Schema(implementation = MeliSearchResponse::class) | ||
) | ||
] | ||
), | ||
SwaggerApiResponse( | ||
responseCode = "500", | ||
description = "Internal server error", | ||
content = [Content()] | ||
) | ||
] | ||
) | ||
@Parameters( | ||
Parameter( | ||
name = "keyword", | ||
description = "The keyword used to search for items.", | ||
required = true, | ||
example = "don satur" | ||
), | ||
Parameter( | ||
name = "params", | ||
description = "Filters to be applied on the search. This is a dynamic set of key-value pairs.", | ||
required = false, | ||
schema = Schema( | ||
type = "object", | ||
), | ||
examples = [ | ||
ExampleObject( | ||
name = "filters", | ||
value = "{\"discount\": \"10-100\", \"shipping\": \"mercadoenvios\"}" | ||
) | ||
], | ||
) | ||
) | ||
fun findByKeyword(@RequestParam keyword: String, @RequestParam(required = false) filters: Map<String, String>): ResponseEntity<ApiResponse<MeliSearchResponse>> { | ||
return ResponseEntity.ok(ApiResponse(meliSearchService.findByKeyword(keyword, filters))) | ||
} | ||
|
||
@GetMapping("/{id}") | ||
fun findById(@PathVariable id: String): ResponseEntity<ApiResponse<ProductResponse>> { | ||
return ResponseEntity.ok(ApiResponse(meliSearchService.findById(id))) | ||
} | ||
|
||
} |
2 changes: 1 addition & 1 deletion
2
...p/backend/dto/apc/auth/AuthRequestBody.kt → ...liapp/backend/dto/auth/AuthRequestBody.kt
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
16 changes: 16 additions & 0 deletions
16
src/main/kotlin/org/meliapp/backend/dto/bookmark/BookmarkDetails.kt
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,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, | ||
|
||
) |
11 changes: 11 additions & 0 deletions
11
src/main/kotlin/org/meliapp/backend/dto/bookmark/BookmarkRequestBody.kt
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,11 @@ | ||
package org.meliapp.backend.dto.bookmark | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator | ||
import com.fasterxml.jackson.annotation.JsonProperty | ||
|
||
data class BookmarkRequestBody @JsonCreator constructor( | ||
@JsonProperty(value = "meli_id") | ||
val meliId: String, | ||
val stars: Int, | ||
val comment: String | ||
) |
12 changes: 12 additions & 0 deletions
12
src/main/kotlin/org/meliapp/backend/dto/bookmark/BookmarkSummary.kt
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 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, | ||
) |
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
3 changes: 3 additions & 0 deletions
3
src/main/kotlin/org/meliapp/backend/exception/apc/BookmarkException.kt
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,3 @@ | ||
package org.meliapp.backend.exception.apc | ||
|
||
open class BookmarkException(message: String) : RuntimeException(message) |
3 changes: 3 additions & 0 deletions
3
src/main/kotlin/org/meliapp/backend/exception/apc/BookmarkNotFoundException.kt
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,3 @@ | ||
package org.meliapp.backend.exception.apc | ||
|
||
class BookmarkNotFoundException(id: Long) : BookmarkException("Bookmark with id $id not found") |
4 changes: 4 additions & 0 deletions
4
src/main/kotlin/org/meliapp/backend/exception/apc/ProductNotFoundException.kt
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,4 @@ | ||
package org.meliapp.backend.exception.apc | ||
|
||
class ProductNotFoundException(id: String) : RuntimeException("Product with id $id not found") { | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package org.meliapp.backend.model | ||
|
||
import jakarta.persistence.* | ||
|
||
@Entity | ||
@Table(uniqueConstraints = [UniqueConstraint(columnNames = ["user_id", "product_id"])]) | ||
class Bookmark { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
var id: Long = 0 | ||
|
||
@ManyToOne | ||
lateinit var product: Product | ||
|
||
@ManyToOne | ||
lateinit var user: User | ||
|
||
var stars: Int = 0 | ||
var comment: String = "" | ||
|
||
} |
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,16 @@ | ||
package org.meliapp.backend.model | ||
|
||
import jakarta.persistence.* | ||
import java.math.BigDecimal | ||
|
||
@Entity | ||
class Product { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
var id: Long = 0 | ||
@Column(unique = true) | ||
lateinit var meliId: String | ||
lateinit var title: String | ||
lateinit var thumbnail: String | ||
lateinit var price: BigDecimal | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/kotlin/org/meliapp/backend/repository/BookmarkRepository.kt
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 org.meliapp.backend.repository | ||
|
||
import org.meliapp.backend.model.Bookmark | ||
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 java.util.* | ||
|
||
@Repository | ||
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.id = :bookmarkId AND b.user.id = :userId") | ||
fun findByIdAndUserId(@Param("bookmarkId") id: Long, @Param("userId") userId: Long): Optional<Bookmark> | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/kotlin/org/meliapp/backend/repository/ProductRepository.kt
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,11 @@ | ||
package org.meliapp.backend.repository | ||
|
||
import org.meliapp.backend.model.Product | ||
import org.springframework.data.jpa.repository.JpaRepository | ||
import org.springframework.stereotype.Repository | ||
import java.util.* | ||
|
||
@Repository | ||
interface ProductRepository : JpaRepository<Product, Long> { | ||
fun findByMeliId(meliId: String): Optional<Product> | ||
} |
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
Oops, something went wrong.