public async Task <EventoCategoriaTbl> Put(int id, EventoCategoriaTbl categoria)
        {
            context.Entry(categoria).State = EntityState.Modified;
            await context.SaveChangesAsync();

            return(categoria);
        }
        public async Task <EventoCategoriaTbl> Post(EventoCategoriaTbl categoria)
        {
            await context.EventoCategoriaTbl.AddAsync(categoria);

            await context.SaveChangesAsync();

            return(categoria);
        }
        public async Task <EventoCategoriaTbl> Delete(int id)
        {
            EventoCategoriaTbl categoriaRetornada = await context.EventoCategoriaTbl.FindAsync(id);

            context.EventoCategoriaTbl.Remove(categoriaRetornada);
            await context.SaveChangesAsync();

            return(categoriaRetornada);
        }
Exemple #4
0
        public async Task <ActionResult <EventoCategoriaTbl> > Post(EventoCategoriaTbl categoria)
        {
            if (categoria.CategoriaNome == null || categoria.CategoriaNome.Length == 0)
            {
                throw new System.ArgumentNullException("A categoria deve ter um nome");
            }
            else if (categoria.CategoriaNome.Length < 3)
            {
                throw new System.ArgumentException("Nome da categoria deve conter um mínimo de 3 caracteres");
            }

            return(await repositorio.Post(categoria));
        }
        public async Task <ActionResult <EventoCategoriaTbl> > Delete(int id)
        {
            EventoCategoriaTbl categoriaRetornada = await repositorio.Get(id);

            if (categoriaRetornada == null)
            {
                return(NotFound("Categoria não encontrada."));
            }

            context.EventoCategoriaTbl.Remove(categoriaRetornada);
            await context.SaveChangesAsync();

            return(Ok("Categoria deletada com sucesso."));
        }
        public async Task <ActionResult> Put(int id, EventoCategoriaTbl categoria)
        {
            EventoCategoriaTbl categoriaRetornada = await repositorio.Get(id);

            if (categoriaRetornada == null)
            {
                return(NotFound("Categoria não encontrada."));
            }

            if (categoriaRetornada.CategoriaId != categoria.CategoriaId)
            {
                return(BadRequest("Id inválido"));
            }

            categoriaRetornada.CategoriaNome = categoria.CategoriaNome;
            context.EventoCategoriaTbl.Update(categoriaRetornada);
            await context.SaveChangesAsync();

            return(Ok("Categoria atualizada com sucesso."));
        }
        public async Task <ActionResult <EventoCategoriaTbl> > Post(EventoCategoriaTbl categoria)
        {
            if (categoria.CategoriaNome == null || categoria.CategoriaNome.Length == 0)
            {
                throw new System.ArgumentNullException("A categoria deve ter um nome");
            }
            else if (categoria.CategoriaNome.Length < 3)
            {
                throw new System.ArgumentException("Nome da categoria deve conter um mínimo de 3 caracteres");
            }

            List <EventoCategoriaTbl> listaC = await repositorio.Get();

            foreach (var element in listaC)
            {
                if (element.CategoriaNome.Contains(categoria.CategoriaNome))
                {
                    throw new System.ArgumentException("Categoria já existe.");
                }
            }

            return(await repositorio.Post(categoria));
        }
        public async Task <EventoCategoriaTbl> Get(int id)
        {
            EventoCategoriaTbl categoriaEncontrada = await context.EventoCategoriaTbl.FindAsync(id);

            return(categoriaEncontrada);
        }