From 7b512599c718f0e03358302a2d9269db7798c436 Mon Sep 17 00:00:00 2001 From: 96Harlouie <74408053+96Harlouie@users.noreply.github.com> Date: Tue, 17 Nov 2020 18:50:29 +0800 Subject: [PATCH 1/2] Update --- MyShop/MyShop.Core/Models/Product.cs | 29 +++++++ MyShop/MyShop.Core/MyShop.Core.csproj | 2 + .../MyShop.DataAccess.InMemory.csproj | 8 ++ .../ProductRepository.cs | 75 +++++++++++++++++++ 4 files changed, 114 insertions(+) create mode 100644 MyShop/MyShop.Core/Models/Product.cs create mode 100644 MyShop/MyShop.DataAccess.InMemory/ProductRepository.cs 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"); + } + } + } +} From c650d952d53d414faa74c733614e4822662bbbd9 Mon Sep 17 00:00:00 2001 From: 96Harlouie <74408053+96Harlouie@users.noreply.github.com> Date: Tue, 17 Nov 2020 19:20:49 +0800 Subject: [PATCH 2/2] Product manager build and tested --- .../Controllers/ProductManagerController.cs | 108 ++++++++++++++++++ MyShop/MyShop.WebUI/MyShop.WebUI.csproj | 23 ++++ .../Views/ProductManager/Create.cshtml | 72 ++++++++++++ .../Views/ProductManager/Delete.cshtml | 64 +++++++++++ .../Views/ProductManager/Edit.cshtml | 74 ++++++++++++ .../Views/ProductManager/Index.cshtml | 56 +++++++++ .../MyShop.WebUI/Views/Shared/_Layout.cshtml | 2 +- 7 files changed, 398 insertions(+), 1 deletion(-) create mode 100644 MyShop/MyShop.WebUI/Controllers/ProductManagerController.cs create mode 100644 MyShop/MyShop.WebUI/Views/ProductManager/Create.cshtml create mode 100644 MyShop/MyShop.WebUI/Views/ProductManager/Delete.cshtml create mode 100644 MyShop/MyShop.WebUI/Views/ProductManager/Edit.cshtml create mode 100644 MyShop/MyShop.WebUI/Views/ProductManager/Index.cshtml diff --git a/MyShop/MyShop.WebUI/Controllers/ProductManagerController.cs b/MyShop/MyShop.WebUI/Controllers/ProductManagerController.cs new file mode 100644 index 0000000..bd29dc0 --- /dev/null +++ b/MyShop/MyShop.WebUI/Controllers/ProductManagerController.cs @@ -0,0 +1,108 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; +using System.Web.Mvc; +using MyShop.Core.Models; +using MyShop.DataAccess.InMemory; + +namespace MyShop.WebUI.Controllers +{ + public class ProductManagerController : Controller + { + ProductRepository context; + + public ProductManagerController() { + context = new ProductRepository(); + } + // GET: ProductManager + public ActionResult Index() + { + List products = context.Collection().ToList(); + return View(products); + } + + public ActionResult Create() { + Product product = new Product(); + return View(product); + } + + [HttpPost] + public ActionResult Create(Product product) { + if (!ModelState.IsValid) + { + return View(product); + } + else { + context.Insert(product); + context.Commit(); + + return RedirectToAction("Index"); + } + } + + public ActionResult Edit(string Id) { + Product product = context.Find(Id); + if (product == null) + { + return HttpNotFound(); + } + else { + return View(product); + } + } + + [HttpPost] + public ActionResult Edit(Product product, string Id) { + Product productToEdit = context.Find(Id); + if (productToEdit == null) + { + return HttpNotFound(); + } + else { + if (!ModelState.IsValid) + { + return View(productToEdit); + } + productToEdit.Name = product.Name; + productToEdit.Description = product.Description; + productToEdit.Price = product.Price; + productToEdit.Category = product.Category; + productToEdit.Image = product.Image; + + context.Commit(); + + return RedirectToAction("Index"); + } + } + + public ActionResult Delete(string Id) { + Product productToDelete = context.Find(Id); + if (productToDelete == null) + { + return HttpNotFound(); + } + else { + return View(productToDelete); + } + } + + [HttpPost] + [ActionName("Delete")] + public ActionResult ConfirmDelete(string Id) + { + Product productToDelete = context.Find(Id); + if (productToDelete == null) + { + return HttpNotFound(); + } + else + { + context.Delete(Id); + context.Commit(); + return RedirectToAction("Index"); + } + + } + } +} \ No newline at end of file diff --git a/MyShop/MyShop.WebUI/MyShop.WebUI.csproj b/MyShop/MyShop.WebUI/MyShop.WebUI.csproj index 5ff57ac..eed1222 100644 --- a/MyShop/MyShop.WebUI/MyShop.WebUI.csproj +++ b/MyShop/MyShop.WebUI/MyShop.WebUI.csproj @@ -196,6 +196,7 @@ + Global.asax @@ -276,6 +277,10 @@ + + + + @@ -285,6 +290,24 @@ + + + {9b029460-6f4e-46ae-9b88-08c5fe08c0a4} + MyShop.Core + + + {e7fd92a1-7304-4f70-9ba5-e214cb28a863} + MyShop.DataAccess.InMemory + + + {3a4704f8-042d-4450-a79a-aa096ed4f5cd} + MyShop.DataAccess.SQL + + + {462aa931-d9e7-4b8a-840b-c8026f2c0fc0} + MyShop.Services + + 10.0 $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) diff --git a/MyShop/MyShop.WebUI/Views/ProductManager/Create.cshtml b/MyShop/MyShop.WebUI/Views/ProductManager/Create.cshtml new file mode 100644 index 0000000..0ff0639 --- /dev/null +++ b/MyShop/MyShop.WebUI/Views/ProductManager/Create.cshtml @@ -0,0 +1,72 @@ +@model MyShop.Core.Models.Product + +@{ + ViewBag.Title = "Create"; +} + +

