Esempio n. 1
0
        /// <summary>
        /// Registers a user
        /// </summary>
        /// <param name="user">User parameter to register</param>
        /// <returns>Returns a string with error or if succeeded</returns>
        public string Register(Users user)
        {
            if (user != null)
            {
                //Check the object given for errors
                string response = Validate(user);
                //If errors return
                if (response.Length > 0)
                    return response;

                //Find user in database from given user data
                DatabaseEntities db = new DatabaseEntities();
                var existUser = db.Users.Find(user.Email);

                //If he doesnt exist, add new user to database, else return error string
                if (existUser == null)
                {
                    db.Users.Add(user);
                    db.SaveChanges();
                    return "Success";
                }
                else
                {
                    return "Error already exists";
                }
            }
            else
                return "No user given";
        }
Esempio n. 2
0
        /// <summary>
        /// Saves a new exercise for a user.
        /// </summary>
        /// <param name="user">The user that has done the exercise.</param>
        /// <param name="exercise">The exercise to be saved.</param>
        /// <returns></returns>
        public string SaveExercise(Users user, Exercises exercise)
        {
            if (user != null && exercise != null)
            {
                if(Login(user) == "Success")
                {
                    using (var dbC = new DatabaseEntities())
                    {

                            Users dbuser = dbC.Users.Find(user.Email);
                            dbuser.Exercises.Add(exercise);

                            dbC.SaveChanges();

                            return ("Success");

                    }
                }
                else
                    return ("User not found");
            }

            return ("Invalid values");
        }