public async Task <IHttpActionResult> PostUser(UserProfile user) { var id = await IdentitiyHelper.FindSidAsync(User, Request); user.UserId = id; UserProfile current = await InsertAsync(user); return(CreatedAtRoute("Tables", new { id = current.Id }, current)); }
public async Task <IQueryable <Trip> > GetAllTrips() { var id = await IdentitiyHelper.FindSidAsync(User, Request); if (string.IsNullOrWhiteSpace(id)) { return(null); } return(Query().Where(s => s.UserId == id)); }
public async Task <IHttpActionResult> PostTrip(Trip trip) { var id = await IdentitiyHelper.FindSidAsync(User, Request); trip.UserId = id; Trip current = await InsertAsync(trip); if (dbContext == null) { dbContext = new MyDrivingContext(); } var curUser = dbContext.UserProfiles.FirstOrDefault(u => u.UserId == id); //update user with stats if (curUser != null) { curUser.FuelConsumption += trip.FuelUsed; var max = trip?.Points.Max(s => s.Speed) ?? 0; if (max > curUser.MaxSpeed) { curUser.MaxSpeed = max; } curUser.TotalDistance += trip.Distance; curUser.HardAccelerations += trip.HardAccelerations; curUser.HardStops += trip.HardStops; curUser.TotalTrips++; curUser.TotalTime += (long)(trip.EndTimeStamp - trip.RecordedTimeStamp).TotalSeconds; dbContext.SaveChanges(); } return(CreatedAtRoute("Tables", new { id = current.Id }, current)); }
public async Task <IHttpActionResult> PostTrip(Trip trip) { var aiTelemetry = new TelemetryClient(); var id = await IdentitiyHelper.FindSidAsync(User, Request); if (string.IsNullOrEmpty(id)) { aiTelemetry.TrackEvent("UserId is null or empty!"); } if (trip != null) { trip.UserId = id; } else { aiTelemetry.TrackEvent("Trip is null!"); return(BadRequest("Null trip")); } Trip current = null; try { current = await InsertAsync(trip); } catch (HttpResponseException httpResponseException) { aiTelemetry.TrackException(httpResponseException); aiTelemetry.TrackEvent("Caught HttpResponseException. Response:" + httpResponseException.Response); } catch (System.Exception ex) { aiTelemetry.TrackException(ex); } if (current == null) { aiTelemetry.TrackEvent("Inserting trip failed!"); return(BadRequest("Inserting trip failed")); } if (dbContext == null) { dbContext = new MyDrivingContext(); } var curUser = dbContext.UserProfiles.FirstOrDefault(u => u.UserId == id); //update user with stats if (curUser != null) { curUser.FuelConsumption += trip.FuelUsed; var max = trip?.Points?.Max(s => s.Speed) ?? 0; if (max > curUser.MaxSpeed) { curUser.MaxSpeed = max; } curUser.TotalDistance += trip.Distance; curUser.HardAccelerations += trip.HardAccelerations; curUser.HardStops += trip.HardStops; curUser.TotalTrips++; curUser.TotalTime += (long)(trip.EndTimeStamp - trip.RecordedTimeStamp).TotalSeconds; dbContext.SaveChanges(); } else { aiTelemetry.TrackEvent("Cannot find user " + id); } //track large trips var pointsCount = trip?.Points?.Count ?? 0; if (pointsCount > 1000) { aiTelemetry.TrackEvent(string.Format("Saved large trip {0}. Points:{1}", current.Id, pointsCount)); } return(CreatedAtRoute("Tables", new { id = current.Id }, current)); }
public async Task <MyDriving.DataObjects.UserProfile> Get() { //return the current authenticated user profile ClaimsPrincipal user = User as ClaimsPrincipal; bool? isAuthenticated = user?.Identity?.IsAuthenticated; if (!isAuthenticated.GetValueOrDefault()) { return(null); } var userId = string.Empty; // Get the credentials for the logged-in user. var fbCredentials = await user.GetAppServiceIdentityAsync <FacebookCredentials>(Request); var msCredentials = await user.GetAppServiceIdentityAsync <MicrosoftAccountCredentials>(Request); var twitterCredentials = await user.GetAppServiceIdentityAsync <TwitterCredentials>(Request); string first = string.Empty, last = string.Empty, profile = string.Empty; userId = await IdentitiyHelper.FindSidAsync(User, Request); if (fbCredentials?.UserClaims?.Count() > 0) { FillDataFromFacebook(fbCredentials, out first, out last, out profile); } else if (msCredentials?.UserClaims?.Count() > 0) { FillDataFromMS(msCredentials, out first, out last, out profile); } else if (twitterCredentials?.UserClaims?.Count() > 0) { FillDataFromTwitter(twitterCredentials, out first, out last, out profile); } else { return(null); } var context = new MyDrivingContext(); var curUser = context.UserProfiles.FirstOrDefault(u => u.UserId == userId); if (curUser == null) { curUser = new MyDriving.DataObjects.UserProfile { UserId = userId, ProfilePictureUri = profile, FirstName = first, LastName = last }; context.UserProfiles.Add(curUser); } else { curUser.FirstName = first; curUser.LastName = last; curUser.ProfilePictureUri = profile; } await context.SaveChangesAsync(); return(curUser); }
public async Task <IQueryable <UserProfile> > GetAllUsers() { var id = await IdentitiyHelper.FindSidAsync(User, Request); return(Query().Where(s => s.UserId == id)); }