Create

+ + +@using (Html.BeginForm()) +{ + @Html.AntiForgeryToken() + +
+

Product

+
+ @Html.ValidationSummary(true, "", new { @class = "text-danger" }) +
+ @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" }) +
+ @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } }) + @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" }) +
+
+ +
+ @Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" }) +
+ @Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } }) + @Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" }) +
+
+ +
+ @Html.LabelFor(model => model.Price, htmlAttributes: new { @class = "control-label col-md-2" }) +
+ @Html.EditorFor(model => model.Price, new { htmlAttributes = new { @class = "form-control" } }) + @Html.ValidationMessageFor(model => model.Price, "", new { @class = "text-danger" }) +
+
+ +
+ @Html.LabelFor(model => model.Category, htmlAttributes: new { @class = "control-label col-md-2" }) +
+ @Html.EditorFor(model => model.Category, new { htmlAttributes = new { @class = "form-control" } }) + @Html.ValidationMessageFor(model => model.Category, "", new { @class = "text-danger" }) +
+
+ +
+ @Html.LabelFor(model => model.Image, htmlAttributes: new { @class = "control-label col-md-2" }) +
+ @Html.EditorFor(model => model.Image, new { htmlAttributes = new { @class = "form-control" } }) + @Html.ValidationMessageFor(model => model.Image, "", new { @class = "text-danger" }) +
+
+ +
+
+ +
+
+
+} + +
+ @Html.ActionLink("Back to List", "Index") +
+ +@section Scripts { + @Scripts.Render("~/bundles/jqueryval") +} diff --git a/MyShop/MyShop.WebUI/Views/ProductManager/Delete.cshtml b/MyShop/MyShop.WebUI/Views/ProductManager/Delete.cshtml new file mode 100644 index 0000000..a39ab88 --- /dev/null +++ b/MyShop/MyShop.WebUI/Views/ProductManager/Delete.cshtml @@ -0,0 +1,64 @@ +@model MyShop.Core.Models.Product + +@{ + ViewBag.Title = "Delete"; +} + +

Delete

+ +

Are you sure you want to delete this?

+
+

Product

