Skip to content
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ $RECYCLE.BIN/
*.user
*.userosscache
*.sln.docstates
*appsettings.json

# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs
Expand Down
13 changes: 10 additions & 3 deletions Testing.sln
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
77 changes: 77 additions & 0 deletions Testing/Controllers/ProductController.cs
Original file line number Diff line number Diff line change
@@ -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: /<controller>/
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");
}



}
}
19 changes: 19 additions & 0 deletions Testing/IProductRepository.cs
Original file line number Diff line number Diff line change
@@ -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<Product> GetAllProducts();
public Product GetProduct(int Key);
public void UpdateProduct(Product product);
public void InsertProduct(Product productToInsert);
public IEnumerable<Category> GetCategories();
public Product AssignCategory();
public void DeleteProduct(Product product);
}
}
14 changes: 14 additions & 0 deletions Testing/Models/Category.cs
Original file line number Diff line number Diff line change
@@ -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; }

}
}
23 changes: 23 additions & 0 deletions Testing/Models/Product.cs
Original file line number Diff line number Diff line change
@@ -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<Category> Categories { get; set; }
}
}
68 changes: 68 additions & 0 deletions Testing/ProductRepository.cs
Original file line number Diff line number Diff line change
@@ -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<Product> GetAllProducts()
{
return _conn.Query<Product>("SELECT * FROM adventureworks_products;");
}

public Product GetProduct(int id)
{
return _conn.QuerySingle<Product>("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<Category> GetCategories()
{
return _conn.Query<Category>("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 });
}


}
}
11 changes: 11 additions & 0 deletions Testing/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
Expand All @@ -8,6 +9,7 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using MySql.Data.MySqlClient;

namespace Testing
{
Expand All @@ -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<IDbConnection>((s) =>
{
IDbConnection conn = new MySqlConnection(Configuration.GetConnectionString("adventure_works"));
conn.Open();
return conn;
});

services.AddTransient<IProductRepository, ProductRepository>();

services.AddControllersWithViews();
}

Expand Down
5 changes: 5 additions & 0 deletions Testing/Testing.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Dapper" Version="2.0.90" />
<PackageReference Include="MySql.Data" Version="8.0.25" />
</ItemGroup>



</Project>
2 changes: 1 addition & 1 deletion Testing/Views/Home/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
}

<div class="text-center">
<h1 class="display-4">Welcome</h1>
<h1 class="display-4">Welcome to the Adventure Works Internal Database</h1>
<p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
</div>
41 changes: 41 additions & 0 deletions Testing/Views/Product/Index.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
@model IEnumerable<Product>

<h2>Products</h2>

<div style="display: block">
<a href="/Product/InsertProduct/">Create a New Product</a>
</div>

<table class="table">
<tr>
<th>Key</th>
<th>SubcategoryKey</th>
<th>SKU</th>
<th>Name</th>
<th>ModelName</th>
<th>Description</th>
<th>Color</th>
<th>Size</th>
<th>Style</th>
<th>Cost</th>
<th>Price</th>
</tr>

@foreach (var product in Model)
{
<tr>
<td><a href=/Product/ViewProduct/@product.ProductKey>@product.ProductKey</a></td>
<td>@product.ProductSubcategoryKey</td>
<td>@product.ProductSKU</td>
<td>@product.ProductName</td>
<td>@product.ModelName</td>
<td>@product.ProductDescription</td>
<td>@product.ProductColor</td>
<td>@product.ProductSize</td>
<td>@product.ProductStyle</td>
<td>@product.ProductCost</td>
<td>@product.ProductPrice</td>
</tr>

}
</table>
20 changes: 20 additions & 0 deletions Testing/Views/Product/InsertProduct.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
@model Product
<h2>Create a New Product</h2>
@using (Html.BeginForm("InsertProductToDatabase", "Product", "Post"))
{
<input asp-for="ProductKey" type="hidden" value="" />
<label asp-for="ProductName" class="control-label">Name</label>
<input type="text" name="Name" class="form-control" value="" />
<label asp-for="ProductPrice" class="control-label">Price</label>
<input type="number" step="0.01" name="Price" class="form-control" />
<div class="form-group">
<label for="">Select a Category</label>
<select class="form-control" name="CategoryID" value="">
@foreach (var cat in Model.Categories)
{
<option value="@cat.ProductName">@cat.ProductName @cat.ProductName</option>
}
</select>
</div>
<input type="submit" value="Create Product" class="btn btn-primary" />
}
17 changes: 17 additions & 0 deletions Testing/Views/Product/UpdateProduct.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
@model Product

<h2>Update @Model.ProductName</h2>

@using (Html.BeginForm("UpdateProductToDatabase", "Product", "Post"))
{
<input asp-for="ProductKey" type="hidden" value="@Model.ProductKey" />

<label asp-for="ProductName" class="control-label">Name</label>
<input type="text" name="Name" class="form-control" value="@Model.ProductName" />

<label asp-for="ProductPrice" class="control-label">Price</label>
<input type="number" step="0.01" name="Price" class="form-control" value="@Model.ProductPrice" />

<input type="submit" value="Update Product" class="btn btn-primary" />

}
Loading