public void UpdateDailyWorkout()
        {
            //Arrangevar listExercises = new List<ExerciseItem>();
            var listExercises = new List <ExerciseItem>();

            listExercises.Add(new ExerciseItem()
            {
                Name         = "teszt78",
                Description  = "",
                MuscleGroup  = "",
                Reps         = 1,
                SerialNumber = 1
            });
            var saveInstance = new DailyWorkout()
            {
                Name = "Test788", Exercises = listExercises
            };

            //Act
            saveInstance.Id   = _dailyWorkoutService.InsertDailyWorkout(saveInstance, _testUser).Id;
            saveInstance.Name = "TestUpdate";
            _dailyWorkoutService.UpdateDailyWorkout(saveInstance.Id, saveInstance);

            //Assert
            Assert.AreEqual(_dailyWorkoutService
                            .GetDailyWorkout(saveInstance.Id, _testUser).Name, "TestUpdate");
        }
Beispiel #2
0
        public void Edit_ShouldReturnCorrectJsonWhenModelInvalid()
        {
            var mapperStub = new Mock <IMapper>();
            var weekScheduleServiceStub       = new Mock <IWeekScheduleService>();
            var workoutInformationServiceStub = new Mock <IWorkoutInformationService>();
            var modelViewFactoryStub          = new Mock <IModelViewFactory>();
            var dailyWorkoutServiceStub       = new Mock <IDailyWorkoutService>();

            var sut = new ScheduleController(mapperStub.Object, weekScheduleServiceStub.Object, workoutInformationServiceStub.Object, dailyWorkoutServiceStub.Object, modelViewFactoryStub.Object);

            var dailyWorkout = new DailyWorkoutViewModel();

            dailyWorkout.Name       = "awesomeness";
            dailyWorkout.ScheduleId = new Guid();

            var wodInfo      = new WorkoutInformation();
            var wodDataModel = new DailyWorkout();
            var schedule     = new WeekSchedule();


            workoutInformationServiceStub.Setup(x => x.GetByName(It.IsAny <string>())).Returns(wodInfo);
            mapperStub.Setup(x => x.Map <DailyWorkout>(It.IsAny <DailyWorkoutViewModel>())).Returns(wodDataModel);
            dailyWorkoutServiceStub.Setup(x => x.AddAllDailyWorkouts(It.IsAny <DailyWorkout>(), It.IsAny <DailyWorkout>(), It.IsAny <DailyWorkout>()));
            weekScheduleServiceStub.Setup(x => x.GetById(It.IsAny <Guid>())).Returns(schedule);
            weekScheduleServiceStub.Setup(x => x.AddDailiesToSchedule(It.IsAny <WeekSchedule>(), It.IsAny <DailyWorkout>(), It.IsAny <DailyWorkout>(), It.IsAny <DailyWorkout>()));

            sut
            .WithModelErrors()
            .WithCallTo(c => c.Edit(dailyWorkout))
            .ShouldReturnJson();
        }
Beispiel #3
0
        private Task <DailyWorkout> MakeDailyWorkout(WorkoutDisplayDTO workout)
        {
            _logger.LogInformation("SaveDailyWorkoutCommandHandler");

            DailyWorkout dailyWorkout = new DailyWorkout();

            dailyWorkout.Phase       = workout.Phase;
            dailyWorkout.WorkoutDate = DateTime.Now;
            dailyWorkout.WorkoutId   = workout.WorkoutId;
            dailyWorkout.Duration    = workout.Duration;

            foreach (SetDisplayDTO set in workout.Set)
            {
                set.Exercise.ForEach(ex => ex.Reps.ForEach(rep => dailyWorkout.DailyWorkoutInfo.Add(new DailyWorkoutInfo
                {
                    ExerciseId = ex.ExerciseId,
                    SetId      = ex.SetId,
                    RepsId     = rep.RepsId,
                    WeightUsed = rep.Weight,
                    WorkoutId  = workout.WorkoutId
                })));
            }

            return(Task.FromResult(dailyWorkout));
        }
