Skip to content

Commit de5d722

Browse files
committed
added create to productcontroller
1 parent b157cef commit de5d722

3 files changed

Lines changed: 20 additions & 24 deletions

File tree

Data/Contexts/ProductSqlContext.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ public void Update(Product entity)
5353
public Product GetById(int id)
5454
{
5555
return ExecuteQuery(
56-
"SELECT * FROM Product" +
57-
$"WHERE Id = {id}"
56+
$"SELECT * FROM Product WHERE Id = {id}"
5857
).First();
5958
}
6059

ShopNotch/Controllers/ProductsController.cs

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Microsoft.AspNetCore.Mvc;
1+
using Data.Models;
2+
using Microsoft.AspNetCore.Mvc;
23
using Logic;
34
using Logic.Interfaces;
45

@@ -8,9 +9,9 @@ public class ProductsController : Controller
89
{
910
private readonly ProductLogic _productLogic;
1011

11-
public ProductsController(ProductLogic productLogic)
12+
public ProductsController()
1213
{
13-
_productLogic = productLogic;
14+
_productLogic = new ProductLogic();
1415
}
1516

1617
// GET: Products
@@ -45,35 +46,30 @@ public IActionResult Create()
4546
// POST: Products/Create
4647
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
4748
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
48-
/*[HttpPost]
49+
[HttpPost]
4950
[ValidateAntiForgeryToken]
50-
public async Task<IActionResult> Create([Bind("Id,Name,Description,Price,Sku,Length,Width,Height")] Product product)
51+
public IActionResult Create([Bind("Id,Name,Description,Price,Sku,Length,Width,Height")] Product product)
5152
{
5253
if (ModelState.IsValid)
5354
{
54-
_context.Add(product);
55-
await _context.SaveChangesAsync();
55+
_productLogic.Add(product);
56+
5657
return RedirectToAction(nameof(Index));
5758
}
5859
return View(product);
59-
}*/
60+
}
6061

6162
// GET: Products/Edit/5
62-
/*public async Task<IActionResult> Edit(int? id)
63+
public IActionResult Edit(int? id)
6364
{
64-
if (id == null)
65-
{
66-
return NotFound();
67-
}
65+
if (id == null) { return NotFound(); }
66+
67+
var product = _productLogic.GetById((int)id);
68+
69+
if (product == null) { return NotFound(); }
6870

69-
var product = await _context.Product.FindAsync(id);
70-
if (product == null)
71-
{
72-
return NotFound();
73-
}
7471
return View(product);
7572
}
76-
*/
7773

7874
// POST: Products/Edit/5
7975
// To protect from overposting attacks, please enable the specific properties you want to bind to, for

ShopNotch/Startup.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,15 @@ public void ConfigureServices(IServiceCollection services)
3838
options.MinimumSameSitePolicy = SameSiteMode.None;
3939

4040
});
41+
4142
// services.AddScoped<ILogic, ProductLogic>();
42-
services.AddSingleton(typeof(ILogic<IEntity>), typeof(ProductLogic));
43+
// services.AddSingleton<ILogic<Product>>( new ProductLogic());
4344

4445
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
4546

4647
// var connection = @"Data Source=(localdb)\mssqllocaldb;AttachDbFilename=C:\Users\dtril\Documents\ShopNotch\TestDB.mdf;Initial Catalog=TestDb;Integrated Security=True";
47-
var connection = @"Data Source=mssql.fhict.local;Initial Catalog=dbi391176_elayed;Integrated Security=False;Persist Security Info=False;User ID=dbi391176_elayed;Password=testappels123;";
48-
services.AddDbContext<ShopNotchDbContext>(options => options.UseSqlServer(connection));
48+
//var connection = @"Data Source=mssql.fhict.local;Initial Catalog=dbi391176_elayed;Integrated Security=False;Persist Security Info=False;User ID=dbi391176_elayed;Password=testappels123;";
49+
//services.AddDbContext<ShopNotchDbContext>(options => options.UseSqlServer(connection));
4950
}
5051

5152
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

0 commit comments

Comments
 (0)