Exemplo n.º 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));
        }
Exemplo n.º 2
0
        public async Task <ActionResult> ChangeRol(AppUserRoleModel userRoleModel)
        {
            AppUser appUserr = HttpContext.Session.GetObjectFromJson <AppUser>("userId");

            try
            {
                AppUser user = null;
                switch (userRoleModel.Role)
                {
                case "Menecer":
                    if (userRoleModel.Holding != null)
                    {
                        if (userRoleModel.Company != null && userRoleModel.Emporium != 0)
                        {
                            //bele bir sexs varmi
                            user = dbContext.Users.Where(x => x.Id == userRoleModel.AppUserId).FirstOrDefault();

                            if (user == null)
                            {
                                return(RedirectToAction(nameof(AppUserList)));
                            }


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

                            if (appUser)
                            {
                                ModelState.AddModelError("", "This store already has a manager.");
                                return(RedirectToAction(nameof(AppUserList)));
                            }


                            var IsRole = await userManager.IsInRoleAsync(user, userRoleModel.Role);

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

                                await dbContext.SaveChangesAsync();
                            }
                            else
                            {
                                await userManager.AddToRoleAsync(user, userRoleModel.Role);

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

                                await dbContext.SaveChangesAsync();
                            }

                            return(RedirectToAction(nameof(AppUserList)));
                        }
                        return(RedirectToAction(nameof(AppUserList)));
                    }
                    return(RedirectToAction(nameof(AppUserList)));
                }


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

                if (!exists)
                {
                    return(RedirectToAction(nameof(AppUserList)));
                }

                //bele bir sexs varmi
                user = dbContext.Users.Where(x => x.Id == userRoleModel.AppUserId).FirstOrDefault();

                if (user == null)
                {
                    return(RedirectToAction(nameof(AppUserList)));
                }

                if (!await userManager.IsInRoleAsync(user, userRoleModel.Role))
                {
                    await userManager.AddToRoleAsync(user, userRoleModel.Role);

                    await dbContext.SaveChangesAsync();
                }
                else
                {
                    ModelState.AddModelError("", "This user already has a role");
                }
            }
            catch (Exception exp)
            {
                ModelState.AddModelError("", exp.Message);
            }

            return(RedirectToAction("ChangeRol", "Panel", new { id = userRoleModel.AppUserId }));
        }