Beispiel #4
0
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DateTime dtNow      = DateTime.Now;
            string   todaysDate = dtNow.ToString("MM/dd/yyyy");
            txtEntryDate.Text = todaysDate;
            clnDate.Visible   = false;

            //Load HRS & MINS in the Bike & Swimming dropdown lists
            for (int i = 0; i <= 24; i++)
            {
                ddlHrsBike.Items.Add(i.ToString());
                ddlHrsSwim.Items.Add(i.ToString());
            }
            for (int i = 0; i <= 59; i++)
            {
                ddlMinsBike.Items.Add(i.ToString());
                ddlMinsSwim.Items.Add(i.ToString());
            }

            //determine user who is currently logged in
            lblCurrUser.Text = Page.User.Identity.Name;

            DailyWorkout thisDaysWorkout = new DailyWorkout();
            //retrieve this users Workout for todays date if one exists then places it on the screen
            thisDaysWorkout = WorkoutDB.RetrieveScreenFields(dtNow, Page.User.Identity.Name);
            PopulateScreenFields(thisDaysWorkout);

            // Write thisDaysWorkout to Session State
            Session["sessThisDaysWorkout"] = thisDaysWorkout;
            lblLoggedInUser.Text           = Page.User.Identity.Name;
        }
    }
        public void DeleteDailyWorkout()
        {
            //Arrange
            var listExercises = new List <ExerciseItem>();

            listExercises.Add(new ExerciseItem()
            {
                Name         = "teszt",
                Description  = "",
                MuscleGroup  = "",
                Reps         = 1,
                SerialNumber = 1
            });
            var saveInstance = new DailyWorkout()
            {
                Name = "Teszt272", Exercises = listExercises
            };

            //Act

            var id = _dailyWorkoutService.InsertDailyWorkout(saveInstance,
                                                             _testUser).Id;

            _dailyWorkoutService.DeleteDailyWorkout(id, _testUser);

            //Assert
            Assert.IsTrue(_dailyWorkoutService
                          .GetDailyWorkouts(_testUser).Count() == 2);
        }
Beispiel #6
0
        public async Task <DailyWorkout> SaveDailyWorkoutAsync(DailyWorkout workout)
        {
            await _dbContext.DailyWorkout.AddAsync(workout);

            await SaveChangesAsync();

            return(workout);
        }
Beispiel #7
0
        public async Task <DailyWorkoutDTO> Handle(SaveDailyWorkoutCommand request, CancellationToken cancellationToken)
        {
            var dailyWorkout = AsyncHelper.RunSync <DailyWorkout>(() => MakeDailyWorkout(request.Workout));

            DailyWorkout savedWorkout = await _repository.SaveDailyWorkoutAsync(dailyWorkout).ConfigureAwait(false);

            return(_mapper.Map <DailyWorkoutDTO>(savedWorkout));
        }
Beispiel #8
0
 public bool IsValidDailyWorkout(DailyWorkout dw)
 {
     if (dw.Exercises == null)
     {
         return(false);
     }
     return(dw.Name.Length > 3 &&
            dw.Exercises.ToList().Count > 0);
 }
Beispiel #9
0
        public void AddDailyWorkout(DailyWorkout dailyWorkout)
        {
            if (dailyWorkout == null)
            {
                throw new ArgumentException("Daily workout cannot be null");
            }

            this.dailyWorkoutRepo.Add(dailyWorkout);
            this.unitOfWork.Commit();
        }
Beispiel #10
0
        public void AddMemberToDailyWorkout_ShouldThrowWhenInvalidMemberIsPassed()
        {
            var dailyWorkoutRepoMock = new Mock <IEfRepostory <DailyWorkout> >();
            var unitOfWorkStub       = new Mock <IUnitOfWork>();
            var sut = new DailyWorkoutService(dailyWorkoutRepoMock.Object, unitOfWorkStub.Object);

            var workout = new DailyWorkout();

            Assert.Throws <ArgumentException>(() => sut.AddMemberToDailyWorkout(null, workout));
        }
