public async Task<HttpResponseMessage> PostLivroCadastro(LivroModel livro) { try { if (!ModelState.IsValid) { var mensagemError = new MensagemResposta("error", "Dados Incompletos. Porfavor preencha os campos requeridos."); throw new DadosIvalidoException(mensagemError); } var serviceCadastroDeLivros = new CadastrarLivrosService(); await serviceCadastroDeLivros.Cadastrar(livro); } catch(DadosIvalidoException e) { var resp = Request.CreateResponse(HttpStatusCode.BadRequest, e.ExceptionMessage); throw new HttpResponseException(resp); } HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, livro); return response; }
private async Task VerificarExistenciaDoLivro(string[] isbns) { var serviceBuscarLivro = new BuscarLivrosService(); LivroModel livro = null; foreach (var isbn in isbns) livro = await serviceBuscarLivro.PesquisarPorISBN(isbn); if (!livro.Titulo.Equals(null)) { var mensagemError = new MensagemResposta("error", "Este Livro já está cadastrado."); throw new DadosIvalidoException(mensagemError); } }
public async Task<LivroModel> Cadastrar(LivroModel livroModel) { if (!VerificarISBN(livroModel)) { var mensagemError = new MensagemResposta("error", "ISBN enviado está invalido."); throw new DadosIvalidoException(mensagemError); } await VerificarExistenciaDoLivro(livroModel.Isbn); var livro = new MontadoraDeLivro().MontarEntidadeLivro(livroModel); _KitapDB.Livros.Add(livro); await _KitapDB.SaveChangesAsync(); return livroModel; }