+
+
+
+ @Html.DisplayNameFor(model => model.Name) +
+ +
+ @Html.DisplayFor(model => model.Name) +
+ +
+ @Html.DisplayNameFor(model => model.Description) +
+ +
+ @Html.DisplayFor(model => model.Description) +
+ +
+ @Html.DisplayNameFor(model => model.Price) +
+ +
+ @Html.DisplayFor(model => model.Price) +
+ +
+ @Html.DisplayNameFor(model => model.Category) +
+ +
+ @Html.DisplayFor(model => model.Category) +
+ +
+ @Html.DisplayNameFor(model => model.Image) +
+ +
+ @Html.DisplayFor(model => model.Image) +
+ +
+ + @using (Html.BeginForm()) { + @Html.AntiForgeryToken() + +
+ | + @Html.ActionLink("Back to List", "Index") +
+ } +
diff --git a/MyShop/MyShop.WebUI/Views/ProductManager/Edit.cshtml b/MyShop/MyShop.WebUI/Views/ProductManager/Edit.cshtml new file mode 100644 index 0000000..92182a4 --- /dev/null +++ b/MyShop/MyShop.WebUI/Views/ProductManager/Edit.cshtml @@ -0,0 +1,74 @@ +@model MyShop.Core.Models.Product + +@{ + ViewBag.Title = "Edit"; +} + +

Edit

+ + +@using (Html.BeginForm()) +{ + @Html.AntiForgeryToken() + +
+

Product

+
+ @Html.ValidationSummary(true, "", new { @class = "text-danger" }) + @Html.HiddenFor(model => model.Id) + +
+ @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" }) +
+ @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } }) + @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" }) +
+
+ +
+ @Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" }) +
+ @Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } }) + @Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" }) +
+
+ +
+ @Html.LabelFor(model => model.Price, htmlAttributes: new { @class = "control-label col-md-2" }) +
+ @Html.EditorFor(model => model.Price, new { htmlAttributes = new { @class = "form-control" } }) + @Html.ValidationMessageFor(model => model.Price, "", new { @class = "text-danger" }) +
+
+ +
+ @Html.LabelFor(model => model.Category, htmlAttributes: new { @class = "control-label col-md-2" }) +
+ @Html.EditorFor(model => model.Category, new { htmlAttributes = new { @class = "form-control" } }) + @Html.ValidationMessageFor(model => model.Category, "", new { @class = "text-danger" }) +
+
+ +
+ @Html.LabelFor(model => model.Image, htmlAttributes: new { @class = "control-label col-md-2" }) +
+ @Html.EditorFor(model => model.Image, new { htmlAttributes = new { @class = "form-control" } }) + @Html.ValidationMessageFor(model => model.Image, "", new { @class = "text-danger" }) +
+
+ +
+
+ +
+
+
+} + +
+ @Html.ActionLink("Back to List", "Index") +
+ +@section Scripts { + @Scripts.Render("~/bundles/jqueryval") +} diff --git a/MyShop/MyShop.WebUI/Views/ProductManager/Index.cshtml b/MyShop/MyShop.WebUI/Views/ProductManager/Index.cshtml new file mode 100644 index 0000000..e8d46e8 --- /dev/null +++ b/MyShop/MyShop.WebUI/Views/ProductManager/Index.cshtml @@ -0,0 +1,56 @@ +@model IEnumerable + +@{ + ViewBag.Title = "Index"; +} + +

Index

+ +

+ @Html.ActionLink("Create New", "Create") +

+ + + + + + + + + + +@foreach (var item in Model) { + + + + + + + + +} + +
+ @Html.DisplayNameFor(model => model.Name) + + @Html.DisplayNameFor(model => model.Description) + + @Html.DisplayNameFor(model => model.Price) + + @Html.DisplayNameFor(model => model.Category) + + @Html.DisplayNameFor(model => model.Image) +
+ @Html.DisplayFor(modelItem => item.Name) + + @Html.DisplayFor(modelItem => item.Description) + + @Html.DisplayFor(modelItem => item.Price) + + @Html.DisplayFor(modelItem => item.Category) + + @Html.DisplayFor(modelItem => item.Image) + + @Html.ActionLink("Edit", "Edit", new { id=item.Id }) | + @Html.ActionLink("Delete", "Delete", new { id=item.Id }) +
diff --git a/MyShop/MyShop.WebUI/Views/Shared/_Layout.cshtml b/MyShop/MyShop.WebUI/Views/Shared/_Layout.cshtml index ae31cd1..76452e9 100644 --- a/MyShop/MyShop.WebUI/Views/Shared/_Layout.cshtml +++ b/MyShop/MyShop.WebUI/Views/Shared/_Layout.cshtml @@ -22,7 +22,7 @@