public async Task <IHttpActionResult> Put(int Id, Subjacent subjacent) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (Id != subjacent.Id) { return(BadRequest()); } try { _db.UpdateGraph(subjacent, map => map .AssociatedEntity(s => s.SubjacentType) .AssociatedEntity(s => s.ContractType) .AssociatedEntity(s => s.Book) .OwnedCollection(s => s.UnderlyingTerms //, with => with.OwnedEntity(u => u.Currency) ) ); await _db.SaveChangesAsync(); } catch (DbEntityValidationException e) { List <string> errors = new List <string>(); foreach (var eve in e.EntityValidationErrors) { errors.Add( string.Format("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State)); foreach (var ve in eve.ValidationErrors) { errors.Add(string.Format("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage)); } } throw; } catch (DbUpdateConcurrencyException) { if (!subjacentExist(Id)) { return(NotFound()); } else { throw; } } catch (Exception exp) { Console.WriteLine(exp.Message); throw; } return(Updated(subjacent)); }
public async Task <IHttpActionResult> GetSubjacent(int Id) { Subjacent subjacent = await _db.Subjacents.SingleOrDefaultAsync(i => i.Id == Id); if (subjacent == null) { return(NotFound()); } return(Ok(subjacent)); }
public async Task <IHttpActionResult> Delete(int Id) { Subjacent subjacent = await _db.Subjacents.FindAsync(Id); if (subjacent == null) { return(NotFound()); } List <HedgeLeg> hedgeLegs = new List <HedgeLeg>(); List <CommodityHedge> commoHedges = new List <CommodityHedge>(); List <UnderlyingTerm> underLyingTerms = new List <UnderlyingTerm>(); foreach (UnderlyingTerm u in subjacent.UnderlyingTerms) { hedgeLegs.AddRange(u.HedgeLegs); commoHedges.AddRange(u.CommodityHedges); underLyingTerms.Add(u); } foreach (HedgeLeg h in hedgeLegs) { _db.HedgeLegs.Remove(h); } foreach (CommodityHedge c in commoHedges) { _db.CommodityHedges.Remove(c); } foreach (UnderlyingTerm u in underLyingTerms) { _db.UnderlyingTerms.Remove(u); } _db.Subjacents.Remove(subjacent); try { await _db.SaveChangesAsync(); } catch (Exception exp) { throw; } return(Ok(subjacent)); }