Beispiel #11
0
    protected void clnDate_SelectionChanged(object sender, EventArgs e)
    {
        txtEntryDate.Text    = clnDate.SelectedDate.ToShortDateString();
        clnDate.Visible      = false;
        ibtnCalendar.Visible = true;

        thisDaysWorkout = WorkoutDB.RetrieveScreenFields(clnDate.SelectedDate, Page.User.Identity.Name);
        PopulateScreenFields(thisDaysWorkout);
        Session.Remove("sessThisDaysWorkout");
        // Write thisDaysWorkout to Session State
        Session["sessThisDaysWorkout"] = thisDaysWorkout;
    }
Beispiel #12
0
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            if (Session["sessThisDaysWorkout"] != null)
            {
                thisDaysWorkout = (DailyWorkout)Session["sessThisDaysWorkout"];
            }

            thisDaysWorkout.TrainingDate = Convert.ToDateTime(txtEntryDate.Text);
            thisDaysWorkout.WorkoutType  = ddlWorkoutTypes.SelectedValue;

            thisDaysWorkout.TotalMilesRun   = txtTotalMilesRun.Text;
            thisDaysWorkout.QualityMilesRun = txtQualityMilesRun.Text;

            int tempBikeMins =
                (Convert.ToInt32(ddlMinsBike.SelectedValue))
                +
                (Convert.ToInt32(ddlHrsBike.SelectedValue) * 60);
            thisDaysWorkout.BikeMins = Convert.ToString(tempBikeMins);

            int tempSwimMins =
                (Convert.ToInt32(ddlMinsSwim.SelectedValue))
                +
                (Convert.ToInt32(ddlHrsSwim.SelectedValue) * 60);
            thisDaysWorkout.SwimMins = Convert.ToString(tempSwimMins);

            thisDaysWorkout.OverallWorkoutDescription = txtWorkoutDesc.Text;

            if (thisDaysWorkout.insertOrUpdateFlag == "Update")
            {
                WorkoutObjectDB.UpdateWorkoutDay(thisDaysWorkout);
            }
            //SqlDataSource3.Update();
            else
            {
                WorkoutObjectDB.InsertWorkoutDay(thisDaysWorkout);
            }
            //SqlDataSource2.Insert();
            //SqlDataSource3.Insert();

            lblTrainingInfoUpdate.Text = "Training database was successfully updated for " + txtEntryDate.Text;
            //thisDaysWorkout = new DailyWorkout();

            Session.Remove("sessThisDaysWorkout");
            // Write thisDaysWorkout to Session State
            thisDaysWorkout = WorkoutDB.RetrieveScreenFields(Convert.ToDateTime(txtEntryDate.Text), Page.User.Identity.Name);

            PopulateScreenFields(thisDaysWorkout);
            // Write thisDaysWorkout to Session State
            Session["sessThisDaysWorkout"] = thisDaysWorkout;
        }
    }
        public static void Init(TestContext c)
        {
            var options = new DbContextOptionsBuilder
                          <WorkoutContext>()
                          .UseInMemoryDatabase()
                          .Options;

            _context = new WorkoutContext(options, true);
            _weeklyWorkoutService = new WeeklyWorkoutService(_context);
            _testUser             = new ApplicationUser()
            {
                Email           = "*****@*****.**",
                DailyWorkouts   = new List <DailyWorkout>(),
                WeeklyWorkouts  = new List <WeeklyWorkout>(),
                MonthlyWorkouts = new List <MonthlyWorkout>()
            };
            if (_context.Users.Count() != 0)
            {
                foreach (ApplicationUser u in _context.Users)
                {
                    _context.Users.Remove(u);
                }
            }
            if (_context.DailyWorkouts.Count() != 0)
            {
                foreach (DailyWorkout u in _context.DailyWorkouts)
                {
                    _context.DailyWorkouts.Remove(u);
                }
            }
            if (_context.WeeklyWorkouts.Count() != 0)
            {
                foreach (WeeklyWorkout u in _context.WeeklyWorkouts)
                {
                    _context.WeeklyWorkouts.Remove(u);
                }
            }
            if (_context.MonthlyWorkouts.Count() != 0)
            {
                foreach (MonthlyWorkout u in _context.MonthlyWorkouts)
                {
                    _context.MonthlyWorkouts.Remove(u);
                }
            }
            _context.Users.Add(_testUser);
            _testDailyWorkout = new DailyWorkout()
            {
                Id = 100, Name = "Teszt9", UserID = _testUser.Id
            };
            _context.DailyWorkouts.Add(_testDailyWorkout);

            _context.SaveChanges();
        }
