From c5b8492d4fa30dc4f93f6aa84ce1f0ee587c5cfd Mon Sep 17 00:00:00 2001 From: Eber27 Date: Fri, 2 Jul 2021 20:49:04 -0500 Subject: [PATCH 1/5] deleted appsettings.json --- .gitignore | 1 + Testing/appsettings.Development.json | 9 --------- Testing/appsettings.json | 10 ---------- 3 files changed, 1 insertion(+), 19 deletions(-) delete mode 100644 Testing/appsettings.Development.json delete mode 100644 Testing/appsettings.json diff --git a/.gitignore b/.gitignore index ed9dbcb2..32fd0b8e 100644 --- a/.gitignore +++ b/.gitignore @@ -80,6 +80,7 @@ $RECYCLE.BIN/ *.user *.userosscache *.sln.docstates +*appsettings.json # User-specific files (MonoDevelop/Xamarin Studio) *.userprefs diff --git a/Testing/appsettings.Development.json b/Testing/appsettings.Development.json deleted file mode 100644 index 8983e0fc..00000000 --- a/Testing/appsettings.Development.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft": "Warning", - "Microsoft.Hosting.Lifetime": "Information" - } - } -} diff --git a/Testing/appsettings.json b/Testing/appsettings.json deleted file mode 100644 index d9d9a9bf..00000000 --- a/Testing/appsettings.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft": "Warning", - "Microsoft.Hosting.Lifetime": "Information" - } - }, - "AllowedHosts": "*" -} From 0017d9d88f4678e33f25b6c950eca35a46d14a89 Mon Sep 17 00:00:00 2001 From: Eber27 Date: Fri, 9 Jul 2021 14:09:15 -0500 Subject: [PATCH 2/5] adventure_works_proj --- Testing/Controllers/ProductController.cs | 77 ++++++++++++++++++++++ Testing/IProductRepository.cs | 19 ++++++ Testing/Models/Category.cs | 14 ++++ Testing/Models/Product.cs | 23 +++++++ Testing/ProductRepository.cs | 68 +++++++++++++++++++ Testing/Startup.cs | 11 ++++ Testing/Testing.csproj | 5 ++ Testing/Views/Home/Index.cshtml | 2 +- Testing/Views/Product/Index.cshtml | 41 ++++++++++++ Testing/Views/Product/InsertProduct.cshtml | 20 ++++++ Testing/Views/Product/UpdateProduct.cshtml | 17 +++++ Testing/Views/Product/ViewProduct.cshtml | 47 +++++++++++++ Testing/Views/Shared/_Layout.cshtml | 2 +- 13 files changed, 344 insertions(+), 2 deletions(-) create mode 100644 Testing/Controllers/ProductController.cs create mode 100644 Testing/IProductRepository.cs create mode 100644 Testing/Models/Category.cs create mode 100644 Testing/Models/Product.cs create mode 100644 Testing/ProductRepository.cs create mode 100644 Testing/Views/Product/Index.cshtml create mode 100644 Testing/Views/Product/InsertProduct.cshtml create mode 100644 Testing/Views/Product/UpdateProduct.cshtml create mode 100644 Testing/Views/Product/ViewProduct.cshtml diff --git a/Testing/Controllers/ProductController.cs b/Testing/Controllers/ProductController.cs new file mode 100644 index 00000000..5727711b --- /dev/null +++ b/Testing/Controllers/ProductController.cs @@ -0,0 +1,77 @@ +using Microsoft.AspNetCore.Mvc; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Testing.Models; + +namespace Testing.Controllers +{ + public class ProductController : Controller + { + private readonly IProductRepository repo; + + public ProductController(IProductRepository repo) + { + this.repo = repo; + } + + // GET: // + public IActionResult Index() + { + var products = repo.GetAllProducts(); + + return View(products); + } + + public IActionResult ViewProduct(int id) + { + var product = repo.GetProduct(id); + + return View(product); + } + + public IActionResult UpdateProduct(int id) + { + Product prod = repo.GetProduct(id); + + if (prod == null) + { + return View("ProductNotFound"); + } + + return View(prod); + } + + public IActionResult UpdateProductToDatabase(Product product) + { + repo.UpdateProduct(product); + + return RedirectToAction("ViewProduct", new { id = product.ProductKey }); + } + + public IActionResult InsertProduct() + { + var prod = repo.AssignCategory(); + + return View(prod); + } + + public IActionResult InsertProductToDatabase(Product productToInsert) + { + repo.InsertProduct(productToInsert); + + return RedirectToAction("Index"); + } + + public IActionResult DeleteProduct(Product product) + { + repo.DeleteProduct(product); + + return RedirectToAction("Index"); + } + + + + } +} diff --git a/Testing/IProductRepository.cs b/Testing/IProductRepository.cs new file mode 100644 index 00000000..53010948 --- /dev/null +++ b/Testing/IProductRepository.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Testing.Models; + +namespace Testing +{ + public interface IProductRepository + { + public IEnumerable GetAllProducts(); + public Product GetProduct(int Key); + public void UpdateProduct(Product product); + public void InsertProduct(Product productToInsert); + public IEnumerable GetCategories(); + public Product AssignCategory(); + public void DeleteProduct(Product product); + } +} diff --git a/Testing/Models/Category.cs b/Testing/Models/Category.cs new file mode 100644 index 00000000..49f6254a --- /dev/null +++ b/Testing/Models/Category.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Testing.Models +{ + public class Category + { + public int ProductSubcategoryKey { get; set; } + public string ProductName { get; set; } + + } +} diff --git a/Testing/Models/Product.cs b/Testing/Models/Product.cs new file mode 100644 index 00000000..bffe3527 --- /dev/null +++ b/Testing/Models/Product.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Testing.Models +{ + public class Product + { + public int ProductKey { get; set; } + public int ProductSubcategoryKey { get; set; } + public string ProductSKU { get; set; } + public string ProductName { get; set; } + public string ModelName { get; set; } + public string ProductDescription { get; set; } + public string ProductColor { get; set; } + public string ProductSize { get; set; } + public string ProductStyle { get; set; } + public double ProductCost { get; set; } + public double ProductPrice { get; set; } + public IEnumerable Categories { get; set; } + } +} diff --git a/Testing/ProductRepository.cs b/Testing/ProductRepository.cs new file mode 100644 index 00000000..c948c8c7 --- /dev/null +++ b/Testing/ProductRepository.cs @@ -0,0 +1,68 @@ +using Dapper; +using System; +using System.Collections.Generic; +using System.Data; +using System.Linq; +using System.Threading.Tasks; +using Testing.Models; + +namespace Testing +{ + public class ProductRepository : IProductRepository + { + private readonly IDbConnection _conn; + + public ProductRepository(IDbConnection conn) + { + _conn = conn; + } + + public IEnumerable GetAllProducts() + { + return _conn.Query("SELECT * FROM adventureworks_products;"); + } + + public Product GetProduct(int id) + { + return _conn.QuerySingle("SELECT * FROM adventureworks_products WHERE ProductKey = @id", + new { id = id }); + + } + + public void UpdateProduct(Product product) + { + _ = _conn.Execute("UPDATE adventureworks_products SET ProductName = @Name, ProductPrice = @Price WHERE ProductKey = @key", + new { name = product.ProductName, price = product.ProductPrice, key = product.ProductKey }); + } + + public void InsertProduct(Product productToInsert) + { + _conn.Execute("INSERT INTO adventureworks_products (NAME, PRICE, CATEGORYID) VALUES (@name, @price, @categoryID);", + new { name = productToInsert.ProductName, price = productToInsert.ProductPrice, categoryID = productToInsert.ProductKey }); + } + + public IEnumerable GetCategories() + { + return _conn.Query("SELECT * FROM adventureworks_territories;"); + } + + + public Product AssignCategory() + { + var categoryList = GetCategories(); + var product = new Product(); + product.Categories = categoryList; + + return product; + } + + public void DeleteProduct(Product product) + { + + _conn.Execute("DELETE FROM adventureworks_products WHERE ProductKey = @id;", + new { id = product.ProductKey }); + } + + + } +} diff --git a/Testing/Startup.cs b/Testing/Startup.cs index 5113a925..8f56faeb 100644 --- a/Testing/Startup.cs +++ b/Testing/Startup.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Data; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; @@ -8,6 +9,7 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; +using MySql.Data.MySqlClient; namespace Testing { @@ -23,6 +25,15 @@ public Startup(IConfiguration configuration) // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { + services.AddScoped((s) => + { + IDbConnection conn = new MySqlConnection(Configuration.GetConnectionString("adventure_works")); + conn.Open(); + return conn; + }); + + services.AddTransient(); + services.AddControllersWithViews(); } diff --git a/Testing/Testing.csproj b/Testing/Testing.csproj index c8fd6d13..956677ba 100644 --- a/Testing/Testing.csproj +++ b/Testing/Testing.csproj @@ -4,6 +4,11 @@ netcoreapp3.1 + + + + + diff --git a/Testing/Views/Home/Index.cshtml b/Testing/Views/Home/Index.cshtml index d2d19bdf..3841fa3a 100644 --- a/Testing/Views/Home/Index.cshtml +++ b/Testing/Views/Home/Index.cshtml @@ -3,6 +3,6 @@ }
-

Welcome

+

Welcome to the Adventure Works Internal Database

Learn about building Web apps with ASP.NET Core.

diff --git a/Testing/Views/Product/Index.cshtml b/Testing/Views/Product/Index.cshtml new file mode 100644 index 00000000..d7d7b275 --- /dev/null +++ b/Testing/Views/Product/Index.cshtml @@ -0,0 +1,41 @@ +@model IEnumerable + +

Products

+ + + + + + + + + + + + + + + + + + + @foreach (var product in Model) + { + + + + + + + + + + + + + + + } +
KeySubcategoryKeySKUNameModelNameDescriptionColorSizeStyleCostPrice
@product.ProductKey@product.ProductSubcategoryKey@product.ProductSKU@product.ProductName@product.ModelName@product.ProductDescription@product.ProductColor@product.ProductSize@product.ProductStyle@product.ProductCost@product.ProductPrice
diff --git a/Testing/Views/Product/InsertProduct.cshtml b/Testing/Views/Product/InsertProduct.cshtml new file mode 100644 index 00000000..669c77ac --- /dev/null +++ b/Testing/Views/Product/InsertProduct.cshtml @@ -0,0 +1,20 @@ +@model Product +

Create a New Product

+@using (Html.BeginForm("InsertProductToDatabase", "Product", "Post")) +{ + + + + + +
+ + +
+ +} diff --git a/Testing/Views/Product/UpdateProduct.cshtml b/Testing/Views/Product/UpdateProduct.cshtml new file mode 100644 index 00000000..4d5f7f55 --- /dev/null +++ b/Testing/Views/Product/UpdateProduct.cshtml @@ -0,0 +1,17 @@ +@model Product + +

