WebVenta/WebVentaCoche/Controllers/SalesController.cs

48 lines
1.4 KiB
C#

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using WebVentaCoche.DataBase;
using WebVentaCoche.Enums;
using WebVentaCoche.ViewModels;
namespace WebVentaCoche.Controllers
{
[Authorize(Roles = "Administrador")]
public class SalesController : Controller
{
private readonly ApplicationDbContext _context;
public SalesController(ApplicationDbContext context)
{
_context = context;
}
//GET:/Sales/
public IActionResult Index()
{
return View();
}
//GET:/Sales/GetMonthlyData
[HttpGet]
public async Task<IActionResult> GetMonthlyData()
{
var query =
from d in _context.OrderDetails
where d.Order.Status == OrderStatus.Delivered
let m = d.Order.OrderDate
group d by new { d.Product.Name, Year = m.Year, Month = m.Month } into g
orderby g.Key.Name, g.Key.Year, g.Key.Month
select new SalesViewModel
{
ProductName = g.Key.Name!,
Month = $"{g.Key.Year}-{g.Key.Month:D2}",
Units = g.Sum(x => x.Quantity)
};
var data = await query.ToListAsync();
return Json(data);
}
}
}