From f2be572702b069dad5330a6b8c064555fef12c17 Mon Sep 17 00:00:00 2001 From: Dimitris Date: Sat, 14 Nov 2020 12:03:07 +0200 Subject: [PATCH] refactoring --- Crm-Core/Models/Order.cs | 4 +- Crm-Core/Services/CustomerService.cs | 18 ++++- Ms-App-Crm/Controllers/CustomerController.cs | 22 +++--- Ms-App-Crm/Controllers/HomeController.cs | 11 ++- Ms-App-Crm/Models/CustomerModel.cs | 6 ++ Ms-App-Crm/Views/Home/AddCustomer.cshtml | 10 +-- Ms-App-Crm/Views/Home/UpdateCustomer.cshtml | 44 +----------- .../Home/UpdateCustomerWithDetails.cshtml | 70 +++++++++++++++++++ Ms-App-Crm/wwwroot/js/site.js | 13 ++++ 9 files changed, 130 insertions(+), 68 deletions(-) create mode 100644 Ms-App-Crm/Views/Home/UpdateCustomerWithDetails.cshtml diff --git a/Crm-Core/Models/Order.cs b/Crm-Core/Models/Order.cs index 240bb03..cf8b7ef 100644 --- a/Crm-Core/Models/Order.cs +++ b/Crm-Core/Models/Order.cs @@ -7,9 +7,11 @@ namespace ModelCrm.Models public class Order { public int Id { get; set; } - public Customer Customer { get; set; } + public string DeliveryAddress { get; set; } public decimal TotalAmount { get; set; } + + public Customer Customer { get; set; } public List OrderProducts { get; set; } } } diff --git a/Crm-Core/Services/CustomerService.cs b/Crm-Core/Services/CustomerService.cs index a9a0222..391e7b3 100644 --- a/Crm-Core/Services/CustomerService.cs +++ b/Crm-Core/Services/CustomerService.cs @@ -14,6 +14,8 @@ public class CustomerService : ICustomerService public CustomerOptions CreateCustomer(CustomerOptions customerOptions) { + //validation + Customer customer = new Customer { FirstName = customerOptions.FirstName, LastName = customerOptions.LastName, @@ -63,10 +65,9 @@ public List GetAllCustomers() public CustomerOptions UpdateCustomer(CustomerOptions customerOpt, int id) { - + Customer customer = dbContext.Customers.Find(id); - customer.Address = customerOpt.Address; - customer.Email =customerOpt.Email; + customerOptToCustomer(customerOpt, customer); dbContext.SaveChanges(); return new CustomerOptions @@ -81,6 +82,16 @@ public CustomerOptions UpdateCustomer(CustomerOptions customerOpt, int id) }; } + private static void customerOptToCustomer(CustomerOptions customerOpt, Customer customer) + { + customer.FirstName = customerOpt.FirstName; + customer.LastName = customerOpt.LastName; + customer.Dob = customerOpt.Dob; + customer.VatNumber = customerOpt.VatNumber; + customer.Address = customerOpt.Address; + customer.Email = customerOpt.Email; + } + public bool DeleteCustomer(int id) { Customer customer = dbContext.Customers.Find(id); @@ -103,6 +114,7 @@ public CustomerOptions GetCustomerById(int id) VatNumber = customer.VatNumber, Address = customer.Address, Dob = customer.Dob, + Id = customer.Id + "" }; } } diff --git a/Ms-App-Crm/Controllers/CustomerController.cs b/Ms-App-Crm/Controllers/CustomerController.cs index 72d92c5..794d4e4 100644 --- a/Ms-App-Crm/Controllers/CustomerController.cs +++ b/Ms-App-Crm/Controllers/CustomerController.cs @@ -14,33 +14,27 @@ namespace Microsoft_Azure_Academy.Controllers [ApiController] public class CustomerController : ControllerBase { + private ICustomerService customerService = new CustomerService(); + [HttpPost] public CustomerOptions AddCustomer(CustomerOptions customerOpt) { - ICustomerService customerService = new CustomerService(); - CustomerOptions customerOptions = customerService.CreateCustomer( customerOpt); - - + CustomerOptions customerOptions = customerService.CreateCustomer(customerOpt); return customerOptions; } - - + + [HttpPut("{id}")] -public CustomerOptions UpdateCustomer(int id, CustomerOptions customerOpt) + public CustomerOptions UpdateCustomer(int id, CustomerOptions customerOpt) { - - ICustomerService customerService = new CustomerService(); return customerService.UpdateCustomer(customerOpt, id); - + } [HttpDelete("{id}")] - public bool DeleteCustomer(int id ) + public bool DeleteCustomer(int id) { - - ICustomerService customerService = new CustomerService(); return customerService.DeleteCustomer(id); - } } } diff --git a/Ms-App-Crm/Controllers/HomeController.cs b/Ms-App-Crm/Controllers/HomeController.cs index e985d83..835ba76 100644 --- a/Ms-App-Crm/Controllers/HomeController.cs +++ b/Ms-App-Crm/Controllers/HomeController.cs @@ -15,7 +15,7 @@ namespace Microsoft_Azure_Academy.Controllers public class HomeController : Controller { private readonly ILogger _logger; - + private ICustomerService customerService = new CustomerService(); public HomeController(ILogger logger) { @@ -42,6 +42,13 @@ public IActionResult UpdateCustomer() return View(); } + public IActionResult UpdateCustomerWithDetails([FromRoute] int id) + { + CustomerOptions customerOptions = customerService.GetCustomerById(id); + return View(new CustomerOptionModel { customer = customerOptions }); + } + + public IActionResult DeleteCustomer() { return View(); @@ -49,7 +56,7 @@ public IActionResult DeleteCustomer() public IActionResult Customers() { - ICustomerService customerService = new CustomerService(); + List customers = customerService.GetAllCustomers(); CustomerModel customersModel = new CustomerModel { Customers= customers diff --git a/Ms-App-Crm/Models/CustomerModel.cs b/Ms-App-Crm/Models/CustomerModel.cs index 7a83373..601461d 100644 --- a/Ms-App-Crm/Models/CustomerModel.cs +++ b/Ms-App-Crm/Models/CustomerModel.cs @@ -9,4 +9,10 @@ public class CustomerModel { public List Customers { get; set; } } + + + public class CustomerOptionModel + { + public CustomerOptions customer { get; set; } + } } diff --git a/Ms-App-Crm/Views/Home/AddCustomer.cshtml b/Ms-App-Crm/Views/Home/AddCustomer.cshtml index 32f504a..0ac48f0 100644 --- a/Ms-App-Crm/Views/Home/AddCustomer.cshtml +++ b/Ms-App-Crm/Views/Home/AddCustomer.cshtml @@ -10,7 +10,7 @@ FirstName - + @@ -18,7 +18,7 @@ LastName - + @@ -27,7 +27,7 @@ Address - + @@ -35,7 +35,7 @@ Email - + @@ -45,7 +45,7 @@ VatNumber - + diff --git a/Ms-App-Crm/Views/Home/UpdateCustomer.cshtml b/Ms-App-Crm/Views/Home/UpdateCustomer.cshtml index 6b73e74..c49530b 100644 --- a/Ms-App-Crm/Views/Home/UpdateCustomer.cshtml +++ b/Ms-App-Crm/Views/Home/UpdateCustomer.cshtml @@ -17,49 +17,7 @@ - FirstName - - - - - - - LastName - - - - - - - - - Address - - - - - - - - Email - - - - - - - - - - VatNumber - - - - - - - - + diff --git a/Ms-App-Crm/Views/Home/UpdateCustomerWithDetails.cshtml b/Ms-App-Crm/Views/Home/UpdateCustomerWithDetails.cshtml new file mode 100644 index 0000000..1011811 --- /dev/null +++ b/Ms-App-Crm/Views/Home/UpdateCustomerWithDetails.cshtml @@ -0,0 +1,70 @@ +@model Microsoft_Azure_Academy.Models.CustomerOptionModel +@* + For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 +*@ +@{ +} + + +

Update Customer code = @Model.customer.Id

+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ Id +
+ FirstName +
+ LastName + + +
+ Address + + +
+ Email + + + +
+ + VatNumber + + +
+ +
+
diff --git a/Ms-App-Crm/wwwroot/js/site.js b/Ms-App-Crm/wwwroot/js/site.js index 4dfa701..b98aecb 100644 --- a/Ms-App-Crm/wwwroot/js/site.js +++ b/Ms-App-Crm/wwwroot/js/site.js @@ -66,6 +66,8 @@ function updateCustomer() { success: function (data, textStatus, jQxhr) { alert(JSON.stringify(data)) + + window.open("/","_self") }, error: function (jqXhr, textStatus, errorThrown) { alert(errorThrown); @@ -106,4 +108,15 @@ function deleteCustomer() { +} + + + +function findToUpdateCustomer() { + + id = $("#Id").val() + actionUrl = "/home/UpdateCustomerWithDetails/" + id + + window.open(actionUrl, "_self"); + } \ No newline at end of file