Exemplo n.º 1
0
        public async Task <UserEditResponse> EditUser(UserEditRequest request)
        {
            var response = new UserEditResponse();

            var currentUser = _cacheManager.GetCachedCurrentUser(request.CurrentUserId);

            if (!currentUser.IsAdmin)
            {
                response.SetInvalid();
                return(response);
            }

            if (await _organizationRepository.Any(x => x.Id == currentUser.OrganizationId && !x.IsActive))
            {
                response.SetInvalid();
                response.ErrorMessages.Add("organization_not_found");
                return(response);
            }

            var entity = _cacheManager.GetCachedUser(request.UserUid);

            if (entity.OrganizationId != currentUser.OrganizationId)
            {
                response.SetInvalid();
                return(response);
            }

            var language = await _languageRepository.Select(x => x.Uid == request.LanguageUid);

            if (language.IsNotExist())
            {
                response.SetInvalid();
                response.ErrorMessages.Add("language_not_found");
                return(response);
            }

            var updatedEntity = _userFactory.CreateEntityFromRequest(request, entity, language);
            var result        = await _userRepository.Update(request.CurrentUserId, updatedEntity);

            if (result)
            {
                _cacheManager.UpsertUserCache(entity, _userFactory.MapCurrentUser(entity));

                response.Item   = _userFactory.CreateDtoFromEntity(entity);
                response.Status = ResponseStatus.Success;
                return(response);
            }

            response.SetFailed();
            return(response);
        }
Exemplo n.º 2
0
        public async Task <AllUserReadListResponse> GetAllUsers(AllUserReadListRequest request)
        {
            var response = new AllUserReadListResponse();

            var currentUser = _cacheManager.GetCachedCurrentUser(request.CurrentUserId);

            if (!currentUser.IsSuperAdmin)
            {
                response.SetInvalid();
                return(response);
            }

            Expression <Func <User, object> > orderByColumn = x => x.Id;
            Expression <Func <User, bool> >   filter        = null;

            if (request.SearchTerm.IsNotEmpty())
            {
                filter = x => x.Name.Contains(request.SearchTerm);
            }

            List <User> entities;

            if (request.PagingInfo.Skip < 1)
            {
                entities = await _userRepository.SelectAfter(filter, request.PagingInfo.LastUid, request.PagingInfo.Take, orderByColumn, request.PagingInfo.IsAscending);
            }
            else
            {
                entities = await _userRepository.SelectMany(filter, request.PagingInfo.Skip, request.PagingInfo.Take, orderByColumn, request.PagingInfo.IsAscending);
            }

            if (entities != null)
            {
                for (var i = 0; i < entities.Count; i++)
                {
                    var entity = entities[i];
                    var dto    = _userFactory.CreateDtoFromEntity(entity);
                    response.Items.Add(dto);
                }
            }

            response.PagingInfo.Skip           = request.PagingInfo.Skip;
            response.PagingInfo.Take           = request.PagingInfo.Take;
            response.PagingInfo.LastUid        = request.PagingInfo.LastUid;
            response.PagingInfo.IsAscending    = request.PagingInfo.IsAscending;
            response.PagingInfo.TotalItemCount = await _userRepository.Count(filter);

            response.Status = ResponseStatus.Success;
            return(response);
        }