public async Task <IActionResult> PutWorkout([FromRoute] int id, [FromBody] Workout workout)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != workout.Id)
            {
                return(BadRequest());
            }

            _context.Entry(workout).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!WorkoutExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
예제 #2
0
        public async Task <IActionResult> Create([Bind("Id,Name,Password")] User user)
        {
            if (ModelState.IsValid)
            {
                _context.Add(user);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(user));
        }
예제 #3
0
        public async Task <IActionResult> Create([Bind("Id,Name")] Workout workout)
        {
            if (ModelState.IsValid)
            {
                _context.Add(workout);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(workout));
        }
예제 #4
0
        public async Task <IActionResult> Create([Bind("Id,Date,Sets,Repetitions,Weight")] HistoryItem historyItem)
        {
            if (ModelState.IsValid)
            {
                _context.Add(historyItem);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(historyItem));
        }
        public async Task <IActionResult> Create([Bind("Id,Name")] TrainingPlan trainingPlan)
        {
            if (ModelState.IsValid)
            {
                _context.Add(trainingPlan);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(trainingPlan));
        }
        public async Task <IActionResult> AddTrainingToUser([FromBody] UserTrainingForm data)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest());
                }

                var user = await _context.Users.SingleOrDefaultAsync(d => d.Id == data.UserId);

                if (user == null)
                {
                    return(NotFound(data.UserId));
                }

                var trainingPlan = await _context.TrainingPlans.SingleOrDefaultAsync(t => t.Id == data.TrainingPlan.Id);

                if (trainingPlan == null)
                {
                    if (data.TrainingPlan.Id != 0)
                    {
                        return(NotFound(data.TrainingPlan));
                    }
                }
                user.Trainings.Add(data.TrainingPlan);
                await _context.SaveChangesAsync();

                var addedTraining = user.Trainings.LastOrDefault();
                var workoutDays   = new List <TrainingDayWorkout>();
                foreach (var day in addedTraining?.Days)
                {
                    foreach (var workout in _context.Workouts.Where(w => day.Workouts.Any(x => x.Id == w.Id)))
                    {
                        workoutDays.Add(new TrainingDayWorkout {
                            TrainingDay = day, TrainingDayId = day.Id, Workout = workout, WorkoutId = workout.Id
                        });
                    }
                }
                _context.TrainingDayWorkouts.AddRange(workoutDays);
                await _context.SaveChangesAsync();

                user.PrepareSerialize(_context.Workouts.ToList());
                return(Ok(user));
            }
            catch (Exception e)
            {
                return(StatusCode(500));
            }
        }