public async Task <ActionResponse> AddStudent(int batchId, string roll, string name, string email, string phone)
        {
            var batch = await _db.Batches.FirstOrDefaultAsync(x => x.Id == batchId);

            if (batch == null)
            {
                return(new ActionResponse(false, "Invalid Batch"));
            }

            var user = await _db.Users.FirstOrDefaultAsync(x => x.UserName == roll);

            if (user != null)
            {
                return(new ActionResponse(false, "Roll already exists"));
            }
            else
            {
                var allCourses = await _db.Courses.Where(x => x.Semester.Batch.Id == batchId)
                                 .ToListAsync();

                int.TryParse(roll, out int studentRoll);
                var student = new DBUser()
                {
                    Batch                = batch,
                    Email                = email,
                    EnrolledIn           = CurrentTime,
                    Name                 = name,
                    UserName             = roll,
                    Roll                 = studentRoll,
                    Role                 = AppConstants.Student,
                    PhoneNumber          = FixPhone(phone),
                    PhoneNumberConfirmed = false
                };
                var idUser = IdentityDBUser.Create(student);
                var res    = await _usermanager.CreateAsync(idUser);

                if (res.Succeeded)
                {
                    var mRes = await _usermanager.AddToRoleAsync(idUser, AppConstants.Student);

                    if (mRes.Succeeded)
                    {
                        foreach (var course in allCourses)
                        {
                            _db.StudentCourses.Add(new StudentCourse()
                            {
                                Course  = course,
                                Student = student
                            });
                        }
                        await _db.SaveChangesAsync();

                        var msg = EmailMessageCreator.CreateInvitation(phone, roll);
                        _emailSender.SendEmailAsync(student.Email, "Registration", msg);
                        return(new ActionResponse(true));
                    }
                }
                return(new ActionResponse(false, "Failed to create User"));
            }
        }
        internal async Task <List <DBUser> > AddStudents(IList <DBUser> students, int batchID)
        {
            var users = new List <DBUser>();
            var batch = await _db.Batches.FindAsync(batchID);

            var allCourses = await _db.Courses.Where(x => x.Semester.Batch.Id == batchID)
                             .ToListAsync();

            foreach (var student in students)
            {
                var oldUser = await _db.Users.FirstOrDefaultAsync(x => x.UserName == student.UserName);

                if (oldUser != null)
                {
                    continue;
                }

                student.Batch                = batch;
                student.PhoneNumber          = FixPhone(student.PhoneNumber);
                student.UserName             = student.Roll.ToString();
                student.PhoneNumberConfirmed = false;
                student.Role = AppConstants.Student;

                var idbUser = IdentityDBUser.Create(student);
                var res     = await _usermanager.CreateAsync(idbUser);

                if (res.Succeeded)
                {
                    await _usermanager.AddToRoleAsync(idbUser, AppConstants.Student);

                    foreach (var course in allCourses)
                    {
                        _db.StudentCourses.Add(new StudentCourse()
                        {
                            Course  = course,
                            Student = student
                        });
                    }
                }
                var msg = EmailMessageCreator.CreateInvitation(student.PhoneNumber, student.UserName);
                _emailSender.SendEmailAsync(student.Email, "Registration", msg);
            }
            await _db.SaveChangesAsync();

            return(users);
        }
Esempio n. 3
0
        public async Task StartAsync(CancellationToken cancellationToken)
        {
            using var scope = _serviceProvider.CreateScope();
            // Get the DbContext instance
            var db = scope.ServiceProvider.GetRequiredService <StudentDBContext>();
            await db.Database.MigrateAsync();

            var _roleManager = scope.ServiceProvider.GetService <RoleManager <IdentityRole> >();
            var _userManager = scope.ServiceProvider.GetRequiredService <UserManager <IdentityDBUser> >();
            var adminRole    = await _roleManager.FindByNameAsync(AppConstants.Admin);

            if (adminRole == null)
            {
                adminRole = new IdentityRole(AppConstants.Admin);
                //create the roles and seed them to the database
                await _roleManager.CreateAsync(adminRole);

                await _roleManager.CreateAsync(new IdentityRole(AppConstants.Student));
            }
            //Assign Admin role to the main User here we have given our newly registered
            //login id for Admin management
            var user = await _userManager.FindByNameAsync("admin");

            if (user == null)
            {
                var dbUser = new DBUser()
                {
                    UserName    = "******",
                    Email       = "*****@*****.**",
                    Role        = AppConstants.Admin,
                    PhoneNumber = "017000000"
                };
                user = IdentityDBUser.Create(dbUser);
                await _userManager.CreateAsync(user, "pass_WORD_1234");

                await _userManager.AddToRoleAsync(user, AppConstants.Admin);


                var _feedService = scope.ServiceProvider.GetRequiredService <FeedDataService>();
#if DEBUG
                await _feedService.StartAsync(dbUser.Id);
#endif
            }
        }