This commit is contained in:
Your Name 2025-06-03 14:15:04 +03:00
parent 9ba2ada96c
commit 7177f75440
7 changed files with 65 additions and 9 deletions

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" /> <component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="ProjectRootManager" version="2" languageLevel="JDK_22" default="true" project-jdk-name="corretto-22" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_21" project-jdk-name="corretto-22" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" /> <output url="file://$PROJECT_DIR$/out" />
</component> </component>
</project> </project>

View File

@ -7,7 +7,8 @@ plugins {
ktor { ktor {
docker { docker {
jreVersion.set(JavaVersion.VERSION_22) // jreVersion.set(JavaVersion.VERSION_22)
jreVersion.set(JavaVersion.VERSION_21)
localImageName.set("ktor-api") localImageName.set("ktor-api")
imageTag.set("latest") imageTag.set("latest")
portMappings.set( portMappings.set(
@ -57,5 +58,6 @@ tasks.test {
useJUnitPlatform() useJUnitPlatform()
} }
kotlin { kotlin {
jvmToolchain(22) // jvmToolchain(22)
jvmToolchain(21)
} }

View File

@ -18,6 +18,7 @@ CREATE TABLE IF NOT EXISTS sneakers(
name VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL,
description VARCHAR(255) NOT NULL, description VARCHAR(255) NOT NULL,
cost DECIMAL(10,2) NOT NULL, cost DECIMAL(10,2) NOT NULL,
count INTEGER NOT NULL,
discount INTEGER NOT NULL CHECK (discount >= 0 AND discount <= 100), discount INTEGER NOT NULL CHECK (discount >= 0 AND discount <= 100),
photo VARCHAR(255) NOT NULL, photo VARCHAR(255) NOT NULL,
gender CHAR(1) NOT NULL CHECK (gender IN ('M', 'F', 'U')), gender CHAR(1) NOT NULL CHECK (gender IN ('M', 'F', 'U')),
@ -35,11 +36,23 @@ VALUES ('outdoor', 'for walking');
INSERT INTO categories (name, description) INSERT INTO categories (name, description)
VALUES ('football', 'for football'); VALUES ('football', 'for football');
INSERT INTO sneakers (name, description, cost, discount, photo, gender, bootSize, categoryid) INSERT INTO sneakers (name, description, cost, count, 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); 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) INSERT INTO sneakers (name, description, cost, count, 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); 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) INSERT INTO sneakers (name, description, cost, count, discount, photo, gender, bootSize, categoryid)
VALUES ('Puma', 'fast too', 80.12, 80, 'https://www.xxl.se/filespin/63b77c174b30493397b4c328300252ef', 'U', 41, 1); 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);

View File

@ -171,5 +171,14 @@ fun Application.configureRouting() {
} }
} }
} }
post("/buySneakers") {
val request = call.receive<BuySneakersRequest>()
if (AuthService.buySneakers(request)){
call.respond(mapOf("message" to "Куплено"))
} else{
call.respond(mapOf("error" to "Ну вот не куплено"))
}
}
} }
} }

View File

@ -2,6 +2,7 @@ package com.example
import at.favre.lib.crypto.bcrypt.BCrypt import at.favre.lib.crypto.bcrypt.BCrypt
import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.minus
import org.jetbrains.exposed.sql.transactions.transaction import org.jetbrains.exposed.sql.transactions.transaction
import java.net.InetAddress import java.net.InetAddress
import java.util.Random import java.util.Random
@ -100,6 +101,7 @@ object AuthService {
name = it[Sneakers.name], name = it[Sneakers.name],
description = it[Sneakers.description], description = it[Sneakers.description],
cost = it[Sneakers.cost], cost = it[Sneakers.cost],
count = it[Sneakers.count],
discount = it[Sneakers.discount], discount = it[Sneakers.discount],
photo = it[Sneakers.photo], photo = it[Sneakers.photo],
gender = it[Sneakers.gender], gender = it[Sneakers.gender],
@ -129,6 +131,28 @@ object AuthService {
throw RuntimeException("Error selecting categories: ${e.message}", e) 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
}
}
} }

View File

@ -16,6 +16,12 @@ data class UserForgotPasswordRequest(val email: String)
@Serializable @Serializable
data class UserResetPasswordRequest(val code: String, val newPassword: String) 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<BuySneakerRequest>)
@Serializable @Serializable
data class Sneaker( data class Sneaker(
@ -23,6 +29,7 @@ data class Sneaker(
val name: String, val name: String,
val description: String, val description: String,
@Contextual val cost: BigDecimal, @Contextual val cost: BigDecimal,
val count: Int,
val discount: Int, val discount: Int,
val photo: String, val photo: String,
val gender: Char, val gender: Char,

View File

@ -23,6 +23,7 @@ object Sneakers : Table("sneakers") {
val name = varchar("name", 255) val name = varchar("name", 255)
val description = varchar("description", 255) val description = varchar("description", 255)
val cost = decimal("cost", 10, 2) val cost = decimal("cost", 10, 2)
val count = integer("count")
val discount = integer("discount") val discount = integer("discount")
val photo = varchar("photo", 255) val photo = varchar("photo", 255)
val gender = char("gender") val gender = char("gender")