Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Firdavs #2

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions WMS.WebUI/WMS.WebUI/Constants/ApiResourceConstants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace WMS.WebUI.Constants;

public static class ApiResourceConstants
{
public const string Products = nameof(Products);
public const string Categories = nameof(Categories);
public const string Customers = nameof(Customers);
public const string Suppliers = nameof(Suppliers);
public const string Sales = nameof(Sales);
public const string Supplies = nameof(Supplies);
}
18 changes: 13 additions & 5 deletions WMS.WebUI/WMS.WebUI/Controllers/CategoriesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,20 @@ public CategoriesController(ICategoryStore categoryStore)
}

// GET: Categories
public async Task<ActionResult> Index(string? searchString)
public async Task<ActionResult> Index(string? searchString,int? pageNumber)
{
var categories = await _categoryStore.GetCategoriesAsync(searchString);
ViewBag.SearchString = searchString;

return View(categories);
var categories = await _categoryStore.GetCategoriesAsync(searchString, pageNumber);

ViewBag.SearchString = searchString ;
ViewBag.PageSize = categories.PageSize;
ViewBag.TotalPages = categories.PagesCount;
ViewBag.TotalItems = categories.TotalCount;
ViewBag.CurrentPage = categories.CurrentPage;
ViewBag.HasPreviousPage = categories.HasPreviousPage;
ViewBag.HasNextPage = categories.HasNextPage;


return View(categories.Data);
}

// GET: Categories/Details/5
Expand Down
131 changes: 131 additions & 0 deletions WMS.WebUI/WMS.WebUI/Controllers/CustomersController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using WMS.WebUI.Models.PaginatedResponse;
using WMS.WebUI.QueryParams;
using WMS.WebUI.Stores.Interfaces;
using WMS.WebUI.ViewModels.CustomerViewModels;

namespace WMS.WebUI.Controllers;

