Skip to content
Permalink
Browse files

Move data fetching ouside controllre & truncate tables on startup

Closes #6 Closes #7

Fixed databaseV3 field names
  • Loading branch information...
gariasf committed Mar 9, 2019
1 parent ea0dc0b commit c1107a8c20645714ce8f55fcbb3aa9f0f417f3c2
@@ -7,7 +7,7 @@ SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Policy](
[Id] [uniqueidentifier] NOT NULL,
[InsuredAmmount] [decimal](7, 2) NOT NULL,
[AmountInsured] [decimal](7, 2) NOT NULL,
[InceptionDate] [datetime] NOT NULL,
[InstallmentPayment] [bit] NOT NULL,
[ClientId] [uniqueidentifier] NOT NULL,
@@ -16,24 +16,13 @@ namespace VuelingExam.Bussiness.Facade.Controllers
[ApiController]
public class ExamController : ControllerBase
{
private readonly IHttpFetcher<Client> ClientFetcher;
private readonly IHttpFetcher<Policy> PolicyFetcher;
private readonly IBussinessCrud<Client> ClientRepositoryBL;
private readonly IBussinessCrud<Client> PolicyRepositoryBL;
private readonly IBussinessCrud<Policy> PolicyRepositoryBL;

public ExamController(IHttpFetcher<Client> clientFetcher, IHttpFetcher<Policy> policyFetcher, IBussinessCrud<Client> clientRepository, IBussinessCrud<Client> policyRepository)
public ExamController(IBussinessCrud<Client> clientRepository, IBussinessCrud<Policy> policyRepository)
{
ClientFetcher = clientFetcher;
PolicyFetcher = policyFetcher;
ClientRepositoryBL = clientRepository;
PolicyRepositoryBL = policyRepository;
FetchExternalData();
}

private void FetchExternalData()
{
ClientFetcher.FetchAndStoreData();
PolicyFetcher.FetchAndStoreData();
}

// GET api/values
@@ -17,23 +17,30 @@ protected override void Load(ContainerBuilder builder)

builder
.RegisterType<ClientBussinessCrudBL>()
.As<IBussinessCrud<Client>>();
.As<IBussinessCrud<Client>>()
.SingleInstance();

builder
.RegisterType<PolicyBussinessCrudBL>()
.As<IBussinessCrud<Policy>>();
.As<IBussinessCrud<Policy>>()
.SingleInstance();

builder
.RegisterType<AuthenticationBL>()
.As<IAuth<ApiUser>>();
.As<IAuth<ApiUser>>()
.SingleInstance();

builder
.RegisterType<HttpClientFetcherBL>()
.As<IHttpFetcher<Client>>();
.RegisterType<HttpPolicyFetcherBL>()
.AsSelf()
.AutoActivate();

builder
.RegisterType<HttpPolicyFetcherBL>()
.As<IHttpFetcher<Policy>>();
.RegisterType<HttpClientFetcherBL>()
.AsSelf()
.AutoActivate();



builder.RegisterModule(new BussinessModule());

@@ -8,8 +8,10 @@
using Microsoft.IdentityModel.Tokens;
using Swashbuckle.AspNetCore.Swagger;
using System;
using System.Collections.Generic;
using System.Text;
using VuelingExam.Common.Logic;
using VuelingExam.Common.Logic.Utils;

namespace VuelingExam.Bussiness.Facade
{
@@ -62,8 +64,9 @@ public IServiceProvider ConfigureServices(IServiceCollection services)
};
});

var container = AutofacConfigure.Configure(services);
RepositoryCleaner.CleanTables(new List<string> { "Policy", "Client" });

var container = AutofacConfigure.Configure(services);

return new AutofacServiceProvider(container);
}
@@ -22,6 +22,8 @@ public ClientBussinessCrudBL(IRepository<Client> repository, ILogger logger)
Logger = logger;
}



