public async Task <IActionResult> Post([FromForm] PropostaAnexoModel model) { this._logger.LogInformation("Log.NovaPropostaModel", "Getting item {ID}", model); if (model.Id == null) { return(BadRequest(new { Erro = "Aconteceu algo errado, tenta novamente!" })); } var anexo = await Context.PropostaAnexos.FirstOrDefaultAsync(x => x.Proposta.Id == model.Id); Context.PropostaAnexos.Remove(anexo); if (model.Anexo != null) { var proposta = await Context.Propostas.FirstOrDefaultAsync(x => x.Id == model.Id); 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); } } } await Context.PropostaAnexos.AddAsync(anexo); await Context.SaveChangesAsync(); return(Ok(new { Ok = true, Response = "Arquivo alterado com suscesso!" })); }
public async Task <IActionResult> GetProposta(string id) { var propostaAnexo = new PropostaAnexo(); if (!string.IsNullOrEmpty(id) && await Context.PropostaAnexos.AnyAsync(x => x.Proposta.Id == Guid.Parse(id))) { propostaAnexo = await Context.PropostaAnexos.FirstOrDefaultAsync(x => x.Proposta.Id == Guid.Parse(id)); return(Ok(propostaAnexo)); } return(Ok(new { Response = "Nenhum Resultado Encontrado" })); }
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" })); }