public ActionResult Edit(Role role, string MemberListSelect) { if (ModelState.IsValid) { //取得資料庫裏面原來的值 var roleItem = db.Roles.Single(r => r.Id == role.Id); //套用新的值 db.Entry(roleItem).CurrentValues.SetValues(role); //放入新的值 roleItem.Members.Clear(); string[] strArray = MemberListSelect.Split(','); var memberLists = db.Members.ToList().Where(c => strArray.Contains(c.Id.ToString())); foreach (var m in memberLists) { roleItem.Members.Add(m); } db.Entry(roleItem).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(RedirectToAction("Edit")); }
public async Task <IActionResult> PutClazz([FromRoute] string id, [FromBody] Clazz clazz) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != clazz.Id) { return(BadRequest()); } _context.Entry(clazz).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ClazzExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutAccount([FromRoute] string id, [FromBody] Account account) { _context.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking; if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != account.Id) { return(BadRequest("ID is not correct")); } if (await _context.Account.SingleOrDefaultAsync(a => a.Id == account.Id) != null) // Kiem tra account update co ton tai khong { var currentAccount = await _context.Account.SingleOrDefaultAsync(a => a.Id == account.Id); string tokenHeader = Request.Headers["Authorization"]; var token = tokenHeader.Replace("Basic ", ""); var tokenUser = await _context.Credential.SingleOrDefaultAsync(c => c.AccessToken == token); if (tokenUser.OwnerId == currentAccount.Id || (await _context.AccountRoles.SingleOrDefaultAsync(ar => ar.AccountId == tokenUser.OwnerId)).RoleId < (await _context.AccountRoles.SingleOrDefaultAsync(ar => ar.AccountId == currentAccount.Id)).RoleId || tokenUser.OwnerId == "ADMIN" ) { if (account.Password == null) { account.Password = currentAccount.Password; account.Salt = currentAccount.Salt; } else { if (PasswordHandle.GetInstance().EncryptPassword(account.Password, currentAccount.Salt) == currentAccount.Password) //Kiểm tra mật khẩu có trùng với mật khẩu cũ không, nếu trùng thì trả về lỗi { return(BadRequest(new ResponseError("New password do not same old password", 400))); } account.Salt = PasswordHandle.GetInstance().GenerateSalt(); account.Password = PasswordHandle.GetInstance().EncryptPassword(account.Password, account.Salt); } account.UpdatedAt = DateTime.Now; _context.Entry(account).State = EntityState.Modified; _context.Entry(account.GeneralInformation).State = EntityState.Modified; await _context.SaveChangesAsync(); return(Ok(_context.Account.Include(a => a.GeneralInformation).SingleOrDefault(a => a.Id == account.Id))); } } return(BadRequest(account.Id)); }
public void UpdateDepartures2(Variant variant, List <Departure> departuresToRemove, List <Explanation> explanationsToRemove) { using (BackendContext db = new BackendContext()) { var variantEntry = db.Variants.Single(v => v.Id == variant.Id); if (variant.Departures != null) { foreach (var singleDeparture in variant.Departures) { foreach (var singleExplanation in singleDeparture.Explanations) { if (singleExplanation.Id < 1) { db.Entry(singleExplanation).State = System.Data.Entity.EntityState.Added; } } if (singleDeparture.Id < 1 && false) { singleDeparture.Variant = variantEntry; db.Entry(singleDeparture).State = System.Data.Entity.EntityState.Added; } } } if (explanationsToRemove != null) { foreach (var singleExplanation in explanationsToRemove) { if (singleExplanation.Id > 0) { db.Entry(singleExplanation).State = System.Data.Entity.EntityState.Deleted; } } } if (departuresToRemove != null) { foreach (var singleDeparture in departuresToRemove) { if (singleDeparture.Id > 0) { db.Entry(singleDeparture).State = System.Data.Entity.EntityState.Deleted; } } } db.SaveChanges(); } }
public async Task <IActionResult> PutHaandvaerker(int id, Haandvaerker haandvaerker) { if (id != haandvaerker.HaandvaerkerId) { return(BadRequest()); } _context.Entry(haandvaerker).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!HaandvaerkerExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutInvoiceItem([FromRoute] int id, [FromBody] InvoiceItem invoiceItem) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != invoiceItem.ID) { return(BadRequest()); } _context.Entry(invoiceItem).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!InvoiceItemExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutAccountRole([FromRoute] int id, [FromBody] AccountRole accountRole) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != accountRole.RoleId) { return(BadRequest()); } _context.Entry(accountRole).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!AccountRoleExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutLegalCase(long id, LegalCase legalCase) { if (id != legalCase.Id) { return(BadRequest()); } _context.Entry(legalCase).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!LegalCaseExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IHttpActionResult> PutSongs(int id, Songs songs) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != songs.SongId) { return(BadRequest()); } db.Entry(songs).State = EntityState.Modified; try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!SongsExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public async Task <IActionResult> PutVaerktoejskasse(int id, Vaerktoejskasse vaerktoejskasse) { if (id != vaerktoejskasse.VTKId) { return(BadRequest()); } _context.Entry(vaerktoejskasse).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!VaerktoejskasseExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutFolder(long id, Folder folder) { if (id != folder.Id) { return(BadRequest()); } _context.Entry(folder).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!FolderExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public Product Update(Product updateProduct) { var pro = GetProductByID(updateProduct.ID); if (pro != null) { pro.Name = updateProduct.Name; // pro.Price = updateProduct.Price; pro.Color = updateProduct.Color; pro.Type = updateProduct.Type; //pro.CreatedDate = pro.CreatedDate; } _btx.Entry(pro).State = EntityState.Modified; _btx.SaveChanges(); return(pro); }
public async Task <IActionResult> PutGeneralInformation([FromRoute] string id, [FromBody] GeneralInformation generalInformation) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != generalInformation.AccountId) { return(BadRequest()); } _context.Entry(generalInformation).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!GeneralInformationExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public void UpdateLineWithVariants(Line line, List <Variant> variants, List <Variant> variantsToRemove) { using (BackendContext db = new BackendContext()) { if (line.Id > 0) { // entity exists db.Entry(line).State = System.Data.Entity.EntityState.Modified; foreach (var singleVariant in variants) { if (singleVariant.Id > 0) { db.Entry(line.Variants.Single(v => v.Id == singleVariant.Id)).State = System.Data.Entity.EntityState.Modified; } else { singleVariant.Line = db.Lines.Single(l => l.Id == line.Id); db.Entry(singleVariant).State = System.Data.Entity.EntityState.Added; } } } else { // this creates the Variants as well db.Entry(line).State = System.Data.Entity.EntityState.Added; } db.SaveChanges(); if (variantsToRemove != null) { foreach (var singleVariant in variantsToRemove) { if (singleVariant.Id > 0) { db.Departures.RemoveRange(db.Departures.Where(d => d.Variant.Id == singleVariant.Id)); db.VariantStops.RemoveRange(db.VariantStops.Where(vs => vs.Variant.Id == singleVariant.Id)); db.Variants.Remove(db.Variants.Single(v => v.Id == singleVariant.Id)); } } db.SaveChanges(); } } }
public ActionResult ChangePassword(string newPassword) { Member member = db.Members.SingleOrDefault(o => o.Account == User.Identity.Name); if (member != null) { member.Password = Utility.GenerateHashWithSalt(newPassword, member.PasswordSalt); db.Entry(member).State = EntityState.Modified; db.SaveChanges(); ViewBag.message = "修改成功"; return(View()); } ViewBag.message = "修改失敗!請重新登入試試!!"; return(View()); }
public async Task <ActionResult <Customer> > Update(long id, Customer customer) { if (id != customer.Id) { return(BadRequest()); } _context.Entry(customer).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { return(BadRequest()); } return(NoContent()); }
public async Task <IActionResult> PutDrone([FromRoute] int id, [FromBody] Drone drone) { dbContext.Entry(drone).State = EntityState.Modified; try { await dbContext.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!DroneExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public void UpdateVariantStops(int variantId, List <Stop> stops, List <VariantStop> variantStops, List <VariantStop> variantStopsToRemove) { using (BackendContext db = new BackendContext()) { if (variantStopsToRemove != null) { foreach (var singleVariantStop in variantStopsToRemove) { if (singleVariantStop.Id > 0) { db.Entry(singleVariantStop).State = System.Data.Entity.EntityState.Deleted; } } db.SaveChanges(); } Variant variant = db.Variants.Include("VariantStops").Single(v => v.Id == variantId); if (variantStops != null) { foreach (var singleVariantStop in variantStops) { // vs is new if (singleVariantStop.Id < 1) { Stop stop; // stop already exists if (singleVariantStop.Stop.Id > 0) { stop = db.Stops.Single(s => s.Id == singleVariantStop.Stop.Id); } // stop is new else { stop = db.Stops.Add(singleVariantStop.Stop); db.SaveChanges(); } singleVariantStop.Stop = stop; singleVariantStop.Variant = variant; db.Entry(singleVariantStop).State = System.Data.Entity.EntityState.Added; } } db.SaveChanges(); var orderedVariantStops = db.VariantStops.Include("Stop").Where(vs => vs.Variant.Id == variantId).OrderBy(vs => vs.TimeOffset).ToList(); if (orderedVariantStops.Count > 0) { int firstStopId = orderedVariantStops.First().Stop.Id; int lastStopId = orderedVariantStops.Last().Stop.Id; variant.FirstLineStop = db.Stops.Single(s => s.Id == firstStopId); variant.LastLineStop = db.Stops.Single(s => s.Id == lastStopId); } else { variant.FirstLineStop = null; variant.LastLineStop = null; } db.SaveChanges(); } } }
public void UpdateDepartures(Variant variant, List <Departure> departuresToRemove, List <Explanation> explanationsToRemove) { using (BackendContext db = new BackendContext()) { if (departuresToRemove != null) { foreach (var singleDeparture in departuresToRemove) { if (singleDeparture.Id > 0) { db.Entry(singleDeparture).State = EntityState.Deleted; } } db.SaveChanges(); } var dbVariant = db.Variants.Single(v => v.Id == variant.Id); List <Departure> departures = variant.Departures; if (departures != null) { foreach (var singleDeparture in departures) { //dep is new if (singleDeparture.Id < 1) { Explanation explanation; List <Explanation> singleDepartureExplanationCopy = singleDeparture.Explanations.ToList(); foreach (var singleExplanation in singleDepartureExplanationCopy) { //explanation already exists if (singleExplanation.Id > 0) { explanation = db.Explanations.Single(e => e.Id == singleExplanation.Id); } //explanation is new else { explanation = db.Explanations.Add(singleExplanation); db.SaveChanges(); } singleDeparture.Explanations.Remove(singleDeparture.Explanations.Single(e => e.Id == singleExplanation.Id)); singleDeparture.Explanations.Add(explanation); } singleDeparture.VariantId = dbVariant.Id; db.Entry(singleDeparture).State = EntityState.Added; } } db.SaveChanges(); } if (explanationsToRemove != null) { foreach (var singleExplanation in explanationsToRemove) { if (singleExplanation.Id > 0) { db.Explanations.Remove(db.Explanations.Single(e => e.Id == singleExplanation.Id)); } } } } }
public void Atualizar(TEntity entity) { _dbContext.Entry(entity).State = EntityState.Modified; _dbContext.SaveChanges(); }
public void Edit(User updateUser) { _btx.Entry(updateUser).State = EntityState.Modified; _btx.SaveChanges(); }