public async Task AddMap(string imageUrl, string mapName = "", string description = "") { IPictureGallery gallery = await GetMapGalleryAsync(); // Remove any pictures with the same name. foreach (IPicture pictureWithSameName in gallery.Where(picture => picture.Name?.Equals(mapName, StringComparison.OrdinalIgnoreCase) ?? false).ToArray()) { gallery.Pictures.Remove(pictureWithSameName); } // Add the new picture to the gallery. IPicture newPicture = new Picture(imageUrl) { Name = mapName, Description = description }; gallery.Pictures.Add(newPicture); await Db.UpdateGalleryAsync(gallery); if (string.IsNullOrEmpty(mapName)) { await ReplySuccessAsync($"Successfully added new {"map".FromLink(imageUrl)}."); } else { await ReplySuccessAsync($"Successfully added new {"map".FromLink(imageUrl)}, {mapName.ToTitle().ToBold()}."); } }
public static async Task RemovePictureAsync(this SQLiteDatabase database, IPictureGallery gallery, IPicture picture) { if (gallery is null) { throw new ArgumentNullException(nameof(gallery)); } if (picture is null) { throw new ArgumentNullException(nameof(picture)); } if (!picture.Id.HasValue) { throw new ArgumentException(nameof(picture)); } if (!gallery.Id.HasValue) { throw new ArgumentException(nameof(gallery)); } using (SQLiteCommand cmd = new SQLiteCommand("DELETE FROM Picture WHERE gallery_id = $gallery_id AND id = $id")) { cmd.Parameters.AddWithValue("$id", picture.Id); cmd.Parameters.AddWithValue("$gallery_id", gallery.Id); await database.ExecuteNonQueryAsync(cmd); } }
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); } } }
public static async Task AddGalleryAsync(this SQLiteDatabase database, IPictureGallery gallery) { await database.AddGalleryAsync(gallery.Name); IPictureGallery newGallery = await database.GetGalleryAsync(gallery.Name); await database.UpdateGalleryAsync(new PictureGallery(newGallery.Id, gallery.Name, gallery.Pictures)); }
private async Task <IPictureGallery> GetMapGalleryAsync() { IPictureGallery gallery = await Db.GetGalleryAsync(MapGalleryName); if (gallery is null) { gallery = new PictureGallery(null, MapGalleryName, Enumerable.Empty <IPicture>()); } return(gallery); }
private async Task ShowMapAsync(string mapName = "") { // Display a paginated list of maps. // If a "primary" map is specified, show that map first. // If a "labeled" map is specified, omit that map from pagination and show it when the user presses the "Z" reaction. IPictureGallery gallery = await GetMapGalleryAsync(); if (gallery is null || !gallery.Pictures.Any()) { await ReplyInfoAsync($"No map images have been set. Use the `{Config.Prefix}addmap` or `{Config.Prefix}setmap` command to add map images."); }
public static async Task <IPictureGallery> GetGalleryAsync(this SQLiteDatabase database, string name) { IPictureGallery gallery = null; using (SQLiteCommand cmd = new SQLiteCommand("SELECT * FROM Gallery WHERE name = $name")) { cmd.Parameters.AddWithValue("$name", name); DataRow row = await database.GetRowAsync(cmd); if (row != null) { return(await database.CreateGalleryFromDataRowAsync(row)); } } return(gallery); }
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()}."); } } }
public static async Task <IPictureGallery> GetGalleryAsync(this SQLiteDatabase database, long?id) { IPictureGallery gallery = null; if (id.HasValue) { using (SQLiteCommand cmd = new SQLiteCommand("SELECT * FROM Gallery WHERE id = $id")) { cmd.Parameters.AddWithValue("$id", id); DataRow row = await database.GetRowAsync(cmd); if (row != null) { return(await database.CreateGalleryFromDataRowAsync(row)); } } } return(gallery); }
public static async Task AddPictureAsync(this SQLiteDatabase database, IPictureGallery gallery, IPicture picture) { if (gallery is null) { throw new ArgumentNullException(nameof(gallery)); } if (picture is null) { throw new ArgumentNullException(nameof(picture)); } if (!gallery.Id.HasValue) { throw new ArgumentException(nameof(gallery)); } if (!picture.Id.HasValue) { using (SQLiteCommand cmd = new SQLiteCommand("INSERT OR IGNORE INTO Picture(url, gallery_id, artist, name, description) VALUES($url, $gallery_id, $artist, $name, $description)")) { cmd.Parameters.AddWithValue("$url", picture.Url); cmd.Parameters.AddWithValue("$gallery_id", gallery.Id); cmd.Parameters.AddWithValue("$artist", picture.Artist?.Name ?? ""); cmd.Parameters.AddWithValue("$name", picture.Name); cmd.Parameters.AddWithValue("$description", picture.Description); await database.ExecuteNonQueryAsync(cmd); } } else { using (SQLiteCommand cmd = new SQLiteCommand("UPDATE Picture SET url = $url, artist = $artist, description = $description WHERE id = $id")) { cmd.Parameters.AddWithValue("$id", picture.Id); cmd.Parameters.AddWithValue("$url", picture.Url); cmd.Parameters.AddWithValue("$artist", picture.Artist?.Name ?? ""); cmd.Parameters.AddWithValue("$description", picture.Description); await database.ExecuteNonQueryAsync(cmd); } } }
public async Task SetPic(string genusName, string speciesName, string imageUrl) { // Updates the default picture for the given species. // While any users can add pictures for species, only moderators can update the default picture. ISpecies species = await GetSpeciesOrReplyAsync(genusName, speciesName); if (species.IsValid()) { if (await ReplyValidateImageUrlAsync(imageUrl)) { IPictureGallery gallery = await Db.GetGalleryAsync(species) ?? new PictureGallery(); bool isFirstPicture = gallery.Count() <= 0; await Db.SetPictureAsync(species, new Picture { Url = imageUrl, Artist = isFirstPicture ? species.Creator : Context.User.ToCreator() }); await ReplySuccessAsync($"Successfully set the picture for {species.GetShortName().ToBold()}."); } } }
public static IPicture GetPicture(this IPictureGallery gallery, string name) { return(gallery .Where(p => p.GetName().Equals(name, StringComparison.OrdinalIgnoreCase)) .FirstOrDefault()); }
public static async Task <IEnumerable <IPicture> > GetPicturesFromGalleryAsync(this SQLiteDatabase database, IPictureGallery gallery) { List <IPicture> pictures = new List <IPicture>(); if (gallery != null && gallery.Id.HasValue) { using (SQLiteCommand cmd = new SQLiteCommand("SELECT * FROM Picture WHERE gallery_id = $gallery_id ORDER BY id")) { cmd.Parameters.AddWithValue("$gallery_id", gallery.Id); foreach (DataRow row in await database.GetRowsAsync(cmd)) { pictures.Add(CreatePictureFromDataRow(row)); } } } return(pictures); }