diff --git a/TechTest/AnyCompany.Tests/AnyCompany.Tests.csproj b/TechTest/AnyCompany.Tests/AnyCompany.Tests.csproj index b537fc2..f9c2f43 100644 --- a/TechTest/AnyCompany.Tests/AnyCompany.Tests.csproj +++ b/TechTest/AnyCompany.Tests/AnyCompany.Tests.csproj @@ -1,16 +1,20 @@ - + + + Debug AnyCPU - cd5d577e-bdc9-4dfc-ac6a-b1da474995f3 + {CD5D577E-BDC9-4DFC-AC6A-B1DA474995F3} Library Properties AnyCompany.Tests AnyCompany.Tests v4.6.1 512 + + true @@ -30,24 +34,52 @@ 4 - - - - - - - - - - - - - - + + ..\packages\EntityFramework.6.4.4\lib\net45\EntityFramework.dll + True + + + ..\packages\EntityFramework.6.4.4\lib\net45\EntityFramework.SqlServer.dll + True + + + ..\..\..\..\..\..\..\..\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\PublicAssemblies\Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll + + + ..\packages\NUnit.3.12.0\lib\net45\nunit.framework.dll + + + + + + + + + + + + + {c7e15594-7d8f-4c18-9dd7-14f3fbb1572d} + AnyCompany + + + + + + - + + + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. + + + + + + + \ No newline at end of file diff --git a/TechTest/AnyCompany.Tests/App.config b/TechTest/AnyCompany.Tests/App.config new file mode 100644 index 0000000..266e19d --- /dev/null +++ b/TechTest/AnyCompany.Tests/App.config @@ -0,0 +1,14 @@ + + + + +
+ + + + + + + \ No newline at end of file diff --git a/TechTest/AnyCompany.Tests/Class1.cs b/TechTest/AnyCompany.Tests/Class1.cs index 5957505..058093c 100644 --- a/TechTest/AnyCompany.Tests/Class1.cs +++ b/TechTest/AnyCompany.Tests/Class1.cs @@ -1,12 +1,74 @@ -using System; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using AnyCompany; + namespace AnyCompany.Tests { + [TestClass()] public class Class1 { + [TestMethod()] + public void AddCustomer() + { + Customer cust = new Customer + { + CustomerId = 3, + Name = "Kamo", + Country = "Ghana", + DateOfBirth = DateTime.Now + + }; + + int count = CustomerRepository.Save(cust); + + bool result = false; + + if (count > 0) + { + result = true; + } + + Assert.IsTrue(result); + } + + [TestMethod()] + public void PlaceOrder() + { + Order item = new Order + { + OrderId = 3, + Amount = 700, + VAT = 15, + CustomerId = 2 + }; + + OrderService orderService = new OrderService(); + + bool result = orderService.PlaceOrder(item, item.CustomerId); + + Assert.IsTrue(result); + } + + [TestMethod()] + public void GetCustomerOrders() + { + Customer cust = CustomerRepository.GetCustomerOrders(2); + + if (cust.CustomerId == 0) + { + Assert.Fail("Customer Does not exist"); + } + + if (cust.Orders.Count == 0) + { + Assert.Fail("Customer Does not have any orders assigned"); + } + + } } } diff --git a/TechTest/AnyCompany.Tests/packages.config b/TechTest/AnyCompany.Tests/packages.config new file mode 100644 index 0000000..cb50f90 --- /dev/null +++ b/TechTest/AnyCompany.Tests/packages.config @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/TechTest/AnyCompany/AnyCompany.csproj b/TechTest/AnyCompany/AnyCompany.csproj index 5b0498d..278a8d3 100644 --- a/TechTest/AnyCompany/AnyCompany.csproj +++ b/TechTest/AnyCompany/AnyCompany.csproj @@ -1,5 +1,6 @@  + Debug @@ -11,6 +12,8 @@ AnyCompany v4.6.1 512 + + true @@ -29,8 +32,19 @@ prompt 4 + + + + + ..\packages\EntityFramework.6.4.4\lib\net45\EntityFramework.dll + + + ..\packages\EntityFramework.6.4.4\lib\net45\EntityFramework.SqlServer.dll + + + @@ -42,10 +56,25 @@ + + + + + + + + + + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. + + + + + \ No newline at end of file diff --git a/TechTest/AnyCompany/App.config b/TechTest/AnyCompany/App.config new file mode 100644 index 0000000..266e19d --- /dev/null +++ b/TechTest/AnyCompany/App.config @@ -0,0 +1,14 @@ + + + + +
+ + + + + + + \ No newline at end of file diff --git a/TechTest/AnyCompany/Customer.cs b/TechTest/AnyCompany/Customer.cs index aa994b6..7a49feb 100644 --- a/TechTest/AnyCompany/Customer.cs +++ b/TechTest/AnyCompany/Customer.cs @@ -1,13 +1,19 @@ using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; namespace AnyCompany { public class Customer { + [Key] + public int CustomerId { get; set; } public string Country { get; set; } public DateTime DateOfBirth { get; set; } public string Name { get; set; } + + public List Orders { get; set; } } } diff --git a/TechTest/AnyCompany/CustomerRepository.cs b/TechTest/AnyCompany/CustomerRepository.cs index e3de9b7..34f76a1 100644 --- a/TechTest/AnyCompany/CustomerRepository.cs +++ b/TechTest/AnyCompany/CustomerRepository.cs @@ -1,11 +1,12 @@ using System; +using System.Collections.Generic; using System.Data.SqlClient; namespace AnyCompany { public static class CustomerRepository { - private static string ConnectionString = @"Data Source=(local);Database=Customers;User Id=admin;Password=password;"; + private static string ConnectionString = @"Data Source=(local);Database=AnyCompany;User Id=admin;Password=password1;"; public static Customer Load(int customerId) { @@ -14,20 +15,83 @@ public static Customer Load(int customerId) SqlConnection connection = new SqlConnection(ConnectionString); connection.Open(); - SqlCommand command = new SqlCommand("SELECT * FROM Customer WHERE CustomerId = " + customerId, + SqlCommand command = new SqlCommand("select * FROM Customers WHERE CustomerId = " + customerId, connection); var reader = command.ExecuteReader(); while (reader.Read()) { + customer.CustomerId = Convert.ToInt32(reader["CustomerId"]); customer.Name = reader["Name"].ToString(); customer.DateOfBirth = DateTime.Parse(reader["DateOfBirth"].ToString()); customer.Country = reader["Country"].ToString(); + } + connection.Close(); + return customer; + } + + public static Customer GetCustomerOrders(int customerId) + { + Customer customer = new Customer(); + List order = new List(); + + SqlConnection connection = new SqlConnection(ConnectionString); + connection.Open(); + + SqlCommand command = new SqlCommand("select * FROM Customers C INNER JOIN Orders O on O.CustomerId = C.CustomerId WHERE C.CustomerId = " + customerId, + connection); + var reader = command.ExecuteReader(); + + while (reader.Read()) + { + customer.CustomerId = Convert.ToInt32(reader["CustomerId"]); + customer.Name = reader["Name"].ToString(); + customer.DateOfBirth = DateTime.Parse(reader["DateOfBirth"].ToString()); + customer.Country = reader["Country"].ToString(); + Order c = new Order + { + OrderId = Convert.ToInt32(reader["OrderId"]), + Amount = Convert.ToDouble(reader["Amount"]), + VAT = Convert.ToDouble(reader["VAT"]), + CustomerId = Convert.ToInt32(reader["CustomerId"]) + }; + order.Add(c); + + } connection.Close(); + customer.Orders = order; + return customer; } + public static int Save(Customer customer) + { + try + { + SqlConnection connection = new SqlConnection(ConnectionString); + connection.Open(); + + SqlCommand command = new SqlCommand("INSERT INTO Customers VALUES (@CustomerId, @Country, @DateOfBirth, @Name)", connection); + + command.Parameters.AddWithValue("@CustomerId", customer.CustomerId); + command.Parameters.AddWithValue("@Country", customer.Country); + command.Parameters.AddWithValue("@DateOfBirth", customer.DateOfBirth); + command.Parameters.AddWithValue("@Name", customer.Name); + + int result = command.ExecuteNonQuery(); + + connection.Close(); + + return result; + } + catch (Exception ex) + { + ex.ToString(); + + return -1; + } + } } } diff --git a/TechTest/AnyCompany/CustomerService.cs b/TechTest/AnyCompany/CustomerService.cs new file mode 100644 index 0000000..b1898f8 --- /dev/null +++ b/TechTest/AnyCompany/CustomerService.cs @@ -0,0 +1,18 @@ + +namespace AnyCompany +{ + public class CustomerService : ICustomerService + { + public Customer Load(int customerId) + { + Customer customer = CustomerRepository.Load(customerId); + + return customer; + } + + public int Save(Customer customer) + { + return CustomerRepository.Save(customer); + } + } +} diff --git a/TechTest/AnyCompany/ICustomerService.cs b/TechTest/AnyCompany/ICustomerService.cs new file mode 100644 index 0000000..da36454 --- /dev/null +++ b/TechTest/AnyCompany/ICustomerService.cs @@ -0,0 +1,11 @@ + +namespace AnyCompany +{ + public interface ICustomerService + { + Customer Load(int customerId); + + int Save(Customer customer); + + } +} diff --git a/TechTest/AnyCompany/IOrderService.cs b/TechTest/AnyCompany/IOrderService.cs new file mode 100644 index 0000000..73f9f36 --- /dev/null +++ b/TechTest/AnyCompany/IOrderService.cs @@ -0,0 +1,9 @@ + + +namespace AnyCompany +{ + public interface IOrderService + { + bool PlaceOrder(Order order, int customerId); + } +} diff --git a/TechTest/AnyCompany/Order.cs b/TechTest/AnyCompany/Order.cs index fec8e7b..04afdbb 100644 --- a/TechTest/AnyCompany/Order.cs +++ b/TechTest/AnyCompany/Order.cs @@ -5,5 +5,7 @@ public class Order public int OrderId { get; set; } public double Amount { get; set; } public double VAT { get; set; } + public int CustomerId { get; set; } + public Customer Customer { get; set; } } } diff --git a/TechTest/AnyCompany/OrderRepository.cs b/TechTest/AnyCompany/OrderRepository.cs index 3229885..c2bd72d 100644 --- a/TechTest/AnyCompany/OrderRepository.cs +++ b/TechTest/AnyCompany/OrderRepository.cs @@ -1,25 +1,39 @@ -using System.Data.SqlClient; +using System; +using System.Data.SqlClient; namespace AnyCompany { internal class OrderRepository { - private static string ConnectionString = @"Data Source=(local);Database=Orders;User Id=admin;Password=password;"; + private static string ConnectionString = @"Data Source=(local);Database=AnyCompany;User Id=admin;Password=password1;"; - public void Save(Order order) + public int Save(Order order) { - SqlConnection connection = new SqlConnection(ConnectionString); - connection.Open(); + try + { + SqlConnection connection = new SqlConnection(ConnectionString); + connection.Open(); - SqlCommand command = new SqlCommand("INSERT INTO Orders VALUES (@OrderId, @Amount, @VAT)", connection); + SqlCommand command = new SqlCommand("INSERT INTO Orders VALUES (@OrderId, @Amount, @CustomerId, @VAT)", connection); - command.Parameters.AddWithValue("@OrderId", order.OrderId); - command.Parameters.AddWithValue("@Amount", order.Amount); - command.Parameters.AddWithValue("@VAT", order.VAT); + command.Parameters.AddWithValue("@OrderId", order.OrderId); + command.Parameters.AddWithValue("@Amount", order.Amount); + command.Parameters.AddWithValue("@CustomerId", Convert.ToInt32(order.Customer.CustomerId)); + command.Parameters.AddWithValue("@VAT", order.VAT); + - command.ExecuteNonQuery(); + int result = command.ExecuteNonQuery(); - connection.Close(); + connection.Close(); + + return result; + } + catch(Exception ex) + { + ex.ToString(); + + return -1; + } } } } diff --git a/TechTest/AnyCompany/OrderService.cs b/TechTest/AnyCompany/OrderService.cs index ebfb103..7ef520d 100644 --- a/TechTest/AnyCompany/OrderService.cs +++ b/TechTest/AnyCompany/OrderService.cs @@ -1,24 +1,50 @@ -namespace AnyCompany +using System; + +namespace AnyCompany { - public class OrderService - { + public class OrderService : IOrderService + { private readonly OrderRepository orderRepository = new OrderRepository(); public bool PlaceOrder(Order order, int customerId) - { - Customer customer = CustomerRepository.Load(customerId); + { + try + { + Customer customer = CustomerRepository.Load(customerId); - if (order.Amount == 0) - return false; + if (customer != null) + { + if (order.Amount == 0) + return false; - if (customer.Country == "UK") - order.VAT = 0.2d; - else - order.VAT = 0; + if (customer.Country == "UK") + order.VAT = 0.2d; + else + order.VAT = 0; - orderRepository.Save(order); + order.Customer = customer; - return true; + int result = orderRepository.Save(order); + + if (result > 0) + { + return true; + } + else + { + return false; + } + } + else + { + return false; + } + } + catch(Exception ex) + { + ex.ToString(); + return false; + } } } } diff --git a/TechTest/AnyCompany/packages.config b/TechTest/AnyCompany/packages.config new file mode 100644 index 0000000..83e60f0 --- /dev/null +++ b/TechTest/AnyCompany/packages.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file