public HttpResponseMessage Post(Shift shift) { if (ModelState.IsValid) { if (shift.Id == 0) { _repo.Add<Shift>(shift); _repo.SaveChanges(); return Request.CreateResponse(HttpStatusCode.OK, shift); } else { var dbShift = _repo.Find<Shift>(shift.Id); dbShift.StartShift = shift.StartShift; dbShift.EndShift = shift.EndShift; dbShift.Employee = shift.Employee; _repo.SaveChanges(); return Request.CreateResponse(HttpStatusCode.OK, shift); } } else { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, this.ModelState); } }
//method for swapping two shifts employees public void SwapShifts(ShiftDTO shift1, ShiftDTO shift2) { if (shift1.IsAvailable) { var temp = new Shift(); temp.Employee = _repo.Query<ApplicationUser>().FirstOrDefault(u => u.Email == shift1.Employee.Email); var s1 = _repo.Query<Shift>().Include(s => s.Employee).FirstOrDefault(s => s.Id == shift1.Id); var s2 = _repo.Query<Shift>().Include(s => s.Employee).FirstOrDefault(s => s.Id == shift2.Id); s1.Employee = _repo.Query<ApplicationUser>().FirstOrDefault(u => u.Email == s2.Employee.Email); s2.Employee = _repo.Query<ApplicationUser>().FirstOrDefault(u => u.Email == temp.Employee.Email); var trade = _repo.Query<Trade>().Include(t => t.ShiftsOffered.Select(s => s.Employee)).Include(t => t.ShiftForTrade).FirstOrDefault(t => t.ShiftForTrade.Id == shift1.Id); trade.IsPending = false; trade.ShiftsOffered.Clear(); s1.IsAvailable = false; s2.IsAvailable = false; _repo.SaveChanges(); } else { var trade = _repo.Query<Trade>().Include(t => t.ShiftsOffered.Select(s => s.Employee)).Include(t => t.ShiftForTrade).FirstOrDefault(t => t.ShiftForTrade.Id == shift1.Id); trade.IsPending = false; trade.ShiftsOffered.Clear(); _repo.SaveChanges(); } }