コード例 #1
0
        public async Task <ActionResult <FixedSpend> > Put(int id, FixedSpend updatedFixedSpend)
        {
            try
            {
                FixedSpend fixedSpend = await _context.FixedSpends.AsNoTracking().FirstOrDefaultAsync(findIncome => findIncome.Id == id);

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

                _context.Entry(updatedFixedSpend).State = EntityState.Modified;
                if ((await _context.SaveChangesAsync()) > 0)
                {
                    return(Ok(updatedFixedSpend));
                }
                else
                {
                    return(BadRequest($"Não foi possível atualizar o gasto fixo {updatedFixedSpend.Name}"));
                }
            }
            catch (Exception exception)
            {
                return(InternalError(exception));
            }
        }
コード例 #2
0
        public async Task <ActionResult> Delete(int id)
        {
            try
            {
                FixedSpend fixedSpend = await _context.FixedSpends.AsNoTracking().FirstOrDefaultAsync(findFixedSpend => findFixedSpend.Id == id);

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

                _context.FixedSpends.Remove(fixedSpend);
                if ((await _context.SaveChangesAsync()) > 0)
                {
                    return(Ok());
                }
                else
                {
                    return(BadRequest($"Não foi possível apagar o gasto fixo {fixedSpend.Name}"));
                }
            }
            catch (Exception exception)
            {
                return(InternalError(exception));
            }
        }
コード例 #3
0
        public async Task <ActionResult <FixedSpend> > Post(FixedSpend fixedSpend)
        {
            try
            {
                await _context.FixedSpends.AddAsync(fixedSpend);

                if ((await _context.SaveChangesAsync()) > 0)
                {
                    return(Created($"api/fixedspend/{fixedSpend.Id}", fixedSpend));
                }
                else
                {
                    return(BadRequest($"Não foi possível cadastrar gasto fixo {fixedSpend.Name}"));
                }
            }
            catch (Exception exception)
            {
                return(InternalError(exception));
            }
        }