public Client Add(Client model)
{

@@ -24,6 +24,7 @@ public HttpClientFetcherBL(IBussinessCrud<Client> repository, ILogger logger)
{
Repository = repository;
Logger = logger;
FetchAndStoreData();
}

public void FetchAndStoreData()
@@ -61,6 +62,7 @@ public void StoreListToDatabase()
Logger.Debug(StringResources.DebugMethod + System.Reflection.MethodBase.GetCurrentMethod().Name +
StringResources.DebugClass + System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);


ClientList.ForEach(client =>
{
Logger.Debug(client.ToString());
@@ -17,14 +17,15 @@ public class HttpPolicyFetcherBL : IHttpFetcher<Policy>
{
private readonly static string ExternalApiEndpoint = ConfigHelper.Config["externalApiEndpoint"];
private readonly static string ExternalApiPolicyPath = ConfigHelper.Config["externalApiPolicyPath"];
private readonly IRepository<Policy> Repository;
private readonly IBussinessCrud<Policy> Repository;
private List<Policy> PolicyList = null;
private readonly ILogger Logger;

public HttpPolicyFetcherBL(IRepository<Policy> repository, ILogger logger)
public HttpPolicyFetcherBL(IBussinessCrud<Policy> repository, ILogger logger)
{
Repository = repository;
Logger = logger;
FetchAndStoreData();
}

public void FetchAndStoreData()
@@ -2,5 +2,6 @@
{
public interface IBussinessCrud<T> : IGetEntityById<T>, IAdd<T>, IGetAll<T>, IUpdate<T>, IDelete
{

}
}
@@ -22,6 +22,8 @@ public PolicyBussinessCrudBL(IRepository<Policy> repository, ILogger logger)
Logger = logger;
}



public Policy Add(Policy model)
{
Logger.Debug(StringResources.DebugMethod + System.Reflection.MethodBase.GetCurrentMethod().Name +
@@ -33,7 +35,6 @@ public Policy Add(Policy model)
{
result = Repository.Add(model);


}
catch (VuelingDaoException e)
{
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text;

namespace VuelingExam.Common.Logic.Utils
{
public static class RepositoryCleaner
{
private readonly static string ConnectionString = ConfigHelper.Config["dbConnectionString"];

public static void CleanTables(List<string> tableNames)
{
using (SqlConnection connection = new SqlConnection(ConnectionString))
{
connection.Open();
string command = null;

tableNames.ForEach(tableName =>
{
command += $"DELETE FROM dbo.{tableName};";
});

using (var sqlCommand = new SqlCommand(command, connection))
{
sqlCommand.ExecuteNonQuery();
}
}
}
}
}
@@ -7,6 +7,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" />
<PackageReference Include="System.Data.SqlClient" Version="4.6.0" />
</ItemGroup>

</Project>
@@ -4,6 +4,5 @@ namespace VuelingExam.RepositoryLogic.Interfaces
{
public interface IRepository<T> : IAdd<T>, IDelete, IGetAll<T>, IGetEntityById<T>, IUpdate<T>
{

}
}
@@ -25,6 +25,7 @@ public AuthenticationRepository(ILogger logger)
{
Logger = logger;
}

public ApiUser GenerateToken(ApiUser credentials)
{
Logger.Debug(StringResources.DebugMethod + System.Reflection.MethodBase.GetCurrentMethod().Name +
@@ -36,6 +36,9 @@ public Policy Add(Policy model)
command.Parameters.Add("@Id", SqlDbType.UniqueIdentifier).Value = model.Id;
command.Parameters.Add("@InceptionDate", SqlDbType.DateTime).Value = model.InceptionDate;
command.Parameters.Add("@AmountInsured", SqlDbType.Decimal).Value = model.AmountInsured;
SqlParameter ammoutnInsuredParam = command.Parameters["@AmountInsured"];
ammoutnInsuredParam.Precision = 8;
ammoutnInsuredParam.Scale = 4;
command.Parameters.Add("@InstallmentPayment", SqlDbType.Bit).Value = model.InstallmentPayment;
command.Parameters.Add("@ClientId", SqlDbType.UniqueIdentifier).Value = model.ClientId;
command.Parameters.Add("@Email", SqlDbType.VarChar, 150).Value = model.Email;

0 comments on commit c1107a8

Please sign in to comment.
You can’t perform that action at this time.