Skip to content

Commit 32d8d1b

Browse files
author
raihanM95
committed
CRUD for example
1 parent f5021aa commit 32d8d1b

File tree

8 files changed

+352
-0
lines changed

8 files changed

+352
-0
lines changed

Web/Controllers/ProductsController.cs

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
using Application;
2+
using Domain.Entities;
3+
using Microsoft.AspNetCore.Http;
4+
using Microsoft.AspNetCore.Mvc;
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
using System.Threading.Tasks;
9+
using Web.Models;
10+
11+
namespace Web.Controllers
12+
{
13+
public class ProductsController : Controller
14+
{
15+
private IRepositoryWrapper _repoWrapper;
16+
public ProductsController(IRepositoryWrapper repoWrapper)
17+
{
18+
_repoWrapper = repoWrapper;
19+
}
20+
21+
// GET: ProductsController
22+
public IActionResult Index()
23+
{
24+
var products = _repoWrapper.Product.FindAll();
25+
return View(products);
26+
}
27+
28+
// GET: ProductsController/Details/5
29+
public ActionResult Details(int id)
30+
{
31+
Product product = _repoWrapper.Product.FindByCondition(i => i.Id == id).FirstOrDefault();
32+
return View(product);
33+
}
34+
35+
// GET: ProductsController/Create
36+
public ActionResult Create()
37+
{
38+
return View();
39+
}
40+
41+
// POST: ProductsController/Create
42+
[HttpPost]
43+
[ValidateAntiForgeryToken]
44+
public ActionResult Create(Product product)
45+
{
46+
try
47+
{
48+
_repoWrapper.Product.Create(product);
49+
_repoWrapper.Save();
50+
return RedirectToAction(nameof(Index));
51+
}
52+
catch
53+
{
54+
return View();
55+
}
56+
}
57+
58+
// GET: ProductsController/Edit/5
59+
public IActionResult Edit(int? id)
60+
{
61+
if (id == null)
62+
{
63+
return NotFound();
64+
}
65+
66+
Product product = _repoWrapper.Product.FindByCondition(i => i.Id == id).FirstOrDefault();
67+
return View(product);
68+
}
69+
70+
// POST: ProductsController/Edit/5
71+
[HttpPost]
72+
[ValidateAntiForgeryToken]
73+
public ActionResult Edit(Product product)
74+
{
75+
try
76+
{
77+
_repoWrapper.Product.Update(product);
78+
_repoWrapper.Save();
79+
return RedirectToAction(nameof(Index));
80+
}
81+
catch
82+
{
83+
return View();
84+
}
85+
}
86+
87+
// GET: ProductsController/Delete/5
88+
public IActionResult Delete(int id)
89+
{
90+
if (id == null)
91+
{
92+
return NotFound();
93+
}
94+
try
95+
{
96+
Product product = _repoWrapper.Product.FindByCondition(i => i.Id == id).FirstOrDefault();
97+
_repoWrapper.Product.Delete(product);
98+
_repoWrapper.Save();
99+
return RedirectToAction(nameof(Index));
100+
}
101+
catch
102+
{
103+
return View();
104+
}
105+
}
106+
}
107+
}

Web/Models/ProductViewModel.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
6+
namespace Web.Models
7+
{
8+
public class ProductViewModel
9+
{
10+
//public virtual int Id { get; set; }
11+
//public string Name { get; set; }
12+
//public string Barcode { get; set; }
13+
//public string Description { get; set; }
14+
//public string CreatedBy { get; set; }
15+
//public DateTime Created { get; set; }
16+
//public string LastModifiedBy { get; set; }
17+
//public DateTime? LastModified { get; set; }
18+
}
19+
}

