-
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.
- Loading branch information
1 parent
0419155
commit 475857d
Showing
14 changed files
with
462 additions
and
44 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using System.Xml.Linq; | ||
using demoMvcCore.Models; | ||
using demoMvcCore.ViewModels; | ||
using Microsoft.AspNetCore.Cors; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 | ||
|
||
namespace demoMvcCore.Controllers | ||
{ | ||
public class ProductsController : Controller | ||
{ | ||
// GET: /<controller>/ | ||
public IActionResult Index() | ||
{ | ||
var products = ProductsRepository.GetProducts(loadCategory:true); | ||
return View(products); | ||
|
||
} | ||
|
||
public IActionResult Add() | ||
{ | ||
ViewBag.Action = "add"; | ||
var productViewModels = new ProductsViewModel | ||
{ | ||
Categories = CategoryRepository.GetCategories(), | ||
//Product | ||
}; | ||
return View(productViewModels); | ||
|
||
|
||
} | ||
|
||
[HttpPost] | ||
public IActionResult Add(ProductsViewModel productsViewModel) | ||
{ | ||
if (ModelState.IsValid) | ||
{ | ||
ProductsRepository.AddProduct(productsViewModel.Product); | ||
return RedirectToAction(nameof(Index)); | ||
} | ||
ViewBag.Action = "edit"; | ||
productsViewModel.Categories = CategoryRepository.GetCategories(); | ||
return View(productsViewModel); | ||
} | ||
|
||
|
||
|
||
|
||
public IActionResult Edit(int id) | ||
{ | ||
ViewBag.Action = "edit"; | ||
var productView = new ProductsViewModel | ||
{ | ||
Product = ProductsRepository.GetProductById(id) ?? new Product(), | ||
Categories = CategoryRepository.GetCategories() | ||
}; | ||
return View(productView); | ||
} | ||
|
||
[HttpPost] | ||
|
||
public IActionResult Edit(ProductsViewModel productsViewModel) | ||
{ | ||
if (ModelState.IsValid) | ||
{ | ||
ProductsRepository.UpdateProduct(productsViewModel.Product.ProductId, productsViewModel.Product); | ||
|
||
return RedirectToAction(nameof(Index)); | ||
} | ||
ViewBag.Action = "edit"; | ||
productsViewModel.Categories = CategoryRepository.GetCategories(); | ||
return View(productsViewModel); | ||
} | ||
|
||
|
||
public IActionResult Delete(int id) | ||
{ | ||
ProductsRepository.DeleteProduct(id); | ||
return RedirectToAction(nameof(Index)); | ||
} | ||
|
||
|
||
|
||
|
||
|
||
} | ||
} | ||
|
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace demoMvcCore.Models | ||
{ | ||
|
||
public class Product | ||
{ | ||
public int ProductId { get; set; } | ||
|
||
[Required] | ||
[Display(Name = "Category")] | ||
public int? CategoryId { get; set; } | ||
|
||
[Required] | ||
public string Name { get; set; } = string.Empty; | ||
|
||
[Required] | ||
public int? Quantity { get; set; } | ||
|
||
[Required] | ||
[Range(0, int.MaxValue)] | ||
public double? Price { get; set; } | ||
public Category? Category { get; set; } | ||
|
||
|
||
} | ||
} | ||
|
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 |
---|---|---|
@@ -0,0 +1,100 @@ | ||
namespace demoMvcCore.Models | ||
{ | ||
public class ProductsRepository | ||
{ | ||
private static List<Product> _products = new List<Product>() | ||
{ | ||
new Product { ProductId = 1, CategoryId = 1, Name = "Iced Tea", Quantity = 100, Price = 1.99 }, | ||
new Product { ProductId = 2, CategoryId = 1, Name = "Canada Dry", Quantity = 200, Price = 1.99 }, | ||
new Product { ProductId = 3, CategoryId = 2, Name = "Whole Wheat Bread", Quantity = 300, Price = 1.50 }, | ||
new Product { ProductId = 4, CategoryId = 2, Name = "White Bread", Quantity = 300, Price = 1.50 } | ||
}; | ||
|
||
public static void AddProduct(Product product) | ||
{ | ||
if (_products is not null && _products.Count() > 0) | ||
{ | ||
var maxId = _products.Max(x => x.ProductId); | ||
product.ProductId = maxId + 1; | ||
|
||
} | ||
else | ||
{ | ||
var maxId = 1; | ||
product.ProductId = maxId; | ||
} | ||
_products ??= new List<Product>(); | ||
_products.Add(product); | ||
} | ||
|
||
public static List<Product> GetProducts(bool loadCategory = false) | ||
{ | ||
if(!loadCategory) | ||
{ | ||
return _products; | ||
} | ||
else | ||
{ | ||
if(_products !=null && _products.Count > 0 ) | ||
{ | ||
_products.ForEach(x => | ||
{ | ||
if(x.CategoryId.HasValue) | ||
x.Category = CategoryRepository.GetCategoryById(x.CategoryId.Value); | ||
}); | ||
} | ||
} | ||
return _products; | ||
} | ||
|
||
public static Product? GetProductById(int productId,bool loadCategory=false) | ||
{ | ||
var product = _products.FirstOrDefault(x => x.ProductId == productId); | ||
if (product != null) | ||
{ | ||
var prod = new Product | ||
{ | ||
ProductId = product.ProductId, | ||
Name = product.Name, | ||
Quantity = product.Quantity, | ||
Price = product.Price, | ||
CategoryId = product.CategoryId | ||
}; | ||
|
||
if(loadCategory && product.CategoryId.HasValue) | ||
{ | ||
prod.Category = CategoryRepository.GetCategoryById(prod.CategoryId.Value); | ||
} | ||
|
||
return prod; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public static void UpdateProduct(int productId, Product product) | ||
{ | ||
if (productId != product.ProductId) return; | ||
|
||
var productToUpdate = _products.FirstOrDefault(x => x.ProductId == productId); | ||
if (productToUpdate != null) | ||
{ | ||
productToUpdate.Name = product.Name; | ||
productToUpdate.Quantity = product.Quantity; | ||
productToUpdate.Price = product.Price; | ||
productToUpdate.CategoryId = product.CategoryId; | ||
} | ||
} | ||
|
||
public static void DeleteProduct(int productId) | ||
{ | ||
var product = _products.FirstOrDefault(x => x.ProductId == productId); | ||
if (product != null) | ||
{ | ||
_products.Remove(product); | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
| ||
using demoMvcCore.Models; | ||
|
||
namespace demoMvcCore.ViewModels | ||
{ | ||
public class ProductsViewModel | ||
{ | ||
public ProductsViewModel() | ||
{ | ||
|
||
|
||
} | ||
|
||
public IEnumerable<Category> Categories { get; set; } = new List<Category>(); | ||
public Product Product { get; set; } = new(); | ||
} | ||
} | ||
|
Oops, something went wrong.