From 247fe61d9ec055c51fe20cc861ebe82fd8ec5c25 Mon Sep 17 00:00:00 2001 From: Danya_Mochalov Date: Fri, 22 Dec 2023 01:47:27 +0400 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D1=82=D0=BE=D0=B2=D0=B0=D1=80=D0=B0=20?= =?UTF-8?q?=D0=B2=20=D0=BA=D0=BE=D1=80=D0=B7=D0=B8=D0=BD=D1=83=20=D1=81=20?= =?UTF-8?q?=D0=B3=D0=BB=D0=B0=D0=B2=D0=BD=D0=BE=D0=B3=D0=BE=20=D1=8D=D0=BA?= =?UTF-8?q?=D1=80=D0=B0=D0=BD=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../1.json | 208 ++++++++++++++++++ .../2.json | 208 ++++++++++++++++++ .../3.json | 196 +++++++++++++++++ .../java/com/example/shawarma/AppModule.kt | 22 +- .../shawarma/data/api/MyServerService.kt | 29 +++ .../data/api/models/OrderModelRemote.kt | 42 ++++ .../data/api/models/ProductModelRemote.kt | 1 + .../api/repos/RestOrderProductRepository.kt | 17 ++ .../data/api/repos/RestOrderRepository.kt | 15 +- .../data/api/repos/RestUserRepository.kt | 4 + .../example/shawarma/data/db/AppDatabase.kt | 6 +- .../shawarma/data/models/OrderModel.kt | 13 +- .../data/repos/OrderProductRepository.kt | 15 +- .../shawarma/data/repos/OrderRepository.kt | 37 +++- .../shawarma/screens/home/HomeScreen.kt | 2 +- .../shawarma/viewmodels/CartViewModel.kt | 62 +++--- .../shawarma/viewmodels/HomeViewModel.kt | 16 +- 17 files changed, 825 insertions(+), 68 deletions(-) create mode 100644 app/schemas/com.example.shawarma.data.db.AppDatabase/1.json create mode 100644 app/schemas/com.example.shawarma.data.db.AppDatabase/2.json create mode 100644 app/schemas/com.example.shawarma.data.db.AppDatabase/3.json create mode 100644 app/src/main/java/com/example/shawarma/data/api/models/OrderModelRemote.kt create mode 100644 app/src/main/java/com/example/shawarma/data/api/repos/RestOrderProductRepository.kt diff --git a/app/schemas/com.example.shawarma.data.db.AppDatabase/1.json b/app/schemas/com.example.shawarma.data.db.AppDatabase/1.json new file mode 100644 index 0000000..26e30ce --- /dev/null +++ b/app/schemas/com.example.shawarma.data.db.AppDatabase/1.json @@ -0,0 +1,208 @@ +{ + "formatVersion": 1, + "database": { + "version": 1, + "identityHash": "fd664bd9d0a27511fb20675444548276", + "entities": [ + { + "tableName": "users", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `user_login` TEXT NOT NULL, `user_password` TEXT NOT NULL, `user_role` TEXT NOT NULL)", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "INTEGER", + "notNull": false + }, + { + "fieldPath": "login", + "columnName": "user_login", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "password", + "columnName": "user_password", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "role", + "columnName": "user_role", + "affinity": "TEXT", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": true, + "columnNames": [ + "id" + ] + }, + "indices": [], + "foreignKeys": [] + }, + { + "tableName": "orders", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `order_status` TEXT NOT NULL, `user_id` INTEGER, `date` INTEGER NOT NULL, FOREIGN KEY(`user_id`) REFERENCES `users`(`id`) ON UPDATE NO ACTION ON DELETE NO ACTION )", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "INTEGER", + "notNull": false + }, + { + "fieldPath": "status", + "columnName": "order_status", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "userId", + "columnName": "user_id", + "affinity": "INTEGER", + "notNull": false + }, + { + "fieldPath": "date", + "columnName": "date", + "affinity": "INTEGER", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": true, + "columnNames": [ + "id" + ] + }, + "indices": [ + { + "name": "index_orders_user_id", + "unique": false, + "columnNames": [ + "user_id" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_orders_user_id` ON `${TABLE_NAME}` (`user_id`)" + } + ], + "foreignKeys": [ + { + "table": "users", + "onDelete": "NO ACTION", + "onUpdate": "NO ACTION", + "columns": [ + "user_id" + ], + "referencedColumns": [ + "id" + ] + } + ] + }, + { + "tableName": "products", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `product_title` TEXT NOT NULL, `product_price` INTEGER NOT NULL, `product_old_price` INTEGER)", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "INTEGER", + "notNull": false + }, + { + "fieldPath": "title", + "columnName": "product_title", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "price", + "columnName": "product_price", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "oldPrice", + "columnName": "product_old_price", + "affinity": "INTEGER", + "notNull": false + } + ], + "primaryKey": { + "autoGenerate": true, + "columnNames": [ + "id" + ] + }, + "indices": [], + "foreignKeys": [] + }, + { + "tableName": "order_product", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `quantity` INTEGER NOT NULL, `total_price` INTEGER NOT NULL, PRIMARY KEY(`order_id`, `product_id`))", + "fields": [ + { + "fieldPath": "orderId", + "columnName": "order_id", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "productId", + "columnName": "product_id", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "quantity", + "columnName": "quantity", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "totalPrice", + "columnName": "total_price", + "affinity": "INTEGER", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "order_id", + "product_id" + ] + }, + "indices": [ + { + "name": "index_order_product_order_id", + "unique": false, + "columnNames": [ + "order_id" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_order_product_order_id` ON `${TABLE_NAME}` (`order_id`)" + }, + { + "name": "index_order_product_product_id", + "unique": false, + "columnNames": [ + "product_id" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_order_product_product_id` ON `${TABLE_NAME}` (`product_id`)" + } + ], + "foreignKeys": [] + } + ], + "views": [], + "setupQueries": [ + "CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)", + "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'fd664bd9d0a27511fb20675444548276')" + ] + } +} \ No newline at end of file diff --git a/app/schemas/com.example.shawarma.data.db.AppDatabase/2.json b/app/schemas/com.example.shawarma.data.db.AppDatabase/2.json new file mode 100644 index 0000000..e7e3b7e --- /dev/null +++ b/app/schemas/com.example.shawarma.data.db.AppDatabase/2.json @@ -0,0 +1,208 @@ +{ + "formatVersion": 1, + "database": { + "version": 2, + "identityHash": "1ce8ad5b9b5decfebb42bc2f3c33393d", + "entities": [ + { + "tableName": "users", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `user_login` TEXT NOT NULL, `user_password` TEXT NOT NULL, `user_role` TEXT NOT NULL)", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "INTEGER", + "notNull": false + }, + { + "fieldPath": "login", + "columnName": "user_login", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "password", + "columnName": "user_password", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "role", + "columnName": "user_role", + "affinity": "TEXT", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": true, + "columnNames": [ + "id" + ] + }, + "indices": [], + "foreignKeys": [] + }, + { + "tableName": "orders", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `order_status` TEXT NOT NULL, `user_id` INTEGER, `date` TEXT NOT NULL, FOREIGN KEY(`user_id`) REFERENCES `users`(`id`) ON UPDATE NO ACTION ON DELETE NO ACTION )", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "INTEGER", + "notNull": false + }, + { + "fieldPath": "status", + "columnName": "order_status", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "userId", + "columnName": "user_id", + "affinity": "INTEGER", + "notNull": false + }, + { + "fieldPath": "date", + "columnName": "date", + "affinity": "TEXT", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": true, + "columnNames": [ + "id" + ] + }, + "indices": [ + { + "name": "index_orders_user_id", + "unique": false, + "columnNames": [ + "user_id" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_orders_user_id` ON `${TABLE_NAME}` (`user_id`)" + } + ], + "foreignKeys": [ + { + "table": "users", + "onDelete": "NO ACTION", + "onUpdate": "NO ACTION", + "columns": [ + "user_id" + ], + "referencedColumns": [ + "id" + ] + } + ] + }, + { + "tableName": "products", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `product_title` TEXT NOT NULL, `product_price` INTEGER NOT NULL, `product_old_price` INTEGER)", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "INTEGER", + "notNull": false + }, + { + "fieldPath": "title", + "columnName": "product_title", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "price", + "columnName": "product_price", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "oldPrice", + "columnName": "product_old_price", + "affinity": "INTEGER", + "notNull": false + } + ], + "primaryKey": { + "autoGenerate": true, + "columnNames": [ + "id" + ] + }, + "indices": [], + "foreignKeys": [] + }, + { + "tableName": "order_product", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `quantity` INTEGER NOT NULL, `total_price` INTEGER NOT NULL, PRIMARY KEY(`order_id`, `product_id`))", + "fields": [ + { + "fieldPath": "orderId", + "columnName": "order_id", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "productId", + "columnName": "product_id", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "quantity", + "columnName": "quantity", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "totalPrice", + "columnName": "total_price", + "affinity": "INTEGER", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "order_id", + "product_id" + ] + }, + "indices": [ + { + "name": "index_order_product_order_id", + "unique": false, + "columnNames": [ + "order_id" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_order_product_order_id` ON `${TABLE_NAME}` (`order_id`)" + }, + { + "name": "index_order_product_product_id", + "unique": false, + "columnNames": [ + "product_id" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_order_product_product_id` ON `${TABLE_NAME}` (`product_id`)" + } + ], + "foreignKeys": [] + } + ], + "views": [], + "setupQueries": [ + "CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)", + "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '1ce8ad5b9b5decfebb42bc2f3c33393d')" + ] + } +} \ No newline at end of file diff --git a/app/schemas/com.example.shawarma.data.db.AppDatabase/3.json b/app/schemas/com.example.shawarma.data.db.AppDatabase/3.json new file mode 100644 index 0000000..2dd974b --- /dev/null +++ b/app/schemas/com.example.shawarma.data.db.AppDatabase/3.json @@ -0,0 +1,196 @@ +{ + "formatVersion": 1, + "database": { + "version": 3, + "identityHash": "12eb76256f54b2c513a3073a2e582c4c", + "entities": [ + { + "tableName": "users", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `user_login` TEXT NOT NULL, `user_password` TEXT NOT NULL, `user_role` TEXT NOT NULL)", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "INTEGER", + "notNull": false + }, + { + "fieldPath": "login", + "columnName": "user_login", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "password", + "columnName": "user_password", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "role", + "columnName": "user_role", + "affinity": "TEXT", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": true, + "columnNames": [ + "id" + ] + }, + "indices": [], + "foreignKeys": [] + }, + { + "tableName": "orders", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `order_status` TEXT NOT NULL, `user_id` INTEGER, `date` TEXT NOT NULL)", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "INTEGER", + "notNull": false + }, + { + "fieldPath": "status", + "columnName": "order_status", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "userId", + "columnName": "user_id", + "affinity": "INTEGER", + "notNull": false + }, + { + "fieldPath": "date", + "columnName": "date", + "affinity": "TEXT", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": true, + "columnNames": [ + "id" + ] + }, + "indices": [ + { + "name": "index_orders_user_id", + "unique": false, + "columnNames": [ + "user_id" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_orders_user_id` ON `${TABLE_NAME}` (`user_id`)" + } + ], + "foreignKeys": [] + }, + { + "tableName": "products", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `product_title` TEXT NOT NULL, `product_price` INTEGER NOT NULL, `product_old_price` INTEGER)", + "fields": [ + { + "fieldPath": "id", + "columnName": "id", + "affinity": "INTEGER", + "notNull": false + }, + { + "fieldPath": "title", + "columnName": "product_title", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "price", + "columnName": "product_price", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "oldPrice", + "columnName": "product_old_price", + "affinity": "INTEGER", + "notNull": false + } + ], + "primaryKey": { + "autoGenerate": true, + "columnNames": [ + "id" + ] + }, + "indices": [], + "foreignKeys": [] + }, + { + "tableName": "order_product", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`order_id` INTEGER NOT NULL, `product_id` INTEGER NOT NULL, `quantity` INTEGER NOT NULL, `total_price` INTEGER NOT NULL, PRIMARY KEY(`order_id`, `product_id`))", + "fields": [ + { + "fieldPath": "orderId", + "columnName": "order_id", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "productId", + "columnName": "product_id", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "quantity", + "columnName": "quantity", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "totalPrice", + "columnName": "total_price", + "affinity": "INTEGER", + "notNull": true + } + ], + "primaryKey": { + "autoGenerate": false, + "columnNames": [ + "order_id", + "product_id" + ] + }, + "indices": [ + { + "name": "index_order_product_order_id", + "unique": false, + "columnNames": [ + "order_id" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_order_product_order_id` ON `${TABLE_NAME}` (`order_id`)" + }, + { + "name": "index_order_product_product_id", + "unique": false, + "columnNames": [ + "product_id" + ], + "orders": [], + "createSql": "CREATE INDEX IF NOT EXISTS `index_order_product_product_id` ON `${TABLE_NAME}` (`product_id`)" + } + ], + "foreignKeys": [] + } + ], + "views": [], + "setupQueries": [ + "CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)", + "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '12eb76256f54b2c513a3073a2e582c4c')" + ] + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/shawarma/AppModule.kt b/app/src/main/java/com/example/shawarma/AppModule.kt index d2a2371..ca85657 100644 --- a/app/src/main/java/com/example/shawarma/AppModule.kt +++ b/app/src/main/java/com/example/shawarma/AppModule.kt @@ -3,6 +3,8 @@ package com.example.shawarma import android.app.Application import androidx.room.Room import com.example.shawarma.data.api.MyServerService +import com.example.shawarma.data.api.repos.RestOrderProductRepository +import com.example.shawarma.data.api.repos.RestOrderRepository import com.example.shawarma.data.api.repos.RestProductRepository import com.example.shawarma.data.api.repos.RestUserRepository import com.example.shawarma.data.db.AppDatabase @@ -61,14 +63,26 @@ object AppModule { @Provides @Singleton - fun provideOrderRepository(db: AppDatabase) : OrderRepository { - return OrderRepository(db.orderDao()) + fun provideOrderRepository(db: AppDatabase, restOrderRepository: RestOrderRepository) : OrderRepository { + return OrderRepository(db.orderDao(), db.productDao(), db.orderProductDao(), restOrderRepository) } @Provides @Singleton - fun provideOrderProductRepository(db: AppDatabase) : OrderProductRepository { - return OrderProductRepository(db.orderProductDao()) + fun provideRestOrderRepository(service: MyServerService) : RestOrderRepository { + return RestOrderRepository(service) + } + + @Provides + @Singleton + fun provideOrderProductRepository(db: AppDatabase, restOrderProductRepository: RestOrderProductRepository) : OrderProductRepository { + return OrderProductRepository(db.orderProductDao(), restOrderProductRepository) + } + + @Provides + @Singleton + fun provideRestOrderProductRepository(service: MyServerService) : RestOrderProductRepository { + return RestOrderProductRepository(service) } diff --git a/app/src/main/java/com/example/shawarma/data/api/MyServerService.kt b/app/src/main/java/com/example/shawarma/data/api/MyServerService.kt index 18bfdde..5813de9 100644 --- a/app/src/main/java/com/example/shawarma/data/api/MyServerService.kt +++ b/app/src/main/java/com/example/shawarma/data/api/MyServerService.kt @@ -1,5 +1,7 @@ package com.example.shawarma.data.api +import com.example.shawarma.data.api.models.OrderModelRemote +import com.example.shawarma.data.api.models.OrderProductRemote import com.example.shawarma.data.api.models.ProductListResponse import com.example.shawarma.data.api.models.ProductModelRemote import com.example.shawarma.data.api.models.TokenModelRemote @@ -19,6 +21,9 @@ import retrofit2.http.Path interface MyServerService { + // + // USER + // @POST("register") suspend fun createUser( @Body user: UserModelRemote, @@ -34,6 +39,13 @@ interface MyServerService { @Body user: UserModelRemote ) :UserModelRemote? + @GET("user") + suspend fun getUser(@Header("Authorization") token: String) : UserModelRemote? + + // + // PRODUCTS + // + @GET("user/products/{after}") suspend fun getProductsList(@Path("after") after: Int, @Header("Authorization") token: String) : ProductListResponse @@ -58,8 +70,25 @@ interface MyServerService { @DELETE("product/{id}") suspend fun deleteProduct(@Path("id") id: Int, @Header("Authorization") token: String) + // + // ORDERS + // + @GET("order/unpaid") + suspend fun getUnpaidOrder(@Header("Authorization") token: String) : OrderModelRemote + @POST("order") + suspend fun insertOrder(@Body order: OrderModelRemote, @Header("Authorization") token: String) : OrderModelRemote? + + // + // ORDER PRODUCTS + // + + @POST("order/product") + suspend fun insertOrderProduct(@Body orderProduct: OrderProductRemote, @Header("Authorization") token: String) + + @PUT("order/product") + suspend fun updateOrderProduct(@Body orderProduct: OrderProductRemote, @Header("Authorization") token: String) companion object { private const val BASE_URL = "https://10.0.2.2:80/api/" diff --git a/app/src/main/java/com/example/shawarma/data/api/models/OrderModelRemote.kt b/app/src/main/java/com/example/shawarma/data/api/models/OrderModelRemote.kt new file mode 100644 index 0000000..244ba5d --- /dev/null +++ b/app/src/main/java/com/example/shawarma/data/api/models/OrderModelRemote.kt @@ -0,0 +1,42 @@ +package com.example.shawarma.data.api.models + +import com.example.shawarma.data.models.OrderModel +import com.example.shawarma.data.models.OrderProductModel +import kotlinx.serialization.Serializable + +@Serializable +data class OrderModelRemote( + val id: Int? = null, + val status: String = "", + val user_id: Int? = -1, + val date: String = "", + val order_products: List = listOf() +) + +fun OrderModelRemote.toOrderModel() : OrderModel = OrderModel( + id, status, user_id, date = date +) + +fun OrderModel.toOrderModelRemote() : OrderModelRemote = OrderModelRemote( + id, status, userId, date.toString() +) + +@Serializable +data class OrderProductRemote( + val id: Int? = null, + val order_id: Int = -1, + val product_id: Int = -1, + val quantity: Int = 0, + val total_price: Int = 0, + + val order: OrderModelRemote? = null, + val product: ProductModelRemote? = null +) + +fun OrderProductRemote.toOrderProductModel() : OrderProductModel = OrderProductModel( + order_id, product_id, quantity, total_price +) + +fun OrderProductModel.toOrderProductRemote() : OrderProductRemote = OrderProductRemote( + order_id = orderId, product_id = productId, quantity = quantity, total_price = totalPrice +) diff --git a/app/src/main/java/com/example/shawarma/data/api/models/ProductModelRemote.kt b/app/src/main/java/com/example/shawarma/data/api/models/ProductModelRemote.kt index dea6b96..fdc0ee0 100644 --- a/app/src/main/java/com/example/shawarma/data/api/models/ProductModelRemote.kt +++ b/app/src/main/java/com/example/shawarma/data/api/models/ProductModelRemote.kt @@ -10,6 +10,7 @@ data class ProductModelRemote( val price: Int = 0, val old_price: Int? = null, + val order_products: List = listOf() ) @Serializable diff --git a/app/src/main/java/com/example/shawarma/data/api/repos/RestOrderProductRepository.kt b/app/src/main/java/com/example/shawarma/data/api/repos/RestOrderProductRepository.kt new file mode 100644 index 0000000..aa52688 --- /dev/null +++ b/app/src/main/java/com/example/shawarma/data/api/repos/RestOrderProductRepository.kt @@ -0,0 +1,17 @@ +package com.example.shawarma.data.api.repos + +import com.example.shawarma.data.api.MyServerService +import com.example.shawarma.data.api.models.OrderProductRemote +import javax.inject.Inject + +class RestOrderProductRepository @Inject constructor( + private val service: MyServerService +) { + suspend fun insertOrderProduct(token: String, orderProduct: OrderProductRemote) { + service.insertOrderProduct(orderProduct, token) + } + + suspend fun updateOrderProduct(token: String, orderProduct: OrderProductRemote) { + service.updateOrderProduct(orderProduct, token) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/shawarma/data/api/repos/RestOrderRepository.kt b/app/src/main/java/com/example/shawarma/data/api/repos/RestOrderRepository.kt index 6022aa8..b630b29 100644 --- a/app/src/main/java/com/example/shawarma/data/api/repos/RestOrderRepository.kt +++ b/app/src/main/java/com/example/shawarma/data/api/repos/RestOrderRepository.kt @@ -1,4 +1,17 @@ package com.example.shawarma.data.api.repos -class RestOrderRepository { +import com.example.shawarma.data.api.MyServerService +import com.example.shawarma.data.api.models.OrderModelRemote +import javax.inject.Inject + +class RestOrderRepository@Inject constructor( + private val service: MyServerService +) { + suspend fun getUnpaidOrder(token:String) : OrderModelRemote { + return service.getUnpaidOrder(token) + } + + suspend fun insertOrder(token: String, order: OrderModelRemote): OrderModelRemote? { + return service.insertOrder(order, token) + } } \ No newline at end of file diff --git a/app/src/main/java/com/example/shawarma/data/api/repos/RestUserRepository.kt b/app/src/main/java/com/example/shawarma/data/api/repos/RestUserRepository.kt index 1bae05f..27bbfde 100644 --- a/app/src/main/java/com/example/shawarma/data/api/repos/RestUserRepository.kt +++ b/app/src/main/java/com/example/shawarma/data/api/repos/RestUserRepository.kt @@ -21,4 +21,8 @@ class RestUserRepository @Inject constructor( suspend fun checkLogin(user: UserModel): UserModelRemote? { return service.checkLogin(user.toUserModelRemote()) } + + suspend fun getUser(token: String) : UserModelRemote? { + return service.getUser(token) + } } \ No newline at end of file diff --git a/app/src/main/java/com/example/shawarma/data/db/AppDatabase.kt b/app/src/main/java/com/example/shawarma/data/db/AppDatabase.kt index 6ba312f..dfa97d7 100644 --- a/app/src/main/java/com/example/shawarma/data/db/AppDatabase.kt +++ b/app/src/main/java/com/example/shawarma/data/db/AppDatabase.kt @@ -1,6 +1,7 @@ package com.example.shawarma.data.db import android.content.Context +import androidx.room.AutoMigration import androidx.room.Database import androidx.room.Room import androidx.room.RoomDatabase @@ -26,8 +27,9 @@ import kotlinx.coroutines.launch ProductModel::class, OrderProductModel::class, ], - version = 1, - exportSchema = false + version = 3, + autoMigrations = [AutoMigration (from = 1, to = 2), AutoMigration (from = 2, to = 3)], + exportSchema = true ) @TypeConverters(Converter::class) abstract class AppDatabase : RoomDatabase() { diff --git a/app/src/main/java/com/example/shawarma/data/models/OrderModel.kt b/app/src/main/java/com/example/shawarma/data/models/OrderModel.kt index 44c231c..7e1e2a8 100644 --- a/app/src/main/java/com/example/shawarma/data/models/OrderModel.kt +++ b/app/src/main/java/com/example/shawarma/data/models/OrderModel.kt @@ -2,19 +2,10 @@ package com.example.shawarma.data.models import androidx.room.ColumnInfo import androidx.room.Entity -import androidx.room.ForeignKey import androidx.room.PrimaryKey -import java.util.Date @Entity( - tableName = "orders", - foreignKeys = [ - ForeignKey( - entity = UserModel::class, - parentColumns = ["id"], - childColumns = ["user_id"], - ) - ] + tableName = "orders" ) data class OrderModel( @PrimaryKey(autoGenerate = true) @@ -25,7 +16,7 @@ data class OrderModel( @ColumnInfo(name = "user_id", index = true) val userId: Int?, @ColumnInfo(name = "date") - val date: Date + val date: String ) fun getOrdersByUserId() : List { diff --git a/app/src/main/java/com/example/shawarma/data/repos/OrderProductRepository.kt b/app/src/main/java/com/example/shawarma/data/repos/OrderProductRepository.kt index 251db2e..f19e3be 100644 --- a/app/src/main/java/com/example/shawarma/data/repos/OrderProductRepository.kt +++ b/app/src/main/java/com/example/shawarma/data/repos/OrderProductRepository.kt @@ -1,17 +1,22 @@ package com.example.shawarma.data.repos +import com.example.shawarma.data.api.models.toOrderProductRemote +import com.example.shawarma.data.api.repos.RestOrderProductRepository import com.example.shawarma.data.interfaces.dao.OrderProductDao import com.example.shawarma.data.models.OrderProductModel import javax.inject.Inject class OrderProductRepository @Inject constructor( - private val orderProductDao: OrderProductDao + private val orderProductDao: OrderProductDao, + private val restOrderProductRepository: RestOrderProductRepository ) { - suspend fun insert(order: OrderProductModel) { - return orderProductDao.insert(order) + suspend fun insert(token: String, orderProduct: OrderProductModel) { + orderProductDao.insert(orderProduct) + return restOrderProductRepository.insertOrderProduct(token, orderProduct.toOrderProductRemote()) } - suspend fun update(order: OrderProductModel) { - return orderProductDao.update(order) + suspend fun update(token: String, orderProduct: OrderProductModel) { + orderProductDao.update(orderProduct) + return restOrderProductRepository.updateOrderProduct(token, orderProduct.toOrderProductRemote()) } suspend fun delete(order: OrderProductModel) { return orderProductDao.delete(order) diff --git a/app/src/main/java/com/example/shawarma/data/repos/OrderRepository.kt b/app/src/main/java/com/example/shawarma/data/repos/OrderRepository.kt index 6c181fb..899e550 100644 --- a/app/src/main/java/com/example/shawarma/data/repos/OrderRepository.kt +++ b/app/src/main/java/com/example/shawarma/data/repos/OrderRepository.kt @@ -1,16 +1,31 @@ package com.example.shawarma.data.repos +import com.example.shawarma.data.api.models.toOrderModel +import com.example.shawarma.data.api.models.toOrderModelRemote +import com.example.shawarma.data.api.models.toOrderProductModel +import com.example.shawarma.data.api.models.toProductModel +import com.example.shawarma.data.api.repos.RestOrderRepository import com.example.shawarma.data.interfaces.dao.OrderDao +import com.example.shawarma.data.interfaces.dao.OrderProductDao +import com.example.shawarma.data.interfaces.dao.ProductDao import com.example.shawarma.data.models.OrderModel import com.example.shawarma.data.models.OrderWithProducts import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.first import javax.inject.Inject class OrderRepository @Inject constructor( - private val orderDao: OrderDao + private val orderDao: OrderDao, + private val productDao: ProductDao, + private val orderProductDao: OrderProductDao, + private val restRepository: RestOrderRepository ){ - suspend fun insert(order: OrderModel): Long { - return orderDao.insert(order) + suspend fun insert(token: String, order: OrderModel): OrderModel? { + val result = restRepository.insertOrder(token, order.toOrderModelRemote()) + if (result == null) { + return result + } + return result.toOrderModel() } suspend fun update(order:OrderModel) { return orderDao.update(order) @@ -27,8 +42,20 @@ class OrderRepository @Inject constructor( fun getByUserId(userId: Int): Flow> { return orderDao.getByUserId(userId) } - fun getUnpaidByUser(userId: Int) : Flow { - return orderDao.getUnpaidByUser(userId) + suspend fun getUnpaidByUser(token: String) : OrderWithProducts? { + val order = restRepository.getUnpaidOrder(token) + if (orderDao.getUnpaidByUser(order.user_id!!).first() != null) { + return orderDao.getUnpaidByUser(order.user_id!!).first() + } + if (order!!.id != null) { + orderDao.insert(order.toOrderModel()) + for (product in order.order_products) { + orderProductDao.insert(product.toOrderProductModel()) + productDao.insert(product.product!!.toProductModel()) + } + return orderDao.getUnpaidByUser(order.user_id!!).first() + } + return null } fun getPaidByUser(userId: Int) : Flow> { return orderDao.getPaidByUser(userId) diff --git a/app/src/main/java/com/example/shawarma/screens/home/HomeScreen.kt b/app/src/main/java/com/example/shawarma/screens/home/HomeScreen.kt index db8391d..1818157 100644 --- a/app/src/main/java/com/example/shawarma/screens/home/HomeScreen.kt +++ b/app/src/main/java/com/example/shawarma/screens/home/HomeScreen.kt @@ -167,7 +167,7 @@ fun ProductCard(product: ProductModel){ ) Button( onClick = { - product.id?.let { homeViewModel.addProductToCart(it, preferencesManager.getData("user_id", "null")) } + product.id?.let { homeViewModel.addProductToCart(it, preferencesManager.getData("token", "null")) } }, colors = ButtonDefaults.buttonColors( backgroundColor = MyLightYellow, diff --git a/app/src/main/java/com/example/shawarma/viewmodels/CartViewModel.kt b/app/src/main/java/com/example/shawarma/viewmodels/CartViewModel.kt index 261b1a2..31ed735 100644 --- a/app/src/main/java/com/example/shawarma/viewmodels/CartViewModel.kt +++ b/app/src/main/java/com/example/shawarma/viewmodels/CartViewModel.kt @@ -25,17 +25,17 @@ class CartViewModel @Inject constructor( get() = _paidOrders fun getOrders(userId: String) { - if (userId == "null") return - viewModelScope.launch { - orderRepository.getUnpaidByUser(userId.toInt()).collect { - _unpaidOrder.postValue(it) - } - } - viewModelScope.launch { - orderRepository.getPaidByUser(userId.toInt()).collect { - _paidOrders.postValue(it) - } - } +// if (userId == "null") return +// viewModelScope.launch { +// orderRepository.getUnpaidByUser(userId.toInt()).collect { +// _unpaidOrder.postValue(it) +// } +// } +// viewModelScope.launch { +// orderRepository.getPaidByUser(userId.toInt()).collect { +// _paidOrders.postValue(it) +// } +// } } fun payForOrder(order: OrderWithProducts) { val model = order.order @@ -46,28 +46,28 @@ class CartViewModel @Inject constructor( } fun removeProductFromOrder(order: OrderWithProducts, productId: Int) { - val model = order.orderWithProducts[productId].orderProductModel - if(model.quantity == 1) { - // delete - viewModelScope.launch { - orderProductRepository.delete(model) - } - } - else{ - // update - model.quantity -= 1 - viewModelScope.launch { - orderProductRepository.update(model) - } - } +// val model = order.orderWithProducts[productId].orderProductModel +// if(model.quantity == 1) { +// // delete +// viewModelScope.launch { +// orderProductRepository.delete(model) +// } +// } +// else{ +// // update +// model.quantity -= 1 +// viewModelScope.launch { +// orderProductRepository.update(model) +// } +// } } fun addProductToOrder(order: OrderWithProducts, productId: Int) { - val model = order.orderWithProducts[productId].orderProductModel - // update - model.quantity += 1 - viewModelScope.launch { - orderProductRepository.update(model) - } +// val model = order.orderWithProducts[productId].orderProductModel +// // update +// model.quantity += 1 +// viewModelScope.launch { +// orderProductRepository.update(model) +// } } } \ No newline at end of file diff --git a/app/src/main/java/com/example/shawarma/viewmodels/HomeViewModel.kt b/app/src/main/java/com/example/shawarma/viewmodels/HomeViewModel.kt index d666e1e..e9564e3 100644 --- a/app/src/main/java/com/example/shawarma/viewmodels/HomeViewModel.kt +++ b/app/src/main/java/com/example/shawarma/viewmodels/HomeViewModel.kt @@ -34,18 +34,18 @@ class HomeViewModel @Inject constructor( } - fun addProductToCart(productId: Int, userId: String) { - if (userId == "null") return - + fun addProductToCart(productId: Int, token: String) { viewModelScope.launch { val product = productRepository.getById(productId).first() - val order = orderRepository.getUnpaidByUser(userId.toInt()).first() + val order = orderRepository.getUnpaidByUser(token) if (order == null) { val calendar: Calendar = Calendar.getInstance() calendar.time = Date() calendar.add(Calendar.HOUR_OF_DAY, 4) - val newOrderId = orderRepository.insert(OrderModel(null, OrderStatus.Неоплачено.name, userId.toInt(),calendar.time)) - orderProductRepository.insert(OrderProductModel(newOrderId.toInt(), productId, 1, product.price)) + val newOrder = orderRepository.insert(token, OrderModel(null, OrderStatus.Неоплачено.name, null, calendar.time.toString())) + if (newOrder != null) { + orderProductRepository.insert(token, OrderProductModel(newOrder.id!!, productId, 1, product.price)) + } } else { var isAlreadyAdded = false @@ -54,12 +54,12 @@ class HomeViewModel @Inject constructor( val model = prod.orderProductModel model.quantity += 1 model.totalPrice += prod.product.price - orderProductRepository.update(model) + orderProductRepository.update(token, model) isAlreadyAdded = true } } if (!isAlreadyAdded) { - orderProductRepository.insert(OrderProductModel(order.order.id!!, productId, 1, product.price)) + orderProductRepository.insert(token, OrderProductModel(order.order.id!!, productId, 1, product.price)) } } }