Skip to content

Commit

Permalink
Version projections added
Browse files Browse the repository at this point in the history
  • Loading branch information
wojteksuwala committed Feb 4, 2019
1 parent 34a0393 commit 0081f63
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 6 deletions.
31 changes: 31 additions & 0 deletions SeparateModels/DbScripts/create_readmodel_tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,35 @@ create table policy_info_view
vehicle character varying(150) not null,
policy_holder character varying(50) not null,
total_premium numeric(19,2) not null
);


create table policy_version_view
(
policy_version_id uuid not null primary key,
policy_id uuid not null,
policy_number character varying(50) not null,
product_code character varying(50) not null,
version_number int not null,
version_status character varying(50) not null,
policy_status character varying(50) not null,
policy_holder character varying(250) not null,
insured character varying(250) not null,
car character varying(250) not null,
cover_from timestamp with time zone not null,
cover_to timestamp with time zone not null,
version_from timestamp with time zone not null,
version_to timestamp with time zone not null,
total_premium numeric(19,2) not null
);

create table policy_version_cover
(
policy_version_cover_id uuid not null primary key,
policy_version_id uuid not null,
code character varying(50) not null,
cover_from timestamp with time zone not null,
cover_to timestamp with time zone not null,
premium_amount numeric(19,2) not null,
constraint policy_version_fk foreign key (policy_version_id) references policy_version_view(policy_version_id)
);
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,20 @@ public class PolicyAnnexedProjectionsHandler :
INotificationHandler<PolicyAnnexed>
{
private readonly PolicyInfoDtoProjection policyInfoDtoProjection;
private readonly PolicyVersionDtoProjection policyVersionDtoProjection;

public PolicyAnnexedProjectionsHandler(PolicyInfoDtoProjection policyInfoDtoProjection)
public PolicyAnnexedProjectionsHandler(PolicyInfoDtoProjection policyInfoDtoProjection, PolicyVersionDtoProjection policyVersionDtoProjection)
{
this.policyInfoDtoProjection = policyInfoDtoProjection;
this.policyVersionDtoProjection = policyVersionDtoProjection;
}

public Task Handle(PolicyAnnexed @event, CancellationToken cancellationToken)
{
policyInfoDtoProjection.UpdatePolicyInfoDto(@event.AnnexedPolicy, @event.AnnexVersion);

policyVersionDtoProjection.CreatePolicyVersionDtoProjection(@event.AnnexedPolicy, @event.AnnexVersion);

return Task.CompletedTask;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,20 @@ public class PolicyChangesCancelledProjectionsHandler :
INotificationHandler<PolicyAnnexCancelled>
{
private readonly PolicyInfoDtoProjection policyInfoDtoProjection;
private readonly PolicyVersionDtoProjection policyVersionDtoProjection;

public PolicyChangesCancelledProjectionsHandler(PolicyInfoDtoProjection policyInfoDtoProjection)
public PolicyChangesCancelledProjectionsHandler(PolicyInfoDtoProjection policyInfoDtoProjection, PolicyVersionDtoProjection policyVersionDtoProjection)
{
this.policyInfoDtoProjection = policyInfoDtoProjection;
this.policyVersionDtoProjection = policyVersionDtoProjection;
}

public Task Handle(PolicyAnnexCancelled @event, CancellationToken cancellationToken)
{
policyInfoDtoProjection.UpdatePolicyInfoDto(@event.Policy, @event.CurrentVersionAfterAnnexCancellation);

policyVersionDtoProjection.UpdatePolicyVersionDtoProjection(@event.CancelledAnnexVersion);

return Task.CompletedTask;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,20 @@ public class PolicyCreatedProjectionsHandler :
INotificationHandler<PolicyCreated>
{
private readonly PolicyInfoDtoProjection policyInfoDtoProjection;
private readonly PolicyVersionDtoProjection policyVersionDtoProjection;

public PolicyCreatedProjectionsHandler(PolicyInfoDtoProjection policyInfoDtoProjection)
public PolicyCreatedProjectionsHandler(PolicyInfoDtoProjection policyInfoDtoProjection, PolicyVersionDtoProjection policyVersionDtoProjection)
{
this.policyInfoDtoProjection = policyInfoDtoProjection;
this.policyVersionDtoProjection = policyVersionDtoProjection;
}

public Task Handle(PolicyCreated @event, CancellationToken cancellationToken)
{
policyInfoDtoProjection.CreatePolicyInfoDto(@event.NewPolicy);

policyVersionDtoProjection.CreatePolicyVersionDtoProjection(@event.NewPolicy, @event.NewPolicy.Versions.WithNumber(1));

return Task.CompletedTask;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,20 @@ namespace SeparateModels.EventHandlers
public class PolicyTerminatedProjectionsHandler : INotificationHandler<PolicyTerminated>
{
private readonly PolicyInfoDtoProjection policyInfoDtoProjection;
private readonly PolicyVersionDtoProjection policyVersionDtoProjection;

public PolicyTerminatedProjectionsHandler(PolicyInfoDtoProjection policyInfoDtoProjection)
public PolicyTerminatedProjectionsHandler(PolicyInfoDtoProjection policyInfoDtoProjection, PolicyVersionDtoProjection policyVersionDtoProjection)
{
this.policyInfoDtoProjection = policyInfoDtoProjection;
this.policyVersionDtoProjection = policyVersionDtoProjection;
}

public Task Handle(PolicyTerminated @event, CancellationToken cancellationToken)
{
policyInfoDtoProjection.UpdatePolicyInfoDto(@event.TerminatedPolicy, @event.TerminatedVersion);

policyVersionDtoProjection.CreatePolicyVersionDtoProjection(@event.TerminatedPolicy, @event.TerminatedVersion);

return Task.CompletedTask;
}
}
Expand Down
2 changes: 2 additions & 0 deletions SeparateModels/ReadModels/PolicyDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace SeparateModels.ReadModels
{
public class PolicyVersionDto
{
public Guid Id { get; set; }
public Guid PolicyId { get; set; }
public string PolicyNumber { get; set; }
public string ProductCode { get; set; }
Expand All @@ -25,6 +26,7 @@ public class PolicyVersionDto

public class CoverDto
{
public Guid Id { get; set; }
public string Code { get; set; }
public DateTime CoverFrom { get; set; }
public DateTime CoverTo { get; set; }
Expand Down
2 changes: 2 additions & 0 deletions SeparateModels/ReadModels/PolicyDtoAssembler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public static PolicyVersionDto AssemblePolicyVersionDto(Policy policy, PolicyVer
{
return new PolicyVersionDto
{
Id = version.Id,
PolicyId = policy.Id,
PolicyNumber = policy.Number,
ProductCode = policy.ProductCode,
Expand Down Expand Up @@ -46,6 +47,7 @@ public static CoverDto AssembleCoverDto(PolicyCover policyCover)
{
return new CoverDto
{
Id = policyCover.Id,
Code = policyCover.CoverCode,
CoverFrom = policyCover.CoverPeriod.ValidFrom,
CoverTo = policyCover.CoverPeriod.ValidTo,
Expand Down
81 changes: 79 additions & 2 deletions SeparateModels/ReadModels/PolicyVersionDtoProjection.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
using System;
using Dapper;
using Marten.Linq.MatchesSql;
using Npgsql;
using SeparateModels.Domain;

namespace SeparateModels.ReadModels
{
public class PolicyVersionDtoProjection
Expand All @@ -9,12 +15,83 @@ public PolicyVersionDtoProjection(string cnString)
this.cnString = cnString;
}

public void CreatePolicyVersionDtoProjection()
public void CreatePolicyVersionDtoProjection(Policy policy, PolicyVersion policyVersion)
{
using (var cn = new NpgsqlConnection(cnString))
{
cn.Open();
using (var tx = cn.BeginTransaction())
{
cn.Execute
(
"insert into policy_version_view " +
"(policy_version_id, policy_id,policy_number,product_code,version_number," +
"version_status,policy_status,policy_holder,insured,car," +
"cover_from,cover_to,version_from,version_to,total_premium) " +
"values (@PolicyVersionId, @PolicyId, @PolicyNumber,@ProductCode,@VersionNumber," +
"@VersionStatus,@PolicyStatus,@PolicyHolder,@Insured,@Car," +
"@CoverFrom,@CoverTo,@VersionFrom,@VersionTo,@TotalPremium)",
new
{
PolicyVersionId = policyVersion.Id,
PolicyId = policy.Id,
PolicyNumber = policy.Number,
ProductCode = policy.ProductCode,
VersionNumber = policyVersion.VersionNumber,
VersionStatus = policyVersion.VersionStatus.ToString(),
PolicyStatus = policyVersion.PolicyStatus.ToString(),
PolicyHolder = $"{policyVersion.PolicyHolder.LastName} {policyVersion.PolicyHolder.FirstName} {policyVersion.PolicyHolder.TaxId}",
Insured = "",
Car = $"{policyVersion.Car.PlateNumber} {policyVersion.Car.Make} {policyVersion.Car.ProductionYear}",
CoverFrom = policyVersion.CoverPeriod.ValidFrom,
CoverTo = policyVersion.CoverPeriod.ValidTo,
VersionFrom = policyVersion.VersionValidityPeriod.ValidFrom,
VersionTo = policyVersion.VersionValidityPeriod.ValidTo,
TotalPremium = policyVersion.TotalPremium.Amount
}
);

foreach (var cover in policyVersion.Covers)
{
cn.Execute
(
"insert into policy_version_cover " +
"(policy_version_cover_id,policy_version_id,code,cover_from,cover_to,premium_amount)" +
"values " +
"(@PolicyVersionCoverId,@PolicyVersionId,@Code,@CoverFrom,@CoverTo,@PremiumAmount)",
new
{
PolicyVersionCoverId = Guid.NewGuid(),
PolicyVersionId = policyVersion.Id,
Code = cover.CoverCode,
CoverFrom = cover.CoverPeriod.ValidFrom,
CoverTo = cover.CoverPeriod.ValidTo,
PremiumAmount = cover.Amount.Amount
}
);
}

tx.Commit();
}
}
}

public void UpdatePolicyVersionDtoProjection()
public void UpdatePolicyVersionDtoProjection(PolicyVersion policyVersion)
{
using (var cn = new NpgsqlConnection(cnString))
{
cn.Execute
(
"update policy_version_view " +
"set version_status = @PolicyVersionStatus " +
"where policy_version_id = @PolicyVersionId",
new
{
PolicyVersionId = policyVersion.Id,
PolicyVersionStatus = policyVersion.VersionStatus.ToString()
}
);
}
}
}
}
20 changes: 20 additions & 0 deletions SeparateModels/ReadModels/PolicyVersionsListDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;

namespace SeparateModels.ReadModels
{
public class PolicyVersionsListDto
{
public string PolicyNumber { get; set; }
public List<PolicyVersionInfoDto> VersionsInfo { get; set; }
}

public class PolicyVersionInfoDto
{
public int Number { get; set; }
public DateTime VersionFrom { get; set; }
public DateTime VersionTo { get; set; }
public string VersionStatus { get; set; }
}

}
1 change: 1 addition & 0 deletions SeparateModels/ReadModels/ReadModelsInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public static IServiceCollection AddReadModels(this IServiceCollection services,
private static IServiceCollection AddPolicyInfoDtoProjection(this IServiceCollection services, string cnnString)
{
services.AddSingleton(new PolicyInfoDtoProjection(cnnString));
services.AddSingleton(new PolicyVersionDtoProjection(cnnString));
return services;
}

Expand Down

0 comments on commit 0081f63

Please sign in to comment.