private static bool deleteWorkoutFromList(string _mastersUerName, string _userName, string _workoutName)
        {
            bool flag = false;
            //connect to db
            db_appEntities db = new db_appEntities();
            //check if workout exist in DB
            favorite delWorkout = checkFavoriteWorkout(_mastersUerName, _userName, _workoutName);

            if (delWorkout == null)
            {
                return(false);
            }
            try
            {
                db.Entry(delWorkout).State = EntityState.Deleted;
                db.SaveChanges();
                flag = true;
            }
            catch (Exception e)
            { }
            if (flag == true)
            {
                return(true);
            }
            else
            {
                db.Dispose();
                return(false);
            }
        }
        private static bool deleteTask(string workoutName, string taskName)
        {
            bool flag = false;
            //connect to db
            db_appEntities db = new db_appEntities();

            //check if task exist in DB
            task delTask = findTask(taskName);

            if (delTask == null)
            {
                return(false);
            }

            try
            {
                db.Entry(delTask).State = EntityState.Deleted;
                db.SaveChanges();
                flag = true;
            }
            catch (Exception e)
            {}
            if (flag == true)
            {
                return(true);
            }
            else
            {
                db.Dispose();
                return(false);
            }
        }
        private static bool addToStorage(string _userName, string _workoutName, bool _inStorage)
        {
            bool flag = false;
            //connect to db
            db_appEntities db             = new db_appEntities();
            workout        workoutToUpade = findWorkoutByUsername(_userName, _workoutName);

            if (workoutToUpade == null)
            {
                return(false);
            }
            try
            {
                workoutToUpade.inStorage = true;

                db.Entry(workoutToUpade).State = EntityState.Modified;

                db.SaveChanges();
                flag = true;
            }
            catch (Exception e)
            { }
            if (flag == true)
            {
                return(true);
            }
            else
            {
                db.Dispose();
                return(false);
            }
        }
        private static List <task> findAllTasks(string workoutName)
        {
            db_appEntities db = new db_appEntities();
            //get list of tasks by workoutName
            List <task> listAllTask = db.tasks.Where(x => x.workoutName == workoutName).ToList();//Select(s => s.taskName).ToList();

            db.Dispose();
            //check if list not null
            if (listAllTask == null)
            {
                return(null);
            }
            return(listAllTask);
        }
        //query function to check if workout exist in DB
        private static workout findWorkoutByUsername(string _userName, string _workoutName)
        {
            db_appEntities db = new db_appEntities();
            //get workout by workoutName and userName
            workout _workout = db.workouts.Where(x => x.workoutName == _workoutName && x.userName == _userName).SingleOrDefault();

            db.Dispose();
            //check if workout exist
            if (_workout == null)
            {
                return(null);
            }
            return(_workout);
        }
        private static task findTaskByWorkoutName(string workoutName, string taskName)
        {
            db_appEntities db = new db_appEntities();
            //get task by taskName
            task _task = db.tasks.Where(x => x.workoutName == workoutName && x.taskName == taskName).SingleOrDefault();

            db.Dispose();
            //check if task exist
            if (_task == null)
            {
                return(null);
            }
            return(_task);
        }
        //query function to check if user exist in DB
        private static user findUser(string userName)
        {
            db_appEntities db = new db_appEntities();
            //get user by userName
            user user = db.users.Where(x => x.userName == userName).SingleOrDefault();

            //check if user exist
            if (user == null)
            {
                return(null);
            }
            db.Dispose();
            return(user);
        }
        private static task checkTask(string taskName)
        {
            //connect to db
            db_appEntities db = new db_appEntities();
            //get workout by workoutName
            task _task = db.tasks.Where(x => x.taskName == taskName).SingleOrDefault();

            db.Dispose();
            //check if workout exist and if workoutName is correct
            if (_task == null || !workout.Equals(taskName, taskName))
            {
                return(null);
            }
            return(_task);
        }
        private static favorite checkFavoriteWorkout(string _masterUserName, string _userName, string _workoutName)
        {
            //connect to db
            db_appEntities db = new db_appEntities();
            //get workout by workoutName
            favorite _workout = db.favorites.Where(x => x.masterUser == _masterUserName && x.userName == _userName && x.workoutName == _workoutName).SingleOrDefault();

            db.Dispose();
            //check if workout exist and if workoutName is correct
            if (_workout == null)
            {
                return(null);
            }
            return(_workout);
        }
        private static workout checkWorkout(string workoutName)
        {
            //connect to db
            db_appEntities db = new db_appEntities();
            //get workout by workoutName
            workout _workout = db.workouts.Where(x => x.workoutName == workoutName).SingleOrDefault();

            db.Dispose();
            //check if workout exist and if workoutName is correct
            if (_workout == null || !workout.Equals(workoutName, workoutName))
            {
                return(null);
            }
            return(_workout);
        }
        private static user checkLogin(string userName, string password)
        {
            //connect to db
            db_appEntities db = new db_appEntities();
            //get user by userName
            user _user = db.users.Where(x => x.userName == userName).SingleOrDefault();

            db.Dispose();
            //check if user exist and if password is correct
            //if (_user == null || !Equals(user.password, CryptHelper.getHash(password)))
            if (_user == null || !user.Equals(password, password))
            {
                return(null);
            }
            return(_user);
        }
        public static workoutModelList workoutsRessult(string _userName)
        {
            //connect to db
            db_appEntities db    = new db_appEntities();
            List <string>  names = db.workouts.Where(s => s.userName == _userName).Select(s => s.workoutName).ToList();

            for (int i = 0; i < names.Count; i++)
            {
                names[i] = CryptHelper.Encrypt(names[i]);
            }
            db.Dispose();
            workoutModelList workoutModel = new workoutModelList
            {
                result   = true,
                workouts = names
            };

            return(workoutModel);
        }
        public static taskModelList tasksRrssult(string _workoutName)
        {
            //connect to db
            db_appEntities db = new db_appEntities();

            List <string> names = db.tasks.Where(s => s.workoutName == _workoutName).Select(s => s.taskName).ToList();

            for (int i = 0; i < names.Count; i++)
            {
                names[i] = CryptHelper.Encrypt(names[i]);
            }
            db.Dispose();

            taskModelList taskModel = new taskModelList
            {
                result    = true,
                tasksList = names
            };

            return(taskModel);
        }
        public static UserProperty getUserPropertyResult(string _userName)
        {
            //connect to db
            db_appEntities db    = new db_appEntities();
            user           _user = db.users.Where(x => x.userName == _userName).SingleOrDefault();

            _user.firstName = CryptHelper.Encrypt(_user.firstName);
            _user.lastName  = CryptHelper.Encrypt(_user.lastName);
            _user.userName  = CryptHelper.Encrypt(_user.userName);
            _user.password  = CryptHelper.Encrypt(_user.password);

            db.Dispose();

            UserProperty userPropertyModel = new UserProperty
            {
                result       = true,
                userProperty = _user
            };

            return(userPropertyModel);
        }
        private static bool addTask(string workoutName, string taskName, string descriptionTask, string timeTask, string revTask)
        {
            bool flag = false;
            //connect to db
            db_appEntities db = new db_appEntities();

            //check if task exist in DB
            task newTask = findTask(taskName);

            if (newTask != null)
            {
                return(false);
            }
            try
            {
                newTask = new task();
                //if task not exist create new task
                newTask.workoutName = workoutName;
                newTask.taskName    = taskName;
                newTask.description = descriptionTask;
                newTask.time        = timeTask;
                newTask.rev         = revTask;

                db.tasks.Add(newTask);
                //db.Entry(newTask).State = EntityState.Added;
                db.SaveChanges();
                flag = true;
            }
            catch (Exception e)
            {}
            if (flag == true)
            {
                return(true);
            }
            else
            {
                db.Dispose();
                return(false);
            }
        }
        public static favoritesModelList favoriteslistRessult(string masterUserName)
        {
            //connect to db
            db_appEntities  db           = new db_appEntities();
            List <favorite> workoutsList = db.favorites.Where(s => s.masterUser == masterUserName).ToList();

            for (int i = 0; i < workoutsList.Count; i++)
            {
                workoutsList[i].masterUser  = CryptHelper.Encrypt(workoutsList[i].masterUser);
                workoutsList[i].userName    = CryptHelper.Encrypt(workoutsList[i].userName);
                workoutsList[i].workoutName = CryptHelper.Encrypt(workoutsList[i].workoutName);
            }
            db.Dispose();

            favoritesModelList favoritesModel = new favoritesModelList
            {
                result            = true,
                favoritesWorkouts = workoutsList
            };

            return(favoritesModel);
        }
        public static storageModelList storagelistRessult()
        {
            //connect to db
            db_appEntities db          = new db_appEntities();
            List <workout> storageList = db.workouts.Where(s => s.inStorage == true).ToList();

            for (int i = 0; i < storageList.Count; i++)
            {
                storageList[i].userName    = CryptHelper.Encrypt(storageList[i].userName);
                storageList[i].workoutName = CryptHelper.Encrypt(storageList[i].workoutName);
                storageList[i].inStorage   = storageList[i].inStorage;
            }
            db.Dispose();

            storageModelList storageModel = new storageModelList
            {
                result          = true,
                storageWorkouts = storageList
            };

            return(storageModel);
        }
        public static ItemTaskModel taskByNameResult(string _taskName)
        {
            //connect to db
            db_appEntities db    = new db_appEntities();
            task           _task = db.tasks.Where(x => x.taskName == _taskName).SingleOrDefault();

            _task.workoutName = CryptHelper.Encrypt(_task.workoutName);
            _task.taskName    = CryptHelper.Encrypt(_task.taskName);
            _task.description = CryptHelper.Encrypt(_task.description);
            _task.time        = CryptHelper.Encrypt(_task.time);
            _task.rev         = CryptHelper.Encrypt(_task.rev);

            db.Dispose();

            ItemTaskModel taskModel = new ItemTaskModel
            {
                result   = true,
                itemTask = _task
            };

            return(taskModel);
        }
        //query function to add new user to DB
        private static bool addUser(string firstName, string lastName, string userName, string password)
        {
            bool flag = false;
            //connect to db
            db_appEntities db = new db_appEntities();

            //check if user exist in DB
            user user = findUser(userName);

            if (user != null)
            {
                return(false);
            }
            try
            {
                user = new user();
                //if user not exist create new user
                user.firstName = firstName;
                user.lastName  = lastName;
                user.password  = password; // CryptHelper.getHash(password);
                user.userName  = userName;
                db.users.Add(user);

                db.SaveChanges();
                flag = true;
            }
            catch (Exception e)
            {}
            if (flag == true)
            {
                return(true);
            }
            else
            {
                db.Dispose();
                return(false);
            }
        }
        private static bool addWorkout(string userName, string workoutName)
        {
            bool flag = false;
            //connect to db
            db_appEntities db = new db_appEntities();

            //check if workout exist in DB
            workout newWorkout = findWorkout(workoutName);

            if (newWorkout != null)
            {
                return(false);
            }
            try
            {
                newWorkout = new workout();
                //if workout not exist create new workout
                newWorkout.userName    = userName;
                newWorkout.workoutName = workoutName;
                newWorkout.inStorage   = false;

                db.workouts.Add(newWorkout);
                db.SaveChanges();
                flag = true;
            }
            catch (Exception e)
            {}
            if (flag == true)
            {
                return(true);
            }
            else
            {
                db.Dispose();
                return(false);
            }
        }
        //query function to add new user to DB
        private static bool changeProfile(string firstName, string lastName, string userName, string password)
        {
            bool flag = false;
            //connect to db
            db_appEntities db = new db_appEntities();

            //check if user exist in DB
            user user = findUser(userName);

            if (user == null)
            {
                return(false);
            }
            try
            {
                //if user exist change user profile
                user.firstName = firstName;
                user.lastName  = lastName;
                user.password  = password; // CryptHelper.getHash(password);

                db.Entry(user).State = EntityState.Modified;

                db.SaveChanges();
                flag = true;
            }
            catch (Exception e)
            {}
            if (flag == true)
            {
                return(true);
            }
            else
            {
                db.Dispose();
                return(false);
            }
        }
        private static bool deleteWorkout(string userName, string workoutName)
        {
            bool flag = false;
            //connect to db
            db_appEntities db = new db_appEntities();
            //check if workout exist in DB
            workout delWorkout = findWorkoutByUsername(userName, workoutName);

            List <task> delTaskList = findAllTasks(workoutName);

            if (delWorkout == null && delTaskList == null)
            {
                return(false);
            }
            try
            {
                db.Entry(delWorkout).State = EntityState.Deleted;
                db.SaveChanges();
                foreach (task element in delTaskList)
                {
                    db.Entry(element).State = EntityState.Deleted;
                }
                db.SaveChanges();
                flag = true;
            }
            catch (Exception e)
            {}
            if (flag == true)
            {
                return(true);
            }
            else
            {
                db.Dispose();
                return(false);
            }
        }
        private static bool addWorkoutToTableFavorites(string masterUserName, string userName, string workoutName)
        {
            bool flag = false;
            //connect to db
            db_appEntities db = new db_appEntities();

            ////check if workout exist in DB
            //favorite newWorkout = findWorkout(workoutName);
            //if (newWorkout != null)
            //{
            //    return false;
            //}
            try
            {
                favorite newWorkout = new favorite();
                //if workout not exist create new workout
                newWorkout.masterUser  = masterUserName;
                newWorkout.userName    = userName;
                newWorkout.workoutName = workoutName;

                db.favorites.Add(newWorkout);
                db.SaveChanges();
                flag = true;
            }
            catch (Exception e)
            { }
            if (flag == true)
            {
                return(true);
            }
            else
            {
                db.Dispose();
                return(false);
            }
        }
Example #24
0
 protected override void Dispose(bool disposing)
 {
     db.Dispose();
     base.Dispose(disposing);
 }