コード例 #1
0
        public async Task <LearnerDto> CreateLearner(RegisterLearnerDto registerLearnerDto)
        {
            AppUser user = new AppUser();

            if (registerLearnerDto.UserId == 0) // no user yet
            {
                var orgId = registerLearnerDto.OrganisationId == 0 ?
                            registerLearnerDto.Organisation.Id :
                            registerLearnerDto.OrganisationId;
                if (orgId == 0)
                {
                    return(null);
                }

                user = await CreateUser(new RegisterDto {
                    Username       = registerLearnerDto.Username,
                    Password       = registerLearnerDto.Password,
                    OrganisationId = orgId,
                    UserTypeId     = (await _dataContext.UserTypes.AsQueryable().SingleAsync(ut => ut.Name == "Learner")).Id,
                    Firstname      = registerLearnerDto.Firstname,
                    Lastname       = registerLearnerDto.Lastname
                });
            }

            if (user == null && registerLearnerDto.UserId == 0)
            {
                return(null);
            }
            var learner = new Learner
            {
                UserId = user.Id == 0 ? registerLearnerDto.UserId : user.Id
            };

            _dataContext.Learners.Add(learner);
            await _dataContext.SaveChangesAsync();

            LearnerGroup learnerGroup = new LearnerGroup();

            if (registerLearnerDto.Group != null)
            {
                learnerGroup.LearnerId = learner.LearnerId;
                learnerGroup.GroupId   = registerLearnerDto.Group.Id;
                _dataContext.LearnerGroup.Add(learnerGroup);
                await _dataContext.SaveChangesAsync();
            }

            if (registerLearnerDto.Courses != null && registerLearnerDto.Courses.Count > 0)
            {
                foreach (var course in registerLearnerDto.Courses)
                {
                    _dataContext.LearnerCourses.Add(new LearnerCourse {
                        LearnerId = learner.LearnerId,
                        CourseId  = course.Id
                                    // CourseId = course
                    });
                }
                await _dataContext.SaveChangesAsync();
            }
            return(new LearnerDto {
                UserId = registerLearnerDto.UserId,
                LearnerId = learner.LearnerId,
                OrganisationId = user.OrganisationId,
                Username = user.Username,
                Firstname = user.Firstname,
                Lastname = user.Lastname,
                Group = learnerGroup.Group == null ? null : new GroupDto {
                    Id = learnerGroup.Group.Id,
                    Name = learnerGroup.Group.Name
                },
                Courses = registerLearnerDto.Courses
            });
        }
コード例 #2
0
        public static void Initialize(DataContext context)
        {
            if (context.UserTypes.Any())
            {
                return;
            }

            var userTypes = new UserType[]
            {
                new UserType {
                    Name = "Admin"
                },
                new UserType {
                    Name = "Tutor"
                },
                new UserType {
                    Name = "Learner"
                },
                new UserType {
                    Name = "Other"
                }
            };

            foreach (var userType in userTypes)
            {
                context.UserTypes.Add(userType);
            }

            context.SaveChanges();

            var orgs = new Organisation[]
            {
                new Organisation {
                    Name = "WEST"
                },
                new Organisation {
                    Name = "Tribal"
                },
                new Organisation {
                    Name = "Others"
                }
            };

            foreach (var org in orgs)
            {
                context.Organisations.Add(org);
            }
            context.SaveChanges();

            using var hmac = new HMACSHA512(); //will provide hashing algorithm
            var users = new AppUser[]
            {
                new AppUser
                {
                    Username       = "******",
                    Firstname      = "Admin0 FN",
                    Lastname       = "Admin0 LN",
                    OrganisationId = 1,
                    Birthdate      = DateTime.Parse("1990-01-01"),
                    PasswordHash   = hmac.ComputeHash(Encoding.UTF8.GetBytes("password")),
                    PasswordSalt   = hmac.Key,
                    TypeId         = 1
                },
                new AppUser
                {
                    Username       = "******",
                    Firstname      = "Tutor0 FN",
                    Lastname       = "Tutor0 LN",
                    OrganisationId = 2,
                    Birthdate      = DateTime.Parse("1991-01-01"),
                    PasswordHash   = hmac.ComputeHash(Encoding.UTF8.GetBytes("password")),
                    PasswordSalt   = hmac.Key,
                    TypeId         = 2
                },
                new AppUser
                {
                    Username       = "******",
                    Firstname      = "Learner0 FN",
                    Lastname       = "Learner0 LN",
                    OrganisationId = 2,
                    Birthdate      = DateTime.Parse("2005-01-01"),
                    PasswordHash   = hmac.ComputeHash(Encoding.UTF8.GetBytes("password")),
                    PasswordSalt   = hmac.Key,
                    TypeId         = 3
                }
            };

            foreach (var user in users)
            {
                context.Users.Add(user);
            }
            context.SaveChanges();

            var learnerUsers = users.Where(u => u.TypeId == 3);
            var learners     = new List <Learner>();

            foreach (var learnerUser in learnerUsers)
            {
                var learner = new Learner {
                    UserId = learnerUser.Id
                };
                learners.Add(learner);
                context.Learners.Add(learner);
            }
            context.SaveChanges();

            var courses = new Course[] {
                new Course {
                    Name = "Application of Number 2015", IconPath = "./assets/esm.png"
                },
                new Course {
                    Name = "Communication (English) 2015", IconPath = "./assets/esl.png"
                },
                new Course {
                    Name = "Communication (Welsh) 2015", IconPath = "./assets/esw.png"
                },
                new Course {
                    Name = "Digital Literacy", IconPath = "./assets/esd.png"
                },
                new Course {
                    Name = "ESOL", IconPath = "./assets/esol.png"
                }
            };

            foreach (var course in courses)
            {
                context.Courses.Add(course);
            }
            context.SaveChanges();

            var groups = new Group[] {
                new Group {
                    Name = "default"
                }
            };

            foreach (var group in groups)
            {
                context.Groups.Add(group);
            }
            context.SaveChanges();

            var learnerGroups = new LearnerGroup[]
            {
                new LearnerGroup {
                    LearnerId = learners.Single(l => l.User.Username == "learner0").LearnerId,
                    GroupId   = groups.Single(g => g.Name == "default").Id
                }
            };

            foreach (var learnerGroup in learnerGroups)
            {
                context.LearnerGroup.Add(learnerGroup);
            }
            context.SaveChanges();

            var learnerCourses = new LearnerCourse[] {
                new LearnerCourse {
                    LearnerId = learners.Single(l => l.User.Username == "learner0").LearnerId,
                    CourseId  = courses.Single(c => c.Name == "Application of Number 2015").Id
                },
                new LearnerCourse {
                    LearnerId = learners.Single(l => l.User.Username == "learner0").LearnerId,
                    CourseId  = courses.Single(c => c.Name == "ESOL").Id
                }
            };

            foreach (var learnerCourse in learnerCourses)
            {
                context.LearnerCourses.Add(learnerCourse);
            }
            context.SaveChanges();
        }