Ejemplo n.º 1
0
        /// <summary>
        /// Get blank Collection to add
        /// </summary>
        /// <param name="roadieUser"></param>
        /// <returns></returns>
        public OperationResult <Collection> Add(User roadieUser)
        {
            var sw = Stopwatch.StartNew();

            sw.Start();

            var id         = Guid.Empty;
            var collection = new data.Collection
            {
                Status = Statuses.New
            };
            var result = collection.Adapt <Collection>();

            result.Id              = id;
            result.Thumbnail       = this.MakeNewImage("collection");
            result.MediumThumbnail = this.MakeNewImage("collection");
            result.Maintainer      = new Library.Models.DataToken
            {
                Value = roadieUser.UserId.ToString(),
                Text  = roadieUser.UserName
            };
            sw.Stop();

            return(new OperationResult <Collection>()
            {
                Data = result,
                IsSuccess = true,
                OperationTime = sw.ElapsedMilliseconds
            });
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Updates (or Adds) Collection
        /// </summary>
        public async Task <OperationResult <bool> > UpdateCollection(User user, Collection model)
        {
            var isNew = model.Id == Guid.Empty;
            var now   = DateTime.UtcNow;

            var sw = new Stopwatch();

            sw.Start();
            var errors = new List <Exception>();

            data.Collection collection = new data.Collection();

            if (!isNew)
            {
                collection = this.DbContext.Collections.FirstOrDefault(x => x.RoadieId == model.Id);
                if (collection == null)
                {
                    return(new OperationResult <bool>(true, string.Format("Collection Not Found [{0}]", model.Id)));
                }
            }
            collection.IsLocked        = model.IsLocked;
            collection.Name            = model.Name;
            collection.SortName        = model.SortName;
            collection.Edition         = model.Edition;
            collection.ListInCSVFormat = model.ListInCSVFormat;
            collection.ListInCSV       = model.ListInCSV;
            collection.Description     = model.Description;
            collection.URLs            = model.URLs;
            collection.Status          = SafeParser.ToEnum <Statuses>(model.Status);
            collection.CollectionType  = SafeParser.ToEnum <CollectionType>(model.CollectionType);
            collection.Tags            = model.TagsList.ToDelimitedList();
            collection.URLs            = model.URLsList.ToDelimitedList();
            collection.AlternateNames  = model.AlternateNamesList.ToDelimitedList();
            collection.CollectionCount = model.CollectionCount;

            var collectionImage = ImageHelper.ImageDataFromUrl(model.NewThumbnailData);

            if (collectionImage != null)
            {
                // Ensure is jpeg first
                collection.Thumbnail = ImageHelper.ConvertToJpegFormat(collectionImage);

                // Resize to store in database as thumbnail
                collection.Thumbnail = ImageHelper.ResizeImage(collection.Thumbnail, this.Configuration.MediumImageSize.Width, this.Configuration.MediumImageSize.Height);
            }

            if (model.Maintainer?.Value != null)
            {
                var maintainer = this.DbContext.Users.FirstOrDefault(x => x.RoadieId == SafeParser.ToGuid(model.Maintainer.Value));
                if (maintainer != null)
                {
                    collection.MaintainerId = maintainer.Id;
                }
            }
            collection.LastUpdated = now;

            if (isNew)
            {
                await this.DbContext.Collections.AddAsync(collection);
            }
            await this.DbContext.SaveChangesAsync();

            this.CacheManager.ClearRegion(collection.CacheRegion);
            this.Logger.LogInformation($"UpdateArtist `{ collection }` By User `{ user }`");
            return(new OperationResult <bool>
            {
                IsSuccess = !errors.Any(),
                Data = !errors.Any(),
                OperationTime = sw.ElapsedMilliseconds,
                Errors = errors
            });
        }
Ejemplo n.º 3
0
        /// <summary>
        ///     Updates (or Adds) Collection
        /// </summary>
        public async Task <OperationResult <bool> > UpdateCollection(User user, Collection model)
        {
            var isNew = model.Id == Guid.Empty;
            var now   = DateTime.UtcNow;

            var sw = new Stopwatch();

            sw.Start();
            var errors = new List <Exception>();

            var collection = new data.Collection();

            if (!isNew)
            {
                collection = DbContext.Collections.FirstOrDefault(x => x.RoadieId == model.Id);
                if (collection == null)
                {
                    return(new OperationResult <bool>(true, string.Format("Collection Not Found [{0}]", model.Id)));
                }
                // If collection is being renamed, see if collection already exists with new model supplied name
                var collectionName      = collection.SortNameValue;
                var collectionModelName = model.SortNameValue;
                if (collectionName.ToAlphanumericName() != collectionModelName.ToAlphanumericName())
                {
                    var existingCollection = DbContext.Collections.FirstOrDefault(x => x.Name == model.Name || x.SortName == model.SortName);
                    if (existingCollection != null)
                    {
                        return(new OperationResult <bool>($"Collection already exists `{ collection }` with name [{ collectionModelName }]."));
                    }
                }
            }
            collection.IsLocked = model.IsLocked;
            var oldPathToImage = collection.PathToImage(Configuration);
            var didChangeName  = collection.Name != model.Name && !isNew;

            collection.Name            = model.Name;
            collection.SortName        = model.SortName;
            collection.Edition         = model.Edition;
            collection.ListInCSVFormat = model.ListInCSVFormat;
            collection.ListInCSV       = model.ListInCSV;
            collection.Description     = model.Description;
            collection.URLs            = model.URLs;
            collection.Status          = SafeParser.ToEnum <Statuses>(model.Status);
            collection.CollectionType  = SafeParser.ToEnum <CollectionType>(model.CollectionType);
            collection.Tags            = model.TagsList.ToDelimitedList();
            collection.URLs            = model.URLsList.ToDelimitedList();
            collection.AlternateNames  = model.AlternateNamesList.ToDelimitedList();
            collection.CollectionCount = model.CollectionCount;

            if (model.Maintainer?.Value != null)
            {
                var maintainer = DbContext.Users.FirstOrDefault(x => x.RoadieId == SafeParser.ToGuid(model.Maintainer.Value));
                if (maintainer != null)
                {
                    collection.MaintainerId = maintainer.Id;
                }
            }

            if (isNew)
            {
                await DbContext.Collections.AddAsync(collection);

                await DbContext.SaveChangesAsync();
            }

            if (didChangeName)
            {
                if (File.Exists(oldPathToImage))
                {
                    File.Move(oldPathToImage, collection.PathToImage(Configuration));
                }
            }

            var collectionImage = ImageHelper.ImageDataFromUrl(model.NewThumbnailData);

            if (collectionImage != null)
            {
                // Save unaltered collection image
                File.WriteAllBytes(collection.PathToImage(Configuration, true), ImageHelper.ConvertToJpegFormat(collectionImage));
            }

            collection.LastUpdated = now;
            await DbContext.SaveChangesAsync();

            CacheManager.ClearRegion(collection.CacheRegion);
            Logger.LogInformation($"UpdateCollection `{collection}` By User `{user}`");
            return(new OperationResult <bool>
            {
                IsSuccess = !errors.Any(),
                Data = !errors.Any(),
                OperationTime = sw.ElapsedMilliseconds,
                Errors = errors
            });
        }