Exemplo n.º 1
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());
        }
Exemplo n.º 2
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));
        }