Beispiel #14
0
        public TimeSpan?GetBestTimeDaily(DailyWorkout dailyWorkout)
        {
            if (dailyWorkout == null)
            {
                throw new ArgumentException("Daily workout cannot be null");
            }

            return(this.workoutsRepo.All
                   .Where(x => x.WorkoutInformation.Name == dailyWorkout.WorkoutInformation.Name)
                   .OrderBy(x => x.Time)
                   .Select(x => x.Time)
                   .FirstOrDefault());
        }
Beispiel #15
0
        public void AddMemberToDailyWorkout_ShouldAddMemberToWorkoutSignedMembersCollection()
        {
            var dailyWorkoutRepoMock = new Mock <IEfRepostory <DailyWorkout> >();
            var unitOfWorkStub       = new Mock <IUnitOfWork>();
            var sut = new DailyWorkoutService(dailyWorkoutRepoMock.Object, unitOfWorkStub.Object);

            var workout = new DailyWorkout();
            var member  = new Member();

            sut.AddMemberToDailyWorkout(member, workout);

            Assert.AreEqual(1, workout.SignedMembers.Count);
            Assert.AreSame(member, workout.SignedMembers.First());
        }
Beispiel #16
0
        public void AddAllDailyWorkouts_ShouldAddOneWorkoutToRepoWhenOneWorkoutIsPassed()
        {
            var dailyWorkoutRepoMock = new Mock <IEfRepostory <DailyWorkout> >();
            var unitOfWorkStub       = new Mock <IUnitOfWork>();
            var sut = new DailyWorkoutService(dailyWorkoutRepoMock.Object, unitOfWorkStub.Object);

            var workout = new DailyWorkout();

            unitOfWorkStub.Setup(x => x.Commit());
            dailyWorkoutRepoMock.Setup(x => x.Add(It.IsAny <DailyWorkout>()));

            sut.AddAllDailyWorkouts(workout);

            dailyWorkoutRepoMock.Verify(x => x.Add(It.IsAny <DailyWorkout>()), Times.Once);
        }
Beispiel #17
0
        public void AddMemberToDailyWorkout(Member member, DailyWorkout workout)
        {
            if (member == null)
            {
                throw new ArgumentException("Member cannot be null");
            }

            if (workout == null)
            {
                throw new ArgumentException("Workout cannot be null");
            }

            workout.SignedMembers.Add(member);
            this.Update(workout);
        }
Beispiel #18
0
        public void AddAllDailyWorkouts_ShouldAddTwoWorkoutsToRepoWhenTwoWorkoutsArePassed()
        {
            var dailyWorkoutRepoMock = new Mock <IEfRepostory <DailyWorkout> >();
            var unitOfWorkStub       = new Mock <IUnitOfWork>();
            var sut = new DailyWorkoutService(dailyWorkoutRepoMock.Object, unitOfWorkStub.Object);

            var workoutOne = new DailyWorkout();
            var workoutTwo = new DailyWorkout();

            unitOfWorkStub.Setup(x => x.Commit());
            dailyWorkoutRepoMock.Setup(x => x.Add(It.IsAny <DailyWorkout>()));

            sut.AddAllDailyWorkouts(workoutOne, workoutTwo);

            dailyWorkoutRepoMock.Verify(x => x.Add(It.IsAny <DailyWorkout>()), Times.Exactly(2));
        }
