Пример #1
0
        public async Task <RoleForListDto> AddRoleToUser(int userId, int roleId)
        {
            var userRole = await userRoleDal.GetAsync(x => x.UserId == userId && x.RoleId == roleId);

            if (userRole != null)
            {
                throw new RestException(HttpStatusCode.BadRequest, new { AlreadyExist = Messages.AlreadyExist });
            }
            var role = await roleDal.GetAsync(x => x.Id == roleId);

            if (role == null)
            {
                throw new RestException(HttpStatusCode.BadRequest, new { RolesListNotFound = Messages.RoleListNotFound });
            }

            var user = await userDal.GetAsync(x => x.Id == userId);

            if (user == null)
            {
                throw new RestException(HttpStatusCode.BadRequest, new { UserNotFound = Messages.UserNotFound });
            }

            var userrole = new UserRole
            {
                UserId = user.Id,
                RoleId = role.Id
            };

            await userRoleDal.Add(userrole);

            return(mapper.Map <Role, RoleForListDto>(role));
        }
Пример #2
0
        public async Task <UserForListDto> Update(UserForRegisterDto userForRegisterDto)
        {
            var userFromRepo = await userDal.GetAsync(x => x.Email == userForRegisterDto.Email);

            if (userFromRepo == null)
            {
                throw new RestException(HttpStatusCode.BadRequest, new { UserNotFound = Messages.UserNotFound });
            }

            var userForUpdate = mapper.Map(userForRegisterDto, userFromRepo);



            var userUpdate = await userDal.Update(userForUpdate);

            var spec = new UserWithCampusAndDepartmentAndDegreeSpecification(userUpdate.Email);
            var user = await userDal.GetEntityWithSpecAsync(spec);

            var chekcPublicRoles = await announceStatusCheck.CheckPublicRole(roleDal, roleCategoryDal);

            var userRolePublic = await userRoleDal.GetAsync(x => x.Role.Name.ToLower() == "public");

            if (user.IsActive)
            {
                if (userRolePublic == null)
                {
                    userRolePublic = new UserRole
                    {
                        UserId = user.Id,
                        RoleId = chekcPublicRoles.Id
                    };
                    await userRoleDal.Add(userRolePublic);
                }
            }
            else if (!user.IsActive)
            {
                if (userRolePublic != null)
                {
                    await userRoleDal.Delete(userRolePublic);
                }
            }
            return(mapper.Map <User, UserForListDto>(user));
        }