示例#1
0
        public async Task <(bool result, object res)> Create(CreateCategoriaCommand command)
        {
            if (!await _context.Produtores.AnyAsync(c => c.Id == command.ProdutorId))
            {
                return(false, "Produtor não existe");
            }

            var categoria = new Categoria()
            {
                Id                = command.Id,
                Nome              = command.Nome,
                Ordem             = command.Ordem,
                ProdutorId        = command.ProdutorId,
                CategoriaPadraoId = command.CategoriaPadraoId
            };

            _context.Categorias.Adicionar(categoria);

            return(true, new CategoriaViewModel
            {
                Id = categoria.Id,
                Nome = categoria.Nome,
                Ordem = categoria.Ordem,
                ProdutorId = categoria.ProdutorId,
                CategoriaPadraoId = categoria.CategoriaPadraoId
            });
        }
示例#2
0
        public async Task <IActionResult> Cadastrar([FromBody] CreateCategoriaCommand command)
        {
            var(success, result) = await _service.Create(command);

            if (!success)
            {
                return(BadRequest(new FailViewModel(result as string)));
            }

            return(Ok(result as CategoriaViewModel));
        }
        public async Task <IActionResult> Post(CreateCategoriaCommand command)
        {
            try
            {
                await categoriaApplicationService.Add(command);

                return(Ok(new { Message = "Categoria cadastrada com sucesso." }));
            }
            catch (ValidationException e)
            {
                return(BadRequest(ValidationAdapter.Parse(e.Errors)));
            }
            catch (Exception e)
            {
                return(StatusCode(500, e.Message));
            }
        }
 public async Task Add(CreateCategoriaCommand command)
 {
     await mediator.Send(command);
 }