Skip to content

Commit

Permalink
Made ownerid nulalble for voucher
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Nesterov committed Apr 29, 2020
1 parent 9adb338 commit 7dafb86
Show file tree
Hide file tree
Showing 8 changed files with 273 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/MAVN.Service.SmartVouchers.Domain/Models/Voucher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace MAVN.Service.SmartVouchers.Domain.Repositories
public interface IVouchersRepository
{
Task<long> 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<VoucherWithValidation> GetWithValidationByShortCodeAsync(string shortCode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,7 @@ public async Task<VoucherReservationResult> 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)
{
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -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<Guid>(
name: "owner_id",
schema: "smart_vouchers",
table: "voucher",
nullable: true,
oldClrType: typeof(Guid),
oldType: "uniqueidentifier");
}

protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterColumn<Guid>(
name: "owner_id",
schema: "smart_vouchers",
table: "voucher",
type: "uniqueidentifier",
nullable: false,
oldClrType: typeof(Guid),
oldNullable: true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ protected override void BuildModel(ModelBuilder modelBuilder)
.HasColumnName("campaign_id")
.HasColumnType("uniqueidentifier");

b.Property<Guid>("OwnerId")
b.Property<Guid?>("OwnerId")
.HasColumnName("owner_id")
.HasColumnType("uniqueidentifier");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,15 @@ public async Task<long> CreateAsync(Voucher voucher)
}
}

public async Task ReserveAsync(Voucher voucher)
public async Task ReserveAsync(Voucher voucher, Guid ownerId)
{
var entity = _mapper.Map<VoucherEntity>(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);
Expand All @@ -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);
Expand Down

0 comments on commit 7dafb86

Please sign in to comment.