예제 #1
0
        public async Task <ActionResult <Exercise> > Create([FromBody] ExerciseDTO exercise)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            Exercise newExercise = await exerciseRepository.CreateAsync(exercise);

            return(CreatedAtAction("Get", new { id = newExercise.Id }, newExercise));
        }
        /// <summary>
        ///     Private method only used to populate the database, Normally you will not be doing it like
        ///     this but use a different method of seeding the database
        /// </summary>
        /// <returns></returns>
        private async Task PopulateDatabase()
        {
            var entities = await _exerciseRepository.GetAllAsync();

            if (entities == null || !entities.Any())
            {
                var exercises = new List <Exercise>
                {
                    new Exercise
                    {
                        Name        = "Bench Press",
                        Description = "Compound Exercise"
                    },
                    new Exercise
                    {
                        Name        = "Squat",
                        Description = "Compound Exercise"
                    },
                    new Exercise
                    {
                        Name        = "Deadlift",
                        Description = "Compound Exercise"
                    },
                    new Exercise
                    {
                        Name        = "Shoulder press",
                        Description = "Compound Exercise"
                    }
                };

                foreach (var exercise in exercises)
                {
                    await _exerciseRepository.CreateAsync(exercise);
                }
                LogInformation("PopulateDatabase", "Database populated");
            }
            else
            {
                LogInformation("PopulateDatabase", "Database already populated");
            }
        }