public class CustomersController(ICustomerStore customerStore) : Controller
{
private readonly ICustomerStore _customerStore = customerStore;
public async Task<IActionResult> Index(
string? searchString,
decimal? balanceGreaterThan,
decimal? balanceLessThan,
int? pageNumber)
{
var queryParameters = new CustomerQueryParameters()
{
Search = searchString,
BalanceGreaterThan = balanceGreaterThan,
BalanceLessThan = balanceLessThan,
PageNumber = pageNumber
};

var customers = await _customerStore.GetCustomers(queryParameters);

PopulateViewBag(customers, queryParameters);

return View(customers.Data);
}
public async Task<IActionResult> Details(int id)
{
var customer = await _customerStore.GetById(id);
return View(customer);
}
[HttpGet]
public async Task<IActionResult> Create()
{
return View();
}
[HttpPost]
public async Task<IActionResult> Create(CustomerActionViewModel customerActionViewModel)
{
if (!ModelState.IsValid)
{
return View(customerActionViewModel);
}
var createdCustomer = await _customerStore.Create(customerActionViewModel);

return RedirectToAction(nameof(Details), new { id = createdCustomer.Id });
}
[HttpGet]
public async Task<IActionResult> Edit(int id)
{
var customer = await _customerStore.GetById(id);

var customerForEdit = ParseCustomerActionViewModel(customer);

return View(customerForEdit);
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id,CustomerActionViewModel customerActionViewModel)
{
if (!ModelState.IsValid)
{
return View(customerActionViewModel);
}
if(customerActionViewModel.Id != id )
{
return BadRequest("Does not match id with customer id");
}

await _customerStore.Update(customerActionViewModel);


return RedirectToAction(nameof(Details),new {id = customerActionViewModel.Id});
}
[HttpGet]
public async Task<IActionResult> Delete(int id)
{
var customer = await _customerStore.GetById(id);

return View(customer);
}
[HttpPost,ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(int id)
{
await _customerStore.Delete(id);
return RedirectToAction(nameof(Index));
}
private void PopulateViewBag(PaginatedApiResponse<CustomerDisplayViewModel> customers,CustomerQueryParameters queryParameters)
{
ViewBag.BalanceLessThan = queryParameters.BalanceLessThan;
ViewBag.BalanceGreaterThan= queryParameters.BalanceGreaterThan;
ViewBag.SearchString = queryParameters.Search;

ViewBag.PageSize = customers.PageSize;
ViewBag.TotalPages = customers.PagesCount;
ViewBag.TotalItems = customers.TotalCount;
ViewBag.CurrentPage = customers.CurrentPage;
ViewBag.HasPreviousPage = customers.HasPreviousPage;
ViewBag.HasNextPage = customers.HasNextPage;
}
private CustomerActionViewModel ParseCustomerActionViewModel(CustomerDisplayViewModel model)
{
string[] nameParts = model.FullName.Split(new char[] { ' ' }, 2);

string firstName = nameParts[0]; // First part is the first name

// If there's more than one part, the rest is considered the last name
string lastName = (nameParts.Length > 1) ? nameParts[1] : "";

var customerForEdit = new CustomerActionViewModel()
{
Id = model.Id,
FirstName = firstName,
LastName = lastName,
Balance = model.Balance,
Address = model.Address,
Discount = model.Discount,
PhoneNumber = model.PhoneNumber,
};
return customerForEdit;
}

}
148 changes: 148 additions & 0 deletions WMS.WebUI/WMS.WebUI/Controllers/ProductsController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using WMS.WebUI.Models.PaginatedResponse;
using WMS.WebUI.QueryParams;
using WMS.WebUI.Stores.Interfaces;
using WMS.WebUI.ViewModels;

namespace WMS.WebUI.Controllers;

public class ProductsController(IProductsStore store,ICategoryStore categoryStore) : Controller
{
private readonly ICategoryStore _categoryStore = categoryStore;
private readonly IProductsStore _productsStore = store ?? throw new ArgumentNullException(nameof(store));

public async Task<IActionResult> Index(
string? searchString,
int? pageNumber,
int? categoryId,
decimal? maxPrice,
decimal? minPrice,
string? isLowQuantity
)
{
var queryParameters = new ProductQueryParameters()
{
Search = searchString,
PageNumber = pageNumber,
CategoryId = categoryId,
MaxPrice = maxPrice,
MinPrice = minPrice,
IsLowQuantity = isLowQuantity == "on",
};

var products = await _productsStore.GetProducts(queryParameters);
//Problem CAtegories get with pagination with max pagesize=15, I dont choose 16 category
var categories = await _categoryStore.GetCategoriesAsync();

ViewBag.Categories = new SelectList(categories.Data.OrderBy(x => x.Name), "Id", "Name", categoryId);
ViewBag.MinPrice = minPrice;
ViewBag.IsLowQuantity = isLowQuantity == "on";
ViewBag.MaxPrice = maxPrice;
ViewBag.SearchString = searchString;

PopulateViewBag(products, searchString);

return View(products.Data);
}
public async Task<IActionResult> Details(int id)
{
var product = await _productsStore.GetById(id);

if(product is null)
return NotFound();

return View(product);
}
public async Task<IActionResult> Create()
{
var categories = await _categoryStore.GetCategoriesAsync();

ViewBag.Categories = new SelectList(categories.Data, "Id", "Name");

return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(ProductViewModel product)
{
if (!ModelState.IsValid)
{
var categories = await _categoryStore.GetCategoriesAsync();

ViewBag.Categories = new SelectList(categories.Data, "Id", "Name", categories);
return View(product);
}
var createdProduct = await _productsStore.Create(product);
return RedirectToAction(nameof(Details),new { id = createdProduct.Id });
}
public async Task<IActionResult> Edit(int id)
{
var product = await _productsStore.GetById(id);

if (product is null)
return NotFound();

var categories = await _categoryStore.GetCategoriesAsync();

ViewBag.Categories = new SelectList(categories.Data, "Id", "Name");

return View(product);
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id,ProductViewModel product)
{
if (!ModelState.IsValid)
{
var categories = await _categoryStore.GetCategoriesAsync();
ViewBag.Categories = new SelectList(categories.Data, "Id", "Name");

return View(product);
}

if (product.Id != id)
{
return BadRequest("Does not match id with product id");
}
await _productsStore.Update(product);
return RedirectToAction(nameof(Details),new { id = product.Id });
}


[HttpGet]
public async Task<IActionResult> Delete(int id)
{
var product = await _productsStore.GetById(id);

if(product is null)
return NotFound();

return View(product);
}
[HttpPost(),ActionName(nameof(Delete))]
public async Task<IActionResult> DeleteConfirmed(int id)
{
var product = await _productsStore.GetById(id);

if (product is null)
return NotFound();

await _productsStore.Delete(id);

return RedirectToAction(nameof(Index));
}
private void PopulateViewBag(
PaginatedApiResponse<ProductViewModel> products,
string? searchString)
{
ViewBag.SearchString = searchString;
ViewBag.PageSize = products.PageSize;
ViewBag.TotalPages = products.PagesCount;
ViewBag.TotalItems = products.TotalCount;
ViewBag.CurrentPage = products.CurrentPage;
ViewBag.HasPreviousPage = products.HasPreviousPage;
ViewBag.HasNextPage = products.HasNextPage;
}
}
57 changes: 57 additions & 0 deletions WMS.WebUI/WMS.WebUI/Controllers/SalesController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using WMS.WebUI.Models.PaginatedResponse;
using WMS.WebUI.QueryParams;
using WMS.WebUI.Stores.Interfaces;
using WMS.WebUI.ViewModels.SaleViewModels;

namespace WMS.WebUI.Controllers;

public class SalesController : Controller
{
private readonly ICustomerStore _customerStore;
private readonly ISaleStore _salesStore;

public SalesController(ISaleStore saleStore, ICustomerStore customerStore)
{
_customerStore = customerStore;
_salesStore = saleStore;
}

public async Task<IActionResult> Index(string? searchString, DateTime? fromDate, DateTime? toDate, int? pageNumber)
{
var queryParameters = new TransactionQueryParameters()
{
Search = searchString,
FromDate = fromDate,
ToDate = toDate,
PageNumber = pageNumber
};

var sales = await _salesStore.GetSales(queryParameters);

PopulateViewBag(sales, queryParameters);

return View(sales.Data);
}

public async Task<IActionResult> Details(int id)
{
var sale = await _salesStore.GetById(id);
return View(sale);
}

private void PopulateViewBag(PaginatedApiResponse<SaleViewModel> sales, TransactionQueryParameters queryParameters)
{
ViewBag.FromDate = queryParameters.FromDate?.ToString("yyyy-MM-dd");
ViewBag.ToDate = queryParameters.ToDate?.ToString("yyyy-MM-dd");
ViewBag.SearchString = queryParameters.Search;

ViewBag.PageSize = sales.PageSize;
ViewBag.TotalPages = sales.PagesCount;
ViewBag.TotalItems = sales.TotalCount;
ViewBag.CurrentPage = sales.CurrentPage;
ViewBag.HasPreviousPage = sales.HasPreviousPage;
ViewBag.HasNextPage = sales.HasNextPage;
}
}
Loading