public async Task <IActionResult> Create([Bind("TarefaId,Nome,Descricao,Inicio,Fim,Importancia,Foto")] Tarefa tarefa, IFormFile foto)
        {
            if (ModelState.IsValid)
            {
                if (foto != null)
                {
                    byte[] b;
                    using (var or = foto.OpenReadStream())
                    {
                        using (var ms = new MemoryStream())
                        {
                            or.CopyTo(ms);
                            b = ms.ToArray();
                        }
                    }
                    tarefa.Foto = b;
                }

                _context.Add(tarefa);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(tarefa));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> PutTarefa([FromRoute] long id, [FromBody] Tarefa tarefa)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != tarefa.Id)
            {
                return(BadRequest());
            }

            _context.Entry(tarefa).State = EntityState.Modified;

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

            return(NoContent());
        }
Exemplo n.º 3
0
        public async Task <IActionResult> Delete(int id)
        {
            var entity = await _context.Tarefas.FindAsync(id);

            _context.Tarefas.Remove(entity ?? throw new InvalidOperationException());
            await _context.SaveChangesAsync();

            return(NoContent());
        }
Exemplo n.º 4
0
 public async Task <Tarefa> InserirTarefa(Tarefa tarefa)
 {
     try
     {
         _tarefaContext.Tarefas.Add(tarefa);
         await _tarefaContext.SaveChangesAsync();
     }
     catch (System.Exception e)
     {
         throw;
     }
     return(tarefa);
 }
Exemplo n.º 5
0
 public async Task <bool> SaveChangesAsync()
 {
     return((await _context.SaveChangesAsync()) > 0);
 }