public IHttpActionResult PutArchiveEntry(Guid id, ArchiveEntry archiveEntry) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != archiveEntry.Id) { return(BadRequest()); } db.Entry(archiveEntry).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!ArchiveEntryExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public async Task <IHttpActionResult> PutQrProfile(Guid id, QrProfile qrProfile) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != qrProfile.Id) { return(BadRequest()); } db.Entry(qrProfile).State = EntityState.Modified; try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!QrProfileExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public async Task <IHttpActionResult> PutQrProfile(Guid id, QrProfile profile) { var existingParent = db.QrProfiles .Where(p => p.Id == profile.Id) .Include(p => p.Links) .SingleOrDefault(); if (existingParent != null) { foreach (var link in profile.Links) { link.QrProfileId = profile.Id; } var userId = this.User.Identity.GetUserId(); profile.UserId = userId; if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != profile.Id) { return(BadRequest()); } // Update parent db.Entry(existingParent).CurrentValues.SetValues(profile); #region Links // Delete children foreach (var existingChild in existingParent.Links.ToList()) { if (!profile.Links.Any(c => c.Id == existingChild.Id)) { db.Links.Remove(existingChild); } } // Update and Insert children foreach (var childModel in profile.Links) { var existingChild = existingParent.Links .Where(c => c.Id == childModel.Id) .SingleOrDefault(); if (existingChild != null) { // Update child db.Entry(existingChild).CurrentValues.SetValues(childModel); } else { // Insert child var newChild = new QrLink { Id = childModel.Id, Label = childModel.Label, QrProfileId = childModel.QrProfileId, Url = childModel.Url }; existingParent.Links.Add(newChild); } } #endregion try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!QrProfileExists(id)) { return(NotFound()); } else { throw; } } } //var i = 0; //foreach (var l in QrProfile.Links) //{ // l.QrProfileId = QrProfile.Id; // l.Id = i++; //} //var userId = this.User.Identity.GetUserId(); //QrProfile.UserId = userId; //if (!ModelState.IsValid) //{ // return BadRequest(ModelState); //} //if (id != QrProfile.Id) //{ // return BadRequest(); //} //db.Entry(QrProfile).State = EntityState.Modified; //try //{ // await db.SaveChangesAsync(); //} //catch (DbUpdateConcurrencyException) //{ // if (!QrProfileExists(id)) // { // return NotFound(); // } // else // { // throw; // } //} return(Ok(existingParent)); }