Skip to content

Commit

Permalink
feat: se agregan dos funciones que se van a usar en la api
Browse files Browse the repository at this point in the history
  • Loading branch information
jmottesi committed Aug 17, 2024
1 parent 289c890 commit 775eeba
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 1 deletion.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
<version>2.8.9</version> <!-- O la versión más reciente -->
</dependency>

<dependency>
<groupId>info.debatty</groupId>
<artifactId>java-string-similarity</artifactId>
<version>RELEASE</version>
</dependency>

<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
Expand Down
18 changes: 17 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Agregar la dependencia:
```xml
<dependency>
<groupId>com.github.unq-ui</groupId>
<artifactId>??</artifactId>
<artifactId>mercadolibre-model</artifactId>
<version>??</version>
</dependency>
```
Expand Down Expand Up @@ -85,6 +85,14 @@ class MercadoLibreService {
*/
fun getAllCategories(): List<Category>

/**
* Retrieves a category by ID.
* @param id The category's ID.
* @return The category.
* @throws CategoryException if the category is not found.
*/
fun getCategory(id: String): Category

/**
* Adds a new product.
* @param userId The user's ID.
Expand Down Expand Up @@ -114,6 +122,14 @@ class MercadoLibreService {
*/
fun getProduct(id: String): Product

/**
* Retrieves related products to a product.
* @param idProduct The product's ID.
* @return The list of related products (max 10 products).
* @throws ProductException if the product is not found.
*/
fun getRelatedProducts(idProduct: String): List<Product>

/**
* Retrieves products by user ID with pagination.
* @param idUser The user's ID.
Expand Down
25 changes: 25 additions & 0 deletions src/main/kotlin/service/MercadoLibreService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import model.*
import utilities.Page
import utilities.getPage
import java.time.LocalDateTime
import info.debatty.java.stringsimilarity.*;

class MercadoLibreService {
private val idGenerator = IdGenerator()
Expand Down Expand Up @@ -96,6 +97,16 @@ class MercadoLibreService {
*/
fun getAllCategories(): List<Category> = categories

/**
* Retrieves a category by ID.
* @param id The category's ID.
* @return The category.
* @throws CategoryException if the category is not found.
*/
fun getCategory(id: String): Category {
return categories.find { it.id == id } ?: throw CategoryException("Not found")
}

/**
* Adds a new product.
* @param userId The user's ID.
Expand Down Expand Up @@ -158,6 +169,20 @@ class MercadoLibreService {
return products.find { it.id == id } ?: throw ProductException("Not found")
}

/**
* Retrieves related products to a product.
* @param idProduct The product's ID.
* @return The list of related products (max 10 products).
* @throws ProductException if the product is not found.
*/
fun getRelatedProducts(idProduct: String): List<Product> {
val product = getProduct(idProduct)
val relatedProducts = products.filter {
Levenshtein().distance(it.title, product.title) < 3 && it.id != product.id
}
return relatedProducts.take(10)
}

/**
* Retrieves products by user ID with pagination.
* @param idUser The user's ID.
Expand Down
22 changes: 22 additions & 0 deletions src/test/kotlin/service/MercadoLibreServiceTest.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package service

import data.initSystem
import model.*
import java.time.LocalDateTime
import kotlin.test.*
Expand Down Expand Up @@ -136,6 +137,21 @@ class MercadoLibreServiceTest {
assertEquals(mercadoLibreService.getAllCategories().size, 1)
}

@Test
fun getCategoryTest() {
val mercadoLibreService = MercadoLibreService()
val category = mercadoLibreService.addCategory("cat1")
assertEquals(mercadoLibreService.getCategory("c_1"), category)
}

@Test
fun getCategoryWrongIdTest() {
val mercadoLibreService = MercadoLibreService()
assertFailsWith<CategoryException> {
mercadoLibreService.getCategory("c_1")
}
}

@Test
fun addProductTest() {
val mercadoLibreService = MercadoLibreService()
Expand Down Expand Up @@ -429,4 +445,10 @@ class MercadoLibreServiceTest {
mercadoLibreService.addAnswer(user2.id, p1.id, "q_1", "answer")
}
}

@Test
fun getRelatedProductsTest() {
val mercadoLibreService = initSystem()
assertEquals(mercadoLibreService.getRelatedProducts("p_90").size, 10)
}
}

0 comments on commit 775eeba

Please sign in to comment.