public async Task <IActionResult> Post([FromBody] Orders body) { await Db.Connection.OpenAsync(); body.Db = Db; var itemQuery = new ItemsQuery(Db); for (int i = 0; i < body.OrdersD.Count; i++) { var itemId = body.OrdersD[i].IdItem; var findItemID = itemQuery.FindOneAsync(itemId); if (findItemID.Result == null) { FoundExceptions.NotFoundException(itemId); } } var orderId = await body.InsertOrdersAsync(); await body.InsertOrderDetailAsync(body); var orderQuery = new OrdersQuery(Db); var result = await orderQuery.FindOneAsync(orderId); return(new OkObjectResult(result[0])); }
public async Task UpdateAsync(Orders body, List <OrdersDetail> result) { for (int i = 0; i < result.Count; i++) { if (body.OrdersD.Exists(x => x.IdItem == result[i].IdItem)) { //Update var bodyMatch = body.OrdersD.Find(x => x.IdItem == result[i].IdItem); using var cmd_d = Db.Connection.CreateCommand(); cmd_d.CommandText = @"UPDATE `orders_detail` SET `Quantity` = @quantity WHERE `id_order` = @id AND `id_item` = @id_item;"; BindParamsOrdersDetails(cmd_d, bodyMatch); BindId(cmd_d); await cmd_d.ExecuteNonQueryAsync(); } else { //Delete using var cmd = Db.Connection.CreateCommand(); cmd.CommandText = @"DELETE FROM `orders_detail` WHERE `orders_detail`.id_order = @id AND `id_item` = @id_item;"; BindParamsOrdersDetails(cmd, result[i]); BindId(cmd); await cmd.ExecuteNonQueryAsync(); } } var itemQuery = new ItemsQuery(Db); for (int i = 0; i < body.OrdersD.Count; i++) { var resultItem = await itemQuery.FindOneAsync(body.OrdersD[i].IdItem); if (resultItem == null) { FoundExceptions.NotFoundException(body.OrdersD[i].IdItem); } if (!result.Exists(x => x.IdItem == body.OrdersD[i].IdItem)) { //insert using var cmd_i = Db.Connection.CreateCommand(); cmd_i.CommandText = @"INSERT INTO `orders_detail` (`id_order`, `id_item`,`Quantity`) VALUES (@id_order, @id_item, @quantity);"; BindParamsOrdersDetails(cmd_i, body.OrdersD[i]); await cmd_i.ExecuteNonQueryAsync(); } } }