public async Task <ProfileProperty> AddProfileProperty(ProfileProperty profileProperty)
        {
            ValidateProfileProperty(profileProperty);

            var existingProperty = await GetProfileProperty(profileProperty.Name, true);

            if (existingProperty != null)
            {
                throw new CallerException("A ProfileProperty already exists with that name");
            }

            ProfileProperty createdProfileProperty;

            using (var uow = new UnitOfWork(Context))
            {
                var repo = new ProfilePropertyRepository(uow);

                createdProfileProperty = await repo.Create(profileProperty);
            }

            await new ProfilePropertyCache(Cache).InvalidateProfilePropertiesCache();
            await new UserProfilePropertyCache(Cache).InvalidateUserProfilePropertiesCache();

            return(createdProfileProperty);
        }
        public async Task <List <ProfileProperty> > GetProfileProperties(bool requiredOnly, bool isAdmin)
        {
            ProfilePropertyCache profilePropertyCache = new ProfilePropertyCache(Cache);

            List <ProfileProperty> profileProperties = await profilePropertyCache.GetProfilePropertiesFromCache(requiredOnly, isAdmin);

            if (profileProperties != null)
            {
                return(profileProperties);
            }

            using (UnitOfWork uow = new UnitOfWork(Context))
            {
                ProfilePropertyRepository repo = new ProfilePropertyRepository(uow);

                IQueryable <ProfileProperty> query = repo.GetAll();

                if (!isAdmin)
                {
                    query = query.Where(c => c.Visibility != ProfilePropertyVisibility.Admin);
                }

                if (requiredOnly)
                {
                    query = query.Where(c => c.Required);
                }

                profileProperties = await query.ToListAsync();
            }

            await profilePropertyCache.AddProfilePropertiesToCache(profileProperties, requiredOnly, isAdmin);

            return(profileProperties);
        }
        public async Task <ProfileProperty> GetProfileProperty(string name, bool isAdmin)
        {
            using (var uow = new UnitOfWork(Context))
            {
                var repo = new ProfilePropertyRepository(uow);

                var query = repo.GetAll().Where(c => c.Name.ToUpper() == name.ToUpper());

                if (!isAdmin)
                {
                    query = query.Where(c => c.Visibility != ProfilePropertyVisibility.Admin);
                }

                return(await query.FirstOrDefaultAsync());
            }
        }
        public async Task <Page <ProfileProperty> > GetGrid(GridRequest gridRequest)
        {
            var data = new Page <ProfileProperty>();

            using (var uow = new UnitOfWork(Context))
            {
                var repo = new ProfilePropertyRepository(uow);

                var query = repo.GetAll();

                var dataGridLogic = new DataGridLogic <ProfileProperty>(gridRequest, query);

                data.Records = await dataGridLogic.GetResults();

                data.PageSize         = dataGridLogic.PageSize;
                data.PageOffset       = dataGridLogic.PageOffset;
                data.TotalRecordCount = dataGridLogic.TotalRecordCount;
                data.SortExpression   = dataGridLogic.SortExpression;
            }

            return(data);
        }