Exemplo n.º 1
0
        /// <summary>
        /// Method to select an Album by id.
        /// </summary>
        /// <returns>An Album entity or null if not found.</returns>
        private AlbumEntity SingleAliasOrNull(IQueryable <AlbumEntity> query, AlbumOptionsSelect op)
        {
            AlbumEntity entity = query.SingleOrDefault(x => x.Alias == op.Alias);

            // Check if user is found, return null instead of default.
            if (entity == null || entity.AlbumId == 0)
            {
                return(null);
            }

            return(entity);
        }
Exemplo n.º 2
0
        public AlbumEntity RemoveSectionDependency(int albumId, int sectionId, bool save = true)
        {
            AlbumOptionsSelect options = new AlbumOptionsSelect {
                PrimaryKey = albumId
            };

            options.Dependencies.Add(EnumEntitiesDependencies.AlbumsInSections);

            AlbumEntity album = Select(options);

            album.AlbumsInSections.Remove(album.AlbumsInSections.SingleOrDefault(x => x.SectionId == sectionId));

            return(Update(album, save));
        }
Exemplo n.º 3
0
        public AlbumEntity RemovePictureDependency(int albumId, int pictureId, bool save = true)
        {
            AlbumOptionsSelect options = new AlbumOptionsSelect {
                PrimaryKey = albumId
            };

            options.Dependencies.Add(EnumEntitiesDependencies.PicturesInAlbums);

            AlbumEntity      album      = Select(options);
            PicturesInAlbums dependency = album.PicturesInAlbums.SingleOrDefault(c => c.PictureId == pictureId);

            album.PicturesInAlbums.Remove(dependency);

            return(Update(album, save));
        }
Exemplo n.º 4
0
        public async Task <AlbumEntity> RemoveSectionDependenciesAsync(int albumId, int sectionId, bool save = true)
        {
            int result = await Connector.Database.ExecuteSqlCommandAsync(
                $"DELETE FROM AlbumsInSections WHERE AlbumId = {albumId} AND SectionId = {sectionId}"
                );

            Save(save);

            AlbumOptionsSelect options = new AlbumOptionsSelect {
                PrimaryKey = albumId
            };

            options.Dependencies.Add(EnumEntitiesDependencies.AlbumsInSections);

            return(Select(options));
        }
Exemplo n.º 5
0
        /// <summary>
        /// Method to select an Album.
        /// </summary>
        /// <param name="op"></param>
        /// <returns>An album entity.</returns>
        public AlbumEntity Select(AlbumOptionsSelect op)
        {
            log.Debug("---------------------------------------------------------------------------------");
            log.Debug($"{GetType().Name}.{MethodBase.GetCurrentMethod().Name} : {op?.GetType()}");

            if (op == null)
            {
                ArgumentNullException e = Exceptions.GetArgumentNull(nameof(op), typeof(AlbumOptionsSelect));
                log.Error($"{GetType().Name}.{MethodBase.GetCurrentMethod().Name} : {e.Output()}");
                throw e;
            }

            // Initialize entity result.
            AlbumEntity entity = null;

            // Safe User
            if (!SetSafeUser(op.UserId))
            {
                return(null);
            }

            // Initialize query.
            IQueryable <AlbumEntity> query = Connector.Albums;

            log.Debug($"{GetType().Name}.{MethodBase.GetCurrentMethod().Name} : Initializing query => {Connector?.Albums?.Count()} item(s)");

            // Load dependencies if required.
            Query_Dependencies(ref query, op);
            log.Debug($"{GetType().Name}.{MethodBase.GetCurrentMethod().Name} : Loading dependencies.");

            // Filter by Album Primary Key property.
            if (op.PrimaryKey != 0)
            {
                entity = SingleIdOrNull(query, op);
                log.Debug($"{GetType().Name}.{MethodBase.GetCurrentMethod().Name} : Filter by Album Primary Key property => {entity?.PrimaryKey}");

                if (entity == null)
                {
                    return(null);
                }
            }

            // Filter by Album Alias property.
            if (op.Alias.IsNotNullOrWhiteSpace())
            {
                entity = SingleAliasOrNull(query, op);
                log.Debug($"{GetType().Name}.{MethodBase.GetCurrentMethod().Name} : Filter by Album Alias property => {entity?.PrimaryKey} | {entity?.Alias}");

                if (entity == null)
                {
                    return(null);
                }
            }

            // Filter by Album BackgroundPictureId property.
            if (op.BackgroundPictureId > 0)
            {
                entity = SingleBackgroundPictureIdOrNull(query, op);
                log.Debug($"{GetType().Name}.{MethodBase.GetCurrentMethod().Name} : Filter by Album BackgroundPictureId property => {entity?.PrimaryKey} | {entity?.Alias}");

                if (entity == null)
                {
                    return(null);
                }
            }

            // Filter by Album PreviewPictureId property.
            if (op.PreviewPictureId > 0)
            {
                entity = SinglePreviewPictureIdOrNull(query, op);
                log.Debug($"{GetType().Name}.{MethodBase.GetCurrentMethod().Name} : Filter by Album PreviewPictureId property => {entity?.PrimaryKey} | {entity?.Alias}");

                if (entity == null)
                {
                    return(null);
                }
            }

            // Filter by Album ThumbnailPictureId property.
            if (op.ThumbnailPictureId > 0)
            {
                entity = SingleThumbnailPictureIdOrNull(query, op);
                log.Debug($"{GetType().Name}.{MethodBase.GetCurrentMethod().Name} : Filter by Album ThumbnailPictureId property => {entity?.PrimaryKey} | {entity?.Alias}");

                if (entity == null)
                {
                    return(null);
                }
            }

            // Filter by User.
            if (entity != null && User != null)
            {
                SectionEntity section = null;

                foreach (AlbumsInSections sectDep in entity.AlbumsInSections)
                {
                    IQueryable <SectionEntity> sections = Connector.Sections;
                    sections = sections.Include(x => x.SectionsInAclGroups);
                    SectionEntity s = sections.SingleOrDefault(x => x.PrimaryKey == sectDep.SectionId);

                    if (s.PrimaryKey == 0)
                    {
                        return(null);
                    }

                    foreach (SectionsInAclGroups aclgDep in s.SectionsInAclGroups)
                    {
                        section = GetUserSection(aclgDep.SectionId, User);
                        if (section != null && section.PrimaryKey > 0)
                        {
                            break;
                        }
                    }

                    if (section != null && section.PrimaryKey > 0)
                    {
                        break;
                    }
                }

                if (section == null)
                {
                    return(null);
                }
            }

            log.Debug("---------------------------------------------------------------------------------");
            return(entity);
        }