public async Task <ActionResult <Course> > PostCourse([FromBody] CourseAddModel cam)
        {
            string cid    = _globalService.GenRandomId(14);
            Course course = new Course
            {
                CourseId = cid,
                Name     = cam.Name,
                Intro    = cam.Intro,
            };

            _context.Course.Add(course);
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (CourseExists(cid))
                {
                    return(Conflict());
                }
                else
                {
                    throw;
                }
            }

            return(CreatedAtAction("GetCourse", new { id = course.CourseId }, course));
        }
Esempio n. 2
0
        public async Task <IActionResult> Insert(CourseAddModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var dto = _mapper.Map <CourseDto>(model);

            try
            {
                int id = await _courseService.AddCourse(dto);

                return(CreatedAtRoute("Course Get", new { Id = id },
                                      new CourseModel(id, model.Name, model.LecturerId, Array.Empty <int>())));
            }
            catch (IncorrectIdException e)
            {
                _logger.LogWarning(e, "It seems, validation does not cover some errors.");
                return(BadRequest(e.Message));
            }
        }