From 9622b20212feae7ab72358031243cf6ae9fe7750 Mon Sep 17 00:00:00 2001
From: HJBrits <35849706+HJBrits@users.noreply.github.com>
Date: Wed, 11 Mar 2020 22:37:20 +0200
Subject: [PATCH] HJB
---
.../AnyCompany.Entities.csproj} | 10 +-
.../Customer.cs | 9 +-
TechTest/AnyCompany.Entities/Order.cs | 16 ++
.../Properties/AssemblyInfo.cs | 10 +-
.../AnyCompany.Repository.csproj} | 43 ++--
TechTest/AnyCompany.Repository/Context.cs | 18 ++
.../CustomerRepository.cs | 134 +++++++++++
.../AnyCompany.Repository/OrderRepository.cs | 109 +++++++++
.../Properties/AssemblyInfo.cs | 10 +-
.../AnyCompany.Service.csproj | 59 +++++
.../AnyCompany.Service/CustomerService.cs | 58 +++++
TechTest/AnyCompany.Service/OrderService.cs | 61 +++++
.../Properties/AssemblyInfo.cs | 36 +++
TechTest/AnyCompany.Tests/Class1.cs | 12 -
.../AnyCompany.UnitTest.csproj | 82 +++++++
.../Properties/AssemblyInfo.cs | 20 ++
.../AnyCompany.UnitTest/UnitTestAnyCompany.cs | 209 ++++++++++++++++++
TechTest/AnyCompany.UnitTest/packages.config | 5 +
TechTest/AnyCompany/CustomerRepository.cs | 33 ---
TechTest/AnyCompany/Order.cs | 9 -
TechTest/AnyCompany/OrderRepository.cs | 25 ---
TechTest/AnyCompany/OrderService.cs | 24 --
TechTest/TechTest.sln | 36 ++-
23 files changed, 874 insertions(+), 154 deletions(-)
rename TechTest/{AnyCompany/AnyCompany.csproj => AnyCompany.Entities/AnyCompany.Entities.csproj} (87%)
rename TechTest/{AnyCompany => AnyCompany.Entities}/Customer.cs (52%)
create mode 100644 TechTest/AnyCompany.Entities/Order.cs
rename TechTest/{AnyCompany => AnyCompany.Entities}/Properties/AssemblyInfo.cs (82%)
rename TechTest/{AnyCompany.Tests/AnyCompany.Tests.csproj => AnyCompany.Repository/AnyCompany.Repository.csproj} (60%)
create mode 100644 TechTest/AnyCompany.Repository/Context.cs
create mode 100644 TechTest/AnyCompany.Repository/CustomerRepository.cs
create mode 100644 TechTest/AnyCompany.Repository/OrderRepository.cs
rename TechTest/{AnyCompany.Tests => AnyCompany.Repository}/Properties/AssemblyInfo.cs (81%)
create mode 100644 TechTest/AnyCompany.Service/AnyCompany.Service.csproj
create mode 100644 TechTest/AnyCompany.Service/CustomerService.cs
create mode 100644 TechTest/AnyCompany.Service/OrderService.cs
create mode 100644 TechTest/AnyCompany.Service/Properties/AssemblyInfo.cs
delete mode 100644 TechTest/AnyCompany.Tests/Class1.cs
create mode 100644 TechTest/AnyCompany.UnitTest/AnyCompany.UnitTest.csproj
create mode 100644 TechTest/AnyCompany.UnitTest/Properties/AssemblyInfo.cs
create mode 100644 TechTest/AnyCompany.UnitTest/UnitTestAnyCompany.cs
create mode 100644 TechTest/AnyCompany.UnitTest/packages.config
delete mode 100644 TechTest/AnyCompany/CustomerRepository.cs
delete mode 100644 TechTest/AnyCompany/Order.cs
delete mode 100644 TechTest/AnyCompany/OrderRepository.cs
delete mode 100644 TechTest/AnyCompany/OrderService.cs
diff --git a/TechTest/AnyCompany/AnyCompany.csproj b/TechTest/AnyCompany.Entities/AnyCompany.Entities.csproj
similarity index 87%
rename from TechTest/AnyCompany/AnyCompany.csproj
rename to TechTest/AnyCompany.Entities/AnyCompany.Entities.csproj
index 5b0498d..0a5ade0 100644
--- a/TechTest/AnyCompany/AnyCompany.csproj
+++ b/TechTest/AnyCompany.Entities/AnyCompany.Entities.csproj
@@ -4,13 +4,14 @@
Debug
AnyCPU
- {C7E15594-7D8F-4C18-9DD7-14F3FBB1572D}
+ {FB9F5EA2-83EA-4964-8F50-7CB67B7CA57F}
Library
Properties
- AnyCompany
- AnyCompany
+ AnyCompany.Entities
+ AnyCompany.Entities
v4.6.1
512
+ true
true
@@ -41,10 +42,7 @@
-
-
-
diff --git a/TechTest/AnyCompany/Customer.cs b/TechTest/AnyCompany.Entities/Customer.cs
similarity index 52%
rename from TechTest/AnyCompany/Customer.cs
rename to TechTest/AnyCompany.Entities/Customer.cs
index aa994b6..68a3f8f 100644
--- a/TechTest/AnyCompany/Customer.cs
+++ b/TechTest/AnyCompany.Entities/Customer.cs
@@ -1,13 +1,16 @@
using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
-namespace AnyCompany
+namespace AnyCompany.Entities
{
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.Entities/Order.cs b/TechTest/AnyCompany.Entities/Order.cs
new file mode 100644
index 0000000..e18e84d
--- /dev/null
+++ b/TechTest/AnyCompany.Entities/Order.cs
@@ -0,0 +1,16 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace AnyCompany.Entities
+{
+ 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/Properties/AssemblyInfo.cs b/TechTest/AnyCompany.Entities/Properties/AssemblyInfo.cs
similarity index 82%
rename from TechTest/AnyCompany/Properties/AssemblyInfo.cs
rename to TechTest/AnyCompany.Entities/Properties/AssemblyInfo.cs
index bcdfb17..efbbe38 100644
--- a/TechTest/AnyCompany/Properties/AssemblyInfo.cs
+++ b/TechTest/AnyCompany.Entities/Properties/AssemblyInfo.cs
@@ -5,12 +5,12 @@
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
-[assembly: AssemblyTitle("AnyCompany")]
+[assembly: AssemblyTitle("AnyCompany.Entities")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
-[assembly: AssemblyCompany("Investec Bank")]
-[assembly: AssemblyProduct("AnyCompany")]
-[assembly: AssemblyCopyright("Copyright © Investec Bank 2018")]
+[assembly: AssemblyCompany("")]
+[assembly: AssemblyProduct("AnyCompany.Entities")]
+[assembly: AssemblyCopyright("Copyright © 2020")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
@@ -20,7 +20,7 @@
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
-[assembly: Guid("c7e15594-7d8f-4c18-9dd7-14f3fbb1572d")]
+[assembly: Guid("fb9f5ea2-83ea-4964-8f50-7cb67b7ca57f")]
// Version information for an assembly consists of the following four values:
//
diff --git a/TechTest/AnyCompany.Tests/AnyCompany.Tests.csproj b/TechTest/AnyCompany.Repository/AnyCompany.Repository.csproj
similarity index 60%
rename from TechTest/AnyCompany.Tests/AnyCompany.Tests.csproj
rename to TechTest/AnyCompany.Repository/AnyCompany.Repository.csproj
index b537fc2..6b78e1b 100644
--- a/TechTest/AnyCompany.Tests/AnyCompany.Tests.csproj
+++ b/TechTest/AnyCompany.Repository/AnyCompany.Repository.csproj
@@ -1,16 +1,17 @@
-
+
Debug
AnyCPU
- cd5d577e-bdc9-4dfc-ac6a-b1da474995f3
+ {F2200DAF-29BA-4ECA-9C88-1D17FA51D56D}
Library
Properties
- AnyCompany.Tests
- AnyCompany.Tests
+ AnyCompany.Repository
+ AnyCompany.Repository
v4.6.1
512
+ true
true
@@ -30,24 +31,26 @@
4
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
-
+
+
+
+
+
+ {fb9f5ea2-83ea-4964-8f50-7cb67b7ca57f}
+ AnyCompany.Entities
+
+
-
+
\ No newline at end of file
diff --git a/TechTest/AnyCompany.Repository/Context.cs b/TechTest/AnyCompany.Repository/Context.cs
new file mode 100644
index 0000000..9c1594e
--- /dev/null
+++ b/TechTest/AnyCompany.Repository/Context.cs
@@ -0,0 +1,18 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace AnyCompany.Repository
+{
+ public static class Context
+ {
+ public static string ConnectionString = @"Data Source=(local);Database=Orders;User Id=admin;Password=password;";
+ public static void CheckAllTableExist()
+ {
+ CustomerRepository.CheckTableExist();
+ OrderRepository.CheckTableExist();
+ }
+ }
+}
diff --git a/TechTest/AnyCompany.Repository/CustomerRepository.cs b/TechTest/AnyCompany.Repository/CustomerRepository.cs
new file mode 100644
index 0000000..462d08c
--- /dev/null
+++ b/TechTest/AnyCompany.Repository/CustomerRepository.cs
@@ -0,0 +1,134 @@
+using AnyCompany.Entities;
+using System;
+using System.Collections.Generic;
+using System.Data.SqlClient;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace AnyCompany.Repository
+{
+ public static class CustomerRepository
+ {
+
+ public static void CheckTableExist()
+ {
+
+ using (SqlConnection connection = new SqlConnection(Context.ConnectionString))
+ {
+ connection.Open();
+
+ string sqlScript = $@"
+ IF Not EXISTS (SELECT *
+ FROM INFORMATION_SCHEMA.TABLES
+ WHERE TABLE_TYPE='BASE TABLE'
+ AND TABLE_NAME='Customer')
+ Begin
+ CREATE TABLE [dbo].[Customer](
+ CustomerId bigint IDENTITY(1,1) NOT NULL,
+ Name nvarchar(255) NULL,
+ DateOfBirth Date NULL,
+ Country nvarchar(255) NULL,
+ CONSTRAINT [PK_Customer] PRIMARY KEY CLUSTERED
+ (
+ [CustomerId] ASC
+ )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
+ ) ON [PRIMARY]
+
+ End";
+
+ using (SqlCommand command = new SqlCommand(sqlScript, connection))
+ {
+ command.ExecuteNonQuery();
+ }
+ connection.Close();
+ }
+
+ }
+ public static void Save(Customer customer)
+ {
+ using (SqlConnection connection = new SqlConnection(Context.ConnectionString))
+ {
+ connection.Open();
+
+ if (customer.CustomerId <= 0)
+ {
+ using (SqlCommand command = new SqlCommand("INSERT INTO Customer(Name, DateOfBirth,Country) output INSERTED.[CustomerId] VALUES (@Name, @DateOfBirth, @Country); ", connection))
+ {
+ command.Parameters.Add(new SqlParameter("@Name", customer.Name));
+ command.Parameters.Add(new SqlParameter("@DateOfBirth", customer.DateOfBirth));
+ command.Parameters.Add(new SqlParameter("@Country", customer.Country));
+ object customerIdValue = command.ExecuteScalar();
+ if (customerIdValue != null)
+ customer.CustomerId = int.Parse(customerIdValue.ToString());
+ }
+ }
+ else
+ {
+ using (SqlCommand command = new SqlCommand("Update Orders Customer Name = @Name, DateOfBirth = @DateOfBirth, Country = @Country) Where CustomerId = @CustomerId ", connection))
+ {
+ command.Parameters.Add(new SqlParameter("@Name", customer.Name));
+ command.Parameters.Add(new SqlParameter("@DateOfBirth", customer.DateOfBirth));
+ command.Parameters.Add(new SqlParameter("@Country", customer.Country));
+ command.Parameters.Add(new SqlParameter("@CustomerId", customer.CustomerId));
+ command.ExecuteNonQuery();
+ }
+ }
+ connection.Close();
+ }
+ }
+ public static Customer Load(int customerId)
+ {
+ Customer customer = new Customer();
+
+ using (SqlConnection connection = new SqlConnection(Context.ConnectionString))
+ {
+ connection.Open();
+
+ using (SqlCommand command = new SqlCommand($"SELECT * FROM Customer WHERE CustomerId = @CustomerIdParam", connection))
+ {
+ command.Parameters.Add(new SqlParameter($"@CustomerIdParam", customerId));
+
+ SqlDataReader reader = command.ExecuteReader();
+
+ while (reader.Read())
+ {
+ customer.CustomerId = int.Parse(reader["CustomerId"].ToString());
+ customer.Name = reader["Name"].ToString();
+ customer.DateOfBirth = DateTime.Parse(reader["DateOfBirth"].ToString());
+ customer.Country = reader["Country"].ToString();
+ }
+ }
+ connection.Close();
+ }
+ return customer;
+ }
+ public static List LoadAll()
+ {
+ List customerList = new List();
+
+ using (SqlConnection connection = new SqlConnection(Context.ConnectionString))
+ {
+ connection.Open();
+
+ using (SqlCommand command = new SqlCommand($"SELECT * FROM Customer", connection))
+ {
+ SqlDataReader reader = command.ExecuteReader();
+
+ while (reader.Read())
+ {
+ customerList.Add(new Customer()
+ {
+ CustomerId = int.Parse(reader["CustomerId"].ToString()),
+ Name = reader["Name"].ToString(),
+ DateOfBirth = DateTime.Parse(reader["DateOfBirth"].ToString()),
+ Country = reader["Country"].ToString(),
+ });
+ }
+ }
+ connection.Close();
+ }
+ return customerList;
+ }
+ }
+}
diff --git a/TechTest/AnyCompany.Repository/OrderRepository.cs b/TechTest/AnyCompany.Repository/OrderRepository.cs
new file mode 100644
index 0000000..663af5d
--- /dev/null
+++ b/TechTest/AnyCompany.Repository/OrderRepository.cs
@@ -0,0 +1,109 @@
+using AnyCompany.Entities;
+using System;
+using System.Collections.Generic;
+using System.Data.SqlClient;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace AnyCompany.Repository
+{
+ public static class OrderRepository
+ {
+ public static void CheckTableExist()
+ {
+
+ using (SqlConnection connection = new SqlConnection(Context.ConnectionString))
+ {
+ connection.Open();
+
+ string sqlScript = $@"
+ IF Not EXISTS (SELECT *
+ FROM INFORMATION_SCHEMA.TABLES
+ WHERE TABLE_TYPE='BASE TABLE'
+ AND TABLE_NAME='Orders')
+ Begin
+ CREATE TABLE [dbo].[Orders](
+ OrderId bigint IDENTITY(1,1) NOT NULL,
+ CustomerId bigint NULL,
+ VAT float NULL,
+ Amount float NULL,
+ CONSTRAINT [PK_Orders] PRIMARY KEY CLUSTERED
+ (
+ [OrderId] ASC
+ )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
+ ) ON [PRIMARY]
+
+ End";
+
+ using (SqlCommand command = new SqlCommand(sqlScript, connection))
+ {
+ command.ExecuteNonQuery();
+ }
+ connection.Close();
+ }
+
+ }
+ public static void Save(Order order)
+ {
+ using (SqlConnection connection = new SqlConnection(Context.ConnectionString))
+ {
+ connection.Open();
+
+ if (order.OrderId <= 0)
+ {
+ using (SqlCommand command = new SqlCommand("INSERT INTO Orders(CustomerId, Amount, VAT) output INSERTED.OrderId VALUES (@CustomerId, @Amount, @VAT) ", connection))
+ {
+ command.Parameters.Add(new SqlParameter("@Amount", order.Amount));
+ command.Parameters.Add(new SqlParameter("@VAT", order.VAT));
+ command.Parameters.Add(new SqlParameter("@CustomerId", order.CustomerId));
+ object OrderIdValue = command.ExecuteScalar();
+ if (OrderIdValue != null)
+ order.OrderId = int.Parse(OrderIdValue.ToString());
+ }
+ }
+ else
+ {
+ using (SqlCommand command = new SqlCommand("Update Orders Set CustomerId = @CustomerId, Amount = @Amount, VAT = @VAT) Where OrderId = @OrderId ", connection))
+ {
+ command.Parameters.Add(new SqlParameter("@Amount", order.Amount));
+ command.Parameters.Add(new SqlParameter("@VAT", order.VAT));
+ command.Parameters.Add(new SqlParameter("@CustomerId", order.CustomerId));
+ command.Parameters.Add(new SqlParameter("@OrderId", order.OrderId));
+ command.ExecuteNonQuery();
+ }
+ }
+ connection.Close();
+ }
+ }
+ public static List LoadByCustomer(int customerId)
+ {
+ List orderList = new List();
+
+ using (SqlConnection connection = new SqlConnection(Context.ConnectionString))
+ {
+ connection.Open();
+
+ using (SqlCommand command = new SqlCommand($"SELECT * FROM Orders WHERE CustomerId = @CustomerIdParam", connection))
+ {
+ command.Parameters.Add(new SqlParameter($"@CustomerIdParam", customerId));
+
+ SqlDataReader reader = command.ExecuteReader();
+
+ while (reader.Read())
+ {
+ orderList.Add(new Order()
+ {
+ Amount = double.Parse(reader["Amount"].ToString()),
+ CustomerId = int.Parse(reader["CustomerId"].ToString()),
+ OrderId = int.Parse(reader["OrderId"].ToString()),
+ VAT = double.Parse(reader["VAT"].ToString()),
+ });
+ }
+ }
+ connection.Close();
+ }
+ return orderList;
+ }
+ }
+}
diff --git a/TechTest/AnyCompany.Tests/Properties/AssemblyInfo.cs b/TechTest/AnyCompany.Repository/Properties/AssemblyInfo.cs
similarity index 81%
rename from TechTest/AnyCompany.Tests/Properties/AssemblyInfo.cs
rename to TechTest/AnyCompany.Repository/Properties/AssemblyInfo.cs
index 726eefa..1e0638a 100644
--- a/TechTest/AnyCompany.Tests/Properties/AssemblyInfo.cs
+++ b/TechTest/AnyCompany.Repository/Properties/AssemblyInfo.cs
@@ -5,12 +5,12 @@
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
-[assembly: AssemblyTitle("AnyCompany.Tests")]
+[assembly: AssemblyTitle("AnyCompany.Repository")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
-[assembly: AssemblyCompany("Investec Bank")]
-[assembly: AssemblyProduct("AnyCompany.Tests")]
-[assembly: AssemblyCopyright("Copyright © Investec Bank 2018")]
+[assembly: AssemblyCompany("")]
+[assembly: AssemblyProduct("AnyCompany.Repository")]
+[assembly: AssemblyCopyright("Copyright © 2020")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
@@ -20,7 +20,7 @@
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
-[assembly: Guid("cd5d577e-bdc9-4dfc-ac6a-b1da474995f3")]
+[assembly: Guid("f2200daf-29ba-4eca-9c88-1d17fa51d56d")]
// Version information for an assembly consists of the following four values:
//
diff --git a/TechTest/AnyCompany.Service/AnyCompany.Service.csproj b/TechTest/AnyCompany.Service/AnyCompany.Service.csproj
new file mode 100644
index 0000000..d90888b
--- /dev/null
+++ b/TechTest/AnyCompany.Service/AnyCompany.Service.csproj
@@ -0,0 +1,59 @@
+
+
+
+
+ Debug
+ AnyCPU
+ {1B198A16-B0E0-47A9-9B31-95816AC1FEF9}
+ Library
+ Properties
+ AnyCompany.Service
+ AnyCompany.Service
+ v4.6.1
+ 512
+ true
+
+
+ true
+ full
+ false
+ bin\Debug\
+ DEBUG;TRACE
+ prompt
+ 4
+
+
+ pdbonly
+ true
+ bin\Release\
+ TRACE
+ prompt
+ 4
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ {fb9f5ea2-83ea-4964-8f50-7cb67b7ca57f}
+ AnyCompany.Entities
+
+
+ {f2200daf-29ba-4eca-9c88-1d17fa51d56d}
+ AnyCompany.Repository
+
+
+
+
\ No newline at end of file
diff --git a/TechTest/AnyCompany.Service/CustomerService.cs b/TechTest/AnyCompany.Service/CustomerService.cs
new file mode 100644
index 0000000..fe89439
--- /dev/null
+++ b/TechTest/AnyCompany.Service/CustomerService.cs
@@ -0,0 +1,58 @@
+using AnyCompany.Entities;
+using AnyCompany.Repository;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace AnyCompany.Service
+{
+
+ public static class CustomerService
+ {
+ public static bool Save(Customer customer, out string message)
+ {
+ message = "";
+ if ((customer.Country??"") == "")
+ {
+ message = "Please select a Country.";
+ return false;
+ }
+ if (customer.DateOfBirth.Year <= 1900 ||
+ customer.DateOfBirth.Year > DateTime.Now.Year)
+ {
+ message = "Please select a valid Date Of Birth.";
+ return false;
+ }
+ if ((customer.Name ?? "") == "")
+ {
+ message = "Please enter a name.";
+ return false;
+ }
+ CustomerRepository.Save(customer);
+ return true;
+ }
+ public static Customer Load(int customerId, out string message)
+ {
+ message = "";
+ if (customerId <= 0)
+ {
+ message = "Please select a customer id.";
+ return null;
+ }
+ Customer customer = CustomerRepository.Load(customerId);
+ if (customer.CustomerId <= 0)
+ {
+ message = $"Customer id {customerId} not found.";
+ return null;
+ }
+ return customer;
+ }
+ public static List LoadAll()
+ {
+ return CustomerRepository.LoadAll();
+ }
+
+ }
+}
diff --git a/TechTest/AnyCompany.Service/OrderService.cs b/TechTest/AnyCompany.Service/OrderService.cs
new file mode 100644
index 0000000..4329b85
--- /dev/null
+++ b/TechTest/AnyCompany.Service/OrderService.cs
@@ -0,0 +1,61 @@
+using AnyCompany.Entities;
+using AnyCompany.Repository;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace AnyCompany.Service
+{
+ public static class OrderService
+ {
+ public static bool PlaceOrder(Order order, out string message)
+ {
+ message = "";
+ if (order.CustomerId <= 0)
+ {
+ message = "Please select a customer.";
+ return false;
+ }
+
+ Customer customer = CustomerRepository.Load(order.CustomerId);
+
+ if (customer.CustomerId <= 0)
+ {
+ message = $"Customer id {order.CustomerId} not found.";
+ return false;
+ }
+
+ if (order.Amount <= 0)
+ {
+ message = "Amount can't be zero.";
+ return false;
+ }
+
+ if (customer.Country == "UK")
+ order.VAT = 0.2d;
+ else
+ order.VAT = 0;
+
+ OrderRepository.Save(order);
+
+ return true;
+ }
+
+ public static List LoadByCustomer(int customerId, out string message)
+ {
+ message = "";
+ List orderList = new List();
+ if (customerId <= 0)
+ {
+ message = "Please select a customer.";
+ return null;
+ }
+
+ orderList = OrderRepository.LoadByCustomer(customerId);
+
+ return orderList;
+ }
+ }
+}
diff --git a/TechTest/AnyCompany.Service/Properties/AssemblyInfo.cs b/TechTest/AnyCompany.Service/Properties/AssemblyInfo.cs
new file mode 100644
index 0000000..ca2a29b
--- /dev/null
+++ b/TechTest/AnyCompany.Service/Properties/AssemblyInfo.cs
@@ -0,0 +1,36 @@
+using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+// General Information about an assembly is controlled through the following
+// set of attributes. Change these attribute values to modify the information
+// associated with an assembly.
+[assembly: AssemblyTitle("AnyCompany.Service")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("")]
+[assembly: AssemblyProduct("AnyCompany.Service")]
+[assembly: AssemblyCopyright("Copyright © 2020")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+
+// Setting ComVisible to false makes the types in this assembly not visible
+// to COM components. If you need to access a type in this assembly from
+// COM, set the ComVisible attribute to true on that type.
+[assembly: ComVisible(false)]
+
+// The following GUID is for the ID of the typelib if this project is exposed to COM
+[assembly: Guid("1b198a16-b0e0-47a9-9b31-95816ac1fef9")]
+
+// Version information for an assembly consists of the following four values:
+//
+// Major Version
+// Minor Version
+// Build Number
+// Revision
+//
+// You can specify all the values or you can default the Build and Revision Numbers
+// by using the '*' as shown below:
+// [assembly: AssemblyVersion("1.0.*")]
+[assembly: AssemblyVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.0.0.0")]
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.UnitTest/AnyCompany.UnitTest.csproj b/TechTest/AnyCompany.UnitTest/AnyCompany.UnitTest.csproj
new file mode 100644
index 0000000..4b61a53
--- /dev/null
+++ b/TechTest/AnyCompany.UnitTest/AnyCompany.UnitTest.csproj
@@ -0,0 +1,82 @@
+
+
+
+
+
+ Debug
+ AnyCPU
+ {3A8D030B-10EC-43A0-991F-981059230FD1}
+ Library
+ Properties
+ AnyCompany.UnitTest
+ AnyCompany.UnitTest
+ v4.6.1
+ 512
+ {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}
+ 15.0
+ $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)
+ $(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages
+ False
+ UnitTest
+
+
+
+
+ true
+ full
+ false
+ bin\Debug\
+ DEBUG;TRACE
+ prompt
+ 4
+
+
+ pdbonly
+ true
+ bin\Release\
+ TRACE
+ prompt
+ 4
+
+
+
+ ..\packages\MSTest.TestFramework.1.3.2\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll
+
+
+ ..\packages\MSTest.TestFramework.1.3.2\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll
+
+
+
+
+
+
+
+
+
+
+
+
+
+ {fb9f5ea2-83ea-4964-8f50-7cb67b7ca57f}
+ AnyCompany.Entities
+
+
+ {f2200daf-29ba-4eca-9c88-1d17fa51d56d}
+ AnyCompany.Repository
+
+
+ {1b198a16-b0e0-47a9-9b31-95816ac1fef9}
+ AnyCompany.Service
+
+
+
+
+
+
+ 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.UnitTest/Properties/AssemblyInfo.cs b/TechTest/AnyCompany.UnitTest/Properties/AssemblyInfo.cs
new file mode 100644
index 0000000..393c23c
--- /dev/null
+++ b/TechTest/AnyCompany.UnitTest/Properties/AssemblyInfo.cs
@@ -0,0 +1,20 @@
+using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+[assembly: AssemblyTitle("AnyCompany.UnitTest")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("")]
+[assembly: AssemblyProduct("AnyCompany.UnitTest")]
+[assembly: AssemblyCopyright("Copyright © 2020")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+
+[assembly: ComVisible(false)]
+
+[assembly: Guid("3a8d030b-10ec-43a0-991f-981059230fd1")]
+
+// [assembly: AssemblyVersion("1.0.*")]
+[assembly: AssemblyVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.0.0.0")]
diff --git a/TechTest/AnyCompany.UnitTest/UnitTestAnyCompany.cs b/TechTest/AnyCompany.UnitTest/UnitTestAnyCompany.cs
new file mode 100644
index 0000000..f36fc2a
--- /dev/null
+++ b/TechTest/AnyCompany.UnitTest/UnitTestAnyCompany.cs
@@ -0,0 +1,209 @@
+using System;
+using System.Collections.Generic;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+namespace AnyCompany.UnitTest
+{
+ [TestClass]
+ public class UnitTestAnyCompany
+ {
+ [TestMethod]
+ public void TestTableCreate()
+ {
+ Repository.Context.CheckAllTableExist();
+ }
+ [TestMethod]
+ public void TestCreateCustomerNoCountry()
+ {
+ string message = "";
+ Entities.Customer customer = new Entities.Customer()
+ {
+ DateOfBirth = new DateTime(1987, 11, 23),
+ Name = "HJB",
+ };
+
+ Service.CustomerService.Save(customer, out message);
+
+ Assert.AreEqual("Please select a Country.", message);
+ }
+
+ [TestMethod]
+ public void TestCreateCustomerInvalidDOB()
+ {
+ string message = "";
+ Entities.Customer customer = new Entities.Customer()
+ {
+ Country = "US",
+ DateOfBirth = new DateTime(1800, 11, 23),
+ Name = "HJB",
+ };
+
+ Service.CustomerService.Save(customer, out message);
+
+ Assert.AreEqual("Please select a valid Date Of Birth.", message);
+ }
+
+ [TestMethod]
+ public void TestCreateCustomerNoName()
+ {
+ string message = "";
+ Entities.Customer customer = new Entities.Customer()
+ {
+ Country = "US",
+ DateOfBirth = new DateTime(1987,11,23),
+ };
+
+ Service.CustomerService.Save(customer, out message);
+
+ Assert.AreEqual("Please enter a name.", message);
+ }
+
+ [TestMethod]
+ public void TestCreateCustomer()
+ {
+ string message = "";
+ bool success = true;
+ if (success)
+ {
+ Entities.Customer customer = new Entities.Customer()
+ {
+ Country = "US",
+ DateOfBirth = new DateTime(1987, 11, 23),
+ Name = "HJB",
+ };
+
+ success = Service.CustomerService.Save(customer, out message);
+ }
+ if (success)
+ {
+ Entities.Customer customer = new Entities.Customer()
+ {
+ Country = "RSA",
+ DateOfBirth = new DateTime(1987, 11, 23),
+ Name = "CJB",
+ };
+ success = Service.CustomerService.Save(customer, out message);
+ }
+
+ if (success)
+ {
+ Entities.Customer customer = new Entities.Customer()
+ {
+ Country = "RSA",
+ DateOfBirth = new DateTime(1987, 11, 23),
+ Name = "CJB",
+ };
+ success = Service.CustomerService.Save(customer, out message);
+ }
+
+ if (success)
+ {
+ Entities.Customer customer = new Entities.Customer()
+ {
+ Country = "UK",
+ DateOfBirth = new DateTime(1988, 11, 23),
+ Name = "JRBCC",
+ };
+ success = Service.CustomerService.Save(customer, out message);
+ }
+
+ Assert.AreEqual(true, success);
+ }
+
+ [TestMethod]
+ public void TestLoadAllCustomer()
+ {
+ List customerList = Service.CustomerService.LoadAll();
+ }
+ [TestMethod]
+ public void TestPlaceOrderInvalidCustomer()
+ {
+ string message = "";
+ Entities.Order order = new Entities.Order()
+ {
+ CustomerId = -1,
+ Amount = 500,
+ };
+
+ Service.OrderService.PlaceOrder(order, out message);
+ Assert.AreEqual("Please select a customer.", message);
+
+ }
+ [TestMethod]
+ public void TestPlaceOrderInvalidAmount()
+ {
+ string message = "";
+ Entities.Order order = new Entities.Order()
+ {
+ CustomerId = 1,
+ Amount = 0,
+ };
+
+ Service.OrderService.PlaceOrder(order, out message);
+ Assert.AreEqual("Amount can't be zero.", message);
+
+ }
+ [TestMethod]
+ public void TestPlaceOrder()
+ {
+ string message = "";
+ Entities.Order order = new Entities.Order()
+ {
+ CustomerId = 1,
+ Amount = 500,
+ };
+
+ Service.OrderService.PlaceOrder(order, out message);
+ Assert.AreEqual("", message);
+
+ }
+
+ [TestMethod]
+ public void TestPlaceMultiOrders()
+ {
+ string message = "";
+ string messageAll = "";
+
+ List customerList = Service.CustomerService.LoadAll();
+ foreach (Entities.Customer customer in customerList)
+ {
+ for (int i = 0; i <= 5; i++)
+ {
+ Entities.Order order = new Entities.Order()
+ {
+ CustomerId = customer.CustomerId,
+ Amount = 500 * customer.CustomerId,
+ };
+ Service.OrderService.PlaceOrder(order, out message);
+ messageAll += message;
+ }
+ }
+
+ Assert.AreEqual("", messageAll);
+
+ }
+
+
+ [TestMethod]
+ public void TestLoadOrders()
+ {
+ string message = "";
+ Service.OrderService.LoadByCustomer(1, out message);
+ Assert.AreEqual("", message);
+ }
+
+ [TestMethod]
+ public void TestLoadCustomerAndOrders()
+ {
+ string message = "";
+ string messageAll = "";
+ List customerList = Service.CustomerService.LoadAll();
+ foreach (Entities.Customer customer in customerList)
+ {
+ Service.OrderService.LoadByCustomer(customer.CustomerId, out message);
+ messageAll += message;
+ }
+ Assert.AreEqual("", messageAll);
+ }
+ }
+}
diff --git a/TechTest/AnyCompany.UnitTest/packages.config b/TechTest/AnyCompany.UnitTest/packages.config
new file mode 100644
index 0000000..102a45c
--- /dev/null
+++ b/TechTest/AnyCompany.UnitTest/packages.config
@@ -0,0 +1,5 @@
+
+
+
+
+
\ 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/Order.cs b/TechTest/AnyCompany/Order.cs
deleted file mode 100644
index fec8e7b..0000000
--- a/TechTest/AnyCompany/Order.cs
+++ /dev/null
@@ -1,9 +0,0 @@
-namespace AnyCompany
-{
- public class Order
- {
- public int OrderId { 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
deleted file mode 100644
index ebfb103..0000000
--- a/TechTest/AnyCompany/OrderService.cs
+++ /dev/null
@@ -1,24 +0,0 @@
-namespace AnyCompany
-{
- public class OrderService
- {
- private readonly OrderRepository orderRepository = new OrderRepository();
-
- public bool PlaceOrder(Order order, int customerId)
- {
- Customer customer = CustomerRepository.Load(customerId);
-
- if (order.Amount == 0)
- return false;
-
- if (customer.Country == "UK")
- order.VAT = 0.2d;
- else
- order.VAT = 0;
-
- orderRepository.Save(order);
-
- return true;
- }
- }
-}
diff --git a/TechTest/TechTest.sln b/TechTest/TechTest.sln
index 1c1c57a..f5916ee 100644
--- a/TechTest/TechTest.sln
+++ b/TechTest/TechTest.sln
@@ -3,29 +3,41 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27004.2005
MinimumVisualStudioVersion = 10.0.40219.1
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AnyCompany", "AnyCompany\AnyCompany.csproj", "{C7E15594-7D8F-4C18-9DD7-14F3FBB1572D}"
-EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AnyCompany.Tests", "AnyCompany.Tests\AnyCompany.Tests.csproj", "{CD5D577E-BDC9-4DFC-AC6A-B1DA474995F3}"
-EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{B6D3C1BB-2A37-4E17-9EE3-DEF28286E782}"
ProjectSection(SolutionItems) = preProject
Instructions.txt = Instructions.txt
EndProjectSection
EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AnyCompany.UnitTest", "AnyCompany.UnitTest\AnyCompany.UnitTest.csproj", "{3A8D030B-10EC-43A0-991F-981059230FD1}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AnyCompany.Entities", "AnyCompany.Entities\AnyCompany.Entities.csproj", "{FB9F5EA2-83EA-4964-8F50-7CB67B7CA57F}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AnyCompany.Repository", "AnyCompany.Repository\AnyCompany.Repository.csproj", "{F2200DAF-29BA-4ECA-9C88-1D17FA51D56D}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AnyCompany.Service", "AnyCompany.Service\AnyCompany.Service.csproj", "{1B198A16-B0E0-47A9-9B31-95816AC1FEF9}"
+EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
- {C7E15594-7D8F-4C18-9DD7-14F3FBB1572D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
- {C7E15594-7D8F-4C18-9DD7-14F3FBB1572D}.Debug|Any CPU.Build.0 = Debug|Any CPU
- {C7E15594-7D8F-4C18-9DD7-14F3FBB1572D}.Release|Any CPU.ActiveCfg = Release|Any CPU
- {C7E15594-7D8F-4C18-9DD7-14F3FBB1572D}.Release|Any CPU.Build.0 = Release|Any CPU
- {CD5D577E-BDC9-4DFC-AC6A-B1DA474995F3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
- {CD5D577E-BDC9-4DFC-AC6A-B1DA474995F3}.Debug|Any CPU.Build.0 = Debug|Any CPU
- {CD5D577E-BDC9-4DFC-AC6A-B1DA474995F3}.Release|Any CPU.ActiveCfg = Release|Any CPU
- {CD5D577E-BDC9-4DFC-AC6A-B1DA474995F3}.Release|Any CPU.Build.0 = Release|Any CPU
+ {3A8D030B-10EC-43A0-991F-981059230FD1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {3A8D030B-10EC-43A0-991F-981059230FD1}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {3A8D030B-10EC-43A0-991F-981059230FD1}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {3A8D030B-10EC-43A0-991F-981059230FD1}.Release|Any CPU.Build.0 = Release|Any CPU
+ {FB9F5EA2-83EA-4964-8F50-7CB67B7CA57F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {FB9F5EA2-83EA-4964-8F50-7CB67B7CA57F}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {FB9F5EA2-83EA-4964-8F50-7CB67B7CA57F}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {FB9F5EA2-83EA-4964-8F50-7CB67B7CA57F}.Release|Any CPU.Build.0 = Release|Any CPU
+ {F2200DAF-29BA-4ECA-9C88-1D17FA51D56D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {F2200DAF-29BA-4ECA-9C88-1D17FA51D56D}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {F2200DAF-29BA-4ECA-9C88-1D17FA51D56D}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {F2200DAF-29BA-4ECA-9C88-1D17FA51D56D}.Release|Any CPU.Build.0 = Release|Any CPU
+ {1B198A16-B0E0-47A9-9B31-95816AC1FEF9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {1B198A16-B0E0-47A9-9B31-95816AC1FEF9}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {1B198A16-B0E0-47A9-9B31-95816AC1FEF9}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {1B198A16-B0E0-47A9-9B31-95816AC1FEF9}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE