diff --git a/TechTest/AnyCompany.Tests/AnyCompany.Tests.csproj b/TechTest/AnyCompany.Tests/AnyCompany.Tests.csproj
index b537fc2..fede6ad 100644
--- a/TechTest/AnyCompany.Tests/AnyCompany.Tests.csproj
+++ b/TechTest/AnyCompany.Tests/AnyCompany.Tests.csproj
@@ -1,53 +1,62 @@
-
-
-
-
- Debug
- AnyCPU
- cd5d577e-bdc9-4dfc-ac6a-b1da474995f3
- Library
- Properties
- AnyCompany.Tests
- AnyCompany.Tests
- v4.6.1
- 512
-
-
- true
- full
- false
- bin\Debug\
- DEBUG;TRACE
- prompt
- 4
-
-
- pdbonly
- true
- bin\Release\
- TRACE
- prompt
- 4
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+ Debug
+ AnyCPU
+ {CD5D577E-BDC9-4DFC-AC6A-B1DA474995F3}
+ Library
+ Properties
+ AnyCompany.Tests
+ AnyCompany.Tests
+ v4.6.1
+ 512
+
+
+ true
+ full
+ false
+ bin\Debug\
+ DEBUG;TRACE
+ prompt
+ 4
+
+
+ pdbonly
+ true
+ bin\Release\
+ TRACE
+ prompt
+ 4
+
+
+
+ ..\packages\MSTest.TestFramework.2.1.0\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll
+
+
+ ..\packages\MSTest.TestFramework.2.1.0\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ {c7e15594-7d8f-4c18-9dd7-14f3fbb1572d}
+ AnyCompany
+
+
+
+
\ No newline at end of file
diff --git a/TechTest/AnyCompany.Tests/Class1.cs b/TechTest/AnyCompany.Tests/Class1.cs
deleted file mode 100644
index 5957505..0000000
--- a/TechTest/AnyCompany.Tests/Class1.cs
+++ /dev/null
@@ -1,12 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-
-namespace AnyCompany.Tests
-{
- public class Class1
- {
- }
-}
diff --git a/TechTest/AnyCompany.Tests/Tests.cs b/TechTest/AnyCompany.Tests/Tests.cs
new file mode 100644
index 0000000..e5e0563
--- /dev/null
+++ b/TechTest/AnyCompany.Tests/Tests.cs
@@ -0,0 +1,61 @@
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using AnyCompany.Interfaces;
+using AnyCompany.Models;
+
+namespace AnyCompany.Tests
+{
+ [TestClass]
+ public class ServiceTests
+ {
+ public static IOrderService orderService;
+ [TestMethod]
+ public void TestOrderInvalidCustomerId()
+ {
+ Order newOrder = new Order
+ {
+ CustomerId = -1,
+ Amount = 10,
+ OrderId = 11,
+ VAT = 0
+ };
+ var success = orderService.PlaceOrder(newOrder, -1, out string error);
+ Assert.AreEqual(success, false);
+ Assert.AreEqual(error, "Please provide a correct Customer Id and try again");
+ }
+
+ [TestMethod]
+ public void TestOrderInvalidAmount()
+ {
+ Order newOrder = new Order
+ {
+ CustomerId = 1,
+ Amount = 0,
+ OrderId = 11,
+ VAT = 0
+ };
+ var success = orderService.PlaceOrder(newOrder, 1, out string error);
+ Assert.AreEqual(success, false);
+ Assert.AreEqual(error, "Amount can not be zero or below, please provide a correct amount and try again");
+ }
+
+ [TestMethod]
+ public void TestOrderSuccess()
+ {
+ Order newOrder = new Order
+ {
+ CustomerId = 1,
+ Amount = 10,
+ OrderId = 11,
+ VAT = 0
+ };
+ var success = orderService.PlaceOrder(newOrder, 1, out string error);
+ Assert.AreEqual(success, true);
+ Assert.AreEqual(error, "");
+ }
+ }
+}
diff --git a/TechTest/AnyCompany.Tests/packages.config b/TechTest/AnyCompany.Tests/packages.config
new file mode 100644
index 0000000..e4cc39d
--- /dev/null
+++ b/TechTest/AnyCompany.Tests/packages.config
@@ -0,0 +1,4 @@
+
+
+
+
\ No newline at end of file
diff --git a/TechTest/AnyCompany/AnyCompany.csproj b/TechTest/AnyCompany/AnyCompany.csproj
index 5b0498d..3b08abf 100644
--- a/TechTest/AnyCompany/AnyCompany.csproj
+++ b/TechTest/AnyCompany/AnyCompany.csproj
@@ -1,51 +1,53 @@
-
-
-
-
- Debug
- AnyCPU
- {C7E15594-7D8F-4C18-9DD7-14F3FBB1572D}
- Library
- Properties
- AnyCompany
- AnyCompany
- v4.6.1
- 512
-
-
- true
- full
- false
- bin\Debug\
- DEBUG;TRACE
- prompt
- 4
-
-
- pdbonly
- true
- bin\Release\
- TRACE
- prompt
- 4
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+ Debug
+ AnyCPU
+ {C7E15594-7D8F-4C18-9DD7-14F3FBB1572D}
+ Library
+ Properties
+ AnyCompany
+ AnyCompany
+ v4.6.1
+ 512
+
+
+ true
+ full
+ false
+ bin\Debug\
+ DEBUG;TRACE
+ prompt
+ 4
+
+
+ pdbonly
+ true
+ bin\Release\
+ TRACE
+ prompt
+ 4
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/TechTest/AnyCompany/CustomerRepository.cs b/TechTest/AnyCompany/CustomerRepository.cs
deleted file mode 100644
index e3de9b7..0000000
--- a/TechTest/AnyCompany/CustomerRepository.cs
+++ /dev/null
@@ -1,33 +0,0 @@
-using System;
-using System.Data.SqlClient;
-
-namespace AnyCompany
-{
- public static class CustomerRepository
- {
- private static string ConnectionString = @"Data Source=(local);Database=Customers;User Id=admin;Password=password;";
-
- public static Customer Load(int customerId)
- {
- Customer customer = new Customer();
-
- SqlConnection connection = new SqlConnection(ConnectionString);
- connection.Open();
-
- SqlCommand command = new SqlCommand("SELECT * FROM Customer WHERE CustomerId = " + customerId,
- connection);
- var reader = command.ExecuteReader();
-
- while (reader.Read())
- {
- customer.Name = reader["Name"].ToString();
- customer.DateOfBirth = DateTime.Parse(reader["DateOfBirth"].ToString());
- customer.Country = reader["Country"].ToString();
- }
-
- connection.Close();
-
- return customer;
- }
- }
-}
diff --git a/TechTest/AnyCompany/Interfaces/IOrderService.cs b/TechTest/AnyCompany/Interfaces/IOrderService.cs
new file mode 100644
index 0000000..f9b80b8
--- /dev/null
+++ b/TechTest/AnyCompany/Interfaces/IOrderService.cs
@@ -0,0 +1,14 @@
+using AnyCompany.Models;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace AnyCompany.Interfaces
+{
+ public interface IOrderService
+ {
+ bool PlaceOrder(Order order, int customerId, out string error);
+ }
+}
diff --git a/TechTest/AnyCompany/Customer.cs b/TechTest/AnyCompany/Models/Customer.cs
similarity index 64%
rename from TechTest/AnyCompany/Customer.cs
rename to TechTest/AnyCompany/Models/Customer.cs
index aa994b6..240e7fb 100644
--- a/TechTest/AnyCompany/Customer.cs
+++ b/TechTest/AnyCompany/Models/Customer.cs
@@ -1,13 +1,13 @@
using System;
-
-namespace AnyCompany
+using System.Collections.Generic;
+
+namespace AnyCompany.Models
{
public class Customer
{
+ public int CustomerId { get; set; }
public string Country { get; set; }
-
public DateTime DateOfBirth { get; set; }
-
public string Name { get; set; }
}
}
diff --git a/TechTest/AnyCompany/Models/CustomerOrder.cs b/TechTest/AnyCompany/Models/CustomerOrder.cs
new file mode 100644
index 0000000..594fe5a
--- /dev/null
+++ b/TechTest/AnyCompany/Models/CustomerOrder.cs
@@ -0,0 +1,14 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace AnyCompany.Models
+{
+ public class CustomerOrder
+ {
+ public Customer Customer { get; set; }
+ public List Orders { get; set; }
+ }
+}
diff --git a/TechTest/AnyCompany/Order.cs b/TechTest/AnyCompany/Models/Order.cs
similarity index 68%
rename from TechTest/AnyCompany/Order.cs
rename to TechTest/AnyCompany/Models/Order.cs
index fec8e7b..c23d592 100644
--- a/TechTest/AnyCompany/Order.cs
+++ b/TechTest/AnyCompany/Models/Order.cs
@@ -1,8 +1,9 @@
-namespace AnyCompany
+namespace AnyCompany.Models
{
public class Order
{
public int OrderId { get; set; }
+ public int CustomerId { get; set; }
public double Amount { get; set; }
public double VAT { get; set; }
}
diff --git a/TechTest/AnyCompany/OrderRepository.cs b/TechTest/AnyCompany/OrderRepository.cs
deleted file mode 100644
index 3229885..0000000
--- a/TechTest/AnyCompany/OrderRepository.cs
+++ /dev/null
@@ -1,25 +0,0 @@
-using System.Data.SqlClient;
-
-namespace AnyCompany
-{
- internal class OrderRepository
- {
- private static string ConnectionString = @"Data Source=(local);Database=Orders;User Id=admin;Password=password;";
-
- public void Save(Order order)
- {
- SqlConnection connection = new SqlConnection(ConnectionString);
- connection.Open();
-
- SqlCommand command = new SqlCommand("INSERT INTO Orders VALUES (@OrderId, @Amount, @VAT)", connection);
-
- command.Parameters.AddWithValue("@OrderId", order.OrderId);
- command.Parameters.AddWithValue("@Amount", order.Amount);
- command.Parameters.AddWithValue("@VAT", order.VAT);
-
- command.ExecuteNonQuery();
-
- connection.Close();
- }
- }
-}
diff --git a/TechTest/AnyCompany/OrderService.cs b/TechTest/AnyCompany/OrderService.cs
index ebfb103..639853d 100644
--- a/TechTest/AnyCompany/OrderService.cs
+++ b/TechTest/AnyCompany/OrderService.cs
@@ -1,20 +1,37 @@
-namespace AnyCompany
+using AnyCompany.Interfaces;
+using AnyCompany.Models;
+using AnyCompany.Repositories;
+
+namespace AnyCompany
{
- public class OrderService
+ public class OrderService : IOrderService
{
private readonly OrderRepository orderRepository = new OrderRepository();
- public bool PlaceOrder(Order order, int customerId)
+ public bool PlaceOrder(Order order, int customerId, out string error)
{
+ error = "";
+ if (customerId <= 0)
+ {
+ error = "Please provide a correct Customer Id and try again";
+ return false;
+ }
Customer customer = CustomerRepository.Load(customerId);
- if (order.Amount == 0)
- return false;
+ order.CustomerId = customer.CustomerId;
+ if (order.Amount <= 0) {
+ error = "Amount can not be zero or below, please provide a correct amount and try again";
+ 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);
diff --git a/TechTest/AnyCompany/Repositories/CustomerRepository.cs b/TechTest/AnyCompany/Repositories/CustomerRepository.cs
new file mode 100644
index 0000000..1dca4de
--- /dev/null
+++ b/TechTest/AnyCompany/Repositories/CustomerRepository.cs
@@ -0,0 +1,99 @@
+using System;
+using System.Collections.Generic;
+using System.Data.SqlClient;
+using System.Linq;
+using AnyCompany.Models;
+
+namespace AnyCompany.Repositories
+{
+ public static class CustomerRepository
+ {
+ private static string ConnectionString = @"Data Source=(local);Database=Customers;User Id=admin;Password=password;";
+
+ public static Customer Load(int customerId)
+ {
+ Customer customer = new Customer();
+
+ using (SqlConnection connection = new SqlConnection(ConnectionString))
+ {
+ connection.Open();
+
+ using (SqlCommand command = new SqlCommand("SELECT * FROM Customer 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 List LoadAllCustomers()
+ {
+ List list = new List();
+ using (SqlConnection connection = new SqlConnection(ConnectionString))
+ {
+ connection.Open();
+
+ using (SqlCommand command = new SqlCommand("SELECT * FROM Customer LEFT JOIN Order ON Customer.CustomerId = Order.CustomerId",
+ connection))
+ {
+ var reader = command.ExecuteReader();
+
+ int customerId = 0;
+ while (reader.Read())
+ {
+ int readerCustomerId = Convert.ToInt32(reader["CustomerId"]);
+ if (readerCustomerId == customerId)
+ {
+ Customer customer = new Customer
+ {
+ CustomerId = readerCustomerId,
+ Name = reader["Name"].ToString(),
+ DateOfBirth = DateTime.Parse(reader["DateOfBirth"].ToString()),
+ Country = reader["Country"].ToString(),
+ };
+ Order order = new Order
+ {
+ CustomerId = readerCustomerId,
+ OrderId = Convert.ToInt32(reader["OrderId"]),
+ Amount = Convert.ToDouble(reader["Amount"]),
+ VAT = Convert.ToDouble(reader["VAT"])
+ };
+
+ list.Add(new CustomerOrder
+ {
+ Customer = customer,
+ Orders = new List { order }
+ });
+ }
+ else
+ {
+ list.Where(x => x.Customer.CustomerId == readerCustomerId).SingleOrDefault().Orders.Add(new Order
+ {
+ CustomerId = readerCustomerId,
+ OrderId = Convert.ToInt32(reader["OrderId"]),
+ Amount = Convert.ToDouble(reader["Amount"]),
+ VAT = Convert.ToDouble(reader["VAT"])
+ });
+ }
+ }
+
+ connection.Close();
+ }
+
+ return list;
+ }
+ }
+ }
+}
diff --git a/TechTest/AnyCompany/Repositories/OrderRepository.cs b/TechTest/AnyCompany/Repositories/OrderRepository.cs
new file mode 100644
index 0000000..ecff345
--- /dev/null
+++ b/TechTest/AnyCompany/Repositories/OrderRepository.cs
@@ -0,0 +1,29 @@
+using System.Data.SqlClient;
+using AnyCompany.Models;
+
+namespace AnyCompany.Repositories
+{
+ internal class OrderRepository
+ {
+ private static string ConnectionString = @"Data Source=(local);Database=Orders;User Id=admin;Password=password;";
+
+ public void Save(Order order)
+ {
+ using (SqlConnection connection = new SqlConnection(ConnectionString))
+ {
+ connection.Open();
+
+ using (SqlCommand command = new SqlCommand("INSERT INTO Orders VALUES (@CustomerId, @OrderId, @Amount, @VAT)", connection))
+ {
+ command.Parameters.AddWithValue("@CustomerId", order.CustomerId);
+ command.Parameters.AddWithValue("@OrderId", order.OrderId);
+ command.Parameters.AddWithValue("@Amount", order.Amount);
+ command.Parameters.AddWithValue("@VAT", order.VAT);
+
+ command.ExecuteNonQuery();
+ }
+ connection.Close();
+ }
+ }
+ }
+}