Skip to content

Commit

Permalink
using DI
Browse files Browse the repository at this point in the history
  • Loading branch information
iracleous committed Nov 16, 2020
1 parent a9a0587 commit 9d47f3c
Show file tree
Hide file tree
Showing 15 changed files with 160 additions and 63 deletions.
2 changes: 1 addition & 1 deletion Crm-Core/Data/CrmAppDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class CrmAppDbContext:DbContext
public DbSet<Order> Orders { get; set; }
public DbSet<OrderProduct> OrderProducts { get; set; }

private readonly string connectionString =
public readonly static string connectionString =
"Server =localhost; " +
"Database =crm; " +
"User Id =sa; " +
Expand Down
8 changes: 7 additions & 1 deletion Crm-Core/Services/CustomerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ namespace ModelCrm.Services
{
public class CustomerService : ICustomerService
{
private readonly CrmAppDbContext dbContext = new CrmAppDbContext();
private readonly CrmAppDbContext dbContext;

public CustomerService(CrmAppDbContext dbContext)
{
this.dbContext = dbContext;
}


public CustomerOptions CreateCustomer(CustomerOptions customerOptions)
{
Expand Down
7 changes: 6 additions & 1 deletion Crm-Core/Services/OrderService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ namespace ModelCrm.Services
{
public class OrderService : IOrderService
{
private readonly CrmAppDbContext dbContext = new CrmAppDbContext();
private readonly CrmAppDbContext dbContext;
public OrderService(CrmAppDbContext dbContext)
{
this.dbContext = dbContext;
}


public OrderOption CreateOrder(CustomerOptions customerOpt)
{
Expand Down
84 changes: 46 additions & 38 deletions Crm-Core/Services/ProductService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ namespace ModelCrm.Services
{
public class ProductService : IProductService
{
private readonly CrmAppDbContext dbContext = new CrmAppDbContext();

private readonly CrmAppDbContext dbContext ;
public ProductService(CrmAppDbContext dbContext)
{
this.dbContext = dbContext;
}

public bool DeleteProduct(int id)
{
Expand All @@ -23,14 +26,37 @@ public bool DeleteProduct(int id)
return true;
}


public Product GetProductById(int id)

ProductOptions IProductService.CreateProduct(ProductOptions productOptions)
{
Product product = GetProductFromProductOptions(productOptions);

dbContext.Products.Add(product);
dbContext.SaveChanges();
productOptions.Id = product.Id;
return productOptions;
}

List<ProductOptions> IProductService.GetAllProduct()
{
List<Product> products= dbContext.Products.ToList();
List<ProductOptions> productOpts = new List<ProductOptions>();
products.ForEach(product => productOpts.Add(
GetProductOptionsFromProduct(product)
));
return productOpts;
}

ProductOptions IProductService.GetProductById(int id)
{
return dbContext.Products.Find(id);
Product product = dbContext.Products.Find(id);
if (product == null) return null;
return GetProductOptionsFromProduct(product) ;
}

public Product UpdateProduct(ProductOptions productOption, int id)
ProductOptions IProductService.UpdateProduct(ProductOptions productOption, int id)
{

Product product = dbContext.Products.Find(id);
product.Code = productOption.Code;
product.Description = productOption.Description;
Expand All @@ -40,51 +66,33 @@ public Product UpdateProduct(ProductOptions productOption, int id)

dbContext.SaveChanges();

return product;
return productOption;
}

ProductOptions IProductService.CreateProduct(ProductOptions productOptions)
{
Product product = new Product
{
Code = productOptions.Code,
Description = productOptions.Description,
Name = productOptions.Name,
Price = productOptions.Price,
Quantity = productOptions.Quantity
};

dbContext.Products.Add(product);
dbContext.SaveChanges();
productOptions.Id = product.Id;
return productOptions;
}

List<ProductOptions> IProductService.GetAllProduct()
private static ProductOptions GetProductOptionsFromProduct(Product product)
{
List<Product> products= dbContext.Products.ToList();
List<ProductOptions> productOpts = new List<ProductOptions>();
products.ForEach(product => productOpts.Add(new ProductOptions {
return new ProductOptions
{
Code = product.Code,
Description = product.Description,
Name = product.Name,
Price = product.Price,
Quantity = product.Quantity,
Id = product.Id


}));
return productOpts;
}

ProductOptions IProductService.GetProductById(int id)
{
throw new NotImplementedException();
};
}

ProductOptions IProductService.UpdateProduct(ProductOptions productOption, int id)
private static Product GetProductFromProductOptions(ProductOptions productOptions)
{
throw new NotImplementedException();
return new Product
{
Code = productOptions.Code,
Description = productOptions.Description,
Name = productOptions.Name,
Price = productOptions.Price,
Quantity = productOptions.Quantity,
};
}
}
}
7 changes: 5 additions & 2 deletions Ms-App-Crm/Controllers/CustomerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ namespace Microsoft_Azure_Academy.Controllers
[ApiController]
public class CustomerController : ControllerBase
{
private ICustomerService customerService = new CustomerService();

private ICustomerService customerService ;
public CustomerController(ICustomerService customerService)
{
this.customerService = customerService;
}
[HttpPost]
public CustomerOptions AddCustomer(CustomerOptions customerOpt)
{
Expand Down
4 changes: 0 additions & 4 deletions Ms-App-Crm/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@ public IActionResult Products()
return View(productModel);
}





public IActionResult AddCustomer()
{
return View();
Expand Down
6 changes: 5 additions & 1 deletion Ms-App-Crm/Controllers/OrderController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ namespace CrmWebApi.Controllers
public class OrderController : ControllerBase
{

public IOrderService orderService = new OrderService();
public IOrderService orderService;
public OrderController(IOrderService orderService)
{
this.orderService = orderService;
}

[HttpGet("{id}")]
public OrderOption getOrder(int id)
Expand Down
6 changes: 5 additions & 1 deletion Ms-App-Crm/Controllers/ProductController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ namespace CrmWebApi.Controllers
[ApiController]
public class ProductController : ControllerBase
{
private readonly IProductService productService = new ProductService();
private readonly IProductService productService ;

public ProductController(IProductService productService)
{
this.productService = productService;
}
[HttpPost]
public ProductOptions CreateProduct(ProductOptions ProductOption)
{
Expand Down
11 changes: 11 additions & 0 deletions Ms-App-Crm/Models/OrderModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Ms_App_Crm.Models
{
public class OrderModel
{
}
}
4 changes: 4 additions & 0 deletions Ms-App-Crm/Ms-App-Crm.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@
<ProjectReference Include="..\Crm-Core\Crm-Core.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="Views\Order\" />
</ItemGroup>

</Project>
6 changes: 5 additions & 1 deletion Ms-App-Crm/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using ModelCrm.CrmDbContext;
using ModelCrm.Services;

namespace Ms_App_Crm
Expand All @@ -23,9 +25,11 @@ 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.AddDbContext<CrmAppDbContext>(options => options.UseSqlServer(CrmAppDbContext.connectionString));

services.AddScoped<ICustomerService, CustomerService>();
services.AddScoped<IProductService, ProductService>();

services.AddScoped<IOrderService, OrderService>();
services.AddControllersWithViews();
}

Expand Down
6 changes: 3 additions & 3 deletions Ms-App-Crm/Views/Home/Customers.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ To add a new customer press <a href="~/home/addcustomer">here</a>



<table id="customerTable" border="1" cellpadding="0" cellspacing="0">
<table id="customerTable" border="1" cellpadding="10" cellspacing="0">
<tr>
<th> Code</th>
<th> Full Name</th>
Expand All @@ -30,8 +30,8 @@ To add a new customer press <a href="~/home/addcustomer">here</a>
<td> @customer.FirstName @customer.LastName</td>
<td> @customer.Address </td>
<td> @customer.Email </td>
<td> <a href="~/home/updatecustomerwithdetails/@customer.Id"><img src="~/images/update.png" width="50" /></a></td>
<td> <a href="~/home/DeleteCustomerFromView/@customer.Id"><img src="~/images/delete.png" width="50" /></a></td>
<td> <a href="~/home/updatecustomerwithdetails/@customer.Id"><img src="~/images/update.png" width="25" /></a></td>
<td> <a href="~/home/DeleteCustomerFromView/@customer.Id"><img src="~/images/delete.png" width="25" /></a></td>
</tr>

}
Expand Down
6 changes: 3 additions & 3 deletions Ms-App-Crm/Views/Home/Products.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ To add a new product press <a href="~/home/addproduct">here</a>



<table id="productTable" border="1" cellpadding="0" cellspacing="0">
<table id="productTable" border="1" cellpadding="10" cellspacing="0">
<tr>
<th> Code</th>
<th> Name</th>
Expand All @@ -35,8 +35,8 @@ To add a new product press <a href="~/home/addproduct">here</a>
<td> @product.Description </td>
<td> @product.Price </td>
<td> @product.Quantity </td>
<td> <a href="~/home/updateProductwithdetails/@product.Id"><img src="~/images/update.png" width="50" /></a></td>
<td> <a href="~/home/DeleteProductFromView/@product.Id"><img src="~/images/delete.png" width="50" /></a></td>
<td> <a href="~/home/updateProductwithdetails/@product.Id"><img src="~/images/update.png" width="25" /></a></td>
<td> <a href="~/home/DeleteProductFromView/@product.Id"><img src="~/images/delete.png" width="25" /></a></td>
</tr>

}
Expand Down
56 changes: 56 additions & 0 deletions Ms-App-Crm/Views/Shared/_OrdrerLayout.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>@ViewData["Title"] - Microsoft_Azure_Academy</title>
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
<link rel="stylesheet" href="~/css/site.css" />
</head>
<body>
<header>
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
<div class="container">
<a class="navbar-brand" asp-area="" asp-controller="Home" asp-action="Index">Microsoft_Azure_Academy</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-controls="navbarSupportedContent"
aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="navbar-collapse collapse d-sm-inline-flex flex-sm-row-reverse">
<ul class="navbar-nav flex-grow-1">

<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Order" asp-action="CreateOrder">Create Order</a>
</li>

<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Order" asp-action="ViewOrders">View Orders</a>
</li>

<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Return</a>
</li>


</ul>
</div>
</div>
</nav>
</header>
<div class="container">
<main role="main" class="pb-3">
@RenderBody()
</main>
</div>

<footer class="border-top footer text-muted">
<div class="container">
&copy; 2020 - Microsoft_Azure_Academy - <a asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
</div>
</footer>
<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
<script src="~/js/site.js" asp-append-version="true"></script>
@RenderSection("Scripts", required: false)
</body>
</html>
10 changes: 3 additions & 7 deletions Ms-App-Crm/wwwroot/js/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ function addCustomer() {
success: function (data, textStatus, jQxhr) {

alert(JSON.stringify(data))
window.open("/home/customers", "_self")
},
error: function (jqXhr, textStatus, errorThrown) {
alert(errorThrown);
Expand Down Expand Up @@ -67,7 +68,7 @@ function updateCustomer() {

alert(JSON.stringify(data))

window.open("/","_self")
window.open("/home/customers","_self")
},
error: function (jqXhr, textStatus, errorThrown) {
alert(errorThrown);
Expand All @@ -86,9 +87,7 @@ function deleteCustomer() {
actiontype = "DELETE"
actionDataType = "json"



$.ajax({
$.ajax({
url: actionUrl,
dataType: actionDataType,
type: actiontype,
Expand All @@ -105,9 +104,6 @@ function deleteCustomer() {
}

});



}


Expand Down

0 comments on commit 9d47f3c

Please sign in to comment.