Пример #1
0
        public IActionResult Add(Models.Student student)
        {
            if (student == null)
            {
                return(NotFound("student is null"));
            }

            var allStudentEmail = _diaryDbContext.Students.FirstOrDefault(user => user.Email == student.Email);
            var allStudentPhone = _diaryDbContext.Students.FirstOrDefault(user => user.Phone == student.Phone);

            if (allStudentEmail == null &&
                allStudentPhone == null &&
                !string.IsNullOrWhiteSpace(student.FirstName) &&
                !string.IsNullOrWhiteSpace(student.LastName) &&
                !string.IsNullOrWhiteSpace(student.Age.ToString())
                )
            {
                var group = _diaryDbContext.Groups.FirstOrDefault(x => x.Id == student.Group.Id);

                _diaryDbContext.Students.Add(new Models.Student
                {
                    FirstName = student.FirstName,
                    LastName  = student.LastName,
                    Age       = student.Age,
                    Email     = student.Email,
                    Phone     = student.Phone,
                    Password  = CreateRandom.Password(),
                    Group     = group
                });
                _diaryDbContext.SaveChanges();

                return(RedirectToAction("Students", "Student"));
            }

            return(NotFound("Error Data"));
        }