public async Task<IHttpActionResult> PutProposal(int id, Proposal proposal) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != proposal.ProposalId) { return BadRequest(); } db.Entry(proposal).State = EntityState.Modified; try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ProposalExists(id)) { return NotFound(); } else { throw; } } return StatusCode(HttpStatusCode.NoContent); }
public async Task<IHttpActionResult> PostProposal(Proposal proposal) { if (!ModelState.IsValid) { return BadRequest(ModelState); } db.Proposals.Add(proposal); db.LibraryComponents.Load(); db.Transactions.Load(); db.Users.Load(); try { await db.SaveChangesAsync(); // Notifico SOLO al client specifico che è stato inviato un messaggio string userToSend = string.Empty; if (proposal.Direction) userToSend = proposal.Transaction.UserReceiving_Id; else userToSend = proposal.Transaction.UserProponent_Id; IHubContext ctx = GlobalHost.ConnectionManager.GetHubContext<MessageConnection>(); string ConnectionId = MessageConnection.GetConnectionByUserID(userToSend); await ctx.Clients.Client(ConnectionId).sendProposalHub(proposal, ConnectionId); } catch (DbEntityValidationException ex) { StringBuilder sb = new StringBuilder(); ex.EntityValidationErrors.ToList().ForEach(entityValidation => { entityValidation.ValidationErrors.ToList().ForEach(validation => sb.Append(string.Concat(validation.PropertyName, " - ", validation.ErrorMessage))); }); _log.Error("{0}{1}Validation errors:{1}{2}", ex, Environment.NewLine, sb.ToString()); throw; } catch (Exception ex) { _log.Error(ex); } return CreatedAtRoute("DefaultApi", new { id = proposal.ProposalId }, proposal); }