Example #1
0
        private ActionResponse ValidateDrill(DrillDTO drill, bool isUpdate = false)
        {
            DrillsRepository       repository     = RepositoriesFactory.CreateRepository <DrillsRepository, Drill>();
            MuscleGroupsRepository muscleGroupRep = RepositoriesFactory.CreateRepository <MuscleGroupsRepository, MuscleGroup>();

            if (repository.Query().Any(currDrill => currDrill.DrillId != drill.Id && currDrill.Name == drill.Name))
            {
                return(new ActionResponse()
                {
                    CompletedSuccessfully = false,
                    ErrorMessage = "A drill with the given name already exists"
                });
            }

            bool doAllMuscleGroupsExist = drill.MuscleGroups.All(currNewMuscleGroup => {
                return(muscleGroupRep.Query().Any(CurrMuscleGroup => CurrMuscleGroup.MuscleGroupId == currNewMuscleGroup.Id));
            });

            if (!doAllMuscleGroupsExist)
            {
                return(new ActionResponse()
                {
                    CompletedSuccessfully = false,
                    ErrorMessage = "None existing muscle group"
                });
            }

            return(null);
        }
Example #2
0
        private ActionResponse CanUserEditProgram(int ProgramId)
        {
            Program wantedProgram = RepositoriesFactory.CreateRepository <ProgramRepository, Program>()
                                    .Query().FirstOrDefault(currProgram => currProgram.ProgramId == ProgramId);

            if (wantedProgram == null)
            {
                return(new ActionResponse()
                {
                    ErrorMessage = "Wanted Program does not exists", CompletedSuccessfully = false
                });
            }

            if (!(GetCurrentUser().Permission == (int)Enums.PermissionsType.Admin))
            {
                if (!wantedProgram.UserPrograms.Any(currUserProgram => currUserProgram.UserId == GetCurrentUser().UserId))
                {
                    return(new ActionResponse()
                    {
                        ErrorMessage = "The given user does not have permissions to update the given program", CompletedSuccessfully = false
                    });
                }
            }

            return(null);
        }
Example #3
0
        private ActionResponse CanUserEditDrill(int drillId)
        {
            Drill wantedDrill = RepositoriesFactory.CreateRepository <DrillsRepository, Drill>()
                                .Query().FirstOrDefault(currDrill => currDrill.DrillId == drillId);

            if (wantedDrill == null)
            {
                return(new ActionResponse()
                {
                    ErrorMessage = "Wanted drill does not exists", CompletedSuccessfully = false
                });
            }

            if (!(GetCurrentUser().Permission == (int)Enums.PermissionsType.Admin))
            {
                if (wantedDrill.CreatorUserId != GetCurrentUser().UserId)
                {
                    return(new ActionResponse()
                    {
                        ErrorMessage = "The given user does not have permissions to update the given drill", CompletedSuccessfully = false
                    });
                }
            }

            return(null);
        }
Example #4
0
        private bool Authorize(HttpActionContext actionContext)
        {
            try
            {
                IEnumerable <String> AuthenticationHeaders;
                actionContext.Request.Headers.TryGetValues(TokenManager.TOKEN_HEADER_NAME, out AuthenticationHeaders);
                String token = AuthenticationHeaders.First();

                String userName  = TokenManager.ExtractUserNameFromToken(token);
                String timeStamp = TokenManager.ExtractUserTimesatmpFromToken(token);


                User WantedUser = RepositoriesFactory.CreateRepository <UsersRepository, User>()
                                  .Query().FirstOrDefault <User>(currUser => currUser.UserName == userName);

                if (WantedUser == null)
                {
                    return(false);
                }
                else if (!_allowedPermissionTypes.Contains(WantedUser.Permission))
                {
                    return(false);
                }


                String hashedPassword = WantedUser.Password;

                return(TokenManager.IsTokenValid(token, userName, hashedPassword, TokenManager.GetAccesseingClientIp(actionContext.Request), timeStamp));
            }
            catch (Exception e)
            {
                return(false);
            }
        }
