public static async Task UpdateGalleryAsync(this SQLiteDatabase database, IPictureGallery gallery)
        {
            if (!gallery.Id.HasValue)
            {
                // If the gallery doesn't have an ID, assume that it's new, and attempt to add it.

                await database.AddGalleryAsync(gallery);
            }
            else
            {
                // Update the gallery in the database.

                IPictureGallery oldGallery = await database.GetGalleryAsync(gallery.Id);

                IEnumerable <IPicture> deletedPictures = oldGallery.Where(oldPicture => !gallery.Any(picture => oldPicture.Id == picture.Id));
                IEnumerable <IPicture> newPictures     = gallery.Where(picture => !oldGallery.Any(oldPicture => picture.Id == oldPicture.Id));

                foreach (IPicture picture in deletedPictures)
                {
                    await database.RemovePictureAsync(gallery, picture);
                }

                foreach (IPicture picture in newPictures)
                {
                    await database.AddPictureAsync(gallery, picture);
                }
            }
        }
Ejemplo n.º 2
0
        public async Task PlusPic(string genusName, string speciesName, string imageUrl, string description)
        {
            // Get the species.

            ISpecies species = await GetSpeciesOrReplyAsync(genusName, speciesName);

            if (species.IsValid() && await ReplyValidateImageUrlAsync(imageUrl))
            {
                // Add the new picture to the gallery.

                // If this is the first picture we've added to the species, set the artist as the species' owner.
                // Otherwise, set the artist to the person submitting the image.

                IPictureGallery gallery = await Db.GetGalleryAsync(species) ?? new PictureGallery();

                bool isFirstPicture       = gallery.Count() <= 0;
                bool pictureAlreadyExists = gallery
                                            .Any(x => x.Url == imageUrl);

                IPicture picture = gallery
                                   .Where(p => p.Url == imageUrl)
                                   .FirstOrDefault() ?? new Picture();

                picture.Url         = imageUrl;
                picture.Description = description;

                if (string.IsNullOrEmpty(picture.Artist?.Name))
                {
                    picture.Artist = isFirstPicture ? species.Creator : Context.User.ToCreator();
                }

                await Db.AddPictureAsync(species, picture);

                if (pictureAlreadyExists)
                {
                    await ReplySuccessAsync($"Successfully updated {imageUrl.ToLink("picture")} for {species.GetShortName().ToBold()}.");
                }
                else
                {
                    await ReplySuccessAsync($"Successfully added new {imageUrl.ToLink("picture")} for {species.GetShortName().ToBold()}.");
                }
            }
        }