From 7dafb86ac74476c802bd8c53c003ee51caa10d1b Mon Sep 17 00:00:00 2001 From: Sergey Nesterov Date: Wed, 29 Apr 2020 13:04:37 +0300 Subject: [PATCH] Made ownerid nulalble for voucher --- .../Models/Voucher.cs | 2 +- .../Repositories/IVouchersRepository.cs | 2 +- .../VouchersService.cs | 5 +- .../Entities/VoucherEntity.cs | 2 +- ...200429095915_MakeOwnerNullable.Designer.cs | 230 ++++++++++++++++++ .../20200429095915_MakeOwnerNullable.cs | 31 +++ .../SmartVouchersContextModelSnapshot.cs | 2 +- .../Repositories/VouchersRepository.cs | 8 +- 8 files changed, 273 insertions(+), 9 deletions(-) create mode 100644 src/MAVN.Service.SmartVouchers.MsSqlRepositories/Migrations/20200429095915_MakeOwnerNullable.Designer.cs create mode 100644 src/MAVN.Service.SmartVouchers.MsSqlRepositories/Migrations/20200429095915_MakeOwnerNullable.cs diff --git a/src/MAVN.Service.SmartVouchers.Domain/Models/Voucher.cs b/src/MAVN.Service.SmartVouchers.Domain/Models/Voucher.cs index e80a37a..af7916b 100644 --- a/src/MAVN.Service.SmartVouchers.Domain/Models/Voucher.cs +++ b/src/MAVN.Service.SmartVouchers.Domain/Models/Voucher.cs @@ -10,7 +10,7 @@ public class Voucher public string ValidationCodeHash { get; set; } public Guid CampaignId { get; set; } public VoucherStatus Status { get; set; } - public Guid OwnerId { get; set; } + public Guid? OwnerId { get; set; } public DateTime? PurchaseDate { get; set; } public DateTime? RedemptionDate { get; set; } } diff --git a/src/MAVN.Service.SmartVouchers.Domain/Repositories/IVouchersRepository.cs b/src/MAVN.Service.SmartVouchers.Domain/Repositories/IVouchersRepository.cs index 0d2a0ab..0e8b7f5 100644 --- a/src/MAVN.Service.SmartVouchers.Domain/Repositories/IVouchersRepository.cs +++ b/src/MAVN.Service.SmartVouchers.Domain/Repositories/IVouchersRepository.cs @@ -9,7 +9,7 @@ namespace MAVN.Service.SmartVouchers.Domain.Repositories public interface IVouchersRepository { Task CreateAsync(Voucher voucher); - Task ReserveAsync(Voucher voucher); + Task ReserveAsync(Voucher voucher, Guid ownerId); Task CancelReservationAsync(Voucher voucher); Task UpdateAsync(Voucher voucher, string validationCode = null); Task GetWithValidationByShortCodeAsync(string shortCode); diff --git a/src/MAVN.Service.SmartVouchers.DomainServices/VouchersService.cs b/src/MAVN.Service.SmartVouchers.DomainServices/VouchersService.cs index 92cc716..d311263 100644 --- a/src/MAVN.Service.SmartVouchers.DomainServices/VouchersService.cs +++ b/src/MAVN.Service.SmartVouchers.DomainServices/VouchersService.cs @@ -92,9 +92,7 @@ public async Task ReserveVoucherAsync(Guid voucherCamp try { voucher = vouchers.FirstOrDefault(); - voucher.OwnerId = ownerId; - voucher.Status = VoucherStatus.Reserved; - await _vouchersRepository.ReserveAsync(voucher); + await _vouchersRepository.ReserveAsync(voucher, ownerId); } catch (Exception e) { @@ -282,7 +280,6 @@ private string GenerateShortCodeFromId(long voucherId) return ProcessingVoucherError.VoucherNotFound; } - voucher.Status = VoucherStatus.InStock; await _vouchersRepository.CancelReservationAsync(voucher); await _redisLocksService.ReleaseLockAsync(voucher.ShortCode, voucher.ShortCode); diff --git a/src/MAVN.Service.SmartVouchers.MsSqlRepositories/Entities/VoucherEntity.cs b/src/MAVN.Service.SmartVouchers.MsSqlRepositories/Entities/VoucherEntity.cs index f173abd..3e37c99 100644 --- a/src/MAVN.Service.SmartVouchers.MsSqlRepositories/Entities/VoucherEntity.cs +++ b/src/MAVN.Service.SmartVouchers.MsSqlRepositories/Entities/VoucherEntity.cs @@ -28,7 +28,7 @@ public class VoucherEntity public VoucherStatus Status { get; set; } [Column("owner_id")] - public Guid OwnerId { get; set; } + public Guid? OwnerId { get; set; } [Column("purchase_date")] public DateTime? PurchaseDate { get; set; } diff --git a/src/MAVN.Service.SmartVouchers.MsSqlRepositories/Migrations/20200429095915_MakeOwnerNullable.Designer.cs b/src/MAVN.Service.SmartVouchers.MsSqlRepositories/Migrations/20200429095915_MakeOwnerNullable.Designer.cs new file mode 100644 index 0000000..1900b8c --- /dev/null +++ b/src/MAVN.Service.SmartVouchers.MsSqlRepositories/Migrations/20200429095915_MakeOwnerNullable.Designer.cs @@ -0,0 +1,230 @@ +// +using System; +using MAVN.Service.SmartVouchers.MsSqlRepositories; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +namespace MAVN.Service.SmartVouchers.MsSqlRepositories.Migrations +{ + [DbContext(typeof(SmartVouchersContext))] + [Migration("20200429095915_MakeOwnerNullable")] + partial class MakeOwnerNullable + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasDefaultSchema("smart_vouchers") + .HasAnnotation("ProductVersion", "3.1.3") + .HasAnnotation("Relational:MaxIdentifierLength", 128) + .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); + + modelBuilder.Entity("MAVN.Service.SmartVouchers.MsSqlRepositories.Entities.PaymentRequestEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnName("id") + .HasColumnType("uniqueidentifier"); + + b.Property("VoucherShortCode") + .IsRequired() + .HasColumnName("short_code") + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("payment_request"); + }); + + modelBuilder.Entity("MAVN.Service.SmartVouchers.MsSqlRepositories.Entities.VoucherCampaignContentEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnName("id") + .HasColumnType("uniqueidentifier"); + + b.Property("CampaignId") + .HasColumnName("campaign_id") + .HasColumnType("uniqueidentifier"); + + b.Property("ContentType") + .HasColumnName("content_type") + .HasColumnType("int"); + + b.Property("Language") + .HasColumnName("language") + .HasColumnType("int"); + + b.Property("Value") + .HasColumnName("value") + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.HasIndex("CampaignId"); + + b.ToTable("campaign_content"); + }); + + modelBuilder.Entity("MAVN.Service.SmartVouchers.MsSqlRepositories.Entities.VoucherCampaignEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnName("id") + .HasColumnType("uniqueidentifier"); + + b.Property("BoughtVouchersCount") + .HasColumnName("bought_vouchers_count") + .HasColumnType("int"); + + b.Property("CreatedBy") + .IsRequired() + .HasColumnName("created_by") + .HasColumnType("nvarchar(max)"); + + b.Property("CreationDate") + .HasColumnName("creation_date") + .HasColumnType("datetime2"); + + b.Property("Currency") + .IsRequired() + .HasColumnName("currency") + .HasColumnType("nvarchar(max)"); + + b.Property("Description") + .HasColumnName("description") + .HasColumnType("nvarchar(max)"); + + b.Property("FromDate") + .HasColumnName("from_date") + .HasColumnType("datetime2"); + + b.Property("Name") + .IsRequired() + .HasColumnName("name") + .HasColumnType("nvarchar(max)"); + + b.Property("PartnerId") + .HasColumnName("partner_id") + .HasColumnType("uniqueidentifier"); + + b.Property("State") + .HasColumnName("state") + .HasColumnType("smallint"); + + b.Property("ToDate") + .HasColumnName("to_date") + .HasColumnType("datetime2"); + + b.Property("VoucherPrice") + .HasColumnName("voucher_price") + .HasColumnType("decimal(18,2)"); + + b.Property("VouchersTotalCount") + .HasColumnName("vouchers_total_count") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.ToTable("campaign"); + }); + + modelBuilder.Entity("MAVN.Service.SmartVouchers.MsSqlRepositories.Entities.VoucherEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnName("id") + .HasColumnType("bigint") + .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); + + b.Property("CampaignId") + .HasColumnName("campaign_id") + .HasColumnType("uniqueidentifier"); + + b.Property("OwnerId") + .HasColumnName("owner_id") + .HasColumnType("uniqueidentifier"); + + b.Property("PurchaseDate") + .HasColumnName("purchase_date") + .HasColumnType("datetime2"); + + b.Property("RedemptionDate") + .HasColumnName("redemption_date") + .HasColumnType("datetime2"); + + b.Property("ShortCode") + .HasColumnName("short_code") + .HasColumnType("nvarchar(450)"); + + b.Property("Status") + .HasColumnName("status") + .HasColumnType("smallint"); + + b.Property("ValidationCodeHash") + .IsRequired() + .HasColumnName("validation_code_hash") + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.HasIndex("CampaignId"); + + b.HasIndex("OwnerId"); + + b.HasIndex("ShortCode") + .IsUnique() + .HasFilter("[short_code] IS NOT NULL"); + + b.ToTable("voucher"); + }); + + modelBuilder.Entity("MAVN.Service.SmartVouchers.MsSqlRepositories.Entities.VoucherValidationEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnName("id") + .HasColumnType("bigint") + .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); + + b.Property("ValidationCode") + .IsRequired() + .HasColumnName("validation_code") + .HasColumnType("nvarchar(max)"); + + b.Property("VoucherId") + .HasColumnName("voucher_id") + .HasColumnType("bigint"); + + b.HasKey("Id"); + + b.HasIndex("VoucherId") + .IsUnique(); + + b.ToTable("voucher_validation"); + }); + + modelBuilder.Entity("MAVN.Service.SmartVouchers.MsSqlRepositories.Entities.VoucherCampaignContentEntity", b => + { + b.HasOne("MAVN.Service.SmartVouchers.MsSqlRepositories.Entities.VoucherCampaignEntity", null) + .WithMany("LocalizedContents") + .HasForeignKey("CampaignId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("MAVN.Service.SmartVouchers.MsSqlRepositories.Entities.VoucherValidationEntity", b => + { + b.HasOne("MAVN.Service.SmartVouchers.MsSqlRepositories.Entities.VoucherEntity", null) + .WithOne("Validation") + .HasForeignKey("MAVN.Service.SmartVouchers.MsSqlRepositories.Entities.VoucherValidationEntity", "VoucherId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/src/MAVN.Service.SmartVouchers.MsSqlRepositories/Migrations/20200429095915_MakeOwnerNullable.cs b/src/MAVN.Service.SmartVouchers.MsSqlRepositories/Migrations/20200429095915_MakeOwnerNullable.cs new file mode 100644 index 0000000..e3d8428 --- /dev/null +++ b/src/MAVN.Service.SmartVouchers.MsSqlRepositories/Migrations/20200429095915_MakeOwnerNullable.cs @@ -0,0 +1,31 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +namespace MAVN.Service.SmartVouchers.MsSqlRepositories.Migrations +{ + public partial class MakeOwnerNullable : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AlterColumn( + name: "owner_id", + schema: "smart_vouchers", + table: "voucher", + nullable: true, + oldClrType: typeof(Guid), + oldType: "uniqueidentifier"); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.AlterColumn( + name: "owner_id", + schema: "smart_vouchers", + table: "voucher", + type: "uniqueidentifier", + nullable: false, + oldClrType: typeof(Guid), + oldNullable: true); + } + } +} diff --git a/src/MAVN.Service.SmartVouchers.MsSqlRepositories/Migrations/SmartVouchersContextModelSnapshot.cs b/src/MAVN.Service.SmartVouchers.MsSqlRepositories/Migrations/SmartVouchersContextModelSnapshot.cs index 4ca75bf..d9d1c3a 100644 --- a/src/MAVN.Service.SmartVouchers.MsSqlRepositories/Migrations/SmartVouchersContextModelSnapshot.cs +++ b/src/MAVN.Service.SmartVouchers.MsSqlRepositories/Migrations/SmartVouchersContextModelSnapshot.cs @@ -142,7 +142,7 @@ protected override void BuildModel(ModelBuilder modelBuilder) .HasColumnName("campaign_id") .HasColumnType("uniqueidentifier"); - b.Property("OwnerId") + b.Property("OwnerId") .HasColumnName("owner_id") .HasColumnType("uniqueidentifier"); diff --git a/src/MAVN.Service.SmartVouchers.MsSqlRepositories/Repositories/VouchersRepository.cs b/src/MAVN.Service.SmartVouchers.MsSqlRepositories/Repositories/VouchersRepository.cs index 8456388..8ab2b54 100644 --- a/src/MAVN.Service.SmartVouchers.MsSqlRepositories/Repositories/VouchersRepository.cs +++ b/src/MAVN.Service.SmartVouchers.MsSqlRepositories/Repositories/VouchersRepository.cs @@ -43,12 +43,15 @@ public async Task CreateAsync(Voucher voucher) } } - public async Task ReserveAsync(Voucher voucher) + public async Task ReserveAsync(Voucher voucher, Guid ownerId) { var entity = _mapper.Map(voucher); using (var context = _contextFactory.CreateDataContext()) { + entity.OwnerId = ownerId; + entity.Status = VoucherStatus.Reserved; + context.Vouchers.Update(entity); var campaign = await context.VoucherCampaigns.FindAsync(entity.CampaignId); @@ -65,6 +68,9 @@ public async Task CancelReservationAsync(Voucher voucher) using (var context = _contextFactory.CreateDataContext()) { + entity.Status = VoucherStatus.InStock; + entity.OwnerId = null; + context.Vouchers.Update(entity); var campaign = await context.VoucherCampaigns.FindAsync(entity.CampaignId);