Skip to content
Permalink
Browse files

Finish implementing API BL Closes #3

  • Loading branch information...
gariasf committed Mar 9, 2019
1 parent c635e55 commit 9cbb548d918407a44bb87287588ec9d7cb9315e2
@@ -1,6 +1,7 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using VuelingExam.Bussiness.Facade.Filters;
using VuelingExam.Bussiness.Logic.Interfaces;
using VuelingExam.Common.Logic.Models;
@@ -39,5 +40,12 @@ public ActionResult<Client> GetClientFromPolicy(Guid policyId)
{
return Ok(InsuranceBL.GetClientFromPolicy(policyId));
}

[ConnectionFilter]
[HttpGet("{clientId:Guid}")]
public ActionResult<List<Policy>> GetClientPolicies(Guid clientId)
{
return Ok(InsuranceBL.GetClientLinkedPolicies(clientId));
}
}
}
@@ -10,6 +10,7 @@
using System;
using System.Collections.Generic;
using System.Text;
using VuelingExam.Bussiness.Facade.Filters;
using VuelingExam.Common.Logic;
using VuelingExam.Common.Logic.Utils;

@@ -33,7 +34,9 @@ public Startup(IConfiguration configuration)
// This method gets called by the runtime. Use this method to add services to the container.
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddMvc(config => {
config.Filters.Add(new ConnectionFilterAttribute());
}).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

services.AddSwaggerGen(c =>
{
@@ -92,9 +92,24 @@ public Policy GetEntityById(Guid id)
return foundPolicy;
}

public Policy GetEntityByName(string name)
public List<Policy> GetListFilteredByConstraintId(Guid clientId)
{
throw new NotImplementedException();
Logger.Debug(StringResources.DebugMethod + System.Reflection.MethodBase.GetCurrentMethod().Name +
StringResources.DebugClass + System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

List<Policy> policyList;
try
{
policyList = PolicyRepository.GetListFilteredByConstraintId(clientId);
}
catch (VuelingDaoException e)
{
Logger.Error(e.Message);
Logger.Information(e.StackTrace);
throw new VuelingBussinessException(e.Message, e.InnerException);
}

return policyList;
}
}
}
@@ -37,7 +37,7 @@ public Client GetClientFromPolicy(Guid policyId)

public List<Policy> GetClientLinkedPolicies(Guid clientId)
{
throw new NotImplementedException();
return PolicyRepository.GetListFilteredByConstraintId(clientId);
}
}
}
@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace VuelingExam.Bussiness.Logic.Interfaces.Crud
{
public interface IGetListFilteredByConstraintId <T>
{
List<T> GetListFilteredByConstraintId(Guid clientId);
}
}
@@ -6,7 +6,7 @@

namespace VuelingExam.Bussiness.Logic.Interfaces
{
public interface IInsurancePolicy : IBussinessCrud<Policy>
public interface IInsurancePolicy : IBussinessCrud<Policy>, IGetListFilteredByConstraintId<Policy>
{
}
}
@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace VuelingExam.RepositoryLogic.Interfaces.Crud
{
public interface IGetListFilteredByConstraintId <T>
{
List<T> GetListFilteredByConstraintId(Guid clientId);
}
}
@@ -2,10 +2,11 @@
using System.Collections.Generic;
using System.Text;
using VuelingExam.Common.Logic.Models;
using VuelingExam.RepositoryLogic.Interfaces.Crud;

namespace VuelingExam.RepositoryLogic.Interfaces
{
public interface IPolicyRepository : IRepository<Policy>
public interface IPolicyRepository : IRepository<Policy>, IGetListFilteredByConstraintId<Policy>
{
}
}
@@ -16,9 +16,11 @@ namespace VuelingExam.RepositoryLogic
public class PolicyRepository : IPolicyRepository
{
private readonly static string ConnectionString = ConfigHelper.Config["dbConnectionString"];
private readonly IClientRepository ClientRepository;
private readonly ILogger Logger;
public PolicyRepository(ILogger logger)
public PolicyRepository(IClientRepository clientRepository,ILogger logger)
{
ClientRepository = clientRepository;
Logger = logger;
}

@@ -175,7 +177,8 @@ public Policy GetEntityById(Guid id)
InceptionDate = DateTime.Parse(record["InceptionDate"].ToString()),
InstallmentPayment = (bool)record["InstallmentPayment"],
ClientId = Guid.Parse(record["ClientId"].ToString()),
Email = record["Email"].ToString()
Email = record["Email"].ToString(),
Client = ClientRepository.GetEntityById(Guid.Parse(record["ClientId"].ToString()))
};
}
}
@@ -203,5 +206,83 @@ public Policy GetEntityById(Guid id)

return foundPolicy;
}

public List<Policy> GetListFilteredByConstraintId(Guid clientId)
{
Logger.Debug(StringResources.DebugMethod + System.Reflection.MethodBase.GetCurrentMethod().Name +
StringResources.DebugClass + System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

var policyList = new List<Policy>();
string sql = "SELECT * FROM dbo.Policy WHERE ClientId = @ClientId;";

try
{
using (SqlConnection connection = new SqlConnection(ConnectionString))
{
connection.Open();
using (SqlCommand command = new SqlCommand(sql, connection))
{
command.Parameters.Add("@ClientId", SqlDbType.UniqueIdentifier).Value = clientId;

using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
var record = (IDataRecord)reader;
var policy = new Policy
{
Id = Guid.Parse(record["Id"].ToString()),
AmountInsured = Decimal.Parse(record["AmountInsured"].ToString(), CultureInfo.InvariantCulture),
InceptionDate = DateTime.Parse(record["InceptionDate"].ToString()),
InstallmentPayment = (bool)record["InstallmentPayment"],
ClientId = Guid.Parse(record["ClientId"].ToString()),
Email = record["Email"].ToString(),
};

policyList.Add(policy);
}
}
}
}
}
catch (InvalidOperationException e)
{
Logger.Error(e.Message);
Logger.Information(e.StackTrace);
throw new VuelingDaoException(e.Message, e.InnerException);
}
catch (FormatException e)
{
Logger.Error(e.Message);
Logger.Information(e.StackTrace);
throw new VuelingDaoException(e.Message, e.InnerException);
}
catch (InvalidCastException e)
{
Logger.Error(e.Message);
Logger.Information(e.StackTrace);
throw new VuelingDaoException(e.Message, e.InnerException);
}
catch (OverflowException e)
{
Logger.Error(e.Message);
Logger.Information(e.StackTrace);
throw new VuelingDaoException(e.Message, e.InnerException);
}
catch (ArgumentNullException e)
{
Logger.Error(e.Message);
Logger.Information(e.StackTrace);
throw new VuelingDaoException(e.Message, e.InnerException);
}
catch (SqlException e)
{
Logger.Error(e.Message);
Logger.Information(e.StackTrace);
throw new VuelingDaoException(e.Message, e.InnerException);
}

return policyList;
}
}
}

0 comments on commit 9cbb548

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