forked from dotnet/eShop
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update OrderQueries to use linq queries (dotnet#115)
* Add public get methods for GetOrderDate and GetDescription * Make query more idiomatic As mentioned in issue dotnet#23 * Remove unused MapOrderItems * Make CardTypesQuery more idomatic * Make buyerId a auto property Make buyerId a auto property in oder to use buyerid inside linq queries. * GetOrdersFromUserAsync to linq Make GetOrdersFromUserAsync more idomatic and remove unused NpgsqlDataSource * - refactor: converted private fields to properties on Order and OrderItem - don't use repository any more * Add buyer navigation property * Introduce SetPaymentMethodVerified method on Order to better align with business operations * Convert GetTotal to expression body and remove unnecessary HasColumnName. * Merge enum Orderstatus changes * Remove explicit config and Add private setters so implicit configuration from ef core will work
- Loading branch information
1 parent
3b49f61
commit 6603215
Showing
12 changed files
with
99 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,53 @@ | ||
namespace eShop.Ordering.API.Application.Queries; | ||
|
||
public class OrderQueries(NpgsqlDataSource dataSource) | ||
public class OrderQueries(OrderingContext context) | ||
: IOrderQueries | ||
{ | ||
public async Task<Order> GetOrderAsync(int id) | ||
{ | ||
using var connection = dataSource.OpenConnection(); | ||
|
||
var result = await connection.QueryAsync<dynamic>(""" | ||
SELECT o."Id" AS ordernumber, o."OrderDate" AS date, o."Description" AS description, o."Address_City" AS city, | ||
o."Address_Country" AS country, o."Address_State" AS state, o."Address_Street" AS street, | ||
o."Address_ZipCode" AS zipcode, o."OrderStatus" AS status, oi."ProductName" AS productname, oi."Units" AS units, | ||
oi."UnitPrice" AS unitprice, oi."PictureUrl" AS pictureurl | ||
FROM ordering.Orders AS o | ||
LEFT JOIN ordering."orderItems" AS oi ON o."Id" = oi."OrderId" | ||
WHERE o."Id" = @id | ||
""", | ||
new { id }); | ||
|
||
if (result.AsList().Count == 0) | ||
var order = await context.Orders | ||
.Include(o => o.OrderItems) | ||
.FirstOrDefaultAsync(o => o.Id == id); | ||
|
||
if (order is null) | ||
throw new KeyNotFoundException(); | ||
|
||
return MapOrderItems(result); | ||
return new Order | ||
{ | ||
ordernumber = order.Id, | ||
date = order.OrderDate, | ||
description = order.Description, | ||
city = order.Address.City, | ||
country = order.Address.Country, | ||
state = order.Address.State, | ||
street = order.Address.Street, | ||
zipcode = order.Address.ZipCode, | ||
status = order.OrderStatus.ToString(), | ||
total = order.GetTotal(), | ||
orderitems = order.OrderItems.Select(oi => new Orderitem | ||
{ | ||
productname = oi.ProductName, | ||
units = oi.Units, | ||
unitprice = (double)oi.UnitPrice, | ||
pictureurl = oi.PictureUrl | ||
}).ToList() | ||
}; | ||
} | ||
|
||
public async Task<IEnumerable<OrderSummary>> GetOrdersFromUserAsync(string userId) | ||
{ | ||
using var connection = dataSource.OpenConnection(); | ||
|
||
return await connection.QueryAsync<OrderSummary>(""" | ||
SELECT o."Id" AS ordernumber, o."OrderDate" AS date, o."OrderStatus" AS status, SUM(oi."Units" * oi."UnitPrice") AS total | ||
FROM ordering.orders AS o | ||
LEFT JOIN ordering."orderItems" AS oi ON o."Id" = oi."OrderId" | ||
LEFT JOIN ordering.buyers AS ob ON o."BuyerId" = ob."Id" | ||
WHERE ob."IdentityGuid" = @userId | ||
GROUP BY o."Id", o."OrderDate", o."OrderStatus" | ||
ORDER BY o."Id" | ||
""", | ||
new { userId }); | ||
} | ||
|
||
public async Task<IEnumerable<CardType>> GetCardTypesAsync() | ||
{ | ||
using var connection = dataSource.OpenConnection(); | ||
|
||
return await connection.QueryAsync<CardType>("SELECT * FROM ordering.cardtypes"); | ||
} | ||
|
||
private Order MapOrderItems(dynamic result) | ||
{ | ||
var order = new Order | ||
{ | ||
ordernumber = result[0].ordernumber, | ||
date = result[0].date, | ||
status = result[0].status, | ||
description = result[0].description, | ||
street = result[0].street, | ||
city = result[0].city, | ||
state = result[0].state, | ||
zipcode = result[0].zipcode, | ||
country = result[0].country, | ||
orderitems = new List<Orderitem>(), | ||
total = 0 | ||
}; | ||
|
||
foreach (dynamic item in result) | ||
{ | ||
var orderitem = new Orderitem | ||
return await context.Orders | ||
.Where(o => o.Buyer.IdentityGuid == userId) | ||
.Select(o => new OrderSummary | ||
{ | ||
productname = item.productname, | ||
units = item.units, | ||
unitprice = (double)item.unitprice, | ||
pictureurl = item.pictureurl | ||
}; | ||
|
||
order.total += item.units * item.unitprice; | ||
order.orderitems.Add(orderitem); | ||
} | ||
|
||
return order; | ||
} | ||
ordernumber = o.Id, | ||
date = o.OrderDate, | ||
status = o.OrderStatus.ToString(), | ||
total =(double) o.OrderItems.Sum(oi => oi.UnitPrice* oi.Units) | ||
}) | ||
.ToListAsync(); | ||
} | ||
|
||
public async Task<IEnumerable<CardType>> GetCardTypesAsync() => | ||
await context.CardTypes.Select(c=> new CardType { Id = c.Id, Name = c.Name }).ToListAsync(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.