diff --git a/SportsStore.Domain/Abstract/IProductRepository.cs b/SportsStore.Domain/Abstract/IProductRepository.cs index c30b209..06ded75 100644 --- a/SportsStore.Domain/Abstract/IProductRepository.cs +++ b/SportsStore.Domain/Abstract/IProductRepository.cs @@ -10,5 +10,7 @@ namespace SportsStore.Domain.Abstract public interface IProductRepository { IEnumerable Products { get; } + + void SaveProduct(Product product); } } diff --git a/SportsStore.Domain/Concrete/EFProductRepository.cs b/SportsStore.Domain/Concrete/EFProductRepository.cs index 0c7e8f1..82a3ede 100644 --- a/SportsStore.Domain/Concrete/EFProductRepository.cs +++ b/SportsStore.Domain/Concrete/EFProductRepository.cs @@ -15,5 +15,26 @@ namespace SportsStore.Domain.Concrete { get { return context.Products; } } + + public void SaveProduct(Product product) + { + if (product.ProductID == 0) + { + context.Products.Add(product); + } + else + { + Product dbEntry = context.Products.Find(product.ProductID); + if (dbEntry != null) + { + dbEntry.Name = product.Name; + dbEntry.Description = product.Description; + dbEntry.Price = product.Price; + dbEntry.Category = product.Category; + } + } + + context.SaveChanges(); + } } } \ No newline at end of file diff --git a/SportsStore.Domain/Entities/Product.cs b/SportsStore.Domain/Entities/Product.cs index e9fda5a..710ba94 100644 --- a/SportsStore.Domain/Entities/Product.cs +++ b/SportsStore.Domain/Entities/Product.cs @@ -2,13 +2,18 @@ using System.Collections.Generic; using System.Linq; using System.Web; +using System.Web.Mvc; +using System.ComponentModel.DataAnnotations; namespace SportsStore.Domain.Entities { public class Product { + [HiddenInput(DisplayValue = false)] public int ProductID { get; set; } public string Name { get; set; } + + [DataType(DataType.MultilineText)] public string Description { get; set; } public decimal Price { get; set; } public string Category { get; set; } diff --git a/SportsStore.WebUI/Controllers/AdminController.cs b/SportsStore.WebUI/Controllers/AdminController.cs new file mode 100644 index 0000000..dea7572 --- /dev/null +++ b/SportsStore.WebUI/Controllers/AdminController.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; +using System.Web.Mvc; +using SportsStore.Domain.Abstract; +using SportsStore.Domain.Entities; + +namespace SportsStore.WebUI.Controllers +{ + public class AdminController : Controller + { + private IProductRepository repository; + + public AdminController(IProductRepository repo) + { + repository = repo; + } + + public ActionResult Index() + { + return View(repository.Products); + } + + public ViewResult Edit(int productId) + { + Product product = repository.Products.FirstOrDefault(p => p.ProductID == productId); + + return View(product); + } + + [HttpPost] + public ActionResult Edit(Product product) + { + if (ModelState.IsValid) + { + repository.SaveProduct(product); + TempData["message"] = string.Format("{0} has been saved", product.Name); + return RedirectToAction("Index"); + } + else + { + return View(product); + } + } + } +} \ No newline at end of file diff --git a/SportsStore.WebUI/SportsStore.WebUI.csproj b/SportsStore.WebUI/SportsStore.WebUI.csproj index a710b57..37c42aa 100644 --- a/SportsStore.WebUI/SportsStore.WebUI.csproj +++ b/SportsStore.WebUI/SportsStore.WebUI.csproj @@ -136,6 +136,7 @@ + @@ -168,6 +169,9 @@ + + + Web.config diff --git a/SportsStore.WebUI/Views/Admin/Edit.cshtml b/SportsStore.WebUI/Views/Admin/Edit.cshtml new file mode 100644 index 0000000..1eaf00b --- /dev/null +++ b/SportsStore.WebUI/Views/Admin/Edit.cshtml @@ -0,0 +1,49 @@ +@model SportsStore.Domain.Entities.Product +@{ + ViewBag.Title = "Admin: Edit " + Model.Name; + Layout = "~/Views/Shared/_AdminLayout.cshtml"; +} + +@*

Edit @Model.Name

+ +@using (Html.BeginForm()) +{ + @Html.EditorForModel() + + @Html.ActionLink("Cancel and return to List", "Index") +}*@ + +
+
+

Edit @Model.Name

+
+ + @using (Html.BeginForm()) + { +
+ @Html.HiddenFor(m => m.ProductID) + @foreach (var property in ViewData.ModelMetadata.Properties) + { + if (property.PropertyName != "ProeuctID") + { +
+ + @if (property.PropertyName == "Description") + { + @Html.TextArea(property.PropertyName, null, new { @class = "form-control", rows = 5 }) + } + else + { + @Html.TextBox(property.PropertyName, null, new { @class = "form-control" }) + } +
+ } + } +
+ + + } +
diff --git a/SportsStore.WebUI/Views/Admin/Index.cshtml b/SportsStore.WebUI/Views/Admin/Index.cshtml new file mode 100644 index 0000000..d7e0687 --- /dev/null +++ b/SportsStore.WebUI/Views/Admin/Index.cshtml @@ -0,0 +1,40 @@ +@model IEnumerable + +@{ + ViewBag.Title = "Admin: All Products"; + Layout = "~/Views/Shared/_AdminLayout.cshtml"; +} + +
+
+

All Products

+
+
+ + + + + + + + @foreach (var item in Model) + { + + + + + + + } +
IDNamePriceActions
@item.ProductID@Html.ActionLink(item.Name, "Edit", new { item.ProductID })@item.Price.ToString("c") + @using (Html.BeginForm("Delete", "Admin")) + { + @Html.Hidden("ProductID", item.ProductID) + + } +
+
+ +
\ No newline at end of file diff --git a/SportsStore.WebUI/Views/Shared/_AdminLayout.cshtml b/SportsStore.WebUI/Views/Shared/_AdminLayout.cshtml new file mode 100644 index 0000000..66064d3 --- /dev/null +++ b/SportsStore.WebUI/Views/Shared/_AdminLayout.cshtml @@ -0,0 +1,24 @@ +@{ + Layout = null; +} + + + + + + + + + + @ViewBag.Title + + +
+ @if (TempData["message"] != null) + { +
@TempData["message"]
+ } + @RenderBody() +
+ +