public async Task <IActionResult> Put(string id, [FromForm] NovaPropostaModel model) { this._logger.LogInformation("Log.NovaPropostaModel", "update item {ID}", id); if (model == null || string.IsNullOrEmpty(id)) { return(BadRequest()); } var proposta = ConsultaProposta(id); if (proposta == null) { return(NotFound()); } var propost = new Proposta(model.NomeProposta, model.Descricao, model.Valor, ConsultaFornecedor(model.FornecedorID), ConsultaCategoria(model.CategoriaID), (PropostaStatus)Enum.ToObject(typeof(PropostaStatus), model.Status)); var usuario = Context.Usuarios.FirstOrDefault(x => x.Id == Guid.Parse(model.Usuario)); var propostaHistorico = new PropostaHistorico(proposta, usuario); await Context.PropostasHistoricos.AddAsync(propostaHistorico); proposta.Atualizar(propost); if (model.Anexo != null) { using (Stream stream = model.Anexo.OpenReadStream()) { using (var binaryReader = new BinaryReader(stream)) { var anexo = await Context.PropostaAnexos.FirstOrDefaultAsync(x => x.Proposta.Id == Guid.Parse(id)); anexo.Excluido = true; Context.PropostaAnexos.Update(anexo); var fileContent = binaryReader.ReadBytes((int)model.Anexo.Length); var propostaAnexo = new PropostaAnexo(fileContent, model.Anexo.FileName, model.Anexo.ContentType, proposta); await Context.PropostaAnexos.AddAsync(propostaAnexo); } } } Context.Propostas.Update(proposta); await Context.SaveChangesAsync(); MemoryCache.Remove("propostas"); return(Ok(new { Response = "Proposta atualizado com sucesso" })); }
public async Task <IActionResult> Post([FromForm] NovaPropostaModel model) { this._logger.LogInformation("Log.NovaPropostaModel", "Getting item {ID}", model); if (model.NomeProposta == null) { return(BadRequest(new { Erro = "aconteceu algo errado, tenta novamente!" })); } model.Status = 1; var proposta = new Proposta(model.NomeProposta, model.Descricao, model.Valor, ConsultaFornecedor(model.FornecedorID), ConsultaCategoria(model.CategoriaID), (PropostaStatus)Enum.ToObject(typeof(PropostaStatus), model.Status)); await Context.Propostas.AddAsync(proposta); if (model.Anexo != null) { using (Stream stream = model.Anexo.OpenReadStream()) { using (var binaryReader = new BinaryReader(stream)) { var fileContent = binaryReader.ReadBytes((int)model.Anexo.Length); var propostaAnexo = new PropostaAnexo(fileContent, model.Anexo.FileName, model.Anexo.ContentType, proposta); await Context.PropostaAnexos.AddAsync(propostaAnexo); } } } var usuario = Context.Usuarios.FirstOrDefault(x => x.Id == Guid.Parse(model.Usuario)); var propostaHistorico = new PropostaHistorico(proposta, usuario); await Context.PropostasHistoricos.AddAsync(propostaHistorico); await Context.SaveChangesAsync(); MemoryCache.Remove("propostas"); return(Ok(new { ok = "true", Response = "Proposta salvo com sucesso" })); }