public SSOContext(string token) { TokenInfo = TokenUtility.GetTokenInfo(Token); string userId = DBUtility.CubeDb.From <Mc_Token>().Where(Mc_Token._.Secret_Key == TokenInfo.SecretKey) .Select(Mc_Token._.All).ToList().FirstOrDefault().User_Id.ToString(); User = DBUtility.CubeDb.From <Mc_User>().Where(Mc_User._.Id == userId).Select(Mc_User._.All).FirstDefault(); }
public async Task <IActionResult> GetFitnessLogs(int userId) { var tokenInfo = TokenUtility.GetTokenInfo(HttpContext); if (userId != tokenInfo.Id) { return(Unauthorized(new { Error = "Invalid UserId" })); } try { var results = await _context.FitnessLogs .AsNoTracking() .Include("WorkoutType") .Include("ExerciseMaps.Exercise.ExerciseType") .Include("ExerciseMaps.Exercise.QuantityType") .Where(log => log.UserId == userId ) .ToListAsync(); return(Ok(new { Data = results })); } catch (Exception ex) { var message = $"Error retrieving fitness logs for user {userId}"; var data = new { Source = ex.Source, Message = ex.Message, InnerException = ex.InnerException, }; var dataString = JsonConvert.SerializeObject(data); _context.Logs.Add(new KravWodLog { Message = message, Data = dataString, TimeStamp = DateTimeOffset.Now }); _context.SaveChanges(); return(StatusCode(500, new { Error = message })); } }
public async Task <IActionResult> CreateFitnessLogs([FromBody] List <FitnessLog> logs, int userId) { var logIds = new List <int>(); var tokenInfo = TokenUtility.GetTokenInfo(HttpContext); if (userId != tokenInfo.Id) { return(Unauthorized(new { Error = "Invalid UserId" })); } foreach (var log in logs) { log.UserId = userId; } try { using (var transaction = _context.Database.BeginTransaction()) { // TODO: Fix how the log is created foreach (var log in logs) { // Don't add the exercises/exercise maps until checking // whether they already exist var logMaps = log.ExerciseMaps; log.ExerciseMaps = new List <FitnessLogExerciseMap>(); // Create the log var newLog = await _context.FitnessLogs.AddAsync(log); // Save the id for future use await _context.SaveChangesAsync(); logIds.Add(newLog.Entity.FitnessLogId); foreach (var exerciseMap in logMaps) { // Verify the exercise type and quantity type exist var dbExerciseType = _context.Enums .AsNoTracking() .Where(e => e.EnumId == exerciseMap.Exercise.ExerciseTypeId ) .FirstOrDefault(); var dbExerciseQuantityType = _context.Enums .AsNoTracking() .Where(e => e.EnumId == exerciseMap.Exercise.QuantityTypeId ) .FirstOrDefault(); int mapExerciseId; if (dbExerciseType != null || dbExerciseQuantityType != null) { // See if an exerise already exists var dbExercise = _context.Exercises .AsNoTracking() .Where(ex => ex.Quantity == exerciseMap.Exercise.Quantity && ex.ExerciseTypeId == exerciseMap.Exercise.ExerciseTypeId && ex.QuantityTypeId == exerciseMap.Exercise.QuantityTypeId ) .FirstOrDefault(); // Create new or use existing exercise if (dbExercise == null) { var newExercise = new Exercise { Quantity = exerciseMap.Exercise.Quantity, ExerciseTypeId = dbExerciseType.EnumId, QuantityTypeId = dbExerciseQuantityType.EnumId }; var newExerciseResult = await _context.Exercises.AddAsync(newExercise); await _context.SaveChangesAsync(); mapExerciseId = newExerciseResult.Entity.ExerciseId; } else { mapExerciseId = dbExercise.ExerciseId; } // Create the map var newMap = new FitnessLogExerciseMap { FitnessLogId = newLog.Entity.FitnessLogId, ExerciseId = mapExerciseId }; var newMapResult = await _context.FitnessLogExerciseMaps.AddAsync(newMap); } } } await _context.SaveChangesAsync(); await transaction.CommitAsync(); } // Retrieve the new objects var results = await _context.FitnessLogs .AsNoTracking() .Include("WorkoutType") .Include("ExerciseMaps.Exercise.ExerciseType") .Include("ExerciseMaps.Exercise.QuantityType") .Where(log => log.UserId == userId && logIds.Contains(log.FitnessLogId) ) .ToListAsync(); return(Ok(new { Data = results })); } catch (Exception ex) { var message = $"Error creating fitness log for user {userId}"; var data = new { Source = ex.Source, Message = ex.Message, InnerException = ex.InnerException, }; var dataString = JsonConvert.SerializeObject(data); _context.Logs.Add(new KravWodLog { Message = message, Data = dataString, TimeStamp = DateTimeOffset.Now }); _context.SaveChanges(); return(StatusCode(500, new { Error = message })); } }
public async Task <IActionResult> CreateNutritionLogs([FromBody] NutritionLog[] logs, int userId) { var logIds = new List <int>(); var tokenInfo = TokenUtility.GetTokenInfo(HttpContext); if (userId != tokenInfo.Id) { return(Unauthorized(new { Error = "Invalid UserId" })); } foreach (var log in logs) { log.UserId = userId; } try { using (var transaction = _context.Database.BeginTransaction()) { // TODO: Fix how the log is created foreach (var log in logs) { // Don't add the food items until checking // whether they already exist var logMaps = log.FoodItemMaps; log.FoodItemMaps = new List <NutritionLogFoodItemMap>(); // Create the log var newLog = await _context.NutritionLogs.AddAsync(log); // Save the id for future use await _context.SaveChangesAsync(); logIds.Add(newLog.Entity.NutritionLogId); // Verify the food items actually exist var logFoodItems = logMaps .Select(m => m.FoodItem.FoodItemId) .ToList(); var dbFoodItems = await _context.FoodItems .AsNoTracking() .Where(f => logFoodItems.Contains(f.FoodItemId) ) .Select(f => f.FoodItemId) .ToListAsync(); // Create the FoodItemMap foreach (var map in logMaps) { if (dbFoodItems.Contains(map.FoodItem.FoodItemId)) { var newMap = new NutritionLogFoodItemMap { NutritionLogId = newLog.Entity.NutritionLogId, FoodItemId = map.FoodItem.FoodItemId }; await _context.NutritionLogFoodItemMaps.AddAsync(newMap); } } } await _context.SaveChangesAsync(); await transaction.CommitAsync(); } // Retrieve the new objects var results = await _context.NutritionLogs .AsNoTracking() .Include("FoodItemMaps.FoodItem.FoodType") .Where(log => log.UserId == userId && logIds.Contains(log.NutritionLogId) ) .ToListAsync(); return(Ok(new { Data = results })); } catch (Exception ex) { var message = $"Error creating nutrition log for user {userId}"; var data = new { Source = ex.Source, Message = ex.Message, InnerException = ex.InnerException, }; var dataString = JsonConvert.SerializeObject(data); _context.Logs.Add(new KravWodLog { Message = message, Data = dataString, TimeStamp = DateTimeOffset.Now }); _context.SaveChanges(); return(StatusCode(500, new { Error = message })); } }