Beispiel #19
0
        public void AddAllDailyWorkouts_ShouldCallUnitOfWorkCommitWhenWorkoutsAdded()
        {
            var dailyWorkoutRepoMock = new Mock <IEfRepostory <DailyWorkout> >();
            var unitOfWorkStub       = new Mock <IUnitOfWork>();
            var sut = new DailyWorkoutService(dailyWorkoutRepoMock.Object, unitOfWorkStub.Object);

            var workoutOne = new DailyWorkout();
            var workoutTwo = new DailyWorkout();

            unitOfWorkStub.Setup(x => x.Commit());
            dailyWorkoutRepoMock.Setup(x => x.Add(It.IsAny <DailyWorkout>()));

            sut.AddAllDailyWorkouts(workoutOne, workoutTwo);

            unitOfWorkStub.Verify(x => x.Commit(), Times.Once);
        }
Beispiel #20
0
        public void AddMemberToDailyWorkout_ShouldCallUnitOfWorkCommitOnce()
        {
            var dailyWorkoutRepoMock = new Mock <IEfRepostory <DailyWorkout> >();
            var unitOfWorkStub       = new Mock <IUnitOfWork>();
            var sut = new DailyWorkoutService(dailyWorkoutRepoMock.Object, unitOfWorkStub.Object);

            dailyWorkoutRepoMock.Setup(x => x.Update(It.IsAny <DailyWorkout>()));
            unitOfWorkStub.Setup(x => x.Commit());

            var workout = new DailyWorkout();
            var member  = new Member();

            sut.AddMemberToDailyWorkout(member, workout);

            unitOfWorkStub.Verify(x => x.Commit(), Times.Once);
        }
Beispiel #21
0
        public void AddDailyWorkout_ShouldCallRepoAddOnceWithCorrectDailyWorkout()
        {
            var dailyWorkoutRepoMock = new Mock <IEfRepostory <DailyWorkout> >();
            var unitOfWorkStub       = new Mock <IUnitOfWork>();

            var sut = new DailyWorkoutService(dailyWorkoutRepoMock.Object, unitOfWorkStub.Object);

            dailyWorkoutRepoMock.Setup(x => x.Add(It.IsAny <DailyWorkout>()));
            unitOfWorkStub.Setup(x => x.Commit());

            var workout = new DailyWorkout();

            sut.AddDailyWorkout(workout);

            dailyWorkoutRepoMock.Verify(x => x.Add(workout), Times.Once);
        }
Beispiel #22
0
    protected void btnAddWorkoutType_Click(object sender, EventArgs e)
    {
        DailyWorkout holdDailyWorkout = new DailyWorkout();

        holdDailyWorkout.TrainingDate              = Convert.ToDateTime(txtEntryDate.Text);
        holdDailyWorkout.WorkoutType               = "";
        holdDailyWorkout.TotalMilesRun             = txtTotalMilesRun.Text;
        holdDailyWorkout.QualityMilesRun           = txtQualityMilesRun.Text;
        holdDailyWorkout.BikeMins                  = ddlMinsBike.SelectedValue + (Convert.ToInt32(ddlHrsSwim.SelectedValue) * 60);
        holdDailyWorkout.SwimMins                  = ddlMinsSwim.SelectedValue + (Convert.ToInt32(ddlHrsSwim.SelectedValue) * 60);
        holdDailyWorkout.OverallWorkoutDescription = txtWorkoutDesc.Text;

        // Write holdDailyWorkout to Session State
        Session["currWorkoutType"] = holdDailyWorkout;

        Response.Redirect("UpdateWorkoutType.aspx");
    }
