Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keamogetswe Masisi #32

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
66 changes: 49 additions & 17 deletions TechTest/AnyCompany.Tests/AnyCompany.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
<?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="..\packages\NUnit.3.12.0\build\NUnit.props" Condition="Exists('..\packages\NUnit.3.12.0\build\NUnit.props')" />
<Import Project="..\packages\EntityFramework.6.4.4\build\EntityFramework.props" Condition="Exists('..\packages\EntityFramework.6.4.4\build\EntityFramework.props')" />
<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>{CD5D577E-BDC9-4DFC-AC6A-B1DA474995F3}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>AnyCompany.Tests</RootNamespace>
<AssemblyName>AnyCompany.Tests</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand All @@ -30,24 +34,52 @@
<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="EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<HintPath>..\packages\EntityFramework.6.4.4\lib\net45\EntityFramework.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="EntityFramework.SqlServer, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<HintPath>..\packages\EntityFramework.6.4.4\lib\net45\EntityFramework.SqlServer.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\..\..\..\..\..\..\..\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\PublicAssemblies\Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll</HintPath>
</Reference>
<Reference Include="nunit.framework, Version=3.12.0.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
<HintPath>..\packages\NUnit.3.12.0\lib\net45\nunit.framework.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<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="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\AnyCompany\AnyCompany.csproj">
<Project>{c7e15594-7d8f-4c18-9dd7-14f3fbb1572d}</Project>
<Name>AnyCompany</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>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}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\EntityFramework.6.4.4\build\EntityFramework.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\EntityFramework.6.4.4\build\EntityFramework.props'))" />
<Error Condition="!Exists('..\packages\EntityFramework.6.4.4\build\EntityFramework.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\EntityFramework.6.4.4\build\EntityFramework.targets'))" />
<Error Condition="!Exists('..\packages\NUnit.3.12.0\build\NUnit.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\NUnit.3.12.0\build\NUnit.props'))" />
</Target>
<Import Project="..\packages\EntityFramework.6.4.4\build\EntityFramework.targets" Condition="Exists('..\packages\EntityFramework.6.4.4\build\EntityFramework.targets')" />
</Project>
14 changes: 14 additions & 0 deletions TechTest/AnyCompany.Tests/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework"
type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
requirePermission="false"/>
</configSections>
<entityFramework>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
</providers>
</entityFramework>
</configuration>
64 changes: 63 additions & 1 deletion TechTest/AnyCompany.Tests/Class1.cs
Original file line number Diff line number Diff line change
@@ -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");
}

}
}
}
5 changes: 5 additions & 0 deletions TechTest/AnyCompany.Tests/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="EntityFramework" version="6.4.4" targetFramework="net461" />
<package id="NUnit" version="3.12.0" targetFramework="net461" />
</packages>
29 changes: 29 additions & 0 deletions TechTest/AnyCompany/AnyCompany.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\EntityFramework.6.4.4\build\EntityFramework.props" Condition="Exists('..\packages\EntityFramework.6.4.4\build\EntityFramework.props')" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
Expand All @@ -11,6 +12,8 @@
<AssemblyName>AnyCompany</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand All @@ -29,8 +32,19 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup>
<StartupObject />
</PropertyGroup>
<ItemGroup>
<Reference Include="EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<HintPath>..\packages\EntityFramework.6.4.4\lib\net45\EntityFramework.dll</HintPath>
</Reference>
<Reference Include="EntityFramework.SqlServer, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<HintPath>..\packages\EntityFramework.6.4.4\lib\net45\EntityFramework.SqlServer.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
Expand All @@ -42,10 +56,25 @@
<ItemGroup>
<Compile Include="Customer.cs" />
<Compile Include="CustomerRepository.cs" />
<Compile Include="CustomerService.cs" />
<Compile Include="ICustomerService.cs" />
<Compile Include="IOrderService.cs" />
<Compile Include="Order.cs" />
<Compile Include="OrderRepository.cs" />
<Compile Include="OrderService.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>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}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\EntityFramework.6.4.4\build\EntityFramework.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\EntityFramework.6.4.4\build\EntityFramework.props'))" />
<Error Condition="!Exists('..\packages\EntityFramework.6.4.4\build\EntityFramework.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\EntityFramework.6.4.4\build\EntityFramework.targets'))" />
</Target>
<Import Project="..\packages\EntityFramework.6.4.4\build\EntityFramework.targets" Condition="Exists('..\packages\EntityFramework.6.4.4\build\EntityFramework.targets')" />
</Project>
14 changes: 14 additions & 0 deletions TechTest/AnyCompany/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework"
type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
requirePermission="false"/>
</configSections>
<entityFramework>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
</providers>
</entityFramework>
</configuration>
6 changes: 6 additions & 0 deletions TechTest/AnyCompany/Customer.cs
Original file line number Diff line number Diff line change
@@ -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<Order> Orders { get; set; }
}
}
68 changes: 66 additions & 2 deletions TechTest/AnyCompany/CustomerRepository.cs
Original file line number Diff line number Diff line change
@@ -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)
{
Expand All @@ -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> order = new List<Order>();

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