public async Task <AccountAction> CreateAsync(AccountAction entity)
        {
            var command = "INSERT INTO public.\"AccountActions\" " +
                          "(" +
                          $"\"{nameof(AccountAction.Id)}\", " +
                          $"\"{nameof(AccountAction.AccountId)}\", " +
                          $"\"{nameof(AccountAction.Type)}\", " +
                          $"\"{nameof(AccountAction.CreationDate)}\")" +
                          "VALUES (@Id, @AccountId, @Type, @CreationDate);";

            int rowsInserted;

            using (var sqlConnection = await _context.CreateConnectionAsync())
            {
                rowsInserted = await sqlConnection.ExecuteAsync(command, new
                {
                    entity.Id,
                    entity.AccountId,
                    entity.Type,
                    entity.CreationDate,
                });
            }

            if (rowsInserted != 1)
            {
                throw new ApplicationApiException(HttpStatusCode.BadRequest, $"Unable to create {nameof(AccountAction)} object");
            }

            return(entity);
        }
Ejemplo n.º 2
0
        public async Task <Transaction> CreateAsync(Transaction entity)
        {
            var command = "INSERT INTO public.\"Transactions\" " +
                          "(" +
                          $"\"{nameof(Transaction.Id)}\", " +
                          $"\"{nameof(Transaction.UserId)}\", " +
                          $"\"{nameof(Transaction.WriteOffAccount)}\", " +
                          $"\"{nameof(Transaction.DestinationAccount)}\", " +
                          $"\"{nameof(Transaction.Status)}\", " +
                          $"\"{nameof(Transaction.Type)}\", " +
                          $"\"{nameof(Transaction.Currency)}\", " +
                          $"\"{nameof(Transaction.Amount)}\", " +
                          $"\"{nameof(Transaction.CreationDate)}\"," +
                          $"\"{nameof(Transaction.ProceedDate)}\")" +
                          "VALUES (@Id, @UserId, @WriteOffAccount, @DestinationAccount, @Status, @Type, @Currency, @Amount, @CreationDate,@ProceedDate);";

            int rowsInserted;

            using (var sqlConnection = await _context.CreateConnectionAsync())
            {
                rowsInserted = await sqlConnection.ExecuteAsync(command, new
                {
                    entity.Id,
                    entity.UserId,
                    entity.WriteOffAccount,
                    entity.DestinationAccount,
                    entity.Status,
                    entity.Type,
                    entity.CreationDate,
                    entity.ProceedDate,
                    entity.Currency,
                    entity.Amount
                });
            }

            if (rowsInserted != 1)
            {
                throw new ApplicationApiException(HttpStatusCode.BadRequest, $"Unable to create {nameof(Transaction)} object");
            }

            return(entity);
        }
        public async Task <Account> CreateAsync(Account entity)
        {
            var createAccountCommand = "INSERT INTO public.\"Accounts\" " +
                                       "(" +
                                       $"\"{nameof(Account.Id)}\", " +
                                       $"\"{nameof(Account.UserId)}\", " +
                                       $"\"{nameof(Account.Balance)}\", " +
                                       $"\"{nameof(Account.IsDeleted)}\", " +
                                       $"\"{nameof(Account.CreationDate)}\", " +
                                       $"\"{nameof(Account.LastModified)}\"," +
                                       $"\"{nameof(Account.ExpiredDate)}\")" +
                                       " VALUES (@Id, @UserId, @Balance, @IsDeleted, @CreationDate, @LastModified, @ExpiredDate);";

            var createAccountDetailCommand = "INSERT INTO public.\"AccountDetails\" " +
                                             "(" +
                                             $"\"{nameof(AccountDetail.AccountId)}\", " +
                                             $"\"{nameof(AccountDetail.Description)}\", " +
                                             $"\"{nameof(AccountDetail.LimitByOperation)}\", " +
                                             $"\"{nameof(AccountDetail.CreationDate)}\", " +
                                             $"\"{nameof(AccountDetail.Currency)}\", " +
                                             $"\"{nameof(AccountDetail.LastModified)}\")" +
                                             " VALUES (@AccountId, @Description, @LimitByOperation, @CreationDate, @Currency, @LastModified);";

            using (var sqlConnection = await _context.CreateConnectionAsync())
            {
                using (var transaction = sqlConnection.BeginTransaction())
                {
                    await sqlConnection.ExecuteAsync(createAccountCommand, new
                    {
                        entity.Id,
                        entity.UserId,
                        entity.Balance,
                        entity.IsDeleted,
                        entity.CreationDate,
                        entity.ExpiredDate,
                        entity.LastModified
                    });

                    await sqlConnection.ExecuteAsync(createAccountDetailCommand, new
                    {
                        entity.AccountDetail.AccountId,
                        entity.AccountDetail.Description,
                        entity.AccountDetail.Currency,
                        entity.AccountDetail.LimitByOperation,
                        entity.AccountDetail.CreationDate,
                        entity.AccountDetail.LastModified
                    });

                    try
                    {
                        await transaction.CommitAsync();
                    }
                    catch (Exception ex)
                    {
                        await transaction.RollbackAsync();

                        throw new ApplicationApiException(HttpStatusCode.InternalServerError, ex.Message);
                    }
                }
            }

            return(await GetAsync(entity.Id, entity.UserId));
        }