Example #5
0
        public ICollection <ProgramDTO> GetUsersPrograms(ProgramDTO newProgram, String userName)
        {
            ActionResponse response = new ActionResponse();

            ProgramRepository repository = RepositoriesFactory.CreateRepository <ProgramRepository, Program>();

            int UserId = GetCurrentUser().UserId;

            ICollection <Program> wantedPrograms;

            if (GetCurrentUser().Permission == (int)Enums.PermissionsType.Admin)
            {
                wantedPrograms = repository.GetALL();
            }
            else
            {
                wantedPrograms =
                    repository.Query().Where(CurrProgram => CurrProgram.UserPrograms.Any(currUser => currUser.UserId == UserId)).ToList();
            }


            ICollection <ProgramDTO> ProgramDrills =
                wantedPrograms.Select <Program, ProgramDTO>(currProgram => ConvertEntityTODTO(currProgram)).ToList();

            return(ProgramDrills);
        }
Example #6
0
        public ActionResponse UpdateProgram(ProgramDTO program)
        {
            ActionResponse response = ValidateProgram(program, true);

            if (response != null)
            {
                return(response);
            }

            response = CanUserEditProgram(program.Id);

            if (response != null)
            {
                return(response);
            }

            Program programForUpdate = ConvertDTOToEntity(program);

            ProgramRepository programRepository = RepositoriesFactory.CreateRepository <ProgramRepository, Program>();

            Program existingProgram = programRepository.Get(programForUpdate.ProgramId);

            existingProgram.ProgramName = programForUpdate.ProgramName;

            foreach (ProgramDrill currDrill in programForUpdate.ProgramDrills)
            {
                ProgramDrill existingProgramDrill =
                    existingProgram.ProgramDrills.FirstOrDefault(currProgramDrill => currProgramDrill.Id == currDrill.Id);
                if (existingProgramDrill == null)
                {
                    existingProgram.ProgramDrills.Add(currDrill);
                }
                else
                {
                    foreach (Set currSet in currDrill.Sets)
                    {
                        Set existingSet =
                            existingProgramDrill.Sets.FirstOrDefault(currExistingSet => currExistingSet.SetId == currSet.SetId);

                        if (existingSet == null)
                        {
                            existingProgramDrill.Sets.Add(currSet);
                        }
                        else
                        {
                            existingSet.Repetitions = currSet.Repetitions;
                            existingSet.Weight      = currSet.Weight;
                            existingSet.Repetitions = currSet.Repetitions;
                        }
                    }
                }
            }

            programRepository.Update(existingProgram);
            return(new ActionResponse()
            {
                CompletedSuccessfully = true
            });
        }
Example #7
0
 public ICollection <UserDTO> GetAllUsers()
 {
     return(RepositoriesFactory.CreateRepository <UsersRepository, User>().GetALL()
            .Select <User, UserDTO>(currUser => new UserDTO()
     {
         Name = currUser.UserName, Permission = currUser.Permission, UserId = currUser.UserId
     }).ToList());
 }
 public ICollection <MuscleGroupDTO> GetUser()
 {
     return(RepositoriesFactory.CreateRepository <MuscleGroupsRepository, MuscleGroup>()
            .GetALL().Select <MuscleGroup, MuscleGroupDTO>(CurrMuscleGroup => new MuscleGroupDTO()
     {
         Id = CurrMuscleGroup.MuscleGroupId,
         Name = CurrMuscleGroup.Name
     }).ToList());
 }
Example #9
0
        public UserDTO GetUser(String userName)
        {
            UserDTO users = RepositoriesFactory.CreateRepository <UsersRepository, User>().Query()
                            .Where(currUser => currUser.UserName == userName)
                            .Select <User, UserDTO>(currUser => new UserDTO()
            {
                Name = currUser.UserName, Permission = currUser.Permission, UserId = currUser.UserId
            })
                            .FirstOrDefault();

            return(users);
        }
