Skip to content

Commit

Permalink
Update workshop images (#484)
Browse files Browse the repository at this point in the history
* Refactored Image uploading process

* Refactored image uploading process

* Added updating workshop Images

* Created WorkshopImagesService, added ErrorDescriber for determing errors

* Created ImageResultsHelper for controllers results

* Added checking workshop images' limits before uploading. Improved xml docs.

* Changed amount validation for images

* Deleted unnecessary method

* Deleted image options from development settings, changed describer lifetime

* Added types for workshops' creation/updating responses

* Changed resources logic

* Changed code while creating operation error

* Changed limits of images' amount

* Optimized images responses

* Changed creating operation errors

* Fixed types

* Added xml docs for changed logic for resources
  • Loading branch information
provicevko authored Jan 11, 2022
1 parent b111033 commit 0d383dc
Show file tree
Hide file tree
Showing 53 changed files with 1,276 additions and 450 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

namespace OutOfSchool.Services.Repository
{
// TODO: think about disposing streams, contexts in future
public class ExternalImageStorage : IExternalImageStorage
{
private const string ContentType = "ContentType";
Expand All @@ -32,7 +33,7 @@ public async Task<ExternalImageModel> GetByIdAsync(string imageId)
try
{
var result = await gridFsBucket.OpenDownloadStreamAsync(new ObjectId(imageId))
?? throw new InvalidOperationException($"Unreal to get non-nullable {nameof(GridFSDownloadStream)} instance."); // think about searching by file name
?? throw new InvalidOperationException($"Unreal to get non-nullable {nameof(GridFSDownloadStream)} instance."); // think about searching by file name
var contentType = result.FileInfo.Metadata[ContentType].AsString;
return new ExternalImageModel { ContentStream = result, ContentType = contentType };
}
Expand Down Expand Up @@ -61,5 +62,18 @@ public async Task<string> UploadImageAsync(ExternalImageModel imageModel, Cancel
throw new ImageStorageException(ex);
}
}

public async Task DeleteImageAsync(string imageId, CancellationToken cancellationToken = default)
{
_ = imageId ?? throw new ArgumentNullException(nameof(imageId));
try
{
await gridFsBucket.DeleteAsync(new ObjectId(imageId), cancellationToken: cancellationToken).ConfigureAwait(false);
}
catch (Exception ex)
{
throw new ImageStorageException(ex);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using MongoDB.Bson;
using OutOfSchool.Services.Models.Images;

namespace OutOfSchool.Services.Repository
Expand All @@ -13,5 +14,7 @@ public interface IExternalImageStorage
Task<ExternalImageModel> GetByIdAsync(string imageId);

Task<string> UploadImageAsync(ExternalImageModel imageModel, CancellationToken cancellationToken = default);

Task DeleteImageAsync(string imageId, CancellationToken cancellationToken = default);
}
}
12 changes: 0 additions & 12 deletions OutOfSchool/OutOfSchool.DataAccess/Repository/IImageRepository.cs

This file was deleted.

20 changes: 0 additions & 20 deletions OutOfSchool/OutOfSchool.DataAccess/Repository/ImageRepository.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public class WorkshopControllerTests
private Mock<IProviderService> providerServiceMoq;
private Mock<IStringLocalizer<SharedResource>> localizer;
private Mock<IImageService> imageServiceMock;
private Mock<IOptions<CommonImagesRequestLimits>> requestLimitOptionsMock;

private string userId;
private Mock<HttpContext> httpContextMoq;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using OutOfSchool.WebApi.Extensions;
using OutOfSchool.WebApi.Models;
using OutOfSchool.WebApi.Services;
using OutOfSchool.WebApi.Services.Images;
using OutOfSchool.WebApi.Util;

namespace OutOfSchool.WebApi.Tests.Services
Expand All @@ -31,6 +32,7 @@ public class WorkshopServiceTests
private Mock<IRatingService> ratingService;
private Mock<ILogger<WorkshopService>> logger;
private Mock<IMapper> mapper;
private Mock<IWorkshopImagesService> workshopImagesService;

[SetUp]
public void SetUp()
Expand All @@ -40,13 +42,15 @@ public void SetUp()
ratingService = new Mock<IRatingService>();
logger = new Mock<ILogger<WorkshopService>>();
mapper = new Mock<IMapper>();
workshopImagesService = new Mock<IWorkshopImagesService>();
workshopService =
new WorkshopService(
workshopRepository.Object,
classRepository.Object,
ratingService.Object,
logger.Object,
mapper.Object);
mapper.Object,
workshopImagesService.Object);
}

