public IActionResult Atualizar([FromBody] EstabelecimentoRequest request, [FromRoute] string Cnpj) { try { Validacoes.ValidarEstabelecimento(request); var existeEstabelecimento = _estabelecimentoRepository.BuscarPor(x => x.Cnpj == Cnpj).FirstOrDefault(); if (existeEstabelecimento == null) { return(BadRequest("Estabelecimento não está cadastrado")); } existeEstabelecimento.Endereco = request.Endereco; existeEstabelecimento.Descricao = request.Descricao; existeEstabelecimento = _estabelecimentoRepository.Atualizar(existeEstabelecimento); return(Ok("Estabelecimento " + existeEstabelecimento.Id + " atualizado com sucesso")); } catch (Exception e) { return(BadRequest("Error: " + e.Message)); } }
public IActionResult Cadastrar([FromBody] EstabelecimentoRequest request, [FromRoute] int UsuarioId) { try { Validacoes.ValidarEstabelecimento(request); var existeEstabelecimento = _estabelecimentoRepository.BuscarPor(x => x.Cnpj == request.Cnpj).FirstOrDefault(); if (existeEstabelecimento != null) { return(BadRequest("Estabelecimento já cadastrado")); } var estabelecimento = _estabelecimentoRepository.Cadastrar(new Estabelecimento() { Cnpj = request.Cnpj, Usuario = new Usuario() { UsuarioId = UsuarioId }, Endereco = request.Endereco, DataCadastro = DateTime.Now, Descricao = request.Descricao }); return(Ok("Estabelecimento " + estabelecimento.Id + "criado com sucesso")); } catch (Exception e) { return(BadRequest("Error: " + e.Message)); } }
public async Task <IActionResult> Editar([FromBody] EstabelecimentoRequest estabelecimento) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } await _estabelecimentoServices.Editar(estabelecimento.ToModel()); return(Ok()); }
public static void ValidarEstabelecimento(EstabelecimentoRequest value) { if (string.IsNullOrEmpty(value.Cnpj)) { throw new Exception("Informe o Cnpj"); } if (string.IsNullOrEmpty(value.Descricao)) { throw new Exception("Informe a descrição"); } if (value.Endereco == null) { throw new Exception("Informe o Endereço"); } else { ValidarEndereco(value.Endereco); } }