Skip to content

4 product grouping #5

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

Open
wants to merge 3 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
29 changes: 29 additions & 0 deletions MyShop/MyShop.Core/Models/Product.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyShop.Core.Models
{
public class Product
{
public string Id { get; set; }

[StringLength(20)]
[DisplayName("Product Name")]
public string Name { get; set; }
public string Description { get; set; }

[Range(0,1000)]
public decimal Price { get; set; }
public string Category { get; set; }
public string Image { get; set; }

public Product() {
this.Id = Guid.NewGuid().ToString();
}
}
}
18 changes: 18 additions & 0 deletions MyShop/MyShop.Core/Models/ProductCategory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyShop.Core.Models
{
public class ProductCategory
{
public string Id { get; set; }
public string Category { get; set; }

public ProductCategory(){
this.Id = Guid.NewGuid().ToString();
}
}
}
4 changes: 4 additions & 0 deletions MyShop/MyShop.Core/MyShop.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
Expand All @@ -40,7 +41,10 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Models\Product.cs" />
<Compile Include="Models\ProductCategory.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ViewModels\ProductManagerViewModel.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
15 changes: 15 additions & 0 deletions MyShop/MyShop.Core/ViewModels/ProductManagerViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using MyShop.Core.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyShop.Core.ViewModels
{
public class ProductManagerViewModel
{
public Product product { get; set; }
public IEnumerable<ProductCategory> ProductCategories { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Runtime.Caching" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
Expand All @@ -40,7 +41,15 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="ProductCategoryRepository.cs" />
<Compile Include="ProductRepository.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MyShop.Core\MyShop.Core.csproj">
<Project>{9b029460-6f4e-46ae-9b88-08c5fe08c0a4}</Project>
<Name>MyShop.Core</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
83 changes: 83 additions & 0 deletions MyShop/MyShop.DataAccess.InMemory/ProductCategoryRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using MyShop.Core.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Caching;
using System.Text;
using System.Threading.Tasks;

namespace MyShop.DataAccess.InMemory
{
public class ProductCategoryRepository
{
ObjectCache cache = MemoryCache.Default;
List<ProductCategory> productsCategory = new List<ProductCategory>();

public ProductCategoryRepository()
{
productsCategory = cache["productsCategory"] as List<ProductCategory>;

if (productsCategory == null)
{
productsCategory = new List<ProductCategory>();
}
}

public void Commit()
{
cache["productsCategory"] = productsCategory;
}

public void Insert(ProductCategory p)
{
productsCategory.Add(p);
}

public void Update(ProductCategory product)
{
ProductCategory productToUpdate = productsCategory.Find(p => p.Id == product.Id);

if (productToUpdate != null)
{
productToUpdate = product;
}
else
{
throw new Exception("Product not found");
}
}

public ProductCategory Find(string Id)
{
ProductCategory product = productsCategory.Find(p => p.Id == Id);

if (product != null)
{
return product;
}
else
{
throw new Exception("Product not found");
}
}

public IQueryable<ProductCategory> Collection()
{
return productsCategory.AsQueryable();
}

public void Delete(string Id)
{
ProductCategory productToDelete = productsCategory.Find(p => p.Id == Id);

if (productToDelete != null)
{
productsCategory.Remove(productToDelete);
}
else
{
throw new Exception("Product not found");
}
}
}
}
75 changes: 75 additions & 0 deletions MyShop/MyShop.DataAccess.InMemory/ProductRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Caching;
using MyShop.Core;
using MyShop.Core.Models;

namespace MyShop.DataAccess.InMemory
{
public class ProductRepository
{
ObjectCache cache = MemoryCache.Default;
List<Product> products = new List<Product>();

public ProductRepository() {
products = cache["products"] as List<Product>;

if (products == null)
{
products = new List<Product>();
}
}

public void Commit() {
cache["products"] = products;
}

public void Insert(Product p) {
products.Add(p);
}

public void Update(Product product) {
Product productToUpdate = products.Find(p => p.Id == product.Id);

if (productToUpdate != null) {
productToUpdate = product;
}
else {
throw new Exception("Product not found");
}
}

public Product Find(string Id) {
Product product = products.Find(p => p.Id == Id);

if (product != null)
{
return product;
}
else
{
throw new Exception("Product not found");
}
}

public IQueryable<Product> Collection() {
return products.AsQueryable();
}

public void Delete(string Id) {
Product productToDelete = products.Find(p => p.Id == Id);

if (productToDelete != null)
{
products.Remove(productToDelete);
}
else
{
throw new Exception("Product not found");
}
}
}
}
115 changes: 115 additions & 0 deletions MyShop/MyShop.WebUI/Controllers/ProductCategoryManagerController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
using MyShop.Core.Models;
using MyShop.DataAccess.InMemory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MyShop.WebUI.Controllers
{
public class ProductCategoryManagerController : Controller
{
ProductCategoryRepository context;

public ProductCategoryManagerController()
{
context = new ProductCategoryRepository();
}

// GET: ProductManager
public ActionResult Index()
{
List<ProductCategory> productsCategories = context.Collection().ToList();
return View(productsCategories);
}

public ActionResult Create()
{
ProductCategory productCategory = new ProductCategory();
return View(productCategory);
}

[HttpPost]
public ActionResult Create(ProductCategory productCategory)
{
if (!ModelState.IsValid)
{
return View(productCategory);
}
else
{
context.Insert(productCategory);
context.Commit();

return RedirectToAction("Index");
}
}

public ActionResult Edit(string Id)
{
ProductCategory productCategory = context.Find(Id);
if (productCategory == null)
{
return HttpNotFound();
}
else
{
return View(productCategory);
}
}

[HttpPost]
public ActionResult Edit(ProductCategory productCategory, string Id)
{
ProductCategory productToEdit = context.Find(Id);
if (productToEdit == null)
{
return HttpNotFound();
}
else
{
if (!ModelState.IsValid)
{
return View(productToEdit);
}
productToEdit.Category = productCategory.Category;

context.Commit();

return RedirectToAction("Index");
}
}

public ActionResult Delete(string Id)
{
ProductCategory productToDelete = context.Find(Id);
if (productToDelete == null)
{
return HttpNotFound();
}
else
{
return View(productToDelete);
}
}

[HttpPost]
[ActionName("Delete")]
public ActionResult ConfirmDelete(string Id)
{
ProductCategory productToDelete = context.Find(Id);
if (productToDelete == null)
{
return HttpNotFound();
}
else
{
context.Delete(Id);
context.Commit();
return RedirectToAction("Index");
}

}
}
}
Loading