Ejemplo n.º 1
0
        public IActionResult Edit(int id, [Bind("Id,FirstName,LastName,BirthDate,PersonalNr,GenderID")] Student student)
        {
            ViewBag.Edit = true;
            if (id != student.Id)
            {
                return(NotFound());
            }

            ModelValidator.ValidateStudent(ModelState, student, _unitOfWork);
            if (ModelState.IsValid)
            {
                try
                {
                    _unitOfWork.StudentRepository.Update(student);
                    _unitOfWork.Save();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!StudentExists(student.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["GenderID"] = new SelectList(_unitOfWork.GenderRepository.Get(), "ID", "GenderName", student.GenderID);
            ViewBag.ActionName   = "edit";
            ViewBag.Heading      = "სტუდენტის რედაქტირება";
            return(View("CreateUpdate", student));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> PutStudent(int id, StudentVM student)
        {
            if (id != student.id)
            {
                return(BadRequest());
            }
            var stud = _context.StudentRepository.GetByID(id);

            stud.FirstName  = student.firstName;
            stud.LastName   = student.lastName;
            stud.PersonalNr = student.pN;
            stud.GenderID   = student.genderId;
            stud.BirthDate  = student.DoB;

            try
            {
                ModelValidator.ValidateStudent(ModelState, stud, _context);
                if (ModelState.IsValid)
                {
                    _context.StudentRepository.Update(stud);
                    _context.Save();
                }
                else
                {
                    var error       = new ApiError(400, "Model Validation Failed");
                    var modelErrors = ModelState.Values.Where(x => x.Errors.Count > 0).Select(e => e.Errors).ToList();
                    foreach (var item in modelErrors)
                    {
                        foreach (var er in item)
                        {
                            error.AddError(er.ErrorMessage);
                        }
                    }
                    return(new ObjectResult(error));
                }
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!StudentExists(stud.Id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Ejemplo n.º 3
0
 public IActionResult Create([Bind("Id,FirstName,LastName,BirthDate,PersonalNr,GenderID")] Student student)
 {
     ModelValidator.ValidateStudent(ModelState, student, _unitOfWork);
     if (ModelState.IsValid)
     {
         _unitOfWork.StudentRepository.Insert(student);
         _unitOfWork.Save();
         return(RedirectToAction(nameof(Index)));
     }
     ViewBag.ActionName   = "create";
     ViewBag.Heading      = "სტუდენტის დამატება";
     ViewData["GenderID"] = new SelectList(_unitOfWork.GenderRepository.Get(), "ID", "GenderName", student.GenderID);
     return(View("CreateUpdate", student));
 }
Ejemplo n.º 4
0
        public async Task <ActionResult <StudentVM> > PostStudent(StudentVM stud)
        {
            Student student = new Student()
            {
                BirthDate  = stud.DoB,
                FirstName  = stud.firstName,
                GenderID   = stud.genderId,
                LastName   = stud.lastName,
                PersonalNr = stud.pN
            };

            ModelValidator.ValidateStudent(ModelState, student, _context);
            if (ModelState.IsValid)
            {
                try
                {
                    _context.StudentRepository.Insert(student);
                    _context.Save();
                }
                catch (DbUpdateConcurrencyException)
                {
                    throw;
                }
            }
            else
            {
                var error       = new ApiError(400, "Model Validation Failed");
                var modelErrors = ModelState.Values.Where(x => x.Errors.Count > 0).Select(e => e.Errors).ToList();
                foreach (var item in modelErrors)
                {
                    foreach (var er in item)
                    {
                        error.AddError(er.ErrorMessage);
                    }
                }
                return(new ObjectResult(error));
            }
            stud.id = student.Id;
            return(CreatedAtAction("GetStudent", new { id = student.Id }, stud));
        }