Skip to content
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

3 product management pages #4

Open
wants to merge 2 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();
}
}
}
2 changes: 2 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,6 +41,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Models\Product.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Expand Down
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,14 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<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>
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");
}
}
}
}
108 changes: 108 additions & 0 deletions MyShop/MyShop.WebUI/Controllers/ProductManagerController.cs
Original file line number Diff line number Diff line change
@@ -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<Product> 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");
}

}
}
}
23 changes: 23 additions & 0 deletions MyShop/MyShop.WebUI/MyShop.WebUI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@
<Compile Include="Controllers\AccountController.cs" />
<Compile Include="Controllers\HomeController.cs" />
<Compile Include="Controllers\ManageController.cs" />
<Compile Include="Controllers\ProductManagerController.cs" />
<Compile Include="Global.asax.cs">
<DependentUpon>Global.asax</DependentUpon>
</Compile>
Expand Down Expand Up @@ -276,6 +277,10 @@
<Content Include="Views\Manage\VerifyPhoneNumber.cshtml" />
<Content Include="Views\Shared\Lockout.cshtml" />
<Content Include="Views\Shared\_LoginPartial.cshtml" />
<Content Include="Views\ProductManager\Index.cshtml" />
<Content Include="Views\ProductManager\Create.cshtml" />
<Content Include="Views\ProductManager\Edit.cshtml" />
<Content Include="Views\ProductManager\Delete.cshtml" />
</ItemGroup>
<ItemGroup>
<Folder Include="App_Data\" />
Expand All @@ -285,6 +290,24 @@
<Content Include="Scripts\jquery-3.2.1.slim.min.map" />
<Content Include="Scripts\jquery-3.2.1.min.map" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MyShop.Core\MyShop.Core.csproj">
<Project>{9b029460-6f4e-46ae-9b88-08c5fe08c0a4}</Project>
<Name>MyShop.Core</Name>
</ProjectReference>
<ProjectReference Include="..\MyShop.DataAccess.InMemory\MyShop.DataAccess.InMemory.csproj">
<Project>{e7fd92a1-7304-4f70-9ba5-e214cb28a863}</Project>
<Name>MyShop.DataAccess.InMemory</Name>
</ProjectReference>
<ProjectReference Include="..\MyShop.DataAccess.SQL\MyShop.DataAccess.SQL.csproj">
<Project>{3a4704f8-042d-4450-a79a-aa096ed4f5cd}</Project>
<Name>MyShop.DataAccess.SQL</Name>
</ProjectReference>
<ProjectReference Include="..\MyShop.Services\MyShop.Services.csproj">
<Project>{462aa931-d9e7-4b8a-840b-c8026f2c0fc0}</Project>
<Name>MyShop.Services</Name>
</ProjectReference>
</ItemGroup>
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
Expand Down
72 changes: 72 additions & 0 deletions MyShop/MyShop.WebUI/Views/ProductManager/Create.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
@model MyShop.Core.Models.Product

@{
ViewBag.Title = "Create";
}

<h2>Create</h2>


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

<div class="form-horizontal">
<h4>Product</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.Price, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Price, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Price, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.Category, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Category, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Category, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.Image, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Image, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Image, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}

<div>
@Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
Loading