Beispiel #23
0
        public void AddDailiesToSchedule_ShouldAddTwoWorkoutsToDailyWorkoutsWhenTwoWorkoutsAreProvided()
        {
            var weekScheduleStub     = new Mock <IEfRepostory <WeekSchedule> >();
            var unitOfWorkStub       = new Mock <IUnitOfWork>();
            var datetimeProviderStub = new Mock <IDatetimeProvider>();

            var sut = new WeekScheduleService(weekScheduleStub.Object, unitOfWorkStub.Object, datetimeProviderStub.Object);

            var schedule   = new WeekSchedule();
            var workoutOne = new DailyWorkout();
            var workoutTwo = new DailyWorkout();

            weekScheduleStub.Setup(x => x.Update(It.IsAny <WeekSchedule>()));
            unitOfWorkStub.Setup(x => x.Commit());

            sut.AddDailiesToSchedule(schedule, workoutOne, workoutTwo);

            Assert.AreEqual(2, schedule.DailyWorkouts.Count);
        }
Beispiel #24
0
        public void AddDailiesToSchedule_ShouldCallCommitOnTheUnitOfWork()
        {
            var weekScheduleStub     = new Mock <IEfRepostory <WeekSchedule> >();
            var unitOfWorkStub       = new Mock <IUnitOfWork>();
            var datetimeProviderStub = new Mock <IDatetimeProvider>();

            var sut = new WeekScheduleService(weekScheduleStub.Object, unitOfWorkStub.Object, datetimeProviderStub.Object);

            var schedule   = new WeekSchedule();
            var workoutOne = new DailyWorkout();
            var workoutTwo = new DailyWorkout();

            weekScheduleStub.Setup(x => x.Update(It.IsAny <WeekSchedule>()));
            unitOfWorkStub.Setup(x => x.Commit());

            sut.AddDailiesToSchedule(schedule, workoutOne, workoutTwo);

            unitOfWorkStub.Verify(x => x.Commit(), Times.Once);
        }
Beispiel #25
0
        public void GetBestTimeDaily_ShouldReturnNullWhenWorkoutRepoIsEmpty()
        {
            var workoutsRepoStub = new Mock <IEfRepostory <Workout> >();
            var unitOfWorkStub   = new Mock <IUnitOfWork>();

            var list = new List <Workout>();

            var dbSetStub = list.AsQueryable();

            var daily = new DailyWorkout();

            var sut = new WorkoutService(workoutsRepoStub.Object, unitOfWorkStub.Object);

            workoutsRepoStub.Setup(x => x.All).Returns(dbSetStub);

            var result = sut.GetBestTimeDaily(daily);

            Assert.IsNull(result);
        }
Beispiel #26
0
        public void GetBestTimeDaily_ShouldReturnBestTimeWhenTwoWorkoutsArePresentInTheRepo()
        {
            var workoutsRepoStub = new Mock <IEfRepostory <Workout> >();
            var unitOfWorkStub   = new Mock <IUnitOfWork>();

            var list       = new List <Workout>();
            var workoutOne = new Workout();

            workoutOne.WorkoutInformation = new WorkoutInformation
            {
                Name = "w1"
            };
            workoutOne.Time = new TimeSpan(0, 10, 0);

            var workoutTwo = new Workout();

            workoutTwo.WorkoutInformation = new WorkoutInformation
            {
                Name = "w1"
            };
            workoutTwo.Time = new TimeSpan(0, 20, 0);

            list.Add(workoutOne);
            list.Add(workoutTwo);
            var dbSetStub = list.AsQueryable();

            var daily = new DailyWorkout();

            daily.WorkoutInformation = new WorkoutInformation
            {
                Name = "w1"
            };

            var sut = new WorkoutService(workoutsRepoStub.Object, unitOfWorkStub.Object);

            workoutsRepoStub.Setup(x => x.All).Returns(dbSetStub);

            var result = sut.GetBestTimeDaily(daily);

            Assert.AreEqual(result, workoutOne.Time);
        }