Example #10
0
        private User GetCurrentUser()
        {
            if (_currUser == null)
            {
                IEnumerable <String> AuthenticationHeaders;
                Request.Headers.TryGetValues(TokenManager.TOKEN_HEADER_NAME, out AuthenticationHeaders);
                String token    = AuthenticationHeaders.First();
                String UserName = TokenManager.ExtractUserNameFromToken(token);
                _currUser = RepositoriesFactory.CreateRepository <UsersRepository, User>().Query().FirstOrDefault(currUser => currUser.UserName == UserName);
            }

            return(_currUser);
        }
Example #11
0
        public ICollection <ProgramDTO> GetAllPrograms(ProgramDTO newProgram)
        {
            ActionResponse response = new ActionResponse();

            ProgramRepository repository = RepositoriesFactory.CreateRepository <ProgramRepository, Program>();

            ICollection <Program> wantedPrograms = repository.GetALL();

            ICollection <ProgramDTO> ProgramDrills = wantedPrograms
                                                     .Select <Program, ProgramDTO>(currProgram => ConvertEntityTODTO(currProgram)).ToList();

            return(ProgramDrills);
        }
Example #12
0
        public ICollection <DrillDTO> GetUserDrills(String UserName)
        {
            User user = GetCurrentUser();

            if (user.Permission == (int)Enums.PermissionsType.Admin)
            {
                return(GetAllDrills());
            }
            else
            {
                return(RepositoriesFactory.CreateRepository <DrillsRepository, Drill>().GetALL().Where(currDrill => currDrill.CreatorUserId == user.UserId)
                       .Select <Drill, DrillDTO>(currDrill => ConvertEntityTODTO(currDrill)).ToList());
            }
        }
Example #13
0
        public ActionResponse DeleteProgram(int programId)
        {
            ActionResponse response = CanUserEditProgram(programId);

            if (response != null)
            {
                return(response);
            }

            ProgramRepository programRepository = RepositoriesFactory.CreateRepository <ProgramRepository, Program>();

            programRepository.Delete(programId);

            return(new ActionResponse()
            {
                CompletedSuccessfully = true
            });
        }
Example #14
0
        public ActionResponse AddNewDrill(DrillDTO newDrill)
        {
            ActionResponse response;

            response = ValidateDrill(newDrill);

            if (response != null)
            {
                return(response);
            }

            Drill drillToSave = ConvertDTOToEntity(newDrill);

            drillToSave.CreatorUserId = GetCurrentUser().UserId;
            RepositoriesFactory.CreateRepository <DrillsRepository, Drill>().Add(drillToSave);

            response = new ActionResponse();
            response.CompletedSuccessfully = true;
            return(response);
        }
Example #15
0
        public ActionResponse DeleteDrill(int drillId)
        {
            ActionResponse response;

            response = CanUserEditDrill(drillId);

            if (response != null)
            {
                return(response);
            }

            DrillsRepository drillRepository = RepositoriesFactory.CreateRepository <DrillsRepository, Drill>();

            drillRepository.Delete(drillId);

            return(new ActionResponse()
            {
                CompletedSuccessfully = true
            });
        }
Example #16
0
        private ActionResponse ValidateUser(User user)
        {
            UsersRepository repository = RepositoriesFactory.CreateRepository <UsersRepository, User>();

            if (repository.Query().Any(currUser => currUser.UserName == user.UserName && currUser.UserId != user.UserId))
            {
                return(new ActionResponse()
                {
                    CompletedSuccessfully = false, ErrorMessage = "a user with the given name already exists"
                });
            }
            else if (!Enum.IsDefined(typeof(PermissionsType), user.Permission))
            {
                return(new ActionResponse()
                {
                    CompletedSuccessfully = false, ErrorMessage = "invalid permission"
                });
            }

            return(null);
        }
