Skip to content

Commit

Permalink
compare query and loop
Browse files Browse the repository at this point in the history
  • Loading branch information
toniasep committed Mar 18, 2024
1 parent 31ae8f5 commit 771ca82
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 8 deletions.
3 changes: 3 additions & 0 deletions Controllers/Requests/OrderListRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@ public class OrderListRequest

[FromQuery(Name = "end_date")]
public DateTime? EndDate { get; set; }

[FromQuery(Name = "without_order_data")]
public bool WithoutOrderData { get; set; }
}
}
2 changes: 1 addition & 1 deletion Controllers/Responses/IncomeResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ public class IncomeResponse
{
public decimal TotalIncome { get; set; }
public int TotalSold { get; set; }
public List<OrderListResponse> Orders { get; set; }
public List<OrderListResponse>? Orders { get; set; } = null;
}
}
18 changes: 17 additions & 1 deletion Controllers/Views/OrderController.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Diagnostics;
using ECommerceDB.Controllers.Requests;
using ECommerceDB.Models.Services;
using Microsoft.AspNetCore.Mvc;
Expand All @@ -17,7 +18,22 @@ OrderService orderService
[HttpGet("/orders/income")]
public IActionResult IndexOrder([FromQuery] OrderListRequest request)
{
return Ok(_orderService.GetIncome(request));
Stopwatch stopwatch = Stopwatch.StartNew();
var result = _orderService.GetIncome(request);
stopwatch.Stop();
double executionTime = stopwatch.Elapsed.TotalSeconds;
return Ok(new {executionTime, result});

}

[HttpGet("/orders/income-with-loop")]
public IActionResult IndexOrderWithLoop([FromQuery] OrderListRequest request)
{
Stopwatch stopwatch = Stopwatch.StartNew();
var result = _orderService.GetIncomeLoop(request);
stopwatch.Stop();
double executionTime = stopwatch.Elapsed.TotalSeconds;
return Ok(new {executionTime, result});
}
}
}
72 changes: 66 additions & 6 deletions Models/Services/OrderService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,15 @@ public IncomeResponse GetIncome(OrderListRequest request)
{
data = data.Where(x => x.OrderDate <= request.EndDate).ToList();
}

var response = new IncomeResponse
{
Orders = data.Select(x => new OrderListResponse
TotalIncome = data.Sum(x => x.OrderItems.Sum(oi => oi.Quantity * oi.Product.Price)),
TotalSold = data.Sum(x => x.OrderItems.Sum(oi => oi.Quantity)),
Orders = null
};
if (!request.WithoutOrderData)
{
response.Orders = data.Select(x => new OrderListResponse
{
Id = x.Id,
OrderDate = x.OrderDate,
Expand All @@ -52,14 +57,69 @@ public IncomeResponse GetIncome(OrderListRequest request)
Product = oi.Product.Name,
Price = oi.Product.Price
}).ToList()
}).ToList(),
TotalIncome = data.Sum(x => x.OrderItems.Sum(oi => oi.Quantity * oi.Product.Price)),
TotalSold = data.Sum(x => x.OrderItems.Sum(oi => oi.Quantity))
};
}).ToList();
}

return response;
}
public IncomeResponse GetIncomeLoop(OrderListRequest request)
{
var data = _context.Orders
.Include(o => o.OrderItems)
.ThenInclude(oi => oi.Product)
.ToList();

if (request.StartDate != null && request.EndDate != null)
{
data = data.Where(x => x.OrderDate >= request.StartDate && x.OrderDate <= request.EndDate).ToList();
}

if (request.StartDate != null)
{
data = data.Where(x => x.OrderDate >= request.StartDate).ToList();
}

if (request.EndDate != null)
{
data = data.Where(x => x.OrderDate <= request.EndDate).ToList();
}

//loop
decimal totalIncome = 0;
var totalSold = 0;
var orders = new List<OrderListResponse>();
foreach (var order in data)
{
totalIncome += order.OrderItems.Sum(oi => oi.Quantity * oi.Product.Price);
totalSold += order.OrderItems.Sum(oi => oi.Quantity);
if (!request.WithoutOrderData)
{
orders.Add(new OrderListResponse
{
Id = order.Id,
OrderDate = order.OrderDate,
ShippingAddress = order.ShippingAddress,
PaymentMethod = order.PaymentMethod,
Status = order.Status,
OrderItems = order.OrderItems.Select(oi => new OrderItemResponse
{
Id = oi.Id,
Quantity = oi.Quantity,
Product = oi.Product.Name,
Price = oi.Product.Price
}).ToList()
});
}
}

var response = new IncomeResponse
{
TotalIncome = totalIncome,
TotalSold = totalSold,
Orders = orders
};

return response;
}
}
}
Binary file modified bin/Debug/net6.0/ECommerceDB.dll
Binary file not shown.
Binary file modified bin/Debug/net6.0/ECommerceDB.pdb
Binary file not shown.
Binary file modified obj/Debug/net6.0/ECommerceDB.dll
Binary file not shown.
Binary file modified obj/Debug/net6.0/ECommerceDB.pdb
Binary file not shown.
Binary file modified obj/Debug/net6.0/ref/ECommerceDB.dll
Binary file not shown.
Binary file modified obj/Debug/net6.0/refint/ECommerceDB.dll
Binary file not shown.
13 changes: 13 additions & 0 deletions obj/staticwebassets.pack.sentinel
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,16 @@
2.0
2.0
2.0
2.0
2.0
2.0
2.0
2.0
2.0
2.0
2.0
2.0
2.0
2.0
2.0
2.0

0 comments on commit 771ca82

Please sign in to comment.