Skip to content

Commit

Permalink
feat: se cambia buy por purchase
Browse files Browse the repository at this point in the history
  • Loading branch information
jmottesi committed Aug 20, 2024
1 parent 674b559 commit 5ac0338
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 17 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.unq-ui</groupId>
<artifactId>mercadolibreModel</artifactId>
<version>1.3.0</version>
<version>1.4.0</version>
<packaging>jar</packaging>

<name>org.example mercadolibreModel</name>
Expand Down
8 changes: 4 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Agregar la dependencia:
<dependency>
<groupId>com.github.unq-ui</groupId>
<artifactId>mercadolibre-model</artifactId>
<version>v1.3.0</version>
<version>v1.4.0</version>
</dependency>
```

Expand Down Expand Up @@ -194,7 +194,7 @@ class MercadoLibreService {
* Completes a purchase for a user.
* @param idUser The user's ID.
* @param payment The payment information.
* @throws BuyException if the cart is empty or items are out of stock.
* @throws PurchaseException if the cart is empty or items are out of stock.
* @throws UserException if the user is not found.
*/
fun purchase(idUser: String, payment: Payment)
Expand Down Expand Up @@ -257,7 +257,7 @@ class User(
val email: String,
var password: String,
val image: String,
val buyHistory: MutableList<BuyHistory>,
val purchaseHistory: MutableList<PurchaseHistory>,
val products: MutableList<Product>,
val likedProducts: MutableList<Product>,
val salesHistory: MutableList<SaleHistory>
Expand All @@ -277,7 +277,7 @@ class SaleHistory(
val user: User,
)

class BuyHistory(
class PurchaseHistory(
val items: MutableList<Item>,
val payment: Payment,
val date: LocalDateTime,
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/model/DataClasses.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class SaleHistory(
val user: User,
)

class BuyHistory(
class PurchaseHistory(
val items: MutableList<Item>,
val payment: Payment,
val date: LocalDateTime,
Expand All @@ -44,7 +44,7 @@ class User(
val email: String,
var password: String,
val image: String,
val buyHistory: MutableList<BuyHistory>,
val purchaseHistory: MutableList<PurchaseHistory>,
val products: MutableList<Product>,
val likedProducts: MutableList<Product>,
val salesHistory: MutableList<SaleHistory>
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/model/Exceptions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class ProductException(msg: String): Exception(msg)

class PageException(msg: String): Exception(msg)

class BuyException(msg: String): Exception(msg)
class PurchaseException(msg: String): Exception(msg)

class CategoryException(msg: String): Exception(msg)

Expand Down
8 changes: 4 additions & 4 deletions src/main/kotlin/service/MercadoLibreService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -273,24 +273,24 @@ class MercadoLibreService {
* Completes a purchase for a user.
* @param idUser The user's ID.
* @param payment The payment information.
* @throws BuyException if the cart is empty or items are out of stock.
* @throws PurchaseException if the cart is empty or items are out of stock.
* @throws UserException if the user is not found.
*/
fun purchase(idUser: String, payment: Payment) {
val user = getUser(idUser)
val cart = getCart(idUser)
if (cart.items.isEmpty()) throw BuyException("Cart is empty")
if (cart.items.isEmpty()) throw PurchaseException("Cart is empty")
if (cart.items.all { it.product.stock >= it.amount }) {
cart.items.forEach {
it.product.stock -= it.amount
it.product.owner.salesHistory.add(
SaleHistory(it.product, it.amount, payment, LocalDateTime.now(), cart.user)
)
}
user.buyHistory.add(BuyHistory(cart.items, payment, LocalDateTime.now()))
user.purchaseHistory.add(PurchaseHistory(cart.items, payment, LocalDateTime.now()))
carts.remove(cart)
} else {
throw BuyException("Unable to complete the purchase because one or more items are out of stock.")
throw PurchaseException("Unable to complete the purchase because one or more items are out of stock.")
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/test/kotlin/service/MercadoLibreServiceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ class MercadoLibreServiceTest {
mercadoLibreService.updateItemCart(buyer.id, p1.id, 1)

assertEquals(seller.salesHistory.size, 0)
assertEquals(buyer.buyHistory.size, 0)
assertEquals(buyer.purchaseHistory.size, 0)
assertEquals(p1.stock, 10)

val payment = Payment("3333", LocalDateTime.now(), "333", "pepe")
Expand All @@ -346,8 +346,8 @@ class MercadoLibreServiceTest {
assertEquals(seller.salesHistory[0].product, p1)
assertEquals(seller.salesHistory[0].payment, payment)

assertEquals(buyer.buyHistory.size, 1)
assertEquals(buyer.buyHistory[0].items[0].product, p1)
assertEquals(buyer.purchaseHistory.size, 1)
assertEquals(buyer.purchaseHistory[0].items[0].product, p1)

assertEquals(p1.stock, 9)
}
Expand All @@ -356,7 +356,7 @@ class MercadoLibreServiceTest {
fun purchaseWithoutItemsTest() {
val mercadoLibreService = MercadoLibreService()
val buyer = mercadoLibreService.registerNewUser(getDraftNewUser("user"))
assertFailsWith<BuyException> {
assertFailsWith<PurchaseException> {
mercadoLibreService.purchase(buyer.id, Payment("3333", LocalDateTime.now(), "333", "pepe"))
}
}
Expand All @@ -369,7 +369,7 @@ class MercadoLibreServiceTest {
val p1 = mercadoLibreService.addProduct(seller.id, getDraftProduct("product"))
mercadoLibreService.updateItemCart(buyer.id, p1.id, 100)

assertFailsWith<BuyException> {
assertFailsWith<PurchaseException> {
mercadoLibreService.purchase(buyer.id, Payment("3333", LocalDateTime.now(), "333", "pepe"))
}
}
Expand Down

0 comments on commit 5ac0338

Please sign in to comment.