Beispiel #27
0
        public void GetById_ShouldReturnCorrectEntryWhenIdMatchesThePasedId()
        {
            var dailyWorkoutRepoMock = new Mock <IEfRepostory <DailyWorkout> >();
            var unitOfWorkStub       = new Mock <IUnitOfWork>();

            var list         = new List <DailyWorkout>();
            var dailyWorkout = new DailyWorkout();

            list.Add(dailyWorkout);

            var dbSetStub = list.AsQueryable();

            var sut = new DailyWorkoutService(dailyWorkoutRepoMock.Object, unitOfWorkStub.Object);

            dailyWorkoutRepoMock.Setup(x => x.All).Returns(dbSetStub);

            var result = sut.GetById(dailyWorkout.Id);

            Assert.IsNotNull(result);
            Assert.IsInstanceOf <DailyWorkout>(result);
            Assert.AreSame(result, dailyWorkout);
        }
Beispiel #28
0
        public void GetById_ShouldReturnNullWhenNoEntryMatchesTheId()
        {
            var dailyWorkoutRepoMock = new Mock <IEfRepostory <DailyWorkout> >();
            var unitOfWorkStub       = new Mock <IUnitOfWork>();

            var list         = new List <DailyWorkout>();
            var dailyWorkout = new DailyWorkout();

            list.Add(dailyWorkout);

            var dbSetStub = list.AsQueryable();

            var sut = new DailyWorkoutService(dailyWorkoutRepoMock.Object, unitOfWorkStub.Object);

            dailyWorkoutRepoMock.Setup(x => x.All).Returns(dbSetStub);

            var newId = new Guid();

            var result = sut.GetById(newId);

            Assert.IsNull(result);
        }
Beispiel #29
0
        public DailyWorkout InsertDailyWorkout(DailyWorkout newDailyWorkout, ApplicationUser user)
        {
            if (!IsValidDailyWorkout(newDailyWorkout))
            {
                throw new NotValidStateException();
            }
            List <ExerciseItem> exercises = new List <ExerciseItem>();

            foreach (var exercise in newDailyWorkout.Exercises)
            {
                var exerciseToSave = new ExerciseItem()
                {
                    Name         = exercise.Name,
                    Reps         = exercise.Reps,
                    SerialNumber = exercise.SerialNumber,
                    Description  = exercise.Description,
                    MuscleGroup  = exercise.MuscleGroup
                };

                _exerciseContext.ExerciseItems.Add(exerciseToSave);
                exercises.Add(exerciseToSave);
            }
            _exerciseContext.SaveChanges();
            var savedInstance = new DailyWorkout()
            {
                Name        = newDailyWorkout.Name,
                WorkoutType = newDailyWorkout.WorkoutType,
                Exercises   = exercises
            };

            savedInstance.UserID = user.Id;
            user.DailyWorkouts.Add(savedInstance);
            _context.DailyWorkouts.Add(savedInstance);

            _context.SaveChanges();

            return(savedInstance);
        }
Beispiel #30
0
        public void UpdateDailyWorkout(int id, DailyWorkout updatedDailyWorkout)
        {
            var current = _context.DailyWorkouts
                          .Include(dw => dw.Exercises)
                          .SingleOrDefault(m => m.Id == updatedDailyWorkout.Id);

            foreach (var exercise in updatedDailyWorkout.Exercises.ToList())
            {
                var newexercise = _exerciseContext.ExerciseItems.SingleOrDefault(m => m.Id == exercise.Id);
                if (newexercise == null)
                {
                    var newElem = new ExerciseItem()
                    {
                        Description  = exercise.Description,
                        Name         = exercise.Name,
                        Reps         = exercise.Reps,
                        SerialNumber = exercise.SerialNumber,
                        MuscleGroup  = exercise.MuscleGroup
                    };
                    _exerciseContext.ExerciseItems.Add(newElem);
                    current.Exercises.Add(newElem);
                }
            }
            current.Name = updatedDailyWorkout.Name;
            try
            {
                _exerciseContext.SaveChanges();
                _context.SaveChanges();
            }
            catch (DbUpdateConcurrencyException e)
            {
                // Létezett-e egyáltalán
                var _ = _context.DailyWorkouts.AsNoTracking().SingleOrDefault(p => p.Id == id)
                        ?? throw new EntityNotFoundException("Nem található az edzés");

                throw;
            }
        }