Example #1
0
 public ActionResult Create(InstructorCreate model)
 {
     if (ModelState.IsValid)
     {
         var service = GetInstructorService();
         if (service.CreateInstructor(model))
         {
             return(RedirectToAction(nameof(Index)));
         }
     }
     return(View(model));
 }
        public ActionResult CreateInstructor(InstructorCreate model)
        {
            if (ModelState.IsValid)
            {
                var service = GetBrowsingService();
                if (service.CreateInstructor(model))
                {
                    return(RedirectToAction(nameof(Index)));
                }
            }
            //ModelState.AddModelError("AcademyName", "Please create the academy first or enter an academy that already exists");

            return(View(model));
        }
        public bool CreateInstructor(InstructorCreate model)
        {
            var entity = new Instructor
            {
                InstructorName = model.InstructorName,
                LocationID     = model.LocationID
            };

            using (var ctx = new ApplicationDbContext())
            {
                ctx.Instructors.Add(entity);
                return(ctx.SaveChanges() > 0);
            }
        }
        public IHttpActionResult PostInstructor([FromBody] InstructorCreate model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var svc = new InstructorService();

            if (!svc.CreateInstructor(model))
            {
                return(InternalServerError());
            }

            return(Ok("Instructor successfully added."));
        }
        public bool CreateInstructor(InstructorCreate model)
        {
            var        ctx       = new ApplicationDbContext();
            var        academyId = ctx.Academies.FirstOrDefault(a => a.Name == model.AcademyName).AcademyId;
            var        programId = ctx.Programs.FirstOrDefault(a => a.Name == model.ProgramName).ProgramId;
            string     createdBy = ctx.Users.FirstOrDefault(u => u.Id == _userId).UserName;
            Instructor entity    = new Instructor
            {
                FullName    = model.FullName,
                ProgramName = model.ProgramName,
                ProgramId   = programId,
                AcademyName = model.AcademyName,
                AcademyId   = academyId,
                OwnerId     = _userId,
                CreatedBy   = createdBy
            };

            _context.Instructors.Add(entity);
            var changeCount = _context.SaveChanges();

            return(changeCount == 1);
        }