Пример #1
0
 public Task <CommandResult> Post([FromBody] AddTransactionDto input)
 {
     return(dispatcher.Dispatch(new AddTransactionCommand()
     {
         Amount = input.Amount,
         CheckNumber = input.CheckNumber,
         Date = input.Date,
         Description = input.Description,
         GoodsOrServicesGiven = input.GoodsOrServicesGiven,
         Type = (Application.Transactions.Commands.TransactionType)input.Type,
         UserId = input.UserId
     }));
 }
Пример #2
0
        public IActionResult AddTransaction([FromBody] AddTransactionDto addTransactionDto)
        {
            // TODO validation, should be a single transaction scope
            var response = new AddTransactionResponse();

            if (addTransactionDto.Vendor.Id == null) // todo to be moved
            {
                var vendor = new Vendor()
                {
                    Name = addTransactionDto.Vendor.Name,
                    DefaultSubcategoryId = addTransactionDto.SubcategoryId
                };
                _vendorStore.SaveVendor(vendor);
                addTransactionDto.Vendor.Id = vendor.Id;
                addTransactionDto.Vendor.DefaultSubcategoryId = addTransactionDto.SubcategoryId;
                response.NewVendor = addTransactionDto.Vendor;
            }

            foreach (var newTag in addTransactionDto.Tags.Where(x => x.Id is null))
            {
                var tag = new Tag()
                {
                    Name = newTag.Name,
                };
                _tagStore.SaveTag(tag);
                newTag.Id = tag.Id;

                response.NewTags.Add(newTag);
            }

            var createdTransaction = new ApiModels.Transaction()
            {
                Amount          = addTransactionDto.Amount,
                CreatedDate     = DateTime.UtcNow,
                CreatedBy       = "API",
                Description     = addTransactionDto.Description,
                IsExpense       = true,
                ModifiedDate    = DateTime.UtcNow,
                SubcategoryId   = addTransactionDto.SubcategoryId,
                TagIds          = addTransactionDto.Tags.Select(x => x.Id.Value).ToList(),
                TransactionDate = addTransactionDto.TransactionDate,
                VendorId        = addTransactionDto.Vendor.Id.Value
            };

            _transactionStore.SaveTransaction(createdTransaction);

            response.Transaction = createdTransaction;

            return(StatusCode(StatusCodes.Status201Created, response));
        }
Пример #3
0
        public async Task <IActionResult> UpdateTransaction([FromRoute] int transactionId, [FromBody] AddTransactionDto updatedTransaction)
        {
            var transaction = await _transactionStore.GetTransaction(transactionId);

            if (transaction == null)
            {
                return(NotFound());
            }

            var response = new AddTransactionResponse();

            if (updatedTransaction.Vendor.Id == null) // todo to be moved
            {
                var vendor = new Vendor()
                {
                    Name = updatedTransaction.Vendor.Name,
                    DefaultSubcategoryId = updatedTransaction.SubcategoryId
                };
                _vendorStore.SaveVendor(vendor);
                updatedTransaction.Vendor.Id = vendor.Id;
                updatedTransaction.Vendor.DefaultSubcategoryId = updatedTransaction.SubcategoryId;
                response.NewVendor = updatedTransaction.Vendor;
            }

            foreach (var newTag in updatedTransaction.Tags.Where(x => x.Id is null))
            {
                var tag = new Tag()
                {
                    Name = newTag.Name,
                };
                _tagStore.SaveTag(tag);
                newTag.Id = tag.Id;

                response.NewTags.Add(newTag);
            }

            transaction.Amount          = updatedTransaction.Amount;
            transaction.Description     = updatedTransaction.Description;
            transaction.ModifiedDate    = DateTime.UtcNow;
            transaction.SubcategoryId   = updatedTransaction.SubcategoryId;
            transaction.TagIds          = updatedTransaction.Tags.Select(x => x.Id.Value).ToList();
            transaction.TransactionDate = updatedTransaction.TransactionDate;
            transaction.VendorId        = updatedTransaction.Vendor.Id.Value;

            await _transactionStore.UpdateTransaction(transaction);

            response.Transaction = transaction;

            return(Ok(response));
        }