Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Created March 5, 2020 02:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dcomartin/aa90fe3d7333d92ca9fd4c5bf005331e to your computer and use it in GitHub Desktop.
Save dcomartin/aa90fe3d7333d92ca9fd4c5bf005331e to your computer and use it in GitHub Desktop.
using System;
using MediatR;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.eShopWeb.Infrastructure.Data;
namespace Microsoft.eShopWeb.Web.Features.MyOrders
{
public class MyOrdersViewModel
{
public IEnumerable<OrderSummaryViewModel> Orders { get; set; }
}
public class OrderSummaryViewModel
{
private const string DEFAULT_STATUS = "Pending";
public int OrderNumber { get; set; }
public DateTimeOffset OrderDate { get; set; }
public decimal Total { get; set; }
public string Status => DEFAULT_STATUS;
}
public class GetMyOrdersHandler : IRequestHandler<GetMyOrders, MyOrdersViewModel>
{
private readonly CatalogContext _db;
public GetMyOrdersHandler(CatalogContext db)
{
_db = db;
}
public async Task<MyOrdersViewModel> Handle(GetMyOrders request, CancellationToken cancellationToken)
{
var result = new MyOrdersViewModel();
result.Orders = await _db.Orders
.Include(x => x.OrderItems)
.Where(x => x.BuyerId == request.UserName)
.Select(o => new OrderSummaryViewModel
{
OrderDate = o.OrderDate,
OrderNumber = o.Id,
Total = o.OrderItems.Sum(x => x.Units * x.UnitPrice),
})
.ToArrayAsync(cancellationToken: cancellationToken);
return result;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment