public ActionResult Index( FormCollection reservation) { if (ModelState.IsValid) { using (var db1 = new WasherContext()) { string id = reservation["ViewReservation.Id"]; int residentId = int.Parse(reservation["selectedResidentId"]); var orgReservation = db1.Reservation.FirstOrDefault(x => x.Id == id ); var resident = db1.Residents.FirstOrDefault(x => x.ResidentId == residentId); orgReservation.Reservationlist[0] = resident; //var orginalReservation = db1.Reservation.Find(reservation.ViewReservation.Id); //var resident = db1.Residents.Find(); //if (orginalReservation == null) //{ // return HttpNotFound(); //} //orginalReservation.Reservationlist[0] = new Resident(); //orginalResident.Email = resident.Email; //orginalResident.Adress = resident.Adress; //orginalResident.Name = resident.Name; db1.Entry(orgReservation).State = EntityState.Modified; try { db1.SaveChanges(); } catch (Exception) { return HttpNotFound(); } } return RedirectToAction("Index", "Reservations"); } return View(); }
/// <summary> /// Uppdatera en resident /// </summary> /// <param name="resident"></param> /// <returns>Den uppdaterade residenten</returns> public static Resident AlterResident(Resident resident) { Resident updatedResident = null; using (var db = new WasherContext()) { var orginalResident = db.Residents.Find(resident.ResidentId); if (orginalResident == null) { return updatedResident; } orginalResident.Email = resident.Email; orginalResident.Adress = resident.Adress; orginalResident.Name = resident.Name; db.Entry(orginalResident).State = EntityState.Modified; try { db.SaveChanges(); } catch (Exception) { throw; } } return resident; }
/// <summary> /// Skapar en bokning /// För att ställa sig i kö behövs det läggas till mer kod /// </summary> /// <param name="residentId"></param> /// <param name="reservationsId"></param> /// <returns></returns> public static MethodResult MakeReservation(int residentId, string reservationsId, byte[] rowversion) { var result = new MethodResult(); using (var db = new WasherContext()) { var x = db.Reservation.FirstOrDefault(r => r.Id == reservationsId); if (x != null) { if (x.ResidentId != 0) { return result; } x.ResidentId = residentId; x.RowVersion = rowversion; var residentToAdd = db.Residents.FirstOrDefault(a => a.ResidentId == residentId); var existingreservations = residentToAdd?.Reservations .Where(r => r.StarTime > DateTime.Now); //if (existingreservations?.Count() > 2) //{ // return result; //} x.Reservationlist.Add(residentToAdd); db.Entry(x).OriginalValues["RowVersion"] = rowversion; db.Entry(x).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException ex) { result.ConcurrEx = true; result.Message = "Någon annan han före! Looser!"; return result; } result.Succes = true; return result; } result.Succes = true; return result; } }
/// <summary> /// Ställa sig i kö för en bokning /// </summary> /// <param name="residentId"></param> /// <param name="reservationsId"></param> /// <returns></returns> public static MethodResult MakeQueReservation(int residentId, string reservationsId, byte[] rowversion) { var result = new MethodResult(); using (var db = new WasherContext()) { var x = db.Reservation.FirstOrDefault(r => r.Id == reservationsId); if (x == null) return result; if (!x.Reservationlist.Any()) { result.Succes = false; result.Message = "Du kan inte ställa dig i kö på en bokning som inte är bokad"; return result; } var residentToAdd = db.Residents.FirstOrDefault(a => a.ResidentId == residentId); x.Reservationlist.Add(residentToAdd); db.Entry(x).OriginalValues["RowVersion"] = rowversion; db.Entry(x).State = EntityState.Modified; try { db.SaveChanges(); } catch (Exception ex) { result.Succes = false; result.Message = ex.Message; return result; } return result; } }
public ActionResult Edit([Bind(Include = "ResidentId,Email,Name,Adress,RowVersion")] Resident resident) { if (ModelState.IsValid) { using (var db1 = new WasherContext()) { var orginalResident = db1.Residents.Find(resident.ResidentId); if (orginalResident == null) { return HttpNotFound(); } orginalResident.Email = resident.Email; orginalResident.Adress = resident.Adress; orginalResident.Name = resident.Name; db1.Entry(orginalResident).State = EntityState.Modified; try { db1.SaveChanges(); } catch (Exception ex) { var sdf = ex; return HttpNotFound(); } } return RedirectToAction("Index"); } return View(resident); }