Exemplo n.º 1
0
        public async Task<ActionResult> AddStudent(StudentViewModel s)
        {
            if (!ModelState.IsValid)
            {
                await PopulateCoursesList();
                return View(s);
            }                

            try
            {
                var student = new StudentModel()
                {
                    User = new UserModel()
                    {
                        Username = s.Username,
                        PersonalDetail = new PersonalDetailModel()
                        {
                            Email = s.Email,
                            FirstName = s.FirstName,
                            LastName = s.LastName
                        },
                        Courses = new int[] {s.CourseId}
                    }
                };

                _studentsService.SetAuthData(AuthHelper.GetTokens(User));

                if (await _studentsService.Add(student))
                {
                    TempData["studentAdded"] = "Student registration successfully created";
                    return RedirectToAction("Index");
                }
                else
                {
                    TempData["error"] = "Failed to add student";
                    return RedirectToAction("AddStudent");
                }
            }
            catch (Exception e)
            {
                Trace.TraceError(e.ToString());
                TempData["error"] = "An error occurred while adding new student";
                return RedirectToAction("AddStudent");
            }                        
        }
Exemplo n.º 2
0
 public async Task<int> AddStudent(StudentModel student)
 {
     using (var dbModel = new EduTestEntities())
     {
         var dbStud = Mapper.Map<Student>(student);
         dbStud.User.Password = "******";   // TODO add logic for generating and sharing password
         if (dbStud.User.Roles != null && dbStud.User.Roles.Any())
         {
             foreach (var role in dbStud.User.Roles)
                 dbModel.Entry(role).State = EntityState.Unchanged;
         }
         if (dbStud.User.Courses != null && dbStud.User.Courses.Any())
         {
             foreach (var course in dbStud.User.Courses)
                 dbModel.Entry(course).State = EntityState.Unchanged;
         }
         dbModel.Students.Add(dbStud);
         await dbModel.SaveChangesAsync();
         return dbStud.UserId;
     }
 }