diff --git a/.idea/misc.xml b/.idea/misc.xml index 3a39a52..0098afc 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,7 +1,7 @@ - + \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts index 3d6a536..fe5e354 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -7,7 +7,8 @@ plugins { ktor { docker { - jreVersion.set(JavaVersion.VERSION_22) +// jreVersion.set(JavaVersion.VERSION_22) + jreVersion.set(JavaVersion.VERSION_21) localImageName.set("ktor-api") imageTag.set("latest") portMappings.set( @@ -57,5 +58,6 @@ tasks.test { useJUnitPlatform() } kotlin { - jvmToolchain(22) +// jvmToolchain(22) + jvmToolchain(21) } \ No newline at end of file diff --git a/init.sql b/init.sql index 80e2420..e2f2d1b 100644 --- a/init.sql +++ b/init.sql @@ -18,6 +18,7 @@ CREATE TABLE IF NOT EXISTS sneakers( name VARCHAR(255) NOT NULL, description VARCHAR(255) NOT NULL, cost DECIMAL(10,2) NOT NULL, + count INTEGER NOT NULL, discount INTEGER NOT NULL CHECK (discount >= 0 AND discount <= 100), photo VARCHAR(255) NOT NULL, gender CHAR(1) NOT NULL CHECK (gender IN ('M', 'F', 'U')), @@ -35,11 +36,23 @@ VALUES ('outdoor', 'for walking'); INSERT INTO categories (name, description) VALUES ('football', 'for football'); -INSERT INTO sneakers (name, description, cost, discount, photo, gender, bootSize, categoryid) -VALUES ('Adidas', 'fast', 120.50, 10, 'https://images.footlocker.com/is/image/FLEU/314312107304_01?wid=500&hei=500&fmt=png-alpha', 'M', 42, 3); +INSERT INTO sneakers (name, description, cost, count, discount, photo, gender, bootSize, categoryid) +VALUES ('Adidas', 'fast', 120.50, 6, 10, 'https://images.footlocker.com/is/image/FLEU/314312107304_01?wid=500&hei=500&fmt=png-alpha', 'M', 42, 3); -INSERT INTO sneakers (name, description, cost, discount, photo, gender, bootSize, categoryid) -VALUES ('Nike Air Max', 'Comfortable running shoes', 140, 40, 'https://static.nike.com/a/images/t_default/c4d6bfc9-f44f-467a-8e27-d9d46cfec67e/NIKE+AIR+MAX+270+GS.png', 'F', 39, 2); +INSERT INTO sneakers (name, description, cost, count, discount, photo, gender, bootSize, categoryid) +VALUES ('Nike', 'Comfortable running shoes', 140, 8, 40, 'https://static.nike.com/a/images/t_default/c4d6bfc9-f44f-467a-8e27-d9d46cfec67e/NIKE+AIR+MAX+270+GS.png', 'F', 39, 2); -INSERT INTO sneakers (name, description, cost, discount, photo, gender, bootSize, categoryid) -VALUES ('Puma', 'fast too', 80.12, 80, 'https://www.xxl.se/filespin/63b77c174b30493397b4c328300252ef', 'U', 41, 1); +INSERT INTO sneakers (name, description, cost, count, discount, photo, gender, bootSize, categoryid) +VALUES ('Puma', 'fast too', 80.12, 4, 80, 'https://www.xxl.se/filespin/63b77c174b30493397b4c328300252ef', 'U', 41, 1); + +INSERT INTO sneakers (name, description, cost, count, discount, photo, gender, bootSize, categoryid) +VALUES ('Puma 2', 'fast too', 80.12, 4, 80, 'https://russiangolfer.ru/15127-large_default/krossovki-puma-gs-fast.jpg', 'U', 41, 1); + +INSERT INTO sneakers (name, description, cost, count, discount, photo, gender, bootSize, categoryid) +VALUES ('Adidas 2', 'fast too', 80.12, 4, 80, 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRiEqdFHbi0oc5NxX7UW5y-3HokGmMc70MkWw&s', 'U', 41, 1); + +INSERT INTO sneakers (name, description, cost, count, discount, photo, gender, bootSize, categoryid) +VALUES ('Nike 2', 'fast too', 80.12, 4, 80, 'https://jordan-nike.ru/image/cache/catalog/!!!!!!!!!!!!!!!!!!!!!!!!!!111111111111111111111111111111111111111111111/_2022-12-29_102143143-300x350.png', 'U', 41, 1); + +INSERT INTO sneakers (name, description, cost, count, discount, photo, gender, bootSize, categoryid) +VALUES ('Puma 3', 'fast too', 80.12, 4, 80, 'https://slamdunk.shop/wp-content/uploads/2024/09/la-france-untouchable-310865-01.webp', 'U', 41, 1); diff --git a/src/main/kotlin/Application.kt b/src/main/kotlin/Application.kt index 3afbda5..984e1df 100644 --- a/src/main/kotlin/Application.kt +++ b/src/main/kotlin/Application.kt @@ -171,5 +171,14 @@ fun Application.configureRouting() { } } } + + post("/buySneakers") { + val request = call.receive() + if (AuthService.buySneakers(request)){ + call.respond(mapOf("message" to "Куплено")) + } else{ + call.respond(mapOf("error" to "Ну вот не куплено")) + } + } } } diff --git a/src/main/kotlin/AuthService.kt b/src/main/kotlin/AuthService.kt index 94e7b80..fa48a1f 100644 --- a/src/main/kotlin/AuthService.kt +++ b/src/main/kotlin/AuthService.kt @@ -2,6 +2,7 @@ package com.example import at.favre.lib.crypto.bcrypt.BCrypt import org.jetbrains.exposed.sql.* +import org.jetbrains.exposed.sql.SqlExpressionBuilder.minus import org.jetbrains.exposed.sql.transactions.transaction import java.net.InetAddress import java.util.Random @@ -100,6 +101,7 @@ object AuthService { name = it[Sneakers.name], description = it[Sneakers.description], cost = it[Sneakers.cost], + count = it[Sneakers.count], discount = it[Sneakers.discount], photo = it[Sneakers.photo], gender = it[Sneakers.gender], @@ -129,6 +131,28 @@ object AuthService { throw RuntimeException("Error selecting categories: ${e.message}", e) } } + + fun buySneakers(req: BuySneakersRequest): Boolean { + return try { + transaction { + req.sneakers.forEach { item -> + val updatedRows = Sneakers.update({ + Sneakers.id eq item.sneakerId + }) { + it[count] = count - item.count + } + + if (updatedRows == 0) { + throw IllegalStateException("Not enough sneakers or sneaker with ID ${item.sneakerId} not found") + } + } + } + true + } catch (e: Exception) { + e.printStackTrace() + false + } + } } diff --git a/src/main/kotlin/UserDTO.kt b/src/main/kotlin/UserDTO.kt index aa8b173..5fa96ce 100644 --- a/src/main/kotlin/UserDTO.kt +++ b/src/main/kotlin/UserDTO.kt @@ -16,6 +16,12 @@ data class UserForgotPasswordRequest(val email: String) @Serializable data class UserResetPasswordRequest(val code: String, val newPassword: String) +@Serializable +data class BuySneakerRequest(val sneakerId: Int, val count: Int) + +@Serializable +data class BuySneakersRequest(val sneakers: List) + @Serializable data class Sneaker( @@ -23,6 +29,7 @@ data class Sneaker( val name: String, val description: String, @Contextual val cost: BigDecimal, + val count: Int, val discount: Int, val photo: String, val gender: Char, diff --git a/src/main/kotlin/Users.kt b/src/main/kotlin/Users.kt index 72f9006..f9785fc 100644 --- a/src/main/kotlin/Users.kt +++ b/src/main/kotlin/Users.kt @@ -23,6 +23,7 @@ object Sneakers : Table("sneakers") { val name = varchar("name", 255) val description = varchar("description", 255) val cost = decimal("cost", 10, 2) + val count = integer("count") val discount = integer("discount") val photo = varchar("photo", 255) val gender = char("gender")