public async Task <OperationResult <UserCurrentLocation> > SetCurrentLocationForUserAsync(UserCurrentLocationUpdate model) { var success = false; var userCurrentLocation = new UserCurrentLocation(); try { userCurrentLocation.Id = model.Id; userCurrentLocation.CurrentLocation = model.CurrentLocation; userCurrentLocation.TimeAtLocation = DateTime.UtcNow; await UpdateCurrentLocationForUser(userCurrentLocation); await UpdateLocationHistoryForUser(userCurrentLocation); await UpdateAllUsersCurrentLocation(userCurrentLocation); success = true; } catch (Exception ex) { _logger.LogError(ex, $"{nameof(SetCurrentLocationForUserAsync)} - {model.Id}"); } return(new OperationResult <UserCurrentLocation>() { Success = success, Model = userCurrentLocation }); }
private async Task UpdateAllUsersCurrentLocation(UserCurrentLocation userCurrentLocation) { var allUsersCurrentLocation = new List <UserCurrentLocation>(); try { allUsersCurrentLocation = await GetAllUsersCurrentLocation(); var index = allUsersCurrentLocation.FindIndex(u => { return(String.Compare(u.Id, userCurrentLocation.Id, StringComparison.InvariantCultureIgnoreCase) == 0); }); if (index > -1) { allUsersCurrentLocation.RemoveAt(index); } allUsersCurrentLocation.Add(userCurrentLocation); var allUsersCurrentLocationJson = JsonSerializer.Serialize(allUsersCurrentLocation); await _distributedCache.SetStringAsync(ALL_USERS_CURRENT_LOCATION_KEY, allUsersCurrentLocationJson); } catch (Exception ex) { _logger.LogError(ex, $"nameof(UpdateAllUsersCurrentLocation): {userCurrentLocation.Id}"); throw new Exception($"nameof(UpdateAllUsersCurrentLocation): {userCurrentLocation.Id}", ex); } }
private async Task UpdateCurrentLocationForUser(UserCurrentLocation userCurrentLocation) { try { var key = $"{userCurrentLocation.Id}{USER_CURRENT_LOCATION_KEY_SUFFIX}"; var userCurrentLocationJson = JsonSerializer.Serialize(userCurrentLocation); await _distributedCache.SetStringAsync(key, userCurrentLocationJson); } catch (Exception ex) { _logger.LogError(ex, $"nameof(UpdateCurrentLocationForUser): {userCurrentLocation.Id}"); throw new Exception($"nameof(UpdateCurrentLocationForUser): {userCurrentLocation.Id}", ex); } }
private async Task UpdateLocationHistoryForUser(UserCurrentLocation userCurrentLocation) { var userLocationHistory = new List <UserCurrentLocation>(); try { var key = $"{userCurrentLocation.Id}{USER_LOCATION_HISTORY_SUFFIX}"; var userLocationHistoryJson = await _distributedCache.GetStringAsync(key); if (!string.IsNullOrWhiteSpace(userLocationHistoryJson)) { userLocationHistory = JsonSerializer.Deserialize <List <UserCurrentLocation> >(userLocationHistoryJson); } userLocationHistory.Add(userCurrentLocation); userLocationHistoryJson = JsonSerializer.Serialize(userLocationHistory); await _distributedCache.SetStringAsync(key, userLocationHistoryJson); } catch (Exception ex) { _logger.LogError(ex, $"nameof(UpdateLocationHistoryForUser): {userCurrentLocation.Id}"); throw new Exception($"nameof(UpdateLocationHistoryForUser): {userCurrentLocation.Id}", ex); } }