private async Task AddFacilityDetails(ApplicationUser user) { var emptyUserOutOfFacility = new UsersOutOfGymDetails(); var entry = _facilityContext.Add(emptyUserOutOfFacility); UsersOutOfGymDetails.EstimatedTimeToCheck = DateTime.Now; UsersOutOfGymDetails.FacilityID = _facilityContext.Facilities.FirstOrDefault().FacilityID; UsersOutOfGymDetails.UniqueEntryID = user.Id; entry.CurrentValues.SetValues(UsersOutOfGymDetails); await _facilityContext.SaveChangesAsync(); }
public async Task <IActionResult> SelectTimeToEstimate(int idForUser, [Bind("UserOutOfGymDetailsID, FacilityID, EstimatedTimeToCheck, UniqueEntryID")] UsersOutOfGymDetails usersOutOfGymDetails, [Bind] DateTime userDetails) { // get the user var user = await _userManager.GetUserAsync(User); if (user.Id == null) { return(NotFound()); } // set the PK entry to be the same as the one the usere created idForUser = _facilityContext.UsersOutofGymDetails.Where(o => o.UniqueEntryID == user.Id).FirstOrDefault().UsersOutOfGymDetailsID; // ensure we are only updating for the user that entered the drop down list value var currentUserDetail = _facilityContext.UsersOutofGymDetails.Where(o => o.UniqueEntryID == user.Id).FirstOrDefault(); if (idForUser != currentUserDetail.UsersOutOfGymDetailsID) { return(NotFound()); } if (ModelState.IsValid) { try { currentUserDetail.EstimatedTimeToCheck = userDetails; _facilityContext.Update(currentUserDetail); await _facilityContext.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!FacilityExists(usersOutOfGymDetails.FacilityID)) { return(NotFound()); } else { throw; } } } return(RedirectToAction("Index", "Home", new { id = user.DefaultGym })); }
// Method that applies action based on whether user is entering or exiting the gym private async Task <bool> DetermineEnterOrExitGym(Facility facilityView, ApplicationUser user, Facility facility, List <UsersInGymDetail> facilityDetails, UsersInGymDetail currentFacilityDetail, UsersInGymDetail currentFacilityDetailDb, bool enteredGym, UsersOutOfGymDetails allGymUserRecords) { if (facilityView.IsOpenDoorRequested) { // perform facial recognition scan if not inside the gym if (!user.IsInsideGym) { await FacialRecognitionScan(user, currentFacilityDetail); } // Location scan (results from HERE API using javascript) is passed to this POST Action Method // when the checkbox's value is checked we assign user.IsWithin10m as true, // satisfying the first requirement to gaining access to the facility if (facilityView.IsWithin10m) { user.IsWithin10m = true; } // if camera scan and location check is true, and user is not in the gym, then we open the door, and access granted is true if (user.IsWithin10m && user.IsCameraScanSuccessful && !user.IsInsideGym) { user.AccessGrantedToFacility = true; ViewBag.AccessGrantedToFacility = true; } // if camera scan is not successful else if (!facilityView.IsCameraScanSuccessful && !user.IsWithin10m) { user.AccessGrantedToFacility = false; } if (user.AccessGrantedToFacility) { facility.DoorOpened = true; // if the user is not in the gym, then say this user is not in the gym, and increase object values as required. if (!user.IsInsideGym) { facility.NumberOfClientsInGym++; user.IsInsideGym = true; user.TimeAccessGranted = DateTime.Now; currentFacilityDetail.TimeAccessGranted = DateTime.Now; currentFacilityDetail.FirstName = user.FirstName; currentFacilityDetail.UniqueEntryID = user.Id; currentFacilityDetail.FacilityID = facility.FacilityID; enteredGym = true; // TODO: Use AJAX to async send to and from the client at the same time } // <--------------------------------- LEAVE GYM ----------------------------------------------------------- // if the user is already in the gym, when button is pushed then make reset all access to false, and decrement the number of ppl in the gym by 1 else if (user.IsInsideGym) { await LeaveGym(facilityView, user, facility, facilityDetails, currentFacilityDetailDb); } } // end access granted else if (!user.AccessGrantedToFacility) { ViewBag.AccessDeniedMsgRecieved = false; user.TimeAccessDenied = DateTime.Now; } // if door has been opened and user is authorised if (facility.DoorOpened && user.AccessGrantedToFacility) { // log the time granted, and wait 5 seconds. System.Threading.Thread.Sleep(5000); } // When 5 second timer finishes, we close the door again automatically facility.IsOpenDoorRequested = false; ViewBag.IsOpenDoorRequested = false; facility.DoorOpened = false; _facilityContext.Update(facility); // if we are entering gym, use the new facility object, if we are leaving, use the facility detail using Db values. if (enteredGym) { _facilityContext.Update(currentFacilityDetail); } // after a facility exist, then we can update facility to avoid foreign key constraint? await _facilityContext.SaveChangesAsync(); await _userManager.UpdateAsync(user); } return(enteredGym); }