Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
iracleous committed Nov 14, 2020
1 parent 745a271 commit f2be572
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 68 deletions.
4 changes: 3 additions & 1 deletion Crm-Core/Models/Order.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<OrderProduct> OrderProducts { get; set; }
}
}
18 changes: 15 additions & 3 deletions Crm-Core/Services/CustomerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class CustomerService : ICustomerService

public CustomerOptions CreateCustomer(CustomerOptions customerOptions)
{
//validation

Customer customer = new Customer {
FirstName = customerOptions.FirstName,
LastName = customerOptions.LastName,
Expand Down Expand Up @@ -63,10 +65,9 @@ public List<CustomerOptions> 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
Expand All @@ -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);
Expand All @@ -103,6 +114,7 @@ public CustomerOptions GetCustomerById(int id)
VatNumber = customer.VatNumber,
Address = customer.Address,
Dob = customer.Dob,
Id = customer.Id + ""
};
}
}
Expand Down
22 changes: 8 additions & 14 deletions Ms-App-Crm/Controllers/CustomerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}
}
}
11 changes: 9 additions & 2 deletions Ms-App-Crm/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Microsoft_Azure_Academy.Controllers
public class HomeController : Controller
{
private readonly ILogger<HomeController> _logger;

private ICustomerService customerService = new CustomerService();

public HomeController(ILogger<HomeController> logger)
{
Expand All @@ -42,14 +42,21 @@ 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();
}

public IActionResult Customers()
{
ICustomerService customerService = new CustomerService();

List<CustomerOptions> customers = customerService.GetAllCustomers();
CustomerModel customersModel = new CustomerModel {
Customers= customers
Expand Down
6 changes: 6 additions & 0 deletions Ms-App-Crm/Models/CustomerModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,10 @@ public class CustomerModel
{
public List<CustomerOptions> Customers { get; set; }
}


public class CustomerOptionModel
{
public CustomerOptions customer { get; set; }
}
}
10 changes: 5 additions & 5 deletions Ms-App-Crm/Views/Home/AddCustomer.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
<td>
FirstName
</td>
<td> <input type="text" id="FirstName" /></td>
<td> <input type="text" id="FirstName" placeholder="FirstName"/></td>
</tr>

<tr align="center">
<td>
LastName
</td>
<td>
<input type="text" id="LastName" />
<input type="text" id="LastName" placeholder="LastName"/>
</td>
</tr>

Expand All @@ -27,15 +27,15 @@
Address
</td>
<td>
<input type="text" id="Address" />
<input type="text" id="Address" placeholder="Address"/>
</td>
</tr>
<tr align="center">
<td>
Email
</td>
<td>
<input type="text" id="Email" />
<input type="text" id="Email" placeholder="Email"/>

</td>
</tr>
Expand All @@ -45,7 +45,7 @@
VatNumber
</td>
<td>
<input type="text" id="VatNumber" />
<input type="text" id="VatNumber" placeholder="VatNumber"/>
</td>
</tr>
<tr align="center">
Expand Down
44 changes: 1 addition & 43 deletions Ms-App-Crm/Views/Home/UpdateCustomer.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -17,49 +17,7 @@

<tr align="center">
<td>
FirstName
</td>
<td> <input type="text" id="FirstName" /></td>
</tr>

<tr align="center">
<td>
LastName
</td>
<td>
<input type="text" id="LastName" />
</td>
</tr>

<tr align="center">
<td>
Address
</td>
<td>
<input type="text" id="Address" />
</td>
</tr>
<tr align="center">
<td>
Email
</td>
<td>
<input type="text" id="Email" />

</td>
</tr>
<tr align="center">
<td>

VatNumber
</td>
<td>
<input type="text" id="VatNumber" />
</td>
</tr>
<tr align="center">
<td>
<input type="button" value="Submit data" onclick="updateCustomer()" />
<input type="button" value="Submit data" onclick="findToUpdateCustomer()" />
</td>
</tr>
</table>
Expand Down
70 changes: 70 additions & 0 deletions Ms-App-Crm/Views/Home/UpdateCustomerWithDetails.cshtml
Original file line number Diff line number Diff line change
@@ -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
*@
@{
}


<h1> Update Customer code = @Model.customer.Id </h1>

<form>
<table align="center">
<tr align="center">
<td>
Id
</td>
<td> <input type="text" id="Id" value="@Model.customer.Id" readonly/></td>
</tr>


<tr align="center">
<td>
FirstName
</td>
<td> <input type="text" id="FirstName" value="@Model.customer.FirstName" /></td>
</tr>

<tr align="center">
<td>
LastName
</td>
<td>
<input type="text" id="LastName" value="@Model.customer.LastName" />
</td>
</tr>

<tr align="center">
<td>
Address
</td>
<td>
<input type="text" id="Address" value="@Model.customer.Address" />
</td>
</tr>
<tr align="center">
<td>
Email
</td>
<td>
<input type="text" id="Email" value="@Model.customer.Email" />

</td>
</tr>
<tr align="center">
<td>

VatNumber
</td>
<td>
<input type="text" id="VatNumber" value="@Model.customer.VatNumber" />
</td>
</tr>
<tr align="center">
<td>
<input type="button" value="Submit data" onclick="updateCustomer()" />
</td>
</tr>

</table>
</form>
13 changes: 13 additions & 0 deletions Ms-App-Crm/wwwroot/js/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -106,4 +108,15 @@ function deleteCustomer() {



}



function findToUpdateCustomer() {

id = $("#Id").val()
actionUrl = "/home/UpdateCustomerWithDetails/" + id

window.open(actionUrl, "_self");

}

0 comments on commit f2be572

Please sign in to comment.