Esempio n. 1
0
        public async Task <ModifyEntityResult> AddImageToCharacterAsync
        (
            [NotNull] Character character,
            [NotNull] string imageName,
            [NotNull] string imageUrl,
            [CanBeNull] string imageCaption = null,
            bool isNSFW = false
        )
        {
            var isImageNameUnique = !character.Images.Any(i => string.Equals(i.Name, imageName, StringComparison.OrdinalIgnoreCase));

            if (!isImageNameUnique)
            {
                return(ModifyEntityResult.FromError("The character already has an image with that name."));
            }

            if (imageName.IsNullOrWhitespace())
            {
                return(ModifyEntityResult.FromError("You need to specify a name."));
            }

            if (imageCaption.IsNullOrWhitespace())
            {
                imageCaption = "No caption set.";
            }

            if (!Uri.IsWellFormedUriString(imageUrl, UriKind.RelativeOrAbsolute))
            {
                return(ModifyEntityResult.FromError
                       (
                           $"That URL doesn't look valid. Please check \"{imageUrl}\" for errors."
                       ));
            }

            var image = new Image(imageName, imageUrl, imageCaption)
            {
                IsNSFW = isNSFW
            };

            character.Images.Add(image);
            await _database.SaveChangesAsync();

            return(ModifyEntityResult.FromSuccess());
        }
 /// <summary>
 /// Removes the given image from the given character.
 /// </summary>
 /// <param name="character">The character to remove the image from.</param>
 /// <param name="image">The image.</param>
 /// <param name="ct">The cancellation token in use.</param>
 /// <returns>A deletion result which may or may not have succeeded.</returns>
 public Task <DeleteEntityResult> RemoveImageFromCharacterAsync
 (
     Character character,
     Image image,
     CancellationToken ct = default
 ) => _characterEditor.RemoveImageFromCharacterAsync(character, image, ct);
 /// <summary>
 /// Removes the given image from the given character.
 /// </summary>
 /// <param name="character">The character to remove the image from.</param>
 /// <param name="image">The image.</param>
 /// <returns>A deletion result which may or may not have succeeded.</returns>
 public async Task <DeleteEntityResult> RemoveImageFromCharacterAsync(Character character, Image image)
 {
     return(await _characters.RemoveImageFromCharacterAsync(character, image));
 }