diff --git a/MyShop/MyShop.Core/Models/Product.cs b/MyShop/MyShop.Core/Models/Product.cs new file mode 100644 index 0000000..b65bc5b --- /dev/null +++ b/MyShop/MyShop.Core/Models/Product.cs @@ -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(); + } + } +} diff --git a/MyShop/MyShop.Core/MyShop.Core.csproj b/MyShop/MyShop.Core/MyShop.Core.csproj index e6e54ce..640f655 100644 --- a/MyShop/MyShop.Core/MyShop.Core.csproj +++ b/MyShop/MyShop.Core/MyShop.Core.csproj @@ -31,6 +31,7 @@ + @@ -40,6 +41,7 @@ + diff --git a/MyShop/MyShop.DataAccess.InMemory/MyShop.DataAccess.InMemory.csproj b/MyShop/MyShop.DataAccess.InMemory/MyShop.DataAccess.InMemory.csproj index c9150aa..3f0648a 100644 --- a/MyShop/MyShop.DataAccess.InMemory/MyShop.DataAccess.InMemory.csproj +++ b/MyShop/MyShop.DataAccess.InMemory/MyShop.DataAccess.InMemory.csproj @@ -32,6 +32,7 @@ + @@ -40,7 +41,14 @@ + + + + {9b029460-6f4e-46ae-9b88-08c5fe08c0a4} + MyShop.Core + + \ No newline at end of file diff --git a/MyShop/MyShop.DataAccess.InMemory/ProductRepository.cs b/MyShop/MyShop.DataAccess.InMemory/ProductRepository.cs new file mode 100644 index 0000000..a1e78b8 --- /dev/null +++ b/MyShop/MyShop.DataAccess.InMemory/ProductRepository.cs @@ -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 products = new List(); + + public ProductRepository() { + products = cache["products"] as List; + + if (products == null) + { + products = new List(); + } + } + + 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 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"); + } + } + } +}