예제 #1
0
        public IActionResult Update(int id, Models.Requests.Workout.WorkoutAdviceCreate adviceCreate)
        {
            UserInfo userInfo = _authenticationService.IsAuthorized(Request, new[] { "EMPLOYEE", "TRAINER" });

            _workoutAdviceService.Update(id, adviceCreate, userInfo);
            return(NoContent());
        }
        public Models.Workout.WorkoutAdvice Create(Models.Requests.Workout.WorkoutAdviceCreate adviceCreate, UserInfo userInfo)
        {
            Client dbClient = _context.Clients.Find(adviceCreate.ClientId);

            if (dbClient == null)
            {
                throw new ResourceNotFoundException($"Client with id {adviceCreate.ClientId} not found");
            }

            if (userInfo.Role == "EMPLOYEE")
            {
                Trainer dbTrainer = _context.Trainers.Find(adviceCreate.TrainerId);
                if (dbTrainer == null)
                {
                    throw new ResourceNotFoundException($"Trainer with id {adviceCreate.TrainerId} not found");
                }
            }
            else
            {
                adviceCreate.TrainerId = userInfo.Id;
            }

            WorkoutAdvice dbWorkoutAdvice = WorkoutAdviceMapper.toDb(adviceCreate);

            _context.WorkoutAdvices.Add(dbWorkoutAdvice);
            _context.SaveChanges();
            return(GetById(dbWorkoutAdvice.Id));
        }
        public void Update(int id, Models.Requests.Workout.WorkoutAdviceCreate adviceCreate, UserInfo userInfo)
        {
            WorkoutAdvice dbWorkoutAdvice = _context.WorkoutAdvices.Find(id);

            if (dbWorkoutAdvice == null)
            {
                throw new ResourceNotFoundException($"Workout advice with id {id} not found");
            }

            Client dbClient = _context.Clients.Find(adviceCreate.ClientId);

            if (dbClient == null)
            {
                throw new ResourceNotFoundException($"Client with id {adviceCreate.ClientId} not found");
            }

            if (userInfo.Role == "TRAINER")
            {
                adviceCreate.TrainerId = userInfo.Id;
            }

            Trainer dbTrainer = _context.Trainers.Find(adviceCreate.TrainerId);

            if (dbTrainer == null)
            {
                throw new ResourceNotFoundException($"Trainer with id {adviceCreate.TrainerId} not found");
            }

            if (userInfo.Role == "TRAINER")
            {
                if (dbWorkoutAdvice.TrainerId != userInfo.Id)
                {
                    throw new OperationNowAllowedException();
                }
            }

            if (adviceCreate.Message != null)
            {
                dbWorkoutAdvice.Message = adviceCreate.Message;
            }
            if (adviceCreate.ClientId != null)
            {
                dbWorkoutAdvice.ClientId = adviceCreate.ClientId;
            }
            if (adviceCreate.TrainerId != null)
            {
                dbWorkoutAdvice.TrainerId = adviceCreate.TrainerId;
            }

            _context.WorkoutAdvices.Update(dbWorkoutAdvice);
            _context.SaveChanges();
        }
예제 #4
0
        public Models.Workout.WorkoutAdvice Create(Models.Requests.Workout.WorkoutAdviceCreate adviceCreate)
        {
            UserInfo userInfo = _authenticationService.IsAuthorized(Request, new[] { "EMPLOYEE", "TRAINER" });

            return(_workoutAdviceService.Create(adviceCreate, userInfo));
        }