public IHttpActionResult GetUsers() { dynamic jObjReturn = new JObject(); try { CoachItEntities _db = new CoachItEntities(); var existingUsers = (from s in _db.webpages_Users select new { s.UserId, s.Username, FullNames = s.FirstName, LastName = s.Surname, s.EmailAddress, TelephoneNumber = s.ContactNumber }).ToList(); _db.Dispose(); jObjReturn.status = "OK"; jObjReturn.result = JsonConvert.SerializeObject(existingUsers); } catch (Exception exception) { jObjReturn.status = "FAILED"; jObjReturn.result = $"Could get Roles.\n{exception.Message}"; } return(Ok(jObjReturn)); }
public IHttpActionResult AvailableRoles() { dynamic jObjReturn = new JObject(); try { CoachItEntities _db = new CoachItEntities(); var existingRoles = (from s in _db.webpages_Roles select new { Id = s.RoleId, RoleName = s.RoleName }).ToList(); _db.Dispose(); jObjReturn.status = "OK"; jObjReturn.result = JsonConvert.SerializeObject(existingRoles); } catch (Exception exception) { jObjReturn.status = "FAILED"; jObjReturn.result = $"Could get Roles.\n{exception.Message}"; } return(Ok(jObjReturn)); }
public IHttpActionResult Exercises() { dynamic jObjReturn = new JObject(); try { CoachItEntities _db = new CoachItEntities(); var workoutExerciseList = (from s in _db.WorkoutExercises select new { ExerciseId = s.Id, WorkoutCategoryId = s.CategoryId, WorkoutCategory = s.WorkoutExerciseCategory.Category, Exercise = s.Exercise, Instructions = s.Instructions }).ToList(); _db.Dispose(); jObjReturn.status = "OK"; jObjReturn.result = JsonConvert.SerializeObject(workoutExerciseList); } catch (Exception exception) { jObjReturn.status = "FAILED"; jObjReturn.result = $"Could not get workout Exercises.\n{exception.Message}"; } return(Ok(jObjReturn)); }
public IHttpActionResult Login([FromBody] JObject jsonResult) { Login model = jsonResult.ToObject <Login>(); if (!ModelState.IsValid) { return(BadRequest("Incorrect username or password")); } if (WebSecurity.Login(model.Username, model.Password, false)) { CoachItEntities _db = new CoachItEntities(); webpages_Users user = _db.webpages_Users.First(x => x.Username == model.Username); List <KeyValuePair <string, string> > claimsForToken = new List <KeyValuePair <string, string> >(); claimsForToken.Add(new KeyValuePair <string, string>("UserId", user.UserId.ToString())); claimsForToken.Add(new KeyValuePair <string, string>("UserRole", JsonConvert.SerializeObject(Roles.GetRolesForUser(model.Username)))); _db.Dispose(); JObject jAuthObj = AuthorisationController.GetToken(claimsForToken); return(Ok(jAuthObj)); } else { return(BadRequest("Incorrect username or password")); } }
public IHttpActionResult UpdateUnit([FromBody] JObject jsonResult) { int id = Convert.ToInt32(jsonResult["id"].ToString()); string unit = jsonResult["unit"].ToString(); CoachItEntities _db = new CoachItEntities(); var metricUnit = _db.MetricUnits.FirstOrDefault(x => x.Id == id); if (metricUnit != null) { metricUnit.Unit = unit; _db.SaveChanges(); var metricUnitReturn = (from s in _db.MetricUnits where s.Id == metricUnit.Id select new { Id = s.Id, Unit = s.Unit }).ToList().FirstOrDefault(); _db.Dispose(); return(Ok(JsonConvert.SerializeObject(metricUnitReturn))); } else { _db.Dispose(); return(BadRequest($"Unit with Id {id} not found")); } }
public IHttpActionResult SendMessage([FromBody] JObject jsonResult) { dynamic jObjReturn = new JObject(); try { int senderId = Convert.ToInt32(jsonResult["SenderId"].ToString()); int teamId = Convert.ToInt32(jsonResult["TeamId"].ToString()); string newMessage = jsonResult["NewMessage"].ToString(); Message newM = new Message() { SenderId = senderId, TeamId = teamId, Timestamp = DateTime.Now, Message1 = newMessage }; CoachItEntities _db = new CoachItEntities(); _db.Messages.Add(newM); _db.SaveChanges(); _db.Dispose(); jObjReturn.status = "OK"; jObjReturn.result = "Message Sent."; } catch (Exception exception) { jObjReturn.status = "FAILED"; jObjReturn.result = $"Could not send message.\n{exception.Message}"; } return(Ok(jObjReturn)); }
public IHttpActionResult ExerciseCategoryStats([FromBody] JObject jsonResult) { dynamic jObjReturn = new JObject(); try { int userId = Convert.ToInt32(jsonResult["UserId"].ToString()); CoachItEntities _db = new CoachItEntities(); var model = _db.Database.SqlQuery <CategoryExerciseNumStat>("select Category, Count(1) NumExercises from [dbo].[WorkoutExerciseLinkCompleted] welc " + "join[dbo].[WorkoutExercisesLInk] wel on welc.WorkoutExerciseLinkId = wel.Id " + "join[dbo].[WorkoutExercises] we on we.Id = wel.ExerciseId " + "join[dbo].[WorkoutExerciseCategories] wec on wec.Id = we.CategoryId " + "join[dbo].[WorkoutUsers] wu on wu.Id = wel.WorkoutUsersId " + $"where wu.UserId = {userId} " + "group by Category"); jObjReturn.status = "OK"; jObjReturn.result = JsonConvert.SerializeObject(model); _db.Dispose(); } catch (Exception ex) { jObjReturn.status = "FAILED"; jObjReturn.result = ex.Message; } return(Ok(jObjReturn)); }
public IHttpActionResult UpdateClass([FromBody] JObject jsonResult) { int id = Convert.ToInt32(jsonResult["id"].ToString()); string className = jsonResult["className"].ToString(); CoachItEntities _db = new CoachItEntities(); var metricClass = _db.MetricClasses.FirstOrDefault(x => x.Id == id); if (metricClass != null) { metricClass.Class = className; _db.SaveChanges(); var metricClassReturn = (from s in _db.MetricClasses where s.Id == id select new { Id = s.Id, Class = s.Class }).ToList().FirstOrDefault(); _db.Dispose(); return(Ok(JsonConvert.SerializeObject(metricClassReturn))); } else { _db.Dispose(); return(BadRequest($"Class with Id {id} not found")); } }
public IHttpActionResult ForgotPassword([FromBody] JObject jsonResult) { dynamic jObj = new JObject(); try { string EmailAddress = jsonResult["Email"].ToString(); using (CoachItEntities db = new CoachItEntities()) { webpages_Users user = db.webpages_Users.FirstOrDefault(x => x.EmailAddress == EmailAddress); if (user == null) { return(BadRequest("Email Not Found")); } string body = System.IO.File.ReadAllText(System.Web.HttpContext.Current.Request.MapPath("~/Helpers/MailTemplates/NewUser.html")); body = body.Replace("#NAME#", user.FirstName) .Replace("#USERNAME#", user.Username) .Replace("#EXPIRATIONDATE#", DateTime.Now.AddHours(2).ToString("yyyy/MM/dd hh:mm tt")); var re = Request; var headers = re.Headers; if (headers.Contains("SetPasswordURI")) { string setPasswordURI = $"{headers.GetValues("SetPasswordURI").First()}?Key={WebSecurity.GeneratePasswordResetToken(user.Username, 120)}"; body = body.Replace("#LINK#", setPasswordURI); jObj.result = setPasswordURI; } if (headers.Contains("ForgotPasswordURI")) { string setPasswordURI = headers.GetValues("ForgotPasswordURI").First(); body = body.Replace("#FORGOTPASSWORDLINK#", $"{setPasswordURI}"); } Mail.Send(EmailAddress, "CoachIt - Registration", body); jObj.status = "OK"; } } catch (Exception ex) { jObj.status = "FAILED"; jObj.result = ex.Message; } return(Ok(jObj)); }
public IHttpActionResult AllWorkouts() { dynamic jObjReturn = new JObject(); try { CoachItEntities _db = new CoachItEntities(); List <DetailsViewModel> modelList = new List <DetailsViewModel>(); foreach (WorkoutUser wu in _db.WorkoutUsers) { DetailsViewModel model = new DetailsViewModel() { WorkoutTitle = wu.Workout, EstimatedDuration = wu.Duration }; //model.Exercises = _db.Database.SqlQuery<WorkoutExercisesLInk>($"SELECT Id, ExerciseId, Sets, Repititions, Duration, Weight FROM WorkoutExercisesLInk WHERE WorkoutUsersId = {wu.Id}").ToList(); model.Exercises = (from s in _db.WorkoutExercisesLInks where s.WorkoutUsersId == wu.Id select new Exercise { Id = s.Id, ExerciseId = s.ExerciseId, ExerciseName = s.WorkoutExercis.Exercise, Sets = s.Sets, Repititions = s.Repititions, Duration = s.Duration, Weight = s.Weight }).ToList(); //_db.WorkoutExercisesLInks.Where(x => x.WorkoutUsersId == wu.Id).ToList(); modelList.Add(model); } jObjReturn.status = "OK"; jObjReturn.result = JsonConvert.SerializeObject(modelList); _db.Dispose(); } catch (Exception exception) { jObjReturn.status = "FAILED"; jObjReturn.result = $"Could not get workouts.\n{exception.Message}"; } return(Ok(jObjReturn)); }
public IHttpActionResult UserTeam([FromBody] JObject jsonResult) { dynamic jObjReturn = new JObject(); int userId = Convert.ToInt32(jsonResult["UserId"].ToString().Trim()); try { CoachItEntities _db = new CoachItEntities(); List <TeamViewModel> existingTeams = (from s in _db.Teams join t in _db.TeamsUsers on s.Id equals t.TeamId where t.UserId == userId select new TeamViewModel { Id = s.Id, TeamName = s.TeamName, Timestamp = s.Timestamp, CreatorId = s.CreatorId, Creator = s.webpages_Users.Username }).ToList(); foreach (TeamViewModel team in existingTeams) { team.TeamMembers = (from s in _db.TeamsUsers where s.TeamId == team.Id select new UserViewModel { UserId = s.webpages_Users.UserId, Username = s.webpages_Users.Username, Nickname = s.webpages_Users.Username, FullNames = s.webpages_Users.FirstName, LastName = s.webpages_Users.Surname, EmailAddress = s.webpages_Users.EmailAddress, TelephoneNumber = s.webpages_Users.ContactNumber }).ToList(); } _db.Dispose(); jObjReturn.status = "OK"; jObjReturn.result = JsonConvert.SerializeObject(existingTeams); } catch (Exception exception) { jObjReturn.status = "FAILED"; jObjReturn.result = $"Could Teams.\n{exception.Message}"; } return(Ok(jObjReturn)); }
public IHttpActionResult Workouts([FromBody] JObject jsonResult) { dynamic jObjReturn = new JObject(); try { int userId = Convert.ToInt32(jsonResult["UserId"].ToString()); CoachItEntities _db = new CoachItEntities(); List <DetailsViewModel> modelList = new List <DetailsViewModel>(); foreach (WorkoutUser wu in _db.WorkoutUsers.Where(x => x.UserId == userId)) { DetailsViewModel model = new DetailsViewModel() { WorkoutId = wu.Id, WorkoutTitle = wu.Workout, EstimatedDuration = wu.Duration }; model.Exercises = (from s in _db.WorkoutExercisesLInks where s.WorkoutUsersId == wu.Id select new Exercise { Id = s.Id, ExerciseId = s.ExerciseId, ExerciseName = s.WorkoutExercis.Exercise, Sets = s.Sets, Repititions = s.Repititions, Duration = s.Duration, Weight = s.Weight }).ToList(); modelList.Add(model); } jObjReturn.status = "OK"; jObjReturn.result = JsonConvert.SerializeObject(modelList); _db.Dispose(); } catch (Exception exception) { jObjReturn.status = "FAILED"; jObjReturn.result = $"Could not get workouts.\n{exception.Message}"; } return(Ok(jObjReturn)); }
public IHttpActionResult NewWorkout([FromBody] JObject jsonResult) { dynamic jObjReturn = new JObject(); try { DetailsViewModel model = jsonResult.ToObject <DetailsViewModel>(); CoachItEntities _db = new CoachItEntities(); WorkoutUser workoutUser = new WorkoutUser() { UserId = model.UserId, Workout = model.WorkoutTitle, Timestamp = DateTime.Now }; _db.WorkoutUsers.Add(workoutUser); _db.SaveChanges(); foreach (Exercise exer in model.Exercises) { WorkoutExercisesLInk wel = new WorkoutExercisesLInk { ExerciseId = exer.ExerciseId, Duration = exer.Duration, Weight = exer.Weight, Repititions = exer.Repititions, Sets = exer.Sets, WorkoutUsersId = workoutUser.Id }; _db.WorkoutExercisesLInks.Add(wel); _db.SaveChanges(); } _db.Dispose(); jObjReturn.status = "OK"; jObjReturn.result = "New workout created succesfuly!"; } catch (Exception exception) { jObjReturn.status = "FAILED"; jObjReturn.result = $"Could not create workout Exercise.\n{exception.Message}"; } return(Ok(jObjReturn)); }
public IHttpActionResult Class() { CoachItEntities _db = new CoachItEntities(); var metricClassList = (from s in _db.MetricClasses select new { Id = s.Id, Class = s.Class }).ToList(); _db.Dispose(); return(Ok(JsonConvert.SerializeObject(metricClassList).Replace("\\", ""))); }
public IHttpActionResult Unit() { CoachItEntities _db = new CoachItEntities(); var metricUnitList = (from s in _db.MetricUnits select new { Id = s.Id, Unit = s.Unit }).ToList(); _db.Dispose(); return(Ok(JsonConvert.SerializeObject(metricUnitList))); }
public IHttpActionResult NewExercise([FromBody] JObject jsonResult) { dynamic jObjReturn = new JObject(); try { int CategoryId = Convert.ToInt32(jsonResult["CategoryId"].ToString().Trim()); string Exercise = jsonResult["Exercise"].ToString().Trim(); string Instructions = jsonResult["Instructions"].ToString().Trim(); CoachItEntities _db = new CoachItEntities(); WorkoutExercis newCat = new WorkoutExercis() { CategoryId = CategoryId, Exercise = Exercise, Instructions = Instructions }; _db.WorkoutExercises.Add(newCat); _db.SaveChanges(); var workoutExercises = (from s in _db.WorkoutExercises select new { Id = s.Id, WorkoutCategoryId = s.CategoryId, WorkoutCategory = s.WorkoutExerciseCategory.Category, Exercise = s.Exercise, Instructions = s.Instructions }).ToList().FirstOrDefault(); _db.Dispose(); jObjReturn.status = "OK"; jObjReturn.result = JsonConvert.SerializeObject(workoutExercises); } catch (Exception exception) { jObjReturn.status = "FAILED"; jObjReturn.result = $"Could not create workout Exercise.\n{exception.Message}"; } return(Ok(jObjReturn)); }
public IHttpActionResult CreateRecord([FromBody] JObject jsonResult) { // Get all data from jsonResult int userId = Convert.ToInt32(jsonResult["userId"].ToString()); double measurement = Convert.ToDouble(jsonResult["measurement"].ToString()); int metricTypeId = Convert.ToInt32(jsonResult["metricTypeId"].ToString()); // If jsonResult does not contain a timestamp (not backdated), give it one. DateTime?timestamp = null; if (jsonResult.ContainsKey("timestamp")) { timestamp = Convert.ToDateTime(jsonResult["timestamp"].ToString()); } else { timestamp = DateTime.Now; } CoachItEntities _db = new CoachItEntities(); // Get the userId to which the entry has to be linked, else return an appropriate error. webpages_Users webUser = _db.webpages_Users.FirstOrDefault(x => x.UserId == userId); if (webUser == null) { _db.Dispose(); return(BadRequest($"User '{userId}' not found")); } MetricRecord newRecord = new MetricRecord() { UserId = webUser.UserId, Measurement = measurement, MetricTypeId = metricTypeId, Timestamp = Convert.ToDateTime(timestamp) }; _db.MetricRecords.Add(newRecord); _db.SaveChanges(); return(Ok("Record added succesfully")); }
public IHttpActionResult UpdateTypeUnit([FromBody] JObject jsonResult) { int id = Convert.ToInt32(jsonResult["id"].ToString()); string unitName = jsonResult["unitName"].ToString(); CoachItEntities _db = new CoachItEntities(); var metricType = _db.MetricTypes.FirstOrDefault(x => x.Id == id); var metricUnit = _db.MetricUnits.FirstOrDefault(x => x.Unit == unitName); if (metricType != null && metricUnit != null) { metricType.MetricUnitId = metricUnit.Id; _db.SaveChanges(); var metricTypeReturn = (from s in _db.MetricTypes where s.Id == id select new { Id = s.Id, Type = s.Type, MetricClassId = s.MetricClassId, MetricUnitId = s.MetricUnitId, Unit = s.MetricUnit.Unit }).ToList().FirstOrDefault(); _db.Dispose(); return(Ok(JsonConvert.SerializeObject(metricTypeReturn))); } else { _db.Dispose(); if (metricUnit == null) { return(BadRequest($"Unit '{unitName}' not found")); } else { return(BadRequest($"Type with Id '{id}' not found")); } } }
[Route("GetClassById")] // Route name does not have to be the same as method name. To overload: [Route("routename/{parameter}")] with url: method/param public IHttpActionResult GetClassById([FromBody] JObject jsonResult) { int id = Convert.ToInt32(jsonResult["id"].ToString()); CoachItEntities _db = new CoachItEntities(); var metricClass = (from s in _db.MetricClasses where s.Id == id select new { Id = s.Id, Class = s.Class }).ToList().FirstOrDefault(); _db.Dispose(); return(Ok(JsonConvert.SerializeObject(metricClass))); }
public IHttpActionResult CreateTeam([FromBody] JObject jsonResult) { dynamic jObjReturn = new JObject(); try { CoachItEntities _db = new CoachItEntities(); string teamName = jsonResult["TeamName"].ToString().Trim(); // Check if team not already exist. Team team = _db.Teams.FirstOrDefault(x => x.TeamName.ToUpper() == teamName.ToUpper()); if (team != null) { jObjReturn.status = "FAILED"; jObjReturn.result = $"Team already exist. Created on {team.Timestamp} by {team.webpages_Users.FirstName}."; _db.Dispose(); } else { team = new Team() { TeamName = teamName, CreatorId = Convert.ToInt32(GetPropertyFromClaims("UserId")), Timestamp = DateTime.Now }; _db.Teams.Add(team); _db.SaveChanges(); _db.Dispose(); jObjReturn.status = "OK"; jObjReturn.result = "Team created successful"; } } catch (Exception exception) { jObjReturn.status = "FAILED"; jObjReturn.result = $"Could not create team.\n{exception.Message}"; } return(Ok(jObjReturn)); }
public IHttpActionResult CreateUnit([FromBody] JObject jsonResult) { dynamic jObjReturn = new JObject(); string unit = jsonResult["unitName"].ToString(); CoachItEntities _db = new CoachItEntities(); MetricUnit metricUnit = _db.MetricUnits.FirstOrDefault(x => x.Unit == unit); if (metricUnit == null) { metricUnit = new MetricUnit() { Unit = unit }; _db.MetricUnits.Add(metricUnit); _db.SaveChanges(); var metricUnitReturn = (from s in _db.MetricUnits where s.Id == metricUnit.Id select new { Id = s.Id, Unit = s.Unit }).ToList().FirstOrDefault(); _db.Dispose(); jObjReturn.status = "OK"; jObjReturn.result = JsonConvert.SerializeObject(metricUnitReturn); } else { _db.Dispose(); jObjReturn.status = "FAILED"; jObjReturn.result = $"Unit '{unit}' already exist!"; } return(Ok(jObjReturn)); }
public IHttpActionResult Type() { CoachItEntities _db = new CoachItEntities(); var metricTypeList = (from s in _db.MetricTypes select new { Id = s.Id, Type = s.Type, MetricClassId = s.MetricClassId, MetricClass = s.MetricClass.Class, MetricUnitId = s.MetricUnitId, Unit = s.MetricUnit.Unit }).ToList(); _db.Dispose(); return(Ok(JsonConvert.SerializeObject(metricTypeList))); }
public IHttpActionResult CreateClass([FromBody] JObject jsonResult) { dynamic jObjReturn = new JObject(); string className = jsonResult["className"].ToString(); CoachItEntities _db = new CoachItEntities(); MetricClass metricClass = _db.MetricClasses.AsNoTracking().FirstOrDefault(x => x.Class == className); if (metricClass == null) { metricClass = new MetricClass() { Class = className }; _db.MetricClasses.Add(metricClass); _db.SaveChanges(); var metricClassReturn = (from s in _db.MetricClasses where s.Id == metricClass.Id select new { Id = s.Id, Class = s.Class }).ToList().FirstOrDefault(); _db.Dispose(); jObjReturn.status = "OK"; jObjReturn.result = JsonConvert.SerializeObject(metricClassReturn); } else { _db.Dispose(); jObjReturn.status = "FAILED"; jObjReturn.result = $"Class '{className}' already exist!"; } return(Ok(jObjReturn)); }
public IHttpActionResult GetTypesByClassId([FromBody] JObject jsonResult) { int id = Convert.ToInt32(jsonResult["id"].ToString()); CoachItEntities _db = new CoachItEntities(); var metricType = (from s in _db.MetricTypes where s.MetricClassId == id select new { Id = s.Id, Type = s.Type, MetricClassId = s.MetricClassId, MetricUnitId = s.MetricUnitId, Unit = s.MetricUnit.Unit }).ToList(); _db.Dispose(); return(Ok(JsonConvert.SerializeObject(metricType))); }
public IHttpActionResult GetChat([FromBody] JObject jsonResult) { dynamic jObjReturn = new JObject(); try { int teamId = Convert.ToInt32(jsonResult["TeamId"].ToString()); CoachItEntities _db = new CoachItEntities(); ChatViewModel model = new ChatViewModel() { TeamId = teamId, TeamName = _db.Teams.First(x => x.Id == teamId).TeamName }; model.ChatMessages = (from s in _db.Messages where s.TeamId == teamId select new ChatMessage { MessageId = s.Id, Timestamp = s.Timestamp, Message = s.Message1, SenderId = s.SenderId, SenderName = s.webpages_Users.Username, }).ToList(); jObjReturn.status = "OK"; jObjReturn.result = JsonConvert.SerializeObject(model); _db.Dispose(); } catch (Exception exception) { jObjReturn.status = "FAILED"; jObjReturn.result = $"Could not get messages.\n{exception.Message}"; } return(Ok(jObjReturn)); }
public IHttpActionResult NewCategory([FromBody] JObject jsonResult) { dynamic jObjReturn = new JObject(); try { string newCategory = jsonResult["categoryName"].ToString().Trim(); CoachItEntities _db = new CoachItEntities(); WorkoutExerciseCategory newCat = new WorkoutExerciseCategory() { Category = newCategory }; _db.WorkoutExerciseCategories.Add(newCat); _db.SaveChanges(); var workoutCategory = (from s in _db.WorkoutExerciseCategories select new { Id = s.Id, Category = s.Category }).ToList().FirstOrDefault(); _db.Dispose(); jObjReturn.status = "OK"; jObjReturn.result = JsonConvert.SerializeObject(workoutCategory); } catch (Exception exception) { jObjReturn.status = "FAILED"; jObjReturn.result = $"Could not create workout category.\n{exception.Message}"; } return(Ok(jObjReturn)); }
public IHttpActionResult UpdateRecord([FromBody] JObject jsonResult) { // Get all data from jsonResult int recordId = Convert.ToInt32(jsonResult["recordId"].ToString()); CoachItEntities _db = new CoachItEntities(); // Get the entry that has to be updated. MetricRecord metricRecord = _db.MetricRecords.FirstOrDefault(x => x.Id == recordId); if (metricRecord == null) { _db.Dispose(); return(BadRequest($"Record '{recordId}' not found")); } if (jsonResult.ContainsKey("measurement")) { double measurement = Convert.ToDouble(jsonResult["measurement"].ToString()); metricRecord.Measurement = measurement; } if (jsonResult.ContainsKey("metricTypeId")) { int metricTypeId = Convert.ToInt32(jsonResult["metricTypeId"].ToString()); metricRecord.MetricTypeId = metricTypeId; } if (jsonResult.ContainsKey("timestamp")) { DateTime timestamp = Convert.ToDateTime(jsonResult["timestamp"].ToString()); metricRecord.Timestamp = timestamp; } _db.SaveChanges(); return(Ok("Record added succesfully")); }
public IHttpActionResult GetUnitByTypeId([FromBody] JObject jsonResult) { int id = Convert.ToInt32(jsonResult["id"].ToString()); CoachItEntities _db = new CoachItEntities(); MetricType metricType = _db.MetricTypes.FirstOrDefault(x => x.Id == id); if (metricType != null) { string unit = metricType.MetricUnit.Unit; _db.Dispose(); return(Ok(JsonConvert.SerializeObject(unit))); } else { _db.Dispose(); return(BadRequest($"Could not find Metric Type Id {id}")); } }
public IHttpActionResult CompleteWorkout([FromBody] JObject jsonResult) { dynamic jObjReturn = new JObject(); try { DetailsViewModel workout = jsonResult.ToObject <DetailsViewModel>(); CoachItEntities _db = new CoachItEntities(); foreach (var exercise in workout.Exercises) { _db.WorkoutExerciseLinkCompleteds.Add( new WorkoutExerciseLinkCompleted() { WorkoutExerciseLinkId = exercise.Id, Timestamp = DateTime.Now, Sets = exercise.Sets, Repititions = exercise.Repititions, Duration = exercise.Duration, Weight = exercise.Weight, }); } _db.SaveChanges(); _db.Dispose(); jObjReturn.status = "OK"; jObjReturn.result = "New workout created successfully!"; } catch (Exception ex) { jObjReturn.status = "FAILED"; jObjReturn.result = ex.Message; } return(Ok(jObjReturn)); }
public IHttpActionResult DeleteRecord([FromBody] JObject jsonResult) { // Get all data from jsonResult int recordId = Convert.ToInt32(jsonResult["recordId"].ToString()); CoachItEntities _db = new CoachItEntities(); // Get the entry that has to be deleted. MetricRecord metricRecord = _db.MetricRecords.FirstOrDefault(x => x.Id == recordId); if (metricRecord == null) { _db.Dispose(); return(BadRequest($"Record '{recordId}' not found")); } _db.MetricRecords.Remove(metricRecord); _db.SaveChanges(); _db.Dispose(); return(Ok("Record deleted succesfully")); }