Пример #1
0
    public async Task <Unit> Handle(UpdateTransactionCommand request, CancellationToken cancellationToken)
    {
        var cardStatement = await _context.CardStatements.FindAsync(new object[] { Guid.Parse(request.CardStatementId) }, cancellationToken);

        if (cardStatement == null || cardStatement.UserId != _userService.UserId)
        {
            throw new NotFoundException(nameof(Transaction), request.Id);
        }

        var bankCard =
            await _context.BankCards.FindAsync(new object[] { Guid.Parse(cardStatement.BankCardId) }, cancellationToken);

        cardStatement.UpdateTransaction(new Transaction
        {
            Id                  = Guid.Parse(request.Id),
            Amount              = request.Amount,
            Description         = request.Description,
            Date                = request.Date,
            PaymentType         = PaymentType.From(request.PaymentType),
            TransactionCategory = TransactionCategory.From(request.TransactionCategory)
        });

        cardStatement.UpdateStatementReward(bankCard.GetEffectiveRewardVersion(cardStatement.MonthYear));
        await _context.SaveChangesAsync(cancellationToken);

        return(Unit.Value);
    }
Пример #2
0
    public async Task <string> Handle(CreateTransactionCommand request, CancellationToken cancellationToken)
    {
        var cardStatement =
            await _context.CardStatements.FindAsync(new object[] { Guid.Parse(request.CardStatementId) }, cancellationToken);

        if (cardStatement == null)
        {
            throw new NotFoundException(nameof(CardStatement), request.CardStatementId);
        }

        var bankCard =
            await _context.BankCards.FindAsync(new object[] { Guid.Parse(cardStatement.BankCardId) }, cancellationToken);

        var transactionEntity = new Transaction
        {
            Id                  = Guid.NewGuid(),
            PaymentType         = PaymentType.From(request.PaymentType),
            TransactionCategory = TransactionCategory.From(request.TransactionCategory),
            Description         = request.Description,
            Date                = request.Date,
            Amount              = request.Amount
        };

        cardStatement.Transactions.Add(transactionEntity);
        cardStatement.UpdateStatementReward(bankCard.GetEffectiveRewardVersion(cardStatement.MonthYear));

        await _context.SaveChangesAsync(cancellationToken);

        return(transactionEntity.Id.ToString());
    }
Пример #3
0
    public void Configure(EntityTypeBuilder <CardStatement> builder)
    {
        builder.HasKey(cardStatement => cardStatement.Id);
        builder.HasPartitionKey(cardStatement => cardStatement.UserId);

        builder.Property(cardStatement => cardStatement.Id)
        .ToJsonProperty("id")
        .HasConversion <string>();

        builder.Property(cardStatement => cardStatement.CardId)
        .ToJsonProperty("cardId")
        .IsRequired();

        builder.Property(cardStatement => cardStatement.BankCardId)
        .ToJsonProperty("bankCardId")
        .IsRequired();

        builder.Property(cardStatement => cardStatement.MonthYear)
        .ToJsonProperty("monthYear")
        .IsRequired();

        builder.Property(cardStatement => cardStatement.UserId)
        .ToJsonProperty("userId")
        .IsRequired();

        builder.OwnsOne(cardStatement => cardStatement.StatementReward, srBuilder =>
        {
            srBuilder.ToJsonProperty("statementReward");
            srBuilder.Property(reward => reward.Miles).ToJsonProperty("miles");
            srBuilder.Property(reward => reward.Cashback).ToJsonProperty("cashback");
            srBuilder.Property(reward => reward.Points).ToJsonProperty("points");
        });

        builder.OwnsMany(cardStatement => cardStatement.Transactions, action =>
        {
            action.ToJsonProperty("transactions");
            action.HasKey(transaction => transaction.Id);

            action.Property(transaction => transaction.Id)
            .ValueGeneratedNever()
            .ToJsonProperty("id");

            action.Property(transaction => transaction.CardStatementId)
            .ToJsonProperty("cardStatementId");

            action.Property(transaction => transaction.Date)
            .ToJsonProperty("date")
            .IsRequired();

            action.Property(transaction => transaction.Amount)
            .ToJsonProperty("amount")
            .IsRequired();

            action.Property(transaction => transaction.Description)
            .ToJsonProperty("description")
            .HasMaxLength(200)
            .IsRequired();

            action.Property(transaction => transaction.TransactionCategory)
            .ToJsonProperty("transactionCategory")
            .HasConversion(
                tc => tc.Value,
                tc => TransactionCategory.From(tc));

            action.Property(transaction => transaction.PaymentType)
            .ToJsonProperty("paymentType")
            .HasConversion(
                pt => pt.Value,
                pt => PaymentType.From(pt));
        });
    }