Skip to content
Open

HJB #21

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{C7E15594-7D8F-4C18-9DD7-14F3FBB1572D}</ProjectGuid>
<ProjectGuid>{FB9F5EA2-83EA-4964-8F50-7CB67B7CA57F}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>AnyCompany</RootNamespace>
<AssemblyName>AnyCompany</AssemblyName>
<RootNamespace>AnyCompany.Entities</RootNamespace>
<AssemblyName>AnyCompany.Entities</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand Down Expand Up @@ -41,10 +42,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Customer.cs" />
<Compile Include="CustomerRepository.cs" />
<Compile Include="Order.cs" />
<Compile Include="OrderRepository.cs" />
<Compile Include="OrderService.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Expand Down
Original file line number Diff line number Diff line change
@@ -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; }
}
}
16 changes: 16 additions & 0 deletions TechTest/AnyCompany.Entities/Order.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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("")]

Expand All @@ -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:
//
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>cd5d577e-bdc9-4dfc-ac6a-b1da474995f3</ProjectGuid>
<ProjectGuid>{F2200DAF-29BA-4ECA-9C88-1D17FA51D56D}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>AnyCompany.Tests</RootNamespace>
<AssemblyName>AnyCompany.Tests</AssemblyName>
<RootNamespace>AnyCompany.Repository</RootNamespace>
<AssemblyName>AnyCompany.Repository</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand All @@ -30,24 +31,26 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System"/>

<Reference Include="System.Core"/>
<Reference Include="System.Xml.Linq"/>
<Reference Include="System.Data.DataSetExtensions"/>


<Reference Include="Microsoft.CSharp"/>

<Reference Include="System.Data"/>

<Reference Include="System.Net.Http"/>

<Reference Include="System.Xml"/>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Class1.cs" />
<Compile Include="Context.cs" />
<Compile Include="CustomerRepository.cs" />
<Compile Include="OrderRepository.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\AnyCompany.Entities\AnyCompany.Entities.csproj">
<Project>{fb9f5ea2-83ea-4964-8f50-7cb67b7ca57f}</Project>
<Name>AnyCompany.Entities</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
</Project>
18 changes: 18 additions & 0 deletions TechTest/AnyCompany.Repository/Context.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
134 changes: 134 additions & 0 deletions TechTest/AnyCompany.Repository/CustomerRepository.cs
Original file line number Diff line number Diff line change
@@ -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<Customer> LoadAll()
{
List<Customer> customerList = new List<Customer>();

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;
}
}
}
Loading