Update @Model.ProductName

+ +@using (Html.BeginForm("UpdateProductToDatabase", "Product", "Post")) +{ + + + + + + + + + + +} diff --git a/Testing/Views/Product/ViewProduct.cshtml b/Testing/Views/Product/ViewProduct.cshtml new file mode 100644 index 00000000..ac35da1d --- /dev/null +++ b/Testing/Views/Product/ViewProduct.cshtml @@ -0,0 +1,47 @@ +@model Product + +

@Model.ProductName Information

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
KeySubcategoryKeySKUNameModelNameDescriptionColorSizeStyleCostPrice
@Model.ProductKey@Model.ProductSubcategoryKey@Model.ProductSKU@Model.ProductName@Model.ModelName@Model.ProductDescription@Model.ProductColor@Model.ProductSize@Model.ProductStyle@Model.ProductCost@Model.ProductPrice
+ + + +
+ @using (Html.BeginForm("DeleteProduct", "Product", "Post")) + { + + + } +
+ + diff --git a/Testing/Views/Shared/_Layout.cshtml b/Testing/Views/Shared/_Layout.cshtml index f25802a0..179da300 100644 --- a/Testing/Views/Shared/_Layout.cshtml +++ b/Testing/Views/Shared/_Layout.cshtml @@ -22,7 +22,7 @@ Home From 9eca84d8113eb6c6c101f841af7a842ea81212b5 Mon Sep 17 00:00:00 2001 From: Eber27 Date: Fri, 9 Jul 2021 14:28:11 -0500 Subject: [PATCH 3/5] AdeventureWorks_Proj --- Testing.sln | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/Testing.sln b/Testing.sln index f97ec138..970b19bf 100644 --- a/Testing.sln +++ b/Testing.sln @@ -1,7 +1,8 @@ - Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 15 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Testing", "Testing\Testing.csproj", "{B534AACF-405C-4D74-AC01-581AC811EDA1}" +# Visual Studio Version 16 +VisualStudioVersion = 16.0.30907.101 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Testing", "Testing\Testing.csproj", "{B534AACF-405C-4D74-AC01-581AC811EDA1}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -14,4 +15,10 @@ Global {B534AACF-405C-4D74-AC01-581AC811EDA1}.Release|Any CPU.ActiveCfg = Release|Any CPU {B534AACF-405C-4D74-AC01-581AC811EDA1}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {F2369E33-DF0B-4E04-B061-6CA8B185DEA6} + EndGlobalSection EndGlobal From 75b61ea27319a23c0f35140e87caf645daa7e3e0 Mon Sep 17 00:00:00 2001 From: Eber27 Date: Fri, 9 Jul 2021 14:33:10 -0500 Subject: [PATCH 4/5] adventureworks_proj --- Testing/ProductRepository.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Testing/ProductRepository.cs b/Testing/ProductRepository.cs index c948c8c7..61f38a41 100644 --- a/Testing/ProductRepository.cs +++ b/Testing/ProductRepository.cs @@ -24,7 +24,7 @@ public IEnumerable GetAllProducts() public Product GetProduct(int id) { - return _conn.QuerySingle("SELECT * FROM adventureworks_products WHERE ProductKey = @id", + return _conn.QuerySingle("SELECT * FROM adventureworks_products WHERE ProductKey = @id", new { id = id }); } From c196b308ce9512a0ae76128b97e26602556d2798 Mon Sep 17 00:00:00 2001 From: Eber27 Date: Fri, 9 Jul 2021 14:55:07 -0500 Subject: [PATCH 5/5] AdventureWorks --- Testing/Views/Product/InsertProduct.cshtml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Testing/Views/Product/InsertProduct.cshtml b/Testing/Views/Product/InsertProduct.cshtml index 669c77ac..48383c14 100644 --- a/Testing/Views/Product/InsertProduct.cshtml +++ b/Testing/Views/Product/InsertProduct.cshtml @@ -12,7 +12,7 @@