Beispiel #1
0
        public async Task <ActionResult> MessageRoleList(AppUserRoleModel userRoleModel)
        {
            var appUserRole = await EmployeeChangeRole.GetAppUserAsync(dbContext);

            try
            {
                Employee employeeEmporium = null;
                AppUser  user             = null;
                switch (userRoleModel.Role)
                {
                case "Menecer":
                    if (userRoleModel.Holding != null)
                    {
                        if (userRoleModel.Company != null && userRoleModel.Emporium != 0)
                        {
                            //bele bir sexs varmi
                            employeeEmporium = await EmployeeChangeRole
                                               .HasIdEmployee(dbContext, userRoleModel.UserId);

                            if (employeeEmporium == null)
                            {
                                ModelState.AddModelError("", "No such user.");
                                return(View(appUserRole));
                            }


                            //bele bir sexhs hazirda isleyirmi
                            if (employeeEmporium.WorkPlaces != null)
                            {
                                //bu sexsh isi sonlandiribmi
                                var workEnds = await EmployeeChangeRole
                                               .IsEmployeeWorkEndDate(dbContext, employeeEmporium.Id);

                                if (!workEnds)
                                {
                                    ModelState.AddModelError("", "Hr did not specify the date of his dismissal.");
                                }
                                return(View(appUserRole));
                            }


                            //bu magazanin meneceri varmi?
                            var appUser = await EmployeeChangeRole.HasEmporiumAppUser(dbContext, userRoleModel.Emporium);

                            if (appUser)
                            {
                                ModelState.AddModelError("", "This store already has a manager.");
                                return(View(appUserRole));
                            }


                            //Appuser Create
                            user = await EmployeeChangeRole.CreateAppUserAsync(dbContext, employeeEmporium, userManager, passwordHasher);


                            if (employeeEmporium.WorkPlaces != null)
                            {
                                //Change Employee Position Table
                                var changepositionrol = EmployeeChangeRole.GetChangePositionRolAsync(dbContext, employeeEmporium.Id, user.Id);
                            }


                            //ofice employee work start date
                            OficeEmployee oficeEmployeee = new OficeEmployee()
                            {
                                StarDate  = DateTime.Now,
                                AppUserId = user.Id
                            };
                            dbContext.OficeEmployees.Add(oficeEmployeee);
                            await dbContext.SaveChangesAsync();


                            //menecer emporium id
                            EmporiumAppUserMenecer emporiumAppUser = new EmporiumAppUserMenecer()
                            {
                                AppUserId  = user.Id,
                                EmporiumId = userRoleModel.Emporium
                            };
                            await dbContext.EmporiumAppUsers.AddAsync(emporiumAppUser);

                            await dbContext.SaveChangesAsync();

                            //user  rol bagla
                            await userManager.AddToRoleAsync(user, userRoleModel.Role);


                            var currentMessagee = await dbContext
                                                  .MessageReciurments
                                                  .Where(x => x.EmployeeId == employeeEmporium.Id).FirstOrDefaultAsync();

                            var data1 = dbContext.Employees
                                        .Where(x => x.Id == employeeEmporium.Id)
                                        .FirstOrDefault();

                            dbContext.MessageReciurments.Remove(currentMessagee);
                            dbContext.Employees.Remove(data1);
                            await dbContext.SaveChangesAsync();

                            ModelState.AddModelError("", "Success");
                            return(View(appUserRole));
                        }
                        ModelState.AddModelError("", "Company or Emporium is empty");
                        return(View(appUserRole));
                    }
                    ModelState.AddModelError("", "Holding is empty");
                    return(View(appUserRole));
                }



                bool exists = await roleManager.RoleExistsAsync(userRoleModel.Role);

                if (!exists)
                {
                    ModelState.AddModelError("", "There is no such role.");
                    return(View(appUserRole));
                }


                //bele bir sexs varmi
                employeeEmporium = await EmployeeChangeRole
                                   .HasIdEmployee(dbContext, userRoleModel.UserId);

                if (employeeEmporium == null)
                {
                    ModelState.AddModelError("", "No such User.");
                    return(View(appUserRole));
                }


                //bele bir sexhs hazirda isleyirmi
                if (employeeEmporium.WorkPlaces != null)
                {
                    //bu sexsh isi sonlandiribmi
                    var workEnds = await EmployeeChangeRole
                                   .IsEmployeeWorkEndDate(dbContext, employeeEmporium.Id);

                    if (!workEnds)
                    {
                        ModelState.AddModelError("", "Hr did not specify the date of his dismissal.");
                    }
                    return(View(appUserRole));
                }


                //Appuser Create
                user = await EmployeeChangeRole.CreateAppUserAsync(dbContext, employeeEmporium, userManager, passwordHasher);


                if (employeeEmporium.WorkPlaces != null)
                {
                    //Change Employee Position Table
                    var changepositionrol = EmployeeChangeRole.GetChangePositionRolAsync(dbContext, employeeEmporium.Id, user.Id);
                }


                OficeEmployee oficeEmployee = new OficeEmployee()
                {
                    StarDate  = DateTime.Now,
                    AppUserId = user.Id
                };
                dbContext.OficeEmployees.Add(oficeEmployee);
                await dbContext.SaveChangesAsync();

                //user add to role
                await userManager.AddToRoleAsync(user, userRoleModel.Role);


                var currentMessage = await dbContext.MessageReciurments.Where(x => x.EmployeeId == employeeEmporium.Id).FirstOrDefaultAsync();

                dbContext.MessageReciurments.Remove(currentMessage);
                var data = dbContext.Employees
                           .Where(x => x.Id == employeeEmporium.Id)
                           .FirstOrDefault();
                dbContext.Employees.Remove(data);
                await dbContext.SaveChangesAsync();
            }
            catch (Exception exp)
            {
                ModelState.AddModelError("", exp.Message);
            }
            return(View(appUserRole));
        }
Beispiel #2
0
        public async Task <ActionResult> MessageRoleList()
        {
            var userRoleModel = await EmployeeChangeRole.GetAppUserAsync(dbContext);

            return(View(userRoleModel));
        }