public async Task <bool> EditCourse(DTOEditCourse courseToEdit)
        {
            try
            {
                Course course1 = await _context.Course.Where(a => a.Id == courseToEdit.Id).Include(b => b.Holes).FirstOrDefaultAsync();

                course1.CourseName = courseToEdit.CourseName;
                foreach (var hole in course1.Holes)
                {
                    var newValues = courseToEdit.Holes.Where(b => b.HoleNumber == hole.hole_nr).FirstOrDefault();
                    if (newValues != null)
                    {
                        hole.Par    = newValues.Par;
                        hole.Stroke = newValues.Stroke;
                    }
                }

                _context.SaveChanges();
                return(true);
            }
            catch (Exception e)
            {
                return(false);
            }
        }
        public async Task <DTOEditCourse> GetCourse(int id)
        {
            try
            {
                var course = await _context.Course.Include(y => y.Holes).Where(x => x.Id == id).SingleAsync();

                var courseObj = new DTOEditCourse()
                {
                    Id         = course.Id,
                    CourseName = course.CourseName,
                    Holes      = new List <DTONewCourseHole>()
                };

                foreach (var hole in course.Holes)
                {
                    DTONewCourseHole newhole = new DTONewCourseHole()
                    {
                        HoleNumber = hole.hole_nr,
                        Par        = hole.Par,
                        Stroke     = hole.Stroke
                    };
                    courseObj.Holes.Add(newhole);
                }
                courseObj.Holes = courseObj.Holes.OrderBy(y => y.HoleNumber).ToList();
                return(courseObj);
            }
            catch (Exception e)
            {
                return(null);
            }
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> EditCourse([FromBody] DTOEditCourse courseToEdit)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(new BadRequestObjectResult(new { msg = "Invalid model" }));
                }

                if (await _courseRepository.EditCourse(courseToEdit))
                {
                    return(Ok(new { msg = "New Course Added" }));
                }
                return(new BadRequestObjectResult(new { msg = "Could Not Edit Course" }));
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }