@@ -59,11 +59,24 @@ class OrderServiceImpl : OrderService {
5959 itemId
6060 }
6161
62- override suspend fun updateItemInOrder (itemId : Int , quantity : Int ): Result <ItemDto > = dbQuery {
62+ override suspend fun updateItemInOrder (itemId : Int , quantity : Int ): Result <Boolean > = dbQuery {
6363 val item = Items .select { Items .id eq itemId }.singleOrNull() ? : throw Exception (" Item not found" )
6464 val food = Foods .select { Foods .id eq item[Items .foodId] }.singleOrNull() ? : throw Exception (" Food not found" )
65-
65+ val order =
66+ Orders .select { Orders .id eq item[Items .orderId] }.singleOrNull() ? : throw Exception (" Order not found" )
6667 val newQuantity = item[Items .quantity] + quantity
68+
69+ if (newQuantity < 0 ) {
70+ Items .deleteWhere { Items .id eq itemId }
71+
72+ val newPrice = order[Orders .totalPrice] - (food[Foods .price] * item[Items .quantity]).toBigDecimal()
73+ Orders .update({ Orders .id eq item[Items .orderId] }) {
74+ it[Orders .totalPrice] = newPrice
75+ }
76+
77+ return @dbQuery true
78+ }
79+
6780 val newPrice = food[Foods .price] * newQuantity
6881 Items .update({ Items .id eq itemId }) {
6982 it[Items .quantity] = newQuantity
@@ -73,17 +86,31 @@ class OrderServiceImpl : OrderService {
7386 it[Orders .totalPrice] = newPrice.toBigDecimal()
7487 }
7588
76- ItemDto (
77- id = item[Items .id],
78- orderId = item[Items .orderId],
79- foodId = item[Items .foodId],
80- quantity = newQuantity,
81- price = food[Foods .price]
82- )
89+ true
90+ }
91+
92+ override suspend fun deleteCurrentOrder (userId : UUID ): Result <Boolean > = dbQuery {
93+ val order = Orders .select {
94+ (Orders .userId eq userId) and (Orders .orderStatus eq OrderStatus .Started )
95+ }.singleOrNull() ? : throw Exception (" No active order found" )
96+
97+ Items .deleteWhere { Items .orderId eq order[Orders .id] }
98+ Orders .update({ Orders .id eq order[Orders .id] }) {
99+ it[orderStatus] = OrderStatus .Cancelled
100+ }
101+
102+ true
83103 }
84104
85- override suspend fun clearCurrentOrder (userId : UUID ): Result <Boolean > {
86- TODO (" Not yet implemented" )
105+ override suspend fun deleteOrder (userId : UUID , orderId : Int ): Result <Boolean > = dbQuery {
106+ val order = Orders .select {
107+ (Orders .userId eq userId) and (Orders .id eq orderId)
108+ }.singleOrNull() ? : throw Exception (" Order not found" )
109+
110+ Items .deleteWhere { Items .orderId eq order[Orders .id] }
111+ Orders .deleteWhere { Orders .id eq order[Orders .id] }
112+
113+ true
87114 }
88115
89116 private fun createOrder (userId : UUID ) {
0 commit comments