Exemplo n.º 1
0
        public async Task <ActionResult <CourseDto> > PostCourseDto(CourseDtoForCreation courseDto)
        {
            try
            {
                var result = await _unitOfWork.CourseRepository.GetCourseByNameAsync(courseDto.CourseName);

                if (result != null)
                {
                    return(BadRequest($"Kursen {courseDto.CourseName} existerar redan!"));
                }

                _unitOfWork.CourseRepository.AddCourseToRepo(courseDto);
                if (await _unitOfWork.Complete())
                {
                    return(StatusCode(201, "Succesfull, a new course resource was created"));
                }
                return(StatusCode(500, "Could not save course resource"));
            }
            catch (Exception ex)
            {
                return(StatusCode(500, ex.Message));
            }
        }
        public void AddCourseToRepo(CourseDtoForCreation course)
        {
            var creationCourseMappedToEntityModel = _mapper.Map <Course>(course);

            _context.Entry(creationCourseMappedToEntityModel).State = EntityState.Added;
        }