Browse Source

Improvements to app image upload.

pull/419/head
Sebastian Stehle 6 years ago
parent
commit
91c2291fb4
  1. 2
      src/Squidex.Domain.Apps.Entities/Apps/AppGrain.cs
  2. 3
      src/Squidex.Domain.Apps.Entities/Apps/Commands/UploadAppImage.cs
  3. 12
      src/Squidex.Domain.Apps.Entities/Apps/Guards/GuardApp.cs
  4. 2
      src/Squidex/Areas/Api/Controllers/Apps/AppsController.cs
  5. 5
      tests/Squidex.Domain.Apps.Entities.Tests/Apps/AppGrainTests.cs
  6. 18
      tests/Squidex.Domain.Apps.Entities.Tests/Apps/Guards/GuardAppTests.cs

2
src/Squidex.Domain.Apps.Entities/Apps/AppGrain.cs

@ -361,7 +361,7 @@ namespace Squidex.Domain.Apps.Entities.Apps
public void UploadImage(UploadAppImage command)
{
RaiseEvent(SimpleMapper.Map(command, new AppImageUploaded()));
RaiseEvent(SimpleMapper.Map(command, new AppImageUploaded { Image = new AppImage(command.File.MimeType) }));
}
public void RemoveImage(RemoveAppImage command)

3
src/Squidex.Domain.Apps.Entities/Apps/Commands/UploadAppImage.cs

@ -5,15 +5,12 @@
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using Squidex.Domain.Apps.Core.Apps;
using Squidex.Infrastructure.Assets;
namespace Squidex.Domain.Apps.Entities.Apps.Commands
{
public sealed class UploadAppImage : AppCommand
{
public AppImage Image { get; set; }
public AssetFile File { get; set; }
}
}

12
src/Squidex.Domain.Apps.Entities/Apps/Guards/GuardApp.cs

@ -29,12 +29,20 @@ namespace Squidex.Domain.Apps.Entities.Apps.Guards
});
}
public static void CanUpdate(UpdateApp command)
public static void CanUploadImage(UploadAppImage command)
{
Guard.NotNull(command, nameof(command));
Validate.It(() => "Cannot upload image.", e =>
{
if (command.File == null)
{
e(Not.Defined("File"), nameof(command.File));
}
});
}
public static void CanUploadImage(UploadAppImage command)
public static void CanUpdate(UpdateApp command)
{
Guard.NotNull(command, nameof(command));
}

2
src/Squidex/Areas/Api/Controllers/Apps/AppsController.cs

@ -284,7 +284,7 @@ namespace Squidex.Areas.Api.Controllers.Apps
throw new ValidationException("Cannot create asset.", error);
}
return new UploadAppImage { File = file[0].ToAssetFile(), Image = new AppImage(file[0].ContentType) };
return new UploadAppImage { File = file[0].ToAssetFile() };
}
private static FileStream GetTempStream()

5
tests/Squidex.Domain.Apps.Entities.Tests/Apps/AppGrainTests.cs

@ -18,6 +18,7 @@ using Squidex.Domain.Apps.Entities.Apps.State;
using Squidex.Domain.Apps.Entities.TestHelpers;
using Squidex.Domain.Apps.Events.Apps;
using Squidex.Infrastructure;
using Squidex.Infrastructure.Assets;
using Squidex.Infrastructure.Commands;
using Squidex.Infrastructure.Log;
using Squidex.Shared.Users;
@ -123,7 +124,7 @@ namespace Squidex.Domain.Apps.Entities.Apps
[Fact]
public async Task UploadImage_should_create_events_and_update_state()
{
var command = new UploadAppImage { Image = new AppImage("image/png") };
var command = new UploadAppImage { File = new AssetFile("image.png", "image/png", 100, () => null) };
await ExecuteCreateAsync();
@ -135,7 +136,7 @@ namespace Squidex.Domain.Apps.Entities.Apps
LastEvents
.ShouldHaveSameEvents(
CreateEvent(new AppImageUploaded { Image = command.Image })
CreateEvent(new AppImageUploaded { Image = sut.Snapshot.Image })
);
}

18
tests/Squidex.Domain.Apps.Entities.Tests/Apps/Guards/GuardAppTests.cs

@ -11,6 +11,7 @@ using Squidex.Domain.Apps.Entities.Apps.Commands;
using Squidex.Domain.Apps.Entities.Apps.Services;
using Squidex.Domain.Apps.Entities.TestHelpers;
using Squidex.Infrastructure;
using Squidex.Infrastructure.Assets;
using Squidex.Infrastructure.Validation;
using Squidex.Shared.Users;
using Xunit;
@ -56,6 +57,23 @@ namespace Squidex.Domain.Apps.Entities.Apps.Guards
GuardApp.CanCreate(command);
}
[Fact]
public void CanUploadImage_should_throw_exception_if_name_not_valid()
{
var command = new UploadAppImage();
ValidationAssert.Throws(() => GuardApp.CanUploadImage(command),
new ValidationError("File is required.", "File"));
}
[Fact]
public void CanUploadImage_should_not_throw_exception_if_app_name_is_valid()
{
var command = new UploadAppImage { File = new AssetFile("file.png", "image/png", 100, () => null) };
GuardApp.CanUploadImage(command);
}
[Fact]
public void CanChangePlan_should_throw_exception_if_plan_id_is_null()
{

Loading…
Cancel
Save