示例#1
0
        public async Task <ActionResult <Transaction> > PostTransaction(Transaction transaction)
        {
            CategoryValidator validator = new CategoryValidator();

            if (validator.CategoryIsValid(transaction))
            {
                _context.Transactions.Add(transaction);
                await _context.SaveChangesAsync();

                return(CreatedAtAction("GetTransaction", new { id = transaction.Id }, transaction));
            }

            string errorMessage = validator.Message(transaction.Direction);

            return(BadRequest(new { error = errorMessage }));
        }
示例#2
0
        public async Task <ActionResult <Transaction> > PutTransaction(long id, Transaction transaction)
        {
            if (id != transaction.Id)
            {
                return(BadRequest());
            }

            CategoryValidator validator = new CategoryValidator();

            if (validator.CategoryIsValid(transaction))
            {
                _context.Entry(transaction).State = EntityState.Modified;

                try
                {
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!TransactionExists(id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }

                return(transaction);
            }

            string errorMessage = validator.Message(transaction.Direction);

            return(BadRequest(new { error = errorMessage }));
        }