示例#1
0
        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));
        }
示例#2
0
        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));
        }
示例#3
0
        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"));
            }
        }
示例#4
0
        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"));
            }
        }
示例#5
0
        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));
        }
示例#6
0
        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"));
        }
示例#7
0
        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"));
                }
            }
        }
示例#8
0
        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));
        }
示例#9
0
        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));
        }
示例#10
0
        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));
        }
示例#11
0
        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));
        }
示例#12
0
        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"));
        }
示例#13
0
        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));
        }
示例#14
0
        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"));
        }
示例#15
0
        public IHttpActionResult CreateType([FromBody] JObject jsonResult)
        {
            dynamic jObjReturn = new JObject();

            try
            {
                int    metricClassId = Convert.ToInt32(jsonResult["MetricClassId"].ToString());
                int    metricUnitId  = Convert.ToInt32(jsonResult["MetricUnitId"].ToString());
                string metricType    = jsonResult["MetricType"].ToString();

                CoachItEntities _db = new CoachItEntities();

                MetricType newType = new MetricType()
                {
                    MetricClassId = metricClassId,
                    MetricUnitId  = metricUnitId,
                    Type          = metricType
                };

                _db.MetricTypes.Add(newType);
                _db.SaveChanges();
                _db.Dispose();

                jObjReturn.status = "OK";
                jObjReturn.result = "New type added to database";

                return(Ok("New type added to database"));
            }
            catch (Exception exception)
            {
                jObjReturn.status = "FAILED";
                jObjReturn.result = exception.Message;
            }

            return(Ok(jObjReturn));
        }
        public IHttpActionResult NewUser([FromBody] JObject jsonResult)
        {
            dynamic jObj = new JObject();

            try
            {
                string Username      = jsonResult["Username"].ToString();
                string Nickname      = jsonResult["Nickname"].ToString();
                string EmailAddress  = jsonResult["EmailAddress"].ToString();
                string ContactNumber = jsonResult["TelephoneNumber"].ToString();
                string FullNames     = jsonResult["FullNames"].ToString();
                string LastName      = jsonResult["LastName"].ToString();

                int userRole = Convert.ToInt32(jsonResult["RoleId"].ToString());

                int[] TeamIds = jsonResult["TeamIds"].ToObject <int[]>();

                WebSecurity.CreateUserAndAccount(
                    Username,
                    Membership.GeneratePassword(128, 30),
                    new
                {
                    FirstName     = FullNames,
                    Surname       = LastName,
                    EmailAddress  = EmailAddress,
                    ContactNumber = ContactNumber
                });

                CoachItEntities _db       = new CoachItEntities();
                string[]        userRoles = _db.webpages_Roles.Where(x => x.RoleId == userRole).Select(z => z.RoleName).ToArray();

                Roles.AddUserToRoles(Username, userRoles);

                int newUserId = _db.webpages_Users.First(x => x.Username == Username).UserId;

                foreach (int teamId in TeamIds)
                {
                    _db.TeamsUsers.Add(new TeamsUser()
                    {
                        TeamId = teamId, UserId = newUserId, Timestamp = DateTime.Now
                    });
                }
                _db.SaveChanges();
                _db.Dispose();

                string body = System.IO.File.ReadAllText(System.Web.HttpContext.Current.Request.MapPath("~/Helpers/MailTemplates/NewUser.html"));
                body = body.Replace("#NAME#", FullNames)
                       .Replace("#USERNAME#", 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(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));
        }