public static void SeedSchoolAcademy(DataContext context) { if (!context.SchoolAcademy.Any()) { //var fileData = System.IO.File.ReadAllText("Data/UserSeedData.json"); //var schoolAcademies = JsonConvert.DeserializeObject<List<SchoolAcademy>>(fileData); var schoolAcademies = context.SchoolAcademy.FirstOrDefault(); if (schoolAcademies == null) { var schoolAcademy = new SchoolAcademy { Name = "School-01", //"LGS"; PrimaryContactPerson = "FabIntel", // "Qasim Rafi"; SecondaryContactPerson = "FabIntel2", // "Ahsan Meraj"; PrimaryphoneNumber = "0000-0000000", //"03217575840"; SecondaryphoneNumber = "0000-0000000", // "03003207433"; Email = "*****@*****.**", // "*****@*****.**"; PrimaryAddress = "Garden Town", // "Allama Iqbal Town"; SecondaryAddress = "Lahore", // "Garden Tonw"; Active = true }; context.SchoolAcademy.Add(schoolAcademy); context.SaveChanges(); int schoolAcademyId = schoolAcademy.Id; if (schoolAcademyId > 0) { var schoolBranch = new SchoolBranch { BranchName = "Online Academy", SchoolAcademyID = schoolAcademyId, CreatedDateTime = DateTime.UtcNow, Active = true, RegistrationNumber = "20000001" }; context.SchoolBranch.Add(schoolBranch); context.SaveChanges(); schoolBranchId = schoolBranch.Id; } } } }
//private bool verifyPasswordHash(string password, byte[] passwordHash, byte[] passwordSalt) //{ // using (HMACSHA512 hmac = new HMACSHA512(passwordSalt)) // { // var computedHash = hmac.ComputeHash(System.Text.Encoding.UTF8.GetBytes(password)); // for (int i=0;i<computedHash.Length; i++) // { // if (computedHash[i] != passwordHash[i]) // return false; // } // return true; // } //} public async Task <ServiceResponse <object> > Register(UserForRegisterDto model, string regNo) { SchoolBranch branch = null; if (model.UserTypeSignUp != null && model.UserTypeSignUp.ToLower() == "school") { if (!string.IsNullOrEmpty(model.SchoolName)) { var SchoolExist = _context.SchoolAcademy.Where(m => m.Name.ToLower() == model.SchoolName.ToLower()).FirstOrDefault(); if (SchoolExist != null) { _serviceResponse.Success = false; _serviceResponse.Message = CustomMessage.RecordAlreadyExist; return(_serviceResponse); } } var schools = _context.SchoolAcademy.OrderByDescending(m => m.Id).ToList(); var schoolAcademy = new SchoolAcademy { Name = string.IsNullOrEmpty(model.SchoolName) ? "School-" + (schools.Count() + 1) : model.SchoolName, PrimaryContactPerson = model.Username, SecondaryContactPerson = model.Username, PrimaryphoneNumber = "---", SecondaryphoneNumber = "---", Email = model.Email, PrimaryAddress = "---", SecondaryAddress = "---", Active = true }; if (model.files != null && model.files.Count() > 0) { for (int i = 0; i < model.files.Count(); i++) { var dbPath = _fileRepo.SaveFile(model.files[i]); if (string.IsNullOrEmpty(schoolAcademy.Logo)) { schoolAcademy.Logo += dbPath; } else { schoolAcademy.Logo = schoolAcademy.Logo + "||" + dbPath; } } } _context.SchoolAcademy.Add(schoolAcademy); _context.SaveChanges(); int schoolAcademyId = schoolAcademy.Id; if (schoolAcademyId > 0) { var branches = _context.SchoolBranch.OrderByDescending(m => m.Id).ToList(); var schoolBranhes = new SchoolBranch { BranchName = string.IsNullOrEmpty(model.SchoolName) ? "Branch-" + (branches.Count() + 1) : model.SchoolName, SchoolAcademyID = schoolAcademyId, CreatedDateTime = DateTime.UtcNow, Active = true, RegistrationNumber = branches.Count() == 1 ? "10000000" : (Convert.ToInt32(branches.FirstOrDefault().RegistrationNumber) + 1).ToString(), ExamType = model.ExamType, }; _context.AddRange(schoolBranhes); _context.SaveChanges(); branch = await _context.SchoolBranch.OrderByDescending(m => m.Id).FirstOrDefaultAsync(); } var userToCreate = new User { Username = model.Username, FullName = model.FullName, UserTypeId = (int)Enumm.UserType.Admin, Email = model.Email, ContactNumber = model.ContactNumber, SchoolBranchId = branch.Id, Gender = "male", Active = true, CreatedDateTime = DateTime.UtcNow, Role = _context.UserTypes.Where(m => m.Id == (int)Enumm.UserType.Admin).FirstOrDefault()?.Name }; byte[] passwordHash, passwordSalt; Seed.CreatePasswordHash(model.Password, out passwordHash, out passwordSalt); userToCreate.PasswordHash = passwordHash; userToCreate.PasswordSalt = passwordSalt; await _context.Users.AddAsync(userToCreate); await _context.SaveChangesAsync(); } else if (model.UserTypeSignUp != null && model.UserTypeSignUp.ToLower() == "tutor") { branch = await _context.SchoolBranch.Where(m => m.BranchName == "ONLINE ACADEMY").FirstOrDefaultAsync(); var userToCreate = new User { Username = model.Username, FullName = model.Username, UserTypeId = (int)Enumm.UserType.Tutor, Email = model.Email, ContactNumber = model.ContactNumber, SchoolBranchId = branch.Id, Gender = model.Gender, Active = true, CreatedDateTime = DateTime.UtcNow, Role = _context.UserTypes.Where(m => m.Id == (int)Enumm.UserType.Tutor).FirstOrDefault()?.Name }; byte[] passwordHash, passwordSalt; Seed.CreatePasswordHash(model.Password, out passwordHash, out passwordSalt); userToCreate.PasswordHash = passwordHash; userToCreate.PasswordSalt = passwordSalt; await _context.Users.AddAsync(userToCreate); await _context.SaveChangesAsync(); } else if (model.UserTypeSignUp != null && model.UserTypeSignUp.ToLower() == "student") { branch = await _context.SchoolBranch.Where(m => m.BranchName == "ONLINE ACADEMY").FirstOrDefaultAsync(); var userToCreate = new User { Username = model.Username, FullName = model.Username, UserTypeId = (int)Enumm.UserType.OnlineStudent, Email = model.Email, ContactNumber = model.ContactNumber, SchoolBranchId = branch.Id, Gender = model.Gender, Active = true, CreatedDateTime = DateTime.UtcNow, Role = _context.UserTypes.Where(m => m.Id == (int)Enumm.UserType.OnlineStudent).FirstOrDefault()?.Name }; byte[] passwordHash, passwordSalt; Seed.CreatePasswordHash(model.Password, out passwordHash, out passwordSalt); userToCreate.PasswordHash = passwordHash; userToCreate.PasswordSalt = passwordSalt; await _context.Users.AddAsync(userToCreate); await _context.SaveChangesAsync(); } //var UserTypes = _context.UserTypes.ToList(); _serviceResponse.Success = true; _serviceResponse.Message = CustomMessage.Added; return(_serviceResponse); }