Пример #1
0
        public IActionResult ModifyStudent(UpdateStudentsRequest updateStudentsRequest)
        {
            HelperRequests helper = _server.UpdateStudents(updateStudentsRequest);

            switch (helper.Number)
            {
            case 0:
                return(BadRequest("Student with this index not exists"));

            default:
                return(Ok("Student modify"));
            }
        }
Пример #2
0
        public HelperRequests UpdateStudents(UpdateStudentsRequest request)
        {
            var helper = new HelperRequests();

            // var studentToUpdate = _dbContext.Student.First(student => student.IndexNumber == request.IndexNumber);
            // studentToUpdate.BirthDate = request.BirthDate;
            // studentToUpdate.FirstName = request.FirstName;
            // studentToUpdate.LastName = request.LastName;


            var countStudents = _dbContext.Student.Count(stu => stu.IndexNumber == request.IndexNumber);

            if (countStudents == 0)
            {
                helper.Number = 0;
                return(helper);
            }


            var student = new Student
            {
                IndexNumber = request.IndexNumber,
                FirstName   = request.FirstName,
                LastName    = request.LastName,
                BirthDate   = request.BirthDate
            };

            _dbContext.Attach(student);
            // _dbContext.Entry(student).Property("IndexNumber").IsModified = true;
            _dbContext.Entry(student).Property("FirstName").IsModified = true;
            _dbContext.Entry(student).Property("LastName").IsModified  = true;
            _dbContext.Entry(student).Property("BirthDate").IsModified = true;

            _dbContext.SaveChangesAsync();

            helper.Number = 1;

            return(helper);
        }