public Tuple <int, string> RegisterVehicle(Vehicle vehicle) { using (HuboDbContext ctx = new HuboDbContext()) { try { int result = 0; // check rego number doesn't already exist if (ctx.VehicleSet.Any(o => o.RegistrationNo == vehicle.RegistrationNo)) { // Match! return(Tuple.Create(-1, "Registration Number already exists")); } ctx.Entry(vehicle).State = System.Data.Entity.EntityState.Added; result = ctx.SaveChanges(); return(Tuple.Create(vehicle.Id, "Success")); } catch (Exception ex) { return(Tuple.Create(-1, ex.Message)); } } }
public Tuple <int, string> StopBreak(Break stopBreak) { using (HuboDbContext ctx = new HuboDbContext()) { try { Break currentBreak = ctx.BreakSet.Single <Break>(b => b.Id == stopBreak.Id); if (currentBreak.isActive == false) { return(Tuple.Create(-1, "Break has already ended")); } currentBreak.isActive = false; currentBreak.StopBreakDateTime = stopBreak.StopBreakDateTime; currentBreak.StopBreakLocation = stopBreak.StopBreakLocation; currentBreak.EndNote = stopBreak.EndNote; ctx.Entry(currentBreak).State = EntityState.Modified; ctx.SaveChanges(); return(Tuple.Create(1, "Success")); } catch (ArgumentNullException ex) { return(Tuple.Create(-1, ex.Message)); } catch (Exception ex) { return(Tuple.Create(-1, ex.Message)); } } }
public Tuple <int, string> StartBreak(Break newBreak) { using (HuboDbContext ctx = new HuboDbContext()) { try { if (!ctx.WorkShiftSet.Any(s => s.Id == newBreak.ShiftId)) { return(Tuple.Create(-1, "No Shift exists for ID = " + newBreak.ShiftId)); } if (ctx.BreakSet.Any(b => b.isActive == true && b.ShiftId == newBreak.ShiftId)) { return(Tuple.Create(-1, "A break is already active")); } newBreak.isActive = true; ctx.BreakSet.Add(newBreak); ctx.SaveChanges(); return(Tuple.Create(newBreak.Id, "Success")); } catch (Exception ex) { return(Tuple.Create(-1, ex.Message)); } } }
public Tuple <int, string> StopDriving(DrivingShift shiftDetails) { using (HuboDbContext ctx = new HuboDbContext()) { try { DrivingShift shift = ctx.DrivingShiftSet.Single <DrivingShift>(s => s.Id == shiftDetails.Id); if (shift.isActive == false) { return(Tuple.Create(-1, "Driving shift has already ended")); } shift.StopDrivingDateTime = shiftDetails.StopDrivingDateTime; shift.StopHubo = shiftDetails.StopHubo; shift.EndNote = shiftDetails.EndNote; shift.isActive = false; ctx.Entry(shift).State = EntityState.Modified; ctx.SaveChanges(); return(Tuple.Create(1, "Success")); } catch (ArgumentNullException ex) { return(Tuple.Create(-1, ex.Message)); } catch (Exception ex) { return(Tuple.Create(-1, ex.Message)); } } }
public Tuple <int, string> StartDriving(DrivingShift shift) { using (HuboDbContext ctx = new HuboDbContext()) { try { if (!ctx.WorkShiftSet.Any(s => s.Id == shift.ShiftId)) { return(Tuple.Create(-1, "No Shift exists with the ID = " + shift.ShiftId)); } if (!ctx.VehicleSet.Any(v => v.Id == shift.VehicleId)) { return(Tuple.Create(-1, "No Vehicle exists with the ID = " + shift.VehicleId)); } if (ctx.DrivingShiftSet.Any(d => d.isActive == true && shift.ShiftId == d.ShiftId)) { return(Tuple.Create(-1, "A driving shift is already active")); } shift.isActive = true; ctx.DrivingShiftSet.Add(shift); ctx.SaveChanges(); return(Tuple.Create(shift.Id, "Success")); } catch (Exception ex) { return(Tuple.Create(-1, ex.Message)); } } }
public Tuple <int, string> StopShift(WorkShift shift) { using (HuboDbContext ctx = new HuboDbContext()) { try { WorkShift currentShift = ctx.WorkShiftSet.Single <WorkShift>(s => s.Id == shift.Id); if (currentShift.isActive == false) { return(Tuple.Create(-1, "Shift has already ended")); } currentShift.EndDate = shift.EndDate; currentShift.EndLocationLat = shift.EndLocationLat; currentShift.EndLocationLong = shift.EndLocationLong; currentShift.EndLocation = shift.EndLocation; currentShift.EndNote = shift.EndNote; currentShift.isActive = false; ctx.Entry(currentShift).State = EntityState.Modified; ctx.SaveChanges(); return(Tuple.Create(1, "Success")); } catch (ArgumentNullException ex) { return(Tuple.Create(-1, ex.Message)); } catch (Exception ex) { return(Tuple.Create(-1, ex.Message)); } } }
public Tuple <int, string> StartShift(WorkShift shift) { using (HuboDbContext ctx = new HuboDbContext()) { try { //if (!ctx.DriverSet.Any(d => d.Id == shift.DriverId)) //{ // //Driver ID does not exist // return Tuple.Create(-1, "No Driver exists with the ID = " + shift.DriverId); //} //if (!ctx.CompanySet.Any(c => c.Id == shift.CompanyId)) //{ // return Tuple.Create(-1, "No Company exists with the ID = " + shift.CompanyId); //} //if (ctx.WorkShiftSet.Any(c => c.isActive == true && shift.DriverId == c.DriverId)) //{ // return Tuple.Create(-1, "An active shift already exists"); //} shift.isActive = true; ctx.WorkShiftSet.Add(shift); ctx.SaveChanges(); return(Tuple.Create(shift.Id, "Success")); } catch (Exception ex) { return(Tuple.Create(-1, ex.Message)); } } }
public Tuple <int, string> InsertNote(Note note) { using (HuboDbContext ctx = new HuboDbContext()) { try { if (!ctx.WorkShiftSet.Any(s => s.Id == note.ShiftId)) { return(Tuple.Create(-1, "No Shift exists with the ID = " + note.ShiftId)); } ctx.NoteSet.Add(note); ctx.SaveChanges(); return(Tuple.Create(note.Id, "Success")); } catch (Exception ex) { return(Tuple.Create(-1, ex.Message)); } } }
public Tuple <int, string> InsertGeoData(List <GeoData> geoData) { using (HuboDbContext ctx = new HuboDbContext()) { try { foreach (GeoData geoInsert in geoData) { if (!ctx.DrivingShiftSet.Any(d => d.Id == geoInsert.DrivingShiftId)) { return(Tuple.Create(-1, "No Driving Shift exists with ID : " + geoInsert.DrivingShiftId)); } ctx.GeoDataSet.Add(geoInsert); } ctx.SaveChanges(); return(Tuple.Create(1, "Success")); } catch (Exception ex) { return(Tuple.Create(-1, ex.Message)); } } }
public Tuple <int, string> StartDay(int driverId) { //Get all workshifts with driverid using (HuboDbContext ctx = new HuboDbContext()) { try { if (!ctx.DriverSet.Any(d => d.Id == driverId)) { return(Tuple.Create(-1, "No Driver exists with Driver ID = " + driverId)); } //DateTime twoWeeksPrior = default(DateTime); //twoWeeksPrior = DateTime.Now; //twoWeeksPrior = twoWeeksPrior.AddDays(-14); //List<long> listOfDayIds = (from b in ctx.WorkShiftSet // where b.DriverId == driverId && // b.StartDate > twoWeeksPrior // orderby b.DayShiftId descending // select b.DayShiftId).ToList<long>(); //if (listOfDayIds.Count == 0) //{ // // Start new day // DayShift newDayShift = new DayShift(); // ctx.DayShiftSet.Add(newDayShift); // ctx.SaveChanges(); // return Tuple.Create(newDayShift.Id, "Success"); //} //else //{ // // Check if need to send this id, or need to create a new one // long workingDayShiftId = listOfDayIds[0]; // List<WorkShift> listOfWorkShifts = (from b in ctx.WorkShiftSet // where b.DayShiftId == workingDayShiftId // orderby b.StartDate ascending // select b).ToList<WorkShift>(); // WorkShift firstShiftOfTheDay = listOfWorkShifts[0]; // if (firstShiftOfTheDay.StartDate.Value.AddHours(14) > DateTime.Now) // { // // No starting new workday yet // return Tuple.Create(Convert.ToInt32(workingDayShiftId), "Success"); // } // else // { // // New work date // DayShift newDayShift = new DayShift(); // ctx.DayShiftSet.Add(newDayShift); // ctx.SaveChanges(); // return Tuple.Create(newDayShift.Id, "Success"); // } //} DayShift newDayShift = new DayShift(); ctx.DayShiftSet.Add(newDayShift); ctx.SaveChanges(); return(Tuple.Create(newDayShift.Id, "Success")); } catch (Exception ex) { return(Tuple.Create(-1, ex.Message)); } } }