Example #17
0
        public ActionResponse CreateNewUser(User user)
        {
            ActionResponse response = ValidateUser(user);

            if (response != null)
            {
                return(response);
            }

            RepositoriesFactory.CreateRepository <UsersRepository, User>().Add(new User()
            {
                UserName   = user.UserName,
                Password   = TokenManager.HashUsingHmac(user.Password, TokenManager.key),
                Permission = user.Permission
            });

            return(new ActionResponse()
            {
                CompletedSuccessfully = true, ErrorMessage = null
            });
        }
Example #18
0
        public ActionResponse DeleteUser(String userName)
        {
            UsersRepository repository = RepositoriesFactory.CreateRepository <UsersRepository, User>();
            User            user       = repository.Query()
                                         .Where(currUser => currUser.UserName == userName)
                                         .FirstOrDefault();

            if (user == null)
            {
                return(new ActionResponse()
                {
                    CompletedSuccessfully = false, ErrorMessage = "User does not exist"
                });
            }

            repository.Delete(user.UserId);

            return(new ActionResponse()
            {
                CompletedSuccessfully = true
            });
        }
Example #19
0
        public ActionResponse AddNewProgram(ProgramDTO newProgram)
        {
            ProgramRepository repository = RepositoriesFactory.CreateRepository <ProgramRepository, Program>();

            ActionResponse response = ValidateProgram(newProgram);

            if (response == null)
            {
                Program programToSave = ConvertDTOToEntity(newProgram, true);

                repository.Add(programToSave);

                return(new ActionResponse()
                {
                    CompletedSuccessfully = true
                });
            }
            else
            {
                return(response);
            }
        }
Example #20
0
        private ActionResponse ValidateProgram(ProgramDTO newProgram, bool isUpdate = false)
        {
            ActionResponse response = null;

            try
            {
                if (RepositoriesFactory.CreateRepository <ProgramRepository, Program>().Query()
                    .Any(currProgram => currProgram.ProgramId != newProgram.Id && currProgram.ProgramName == newProgram.Name))
                {
                    return(new ActionResponse()
                    {
                        CompletedSuccessfully = false, ErrorMessage = "A Program with the given name already exists"
                    });
                }
            }
            catch (Exception e)
            {
                throw e;
            }

            return(response);
        }
Example #21
0
        public ActionResponse UpdateUser(User user)
        {
            ActionResponse response = ValidateUser(user);

            if (response != null)
            {
                return(response);
            }
            UsersRepository repository = new UsersRepository();

            User existingUser = repository.Get(user.UserId);

            existingUser.UserName   = user.UserName;
            existingUser.Password   = TokenManager.HashUsingHmac(user.Password, TokenManager.key);
            existingUser.Permission = user.Permission;

            RepositoriesFactory.CreateRepository <UsersRepository, User>().Update(existingUser);

            return(new ActionResponse()
            {
                CompletedSuccessfully = true, ErrorMessage = null
            });
        }
Example #22
0
        public ActionResponse UpdateDrill(DrillDTO newDrill)
        {
            ActionResponse response;

            response = ValidateDrill(newDrill);

            if (response != null)
            {
                return(response);
            }

            response = CanUserEditDrill(newDrill.Id);

            if (response != null)
            {
                return(response);
            }


            Drill drillForUpdate = ConvertDTOToEntity(newDrill);

            drillForUpdate.CreatorUserId = GetCurrentUser().UserId;
            DrillsRepository drillRepository = RepositoriesFactory.CreateRepository <DrillsRepository, Drill>();

            Drill existingDrill = drillRepository.Get(drillForUpdate.DrillId);

            existingDrill.Name              = drillForUpdate.Name;
            existingDrill.Description       = drillForUpdate.Description;
            existingDrill.DrillMuscleGroups = drillForUpdate.DrillMuscleGroups;

            drillRepository.Update(existingDrill);

            return(new ActionResponse()
            {
                CompletedSuccessfully = true
            });
        }
Example #23
0
 public ICollection <DrillDTO> GetAllDrills()
 {
     return(RepositoriesFactory.CreateRepository <DrillsRepository, Drill>().GetALL()
            .Select <Drill, DrillDTO>(currDrill => ConvertEntityTODTO(currDrill)).ToList());
 }