public async Task <ActionResult> Create(CreateCourseParticipantViewModel courseParticipant) // todo ViewModel CourseId should be GET param.
        {
            if (!ModelState.IsValid)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            using (var context = new SeminarDbContext())
            {
                var courseModel = await context.Courses.FindAsync(courseParticipant.CourseId);

                if (courseModel == null)
                {
                    return(new HttpStatusCodeResult(HttpStatusCode.NotFound));
                }

                context.CourseParticipants.Add(new CourseParticipant
                {
                    Name   = courseParticipant.Name,
                    Email  = courseParticipant.Email,
                    Course = courseModel
                });

                await context.SaveChangesAsync();
            }

            return(new HttpStatusCodeResult(HttpStatusCode.Created));
        }
Exemplo n.º 2
0
        public async Task <ActionResult> Create([Bind(Exclude = "Id")] CourseViewModel course)
        {
            if (!ModelState.IsValid)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            using (var context = new SeminarDbContext())
            {
                var model = _courseMapper.MapToModel(course);
                context.Courses.Add(model);
                await context.SaveChangesAsync();
            }

            return(new HttpStatusCodeResult(HttpStatusCode.Created));
        }