public NewDB.Departments AddNewDepartment(NewDB.Departments department) { if (department.CompanyId == 0) { return(null); } db.Departments.Add(department); //binding company courses to the dep var company = db.Companies.Where(c => c.Id == department.CompanyId).FirstOrDefault(); if (company != null) { int order = 1; foreach (var course in company.CompanyCourses) { db.DepartmentCourses.Add(new DepartmentCourses() { CourseId = course.CourseId, CourseStatus = course.CourseStatus, DepartmentId = department.Id, EndDate = course.EndDate, StartDate = course.StartDate, OrderNum = order++ }); } } db.SaveChanges(); db.Entry(department).Reload(); return(department); }
public static void AddDepartment(NewDB.Departments department) { companyRepo.AddNewDepartment(department); }
public static Companies AddCompany(HttpServerUtilityBase Server, HttpFileCollectionBase httpFileCollectionBase, EditCompanyModel model) { var UserManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(new ApplicationDbContext())); UserManager.UserValidator = new UserValidator <ApplicationUser>(UserManager) { AllowOnlyAlphanumericUserNames = false }; Companies tmp = model.Companies; CustomizeCompany customize = new CustomizeCompany { StartVideo = model.startVideo ?? "", LogoUrl = model.logoUrl ?? "", LoginLogoUrl = model.loginLogoUrl ?? "", MenuActiveColor = model.menuActiveColor ?? "", MenuPassiveColor = model.menuPassiveColor ?? "", HeaderColor = model.headerColor ?? "", ButtonsBackgroundColor = model.buttonsBackgroundColor ?? "", ButtonsActiveColor = model.buttonsActiveColor ?? "" }; CompanyHelper.SaveFiles(Server, httpFileCollectionBase, model.companyName, customize); string pswd = CommonHelper.UserKey(); tmp.CustomizeCompany = JsonConvert.SerializeObject(customize); tmp.TempPassword = pswd; tmp.Speech = model.speech; tmp.Lang = model.lang; tmp.CompanyStatus = (int)Status.NewlyCreated; tmp.Created = DateTime.Now; companyRepo.AddCompany(tmp); var department = new NewDB.Departments(); department.CompanyId = tmp.Id; department.DepartmentName = CompanyHelper.InnitialDepartment; department = companyRepo.AddNewDepartment(department); //var userName = tmp.Name.Replace(" ", "_"); var userName = tmp.ContactName + "_" + tmp.ContactSurname; var user = new ApplicationUser() { UserName = userName }; var result = UserManager.Create(user, pswd); if (result.Succeeded) { UserManager.AddToRole(user.Id, "User"); UserManager.AddToRole(user.Id, "Manager"); UserManager.AddToRole(user.Id, "Administrator"); var userProfile = new UserProfiles { CompanyId = tmp.Id, Email = tmp.Email, Firstname = tmp.ContactName, Lastname = tmp.ContactSurname, Phone = tmp.ContactPhone, AspNetUserId = user.Id, DepartmentId = department.Id, UserKey = CommonHelper.UserKey(), Lang = 3, Speech = 3 }; userProfile = UserService.AddUser(userProfile); //do not remove. It's magic spell, that makes entity work correctly userProfile.AspNetUsers = new AspNetUsers { UserName = user.UserName, Id = user.Id }; tmp.AdminId = userProfile.Id; } return(tmp); }