Web/Views/Products/Create.cshtml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
@model Domain.Entities.Product
2+
@{
3+
ViewData["Title"] = "Create";
4+
}
5+
6+
<h3>Product</h3>
7+
<hr />
8+
9+
<div class="row">
10+
<div class="col-md-4">
11+
<form asp-action="Create">
12+
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
13+
<div class="form-group">
14+
<label asp-for="Name" class="control-label"></label>
15+
<input asp-for="Name" class="form-control" />
16+
<span asp-validation-for="Name" class="text-danger"></span>
17+
</div>
18+
<div class="form-group">
19+
<label asp-for="Barcode" class="control-label"></label>
20+
<input asp-for="Barcode" class="form-control" />
21+
<span asp-validation-for="Barcode" class="text-danger"></span>
22+
</div>
23+
<div class="form-group">
24+
<label asp-for="Description" class="control-label"></label>
25+
<input asp-for="Description" class="form-control" />
26+
<span asp-validation-for="Description" class="text-danger"></span>
27+
</div>
28+
<div class="form-group">
29+
<label asp-for="CreatedBy" class="control-label"></label>
30+
<input asp-for="CreatedBy" class="form-control" />
31+
<span asp-validation-for="CreatedBy" class="text-danger"></span>
32+
</div>
33+
<div class="form-group">
34+
<label asp-for="Created" class="control-label"></label>
35+
<input asp-for="Created" class="form-control" />
36+
<span asp-validation-for="Created" class="text-danger"></span>
37+
</div>
38+
<div class="form-group">
39+
<label asp-for="LastModifiedBy" class="control-label"></label>
40+
<input asp-for="LastModifiedBy" class="form-control" />
41+
<span asp-validation-for="LastModifiedBy" class="text-danger"></span>
42+
</div>
43+
<div class="form-group">
44+
<label asp-for="LastModified" class="control-label"></label>
45+
<input asp-for="LastModified" class="form-control" />
46+
<span asp-validation-for="LastModified" class="text-danger"></span>
47+
</div>
48+
<input type="hidden" asp-for="Id" />
49+
<div class="form-group">
50+
<input type="submit" value="Create" class="btn btn-default" />
51+
</div>
52+
</form>
53+
</div>
54+
</div>

Web/Views/Products/Details.cshtml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
@model Domain.Entities.Product
2+
@{
3+
ViewData["Title"] = "Details";
4+
}
5+
6+
<h2>Details</h2>
7+
8+
<div>
9+
<h4>Note</h4>
10+
<hr />
11+
<dl class="dl-horizontal">
12+
<dt>
13+
@Html.DisplayNameFor(model => model.Name)
14+
</dt>
15+
<dd>
16+
@Html.DisplayFor(model => model.Name)
17+
</dd>
18+
<dt>
19+
@Html.DisplayNameFor(model => model.Barcode)
20+
</dt>
21+
<dd>
22+
@Html.DisplayFor(model => model.Barcode)
23+
</dd>
24+
<dt>
25+
@Html.DisplayNameFor(model => model.Description)
26+
</dt>
27+
<dd>
28+
@Html.DisplayFor(model => model.Description)
29+
</dd>
30+
<dt>
31+
@Html.DisplayNameFor(model => model.CreatedBy)
32+
</dt>
33+
<dd>
34+
@Html.DisplayFor(model => model.CreatedBy)
35+
</dd>
36+
<dt>
37+
@Html.DisplayNameFor(model => model.Created)
38+
</dt>
39+
<dd>
40+
@Html.DisplayFor(model => model.Created)
41+
</dd>
42+
<dt>
43+
@Html.DisplayNameFor(model => model.LastModifiedBy)
44+
</dt>
45+
<dd>
46+
@Html.DisplayFor(model => model.LastModifiedBy)
47+
</dd>
48+
<dt>
49+
@Html.DisplayNameFor(model => model.LastModified)
50+
</dt>
51+
<dd>
52+
@Html.DisplayFor(model => model.LastModified)
53+
</dd>
54+
</dl>
55+
</div>
56+
<div>
57+
<a asp-action="Edit" asp-route-id="@Model.Id">Edit</a> |
58+
<a asp-action="Index">Back to List</a>
59+
</div>

