60 lines
1.4 KiB
C#
60 lines
1.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Web.Mvc;
|
|
using AutoMapper;
|
|
using AutoMapper.QueryableExtensions;
|
|
using InventoryTraker.Web.ActionResults;
|
|
using InventoryTraker.Web.Core;
|
|
using InventoryTraker.Web.Data;
|
|
using InventoryTraker.Web.Models;
|
|
|
|
namespace InventoryTraker.Web.Controllers
|
|
{
|
|
public class TransactionController : ControllerBase
|
|
{
|
|
private readonly AppDbContext _context;
|
|
|
|
public TransactionController(AppDbContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
public ActionResult Index()
|
|
{
|
|
return View();
|
|
}
|
|
|
|
public JsonResult All()
|
|
{
|
|
var viewModels =
|
|
_context.Transactions
|
|
.ProjectTo<TransactionViewModel>()
|
|
.ToArray();
|
|
|
|
return BetterJson(viewModels);
|
|
}
|
|
|
|
public JsonResult GetTransactions(int? pageNumber, int? pageSize, int? inventoryId)
|
|
{
|
|
IQueryable<Transaction> query =
|
|
_context.Transactions
|
|
.OrderByDescending(t => t.Timestamp);
|
|
|
|
if (inventoryId.HasValue)
|
|
query =
|
|
query.Where(t => t.Inventory.Id == inventoryId.Value);
|
|
|
|
if (pageNumber.HasValue && pageSize.HasValue)
|
|
query = query
|
|
.Skip((pageNumber.Value - 1) * pageSize.Value)
|
|
.Take(pageSize.Value);
|
|
|
|
var totalItems = _context.Transactions.Count();
|
|
var transactions = query
|
|
.ProjectTo<TransactionViewModel>()
|
|
.ToArray();
|
|
return BetterJson(new { totalItems, transactions });
|
|
}
|
|
}
|
|
} |