public async Task <IActionResult> SignIn([FromBody] ProfileSaveRequestModel model) { try { var user = await _context.Users.SingleOrDefaultAsync(a => a.UserId == UserId); if (user == null) { user = new AppUser { UserId = this.UserId }; user.FirstName = model.FirstName; user.LastName = model.LastName; user.Email = model.Email; user.Gender = model.Gender; user.City = model.City; user.Region = model.Region; user.Country = model.Country; user.PhotoUrl = model.PhotoUrl; user.DefaultActivityTypeId = (byte)Enum.Parse <ActivityTypeEnum>(model.DefaultActivityType ?? "Ski", true); _context.Users.Add(user); await _context.SaveChangesAsync(); } var response = Responses.Profile(user, Enumerable.Empty <Activity>()); return(Ok(response)); } catch (Exception e) { Console.WriteLine("Error signing in", e); _logger.LogError(e, "Error siging in"); return(ErrorResponse.AsStatusCodeResult(HttpStatusCode.InternalServerError, "Error siging in")); } }
public async Task <IActionResult> SaveActivityData(Guid activityId, [FromBody] ActivitySaveDataRequestModel model) { try { var sourceTypeID = Enum.Parse <ActivitySourceTypeEnum>(model.Source, true); //get or create activity var activity = await _context.Activities.SingleOrDefaultAsync(a => a.ActivityId == activityId); if (activity == null) { activity = new WaitTime.Entities.Activity { ActivityId = activityId, UserId = this.UserId, SourceTypeId = (byte)sourceTypeID, }; _context.Activities.Add(activity); } var batch = await _context.ActivitySyncBatches.SingleOrDefaultAsync(b => b.ActivitySyncBatchId == model.ActivitySyncBatchId); if (batch != null) { //batch already saved return(Ok(new SuccessResponse())); } var array = model.LocationsArray; if (array.GetLength(1) != 10) { var error = $"Activity {activityId}, Batch {model.BatchNbr} for User {UserId}: Locations array has the wrong number of columns. Expected 10 columns, found {array.GetLength(1)}."; Console.WriteLine(error); throw new ArgumentOutOfRangeException(nameof(model), error); } foreach (var location in Enumerable .Range(0, array.GetLength(0)) .Select(row => new ActivityLocation { Latitude = (double)(array[row, 0] ?? 0), Longitude = (double)(array[row, 1] ?? 0), Accuracy = (float)(array[row, 2] ?? 0), Altitude = (float)(array[row, 3] ?? 0), AltitudeAccuracy = (float)(array[row, 4]), Bearing = (float)(array[row, 5] ?? 0), BearingAccuracy = (float)(array[row, 6]), Speed = (float)(array[row, 7] ?? 0), SpeedAccuracy = (float)(array[row, 8] ?? 0), Timestamp = (double)(array[row, 9] ?? 0), })) { activity.Locations.Add(location); } batch = new ActivitySyncBatch { ActivitySyncBatchId = model.ActivitySyncBatchId, BatchNbr = model.BatchNbr, }; activity.Batches.Add(batch); await _context.SaveChangesAsync(); return(Ok(new SuccessResponse())); } catch (Exception e) { Console.WriteLine("Error processing batch", e); _logger.LogError(e, "Error processing batch"); return(ErrorResponse.AsStatusCodeResult(HttpStatusCode.InternalServerError, "Error processing batch")); } }