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.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 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..61f38a41 --- /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..48383c14 --- /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 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": "*" -}