public UnitOfWork(AssetContext context)
 {
     _context  = context;
     Assets    = new AssetRepository(_context);
     Countries = new CountryRepository(_context);
     MimeTypes = new MimeTypeRepository(_context);
 }
        /// <summary>
        /// Updates the data store records with the values of this instance.
        /// </summary>
        private void SaveExisting()
        {
            if (MimeTypeId == int.MinValue)
            {
                throw new InvalidOperationException("Cannot call SaveExisting for new MIME types.");
            }

            using (var repo = new MimeTypeRepository())
            {
                var mtDto = repo.Find(MimeTypeId);
                if (mtDto != null && mtDto.MimeTypeValue != FullType)
                {
                    mtDto.MimeTypeValue = FullType;
                    repo.Save();
                }
            }

            using (var repo = new MimeTypeGalleryRepository())
            {
                var mtDto = repo.Find(MimeTypeGalleryId);
                if (mtDto != null && mtDto.IsEnabled != AllowAddToGallery)
                {
                    mtDto.IsEnabled = AllowAddToGallery;
                    repo.Save();
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// Verify there is a MIME type/gallery mapping for the current gallery for every MIME type, creating any
        /// if necessary.
        /// </summary>
        /// <returns><c>true</c> if data was changed that necessitates reloading data from the data store, <c>false</c> otherwise.</returns>
        private bool ConfigureMimeTypeGalleryTable()
        {
            var defaultEnabledExtensions = new List <string> {
                ".jpg", ".jpeg"
            };
            var needToClearCache = false;

            using (var repoMt = new MimeTypeRepository())
            {
                using (var repoMtg = new MimeTypeGalleryRepository())
                {
                    // Get MIME types that don't have a match in the MIME Type Gallery table
                    foreach (var mtDto in repoMt.Where(mt => mt.MimeTypeGalleries.All(mtg => mtg.FKGalleryId != GalleryId)))
                    {
                        repoMtg.Add(new MimeTypeGalleryDto()
                        {
                            FKGalleryId  = GalleryId,
                            FKMimeTypeId = mtDto.MimeTypeId,
                            IsEnabled    = defaultEnabledExtensions.Contains(mtDto.FileExtension)
                        });

                        needToClearCache = true;
                    }

                    repoMtg.Save();
                }
            }

            return(needToClearCache);
        }
        /// <summary>
        /// Create a new MimeType and MimeTypeGallery record for this instance.
        /// </summary>
        /// <exception cref="InvalidOperationException">Thrown when <see cref="MimeTypeId" /> is any value other than <see cref="Int32.MinValue" /></exception>
        private void SaveNew()
        {
            if (MimeTypeId != int.MinValue)
            {
                throw new InvalidOperationException("Cannot call SaveNew when the MIME type already exists in the data store.");
            }

            int mimeTypeId;

            using (var repo = new MimeTypeRepository())
            {
                var mimeTypeDto = new MimeTypeDto()
                {
                    FileExtension = Extension, MimeTypeValue = FullType, BrowserMimeTypeValue = BrowserMimeType
                };
                repo.Add(mimeTypeDto);
                repo.Save();

                mimeTypeId = mimeTypeDto.MimeTypeId;
            }

            using (var repo = new MimeTypeGalleryRepository())
            {
                repo.Add(new MimeTypeGalleryDto()
                {
                    FKGalleryId = GalleryId, FKMimeTypeId = mimeTypeId, IsEnabled = AllowAddToGallery
                });
                repo.Save();
            }

            // Clear the gallery cache. This will eventually trigger Gallery.Configure(), which will ensure that all galleries have a MimeTypeGallery record for
            // this MIME type.
            Factory.ClearGalleryCache();
        }
        /// <summary>
        /// Permanently delete this MIME type from the data store.
        /// </summary>
        public void Delete()
        {
            if (MimeTypeId == int.MinValue)
            {
                return;
            }

            using (var repo = new MimeTypeRepository())
            {
                var mtDto = repo.Find(MimeTypeId);
                if (mtDto != null)
                {
                    repo.Delete(mtDto);
                    repo.Save();
                }
            }
        }
        /// <summary>
        /// Loads the set of MIME types from the data store. These MIME types are the master list of MIME types and are not
        /// specific to a particular gallery. That is, the <see cref="IMimeType.GalleryId" /> property is set to <see cref="Int32.MinValue" />
        /// and the <see cref="IMimeType.AllowAddToGallery" /> property is <c>false</c> for all items.
        /// </summary>
        /// <returns>Returns a <see cref="IMimeTypeCollection" /> containing MIME types..</returns>
        /// <exception cref="BusinessException">Thrown when no records were found in the master list of MIME types in the data store.</exception>
        private static IMimeTypeCollection LoadMimeTypesFromDataStore()
        {
            IMimeTypeCollection baseMimeTypes = new MimeTypeCollection();

            using (var repo = new MimeTypeRepository())
            {
                foreach (var mimeTypeDto in repo.GetAll().OrderBy(m => m.FileExtension))
                {
                    baseMimeTypes.Add(new MimeType(mimeTypeDto.MimeTypeId, Int32.MinValue, Int32.MinValue, mimeTypeDto.FileExtension.Trim(), mimeTypeDto.MimeTypeValue.Trim(), mimeTypeDto.BrowserMimeTypeValue.Trim(), false));
                }
            }

            if (baseMimeTypes.Count == 0)
            {
                throw new BusinessException("No records were found in the master list of MIME types in the data store.");
            }

            return(baseMimeTypes);
        }
Beispiel #7
0
        /// <summary>
        /// Loads the set of MIME types from the data store. These MIME types are the master list of MIME types and are not
        /// specific to a particular gallery. That is, the <see cref="IMimeType.GalleryId" /> property is set to <see cref="Int32.MinValue" />
        /// and the <see cref="IMimeType.AllowAddToGallery" /> property is <c>false</c> for all items.
        /// </summary>
        /// <returns>Returns a <see cref="IMimeTypeCollection" /> containing MIME types..</returns>
        /// <exception cref="BusinessException">Thrown when no records were found in the master list of MIME types in the data store.</exception>
        private static IMimeTypeCollection LoadMimeTypesFromDataStore()
        {
            IMimeTypeCollection baseMimeTypes = new MimeTypeCollection();

            using (var repo = new MimeTypeRepository())
            {
                foreach (var mimeTypeDto in repo.GetAll().OrderBy(m => m.FileExtension))
                {
                    baseMimeTypes.Add(new MimeType(mimeTypeDto.MimeTypeId, Int32.MinValue, Int32.MinValue, mimeTypeDto.FileExtension.Trim(), mimeTypeDto.MimeTypeValue.Trim(), mimeTypeDto.BrowserMimeTypeValue.Trim(), false));
                }
            }

            if (baseMimeTypes.Count == 0)
            {
                throw new BusinessException("No records were found in the master list of MIME types in the data store.");
            }

            return baseMimeTypes;
        }
Beispiel #8
0
        /// <summary>
        /// Verify there is a MIME type/gallery mapping for the current gallery for every MIME type, creating any
        /// if necessary.
        /// </summary>
        private void ConfigureMimeTypeGalleryTable()
        {
            var defaultEnabledExtensions = new List<string> { ".jpg", ".jpeg" };

            using (var repoMt = new MimeTypeRepository())
            {
                using (var repoMtg = new MimeTypeGalleryRepository())
                {
                    // Get MIME types that don't have a match in the MIME Type Gallery table
                    foreach (var mtDto in repoMt.Where(mt => mt.MimeTypeGalleries.All(mtg => mtg.FKGalleryId != GalleryId)))
                    {
                        repoMtg.Add(new MimeTypeGalleryDto()
                                                {
                                                    FKGalleryId = GalleryId,
                                                    FKMimeTypeId = mtDto.MimeTypeId,
                                                    IsEnabled = defaultEnabledExtensions.Contains(mtDto.FileExtension)
                                                });
                    }

                    repoMtg.Save();
                }
            }
        }