Web/Views/Products/Edit.cshtml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
@model Domain.Entities.Product
2+
@{
3+
ViewData["Title"] = "Edit";
4+
}
5+
6+
<h2>Edit</h2>
7+
8+
<h4>Note</h4>
9+
<hr />
10+
<div class="row">
11+
<div class="col-md-4">
12+
<form asp-action="Edit">
13+
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
14+
<input type="hidden" asp-for="Id" />
15+
<div class="form-group">
16+
<label asp-for="Name" class="control-label"></label>
17+
<input asp-for="Name" class="form-control" />
18+
<span asp-validation-for="Name" class="text-danger"></span>
19+
</div>
20+
<div class="form-group">
21+
<label asp-for="Barcode" class="control-label"></label>
22+
<input asp-for="Barcode" class="form-control" />
23+
<span asp-validation-for="Barcode" class="text-danger"></span>
24+
</div>
25+
<div class="form-group">
26+
<label asp-for="Description" class="control-label"></label>
27+
<input asp-for="Description" class="form-control" />
28+
<span asp-validation-for="Description" class="text-danger"></span>
29+
</div>
30+
<div class="form-group">
31+
<label asp-for="CreatedBy" class="control-label"></label>
32+
<input asp-for="CreatedBy" class="form-control" />
33+
<span asp-validation-for="CreatedBy" class="text-danger"></span>
34+
</div>
35+
<div class="form-group">
36+
<label asp-for="Created" class="control-label"></label>
37+
<input asp-for="Created" class="form-control" />
38+
<span asp-validation-for="Created" class="text-danger"></span>
39+
</div>
40+
<div class="form-group">
41+
<label asp-for="LastModifiedBy" class="control-label"></label>
42+
<input asp-for="LastModifiedBy" class="form-control" />
43+
<span asp-validation-for="LastModifiedBy" class="text-danger"></span>
44+
</div>
45+
<div class="form-group">
46+
<label asp-for="LastModified" class="control-label"></label>
47+
<input asp-for="LastModified" class="form-control" />
48+
<span asp-validation-for="LastModified" class="text-danger"></span>
49+
</div>
50+
<div class="form-group">
51+
<input type="submit" value="Save" class="btn btn-default" />
52+
</div>
53+
</form>
54+
</div>
55+
</div>
56+
57+
<div>
58+
<a asp-action="Index">Back to List</a>
59+
</div>

Web/Views/Products/Index.cshtml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
@model IEnumerable<Domain.Entities.Product>
2+
@{
3+
ViewData["Title"] = "Index";
4+
}
5+
6+
<h3>Products</h3>
7+
<hr />
8+
<p>
9+
<a asp-action="Create">Create Product</a>
10+
</p>
11+
<table class="table">
12+
<thead>
13+
<tr>
14+
<th>
15+
@Html.DisplayNameFor(model => model.Name)
16+
</th>
17+
<th>
18+
@Html.DisplayNameFor(model => model.Barcode)
19+
</th>
20+
<th>
21+
@Html.DisplayNameFor(model => model.Description)
22+
</th>
23+
24+
<th></th>
25+
</tr>
26+
</thead>
27+
<tbody>
28+
@foreach (var item in Model)
29+
{
30+
<tr>
31+
<td>
32+
@Html.DisplayFor(modelItem => item.Name)
33+
</td>
34+
<td>
35+
@Html.DisplayFor(modelItem => item.Barcode)
36+
</td>
37+
<td>
38+
@Html.DisplayFor(modelItem => item.Description)
39+
</td>
40+
41+
<td>
42+
<a asp-action="Details" asp-route-id="@item.Id">Details</a> |
43+
<a asp-action="Edit" asp-route-id="@item.Id">Edit</a> |
44+
<a asp-action="Delete" asp-route-id="@item.Id" onclick="return confirm('Are sure wants to delete?');">Delete</a>
45+
</td>
46+
</tr>
47+
}
48+
</tbody>
49+
</table>

Web/Views/Shared/_Layout.cshtml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
<li class="nav-item">
2222
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Home</a>
2323
</li>
24+
<li class="nav-item">
25+
<a class="nav-link text-dark" asp-area="" asp-controller="Products" asp-action="Index">Products</a>
26+
</li>
2427
<li class="nav-item">
2528
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
2629
</li>

Web/Web.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1111
</PackageReference>
1212
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.11" />
13+
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.4" />
1314
</ItemGroup>
1415

1516
<ItemGroup>
1617
<ProjectReference Include="..\Application\Application.csproj" />
18+
<ProjectReference Include="..\Domain\Domain.csproj" />
1719
<ProjectReference Include="..\Infrastructure.Persistence\Infrastructure.Persistence.csproj" />
1820
<ProjectReference Include="..\Infrastructure.Service\Infrastructure.Service.csproj" />
1921
</ItemGroup>

0 commit comments

Comments
 (0)