#region Create
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ public class MultipleKeyValueOperationResult
/// </summary>
public string GeneralResultMessage { get; set; }

/// <summary>
/// Gets a value indicating whether the multiple result of the operation is succeeded.
/// </summary>
public bool Succeeded => HasResults && !HasBadResults;

/// <summary>
/// Gets a value indicating whether the <see cref="Results"/> isn't empty.
/// </summary>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
namespace OutOfSchool.WebApi.Common.Resources.Codes
{
/// <summary>
/// Contains error codes of all operations with images.
/// </summary>
public enum ImagesOperationErrorCode
{
/// <summary>
/// The default error.
/// </summary>
[ResourcesKey(nameof(DefaultError))]
DefaultError = 0,

/// <summary>
/// The error that can be used when something wrong has happened while uploading images.
/// </summary>
[ResourcesKey(nameof(UploadingError))]
UploadingError = 1,

/// <summary>
/// The error that can be used when something wrong has happened while removing images.
/// </summary>
[ResourcesKey(nameof(RemovingError))]
RemovingError = 2,

/// <summary>
/// The error that can be used when unable to make some operation because of an image storage.
/// </summary>
[ResourcesKey(nameof(ImageStorageError))]
ImageStorageError = 3,

/// <summary>
/// The error that can be used when some image has not found.
/// </summary>
[ResourcesKey(nameof(ImageNotFoundError))]
ImageNotFoundError = 4,

/// <summary>
/// The error that can be used when some image hasn't passed the validation process because of unexpected factors.
/// </summary>
[ResourcesKey(nameof(UnexpectedValidationError))]
UnexpectedValidationError = 5,

/// <summary>
/// The error that can be used when some image hasn't passed the validation process because of the invalid image size.
/// </summary>
[ResourcesKey(nameof(InvalidSizeError))]
InvalidSizeError = 6,

/// <summary>
/// The error that can be used when some image hasn't passed the validation process because of the invalid image format.
/// </summary>
[ResourcesKey(nameof(InvalidFormatError))]
InvalidFormatError = 7,

/// <summary>
/// The error that can be used when some image hasn't passed the validation process because of the invalid image resolution.
/// </summary>
[ResourcesKey(nameof(InvalidResolutionError))]
InvalidResolutionError = 8,

/// <summary>
/// The error that can be used when cannot find the entity for operations with images.
/// </summary>
[ResourcesKey(nameof(EntityNotFoundError))]
EntityNotFoundError = 9,

/// <summary>
/// The error that can be used when no images were given for making operations.
/// </summary>
[ResourcesKey(nameof(NoGivenImagesError))]
NoGivenImagesError = 10,

/// <summary>
/// The error that can be used when something wrong has happened while updating the entity with changed images.
/// </summary>
[ResourcesKey(nameof(UpdateEntityError))]
UpdateEntityError = 11,

/// <summary>
/// The error that can be used when count of images is more than allowed for this type of entity.
/// </summary>
[ResourcesKey(nameof(ExceedingCountOfImagesError))]
ExceedingCountOfImagesError = 12,
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ namespace OutOfSchool.WebApi.Common.Resources
/// </summary>
internal static class ResourceManagers
{
private static ResourceManager imageResourceManager;

internal static ResourceManager ImageResourceManager => imageResourceManager ??= new ResourceManager(ResourceNames.Images, Assembly.GetExecutingAssembly());
internal static ResourceManager ImageResourceManager => WebApi.Resources.Images.ImageResource.ResourceManager;
}
}
17 changes: 0 additions & 17 deletions OutOfSchool/OutOfSchool.WebApi/Common/Resources/ResourceNames.cs

This file was deleted.

19 changes: 0 additions & 19 deletions OutOfSchool/OutOfSchool.WebApi/Common/Resources/Resources.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace OutOfSchool.WebApi.Common.Resources
{
[AttributeUsage(AttributeTargets.Field)]
public class ResourcesKeyAttribute : Attribute
{
public ResourcesKeyAttribute(string appResourcesKey) => ResourcesKey = appResourcesKey;

public string ResourcesKey { get; set; }
}
}
Loading

0 comments on commit 0d383dc

Please sign in to comment.