Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEAT: single file commands and queries #58

Merged
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
using Monaco.Template.Backend.Api.Auth;
#endif
using Monaco.Template.Backend.Application.DTOs;
using Monaco.Template.Backend.Application.Features.Company.Commands;
using Monaco.Template.Backend.Application.Features.Company.Queries;
using Monaco.Template.Backend.Application.Features.Company;
using Monaco.Template.Backend.Common.Api.Application;
using Monaco.Template.Backend.Common.Api.Application.Enums;
using Monaco.Template.Backend.Common.Domain.Model;
Expand All @@ -34,14 +33,14 @@ public CompaniesController(IMediator mediator)
[Authorize(Scopes.CompaniesRead)]
#endif
public Task<ActionResult<Page<CompanyDto>>> Get() =>
_mediator.ExecuteQueryAsync(new GetCompanyPageQuery(Request.Query));
_mediator.ExecuteQueryAsync(new GetCompanyPage.Query(Request.Query));

[HttpGet("{id:guid}")]
#if (!disableAuth)
[Authorize(Scopes.CompaniesRead)]
#endif
public Task<ActionResult<CompanyDto?>> Get(Guid id) =>
_mediator.ExecuteQueryAsync(new GetCompanyByIdQuery(id));
_mediator.ExecuteQueryAsync(new GetCompanyById.Query(id));

[HttpPost]
#if (!disableAuth)
Expand All @@ -67,6 +66,6 @@ public CompaniesController(IMediator mediator)
[Authorize(Scopes.CompaniesWrite)]
#endif
public Task<IActionResult> Delete(Guid id) =>
_mediator.ExecuteCommandAsync(new CompanyDeleteCommand(id),
_mediator.ExecuteCommandAsync(new DeleteCompany.Command(id),
ModelState);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Monaco.Template.Backend.Application.DTOs;
using Monaco.Template.Backend.Application.Features.Country.Queries;
using Monaco.Template.Backend.Application.Features.Country;
using Monaco.Template.Backend.Common.Api.Application;

namespace Monaco.Template.Backend.Api.Controllers;
Expand All @@ -23,7 +23,7 @@ public CountriesController(IMediator mediator)
/// <returns></returns>
[HttpGet]
public Task<ActionResult<List<CountryDto>>> Get() =>
_mediator.ExecuteQueryAsync(new GetCountryListQuery(Request.Query));
_mediator.ExecuteQueryAsync(new GetCountryList.Query(Request.Query));

/// <summary>
/// Gets a country by Id
Expand All @@ -32,5 +32,5 @@ public CountriesController(IMediator mediator)
/// <returns></returns>
[HttpGet("{id:guid}")]
public Task<ActionResult<CountryDto?>> Get(Guid id) =>
_mediator.ExecuteQueryAsync(new GetCountryByIdQuery(id));
_mediator.ExecuteQueryAsync(new GetCountryById.Query(id));
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
#if (!disableAuth)
using Monaco.Template.Backend.Api.Auth;
#endif
using Monaco.Template.Backend.Application.Features.File.Commands;
using Monaco.Template.Backend.Application.DTOs;
using Monaco.Template.Backend.Application.Features.File.Queries;
using Monaco.Template.Backend.Application.Features.File;
using Monaco.Template.Backend.Common.Api.Application;
using MediatR;
#if (!disableAuth)
Expand Down Expand Up @@ -32,7 +31,7 @@ public FilesController(IMediator mediator)
[Authorize(Scopes.FilesWrite)]
#endif
public Task<ActionResult<Guid>> Post([FromRoute] ApiVersion apiVersion, [FromForm] IFormFile file) =>
_mediator.ExecuteCommandAsync(new FileCreateCommand(file.OpenReadStream(), file.FileName, file.ContentType),
_mediator.ExecuteCommandAsync(new CreateFile.Command(file.OpenReadStream(), file.FileName, file.ContentType),
ModelState,
"api/v{0}/files/{1}",
apiVersion);
Expand All @@ -42,7 +41,7 @@ public FilesController(IMediator mediator)
[Authorize(Scopes.FilesRead)]
#endif
public Task<ActionResult<FileDto>> Get(Guid id) =>
_mediator.ExecuteQueryAsync(new GetFileByIdQuery(id));
_mediator.ExecuteQueryAsync(new GetFileById.Query(id));

[HttpGet("{id:guid}/Download")]
#if (!disableAuth)
Expand All @@ -52,7 +51,7 @@ public FilesController(IMediator mediator)
[ProducesResponseType((int)HttpStatusCode.NotFound)]
public async Task<IActionResult> Download(Guid id)
{
var result = await _mediator.Send(new DownloadFileByIdQuery(id));
var result = await _mediator.Send(new DownloadFileById.Query(id));

if (result == null)
return NotFound();
Expand All @@ -65,7 +64,7 @@ public async Task<IActionResult> Download(Guid id)
[Authorize(Scopes.FilesWrite)]
#endif
public Task<IActionResult> Delete(Guid id) =>
_mediator.ExecuteCommandAsync(new FileDeleteCommand(id),
_mediator.ExecuteCommandAsync(new DeleteFile.Command(id),
ModelState);
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
using Monaco.Template.Backend.Api.Auth;
#endif
using Monaco.Template.Backend.Application.DTOs;
using Monaco.Template.Backend.Application.Features.File.Queries;
using Monaco.Template.Backend.Application.Features.Image.Queries;
using Monaco.Template.Backend.Application.Features.File;
using Monaco.Template.Backend.Application.Features.Image;
using MediatR;
#if (!disableAuth)
using Microsoft.AspNetCore.Authorization;
Expand All @@ -31,14 +31,14 @@ public ImagesController(IMediator mediator)
[Authorize(Scopes.FilesRead)]
#endif
public Task<ActionResult<ImageDto>> Get(Guid id) =>
_mediator.ExecuteQueryAsync(new GetImageByIdQuery(id));
_mediator.ExecuteQueryAsync(new GetImageById.Query(id));

[HttpGet("{id:guid}/Thumbnail")]
#if (!disableAuth)
[Authorize(Scopes.FilesRead)]
#endif
public Task<ActionResult<ImageDto>> GetThumbnail(Guid id) =>
_mediator.ExecuteQueryAsync(new GetThumbnailByImageIdQuery(id));
_mediator.ExecuteQueryAsync(new GetThumbnailByImageId.Query(id));

[HttpGet("{id:guid}/Download")]
#if (!disableAuth)
Expand All @@ -48,7 +48,7 @@ public ImagesController(IMediator mediator)
[ProducesResponseType((int)HttpStatusCode.NotFound)]
public async Task<IActionResult> Download(Guid id)
{
var result = await _mediator.Send(new DownloadFileByIdQuery(id));
var result = await _mediator.Send(new DownloadFileById.Query(id));

if (result is null)
return NotFound();
Expand All @@ -64,7 +64,7 @@ public async Task<IActionResult> Download(Guid id)
[ProducesResponseType((int)HttpStatusCode.NotFound)]
public async Task<IActionResult> DownloadThumbnail(Guid id)
{
var result = await _mediator.Send(new DownloadThumbnailByImageIdQuery(id));
var result = await _mediator.Send(new DownloadThumbnailByImageId.Query(id));

if (result is null)
return NotFound();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using Monaco.Template.Backend.Application.Features.Company.Commands;
using Monaco.Template.Backend.Application.Features.Company;

namespace Monaco.Template.Backend.Api.DTOs.Extensions;

public static class CompanyExtensions
{
public static CompanyCreateCommand MapCreateCommand(this CompanyCreateEditDto value) =>
public static CreateCompany.Command MapCreateCommand(this CompanyCreateEditDto value) =>
new(value.Name!,
value.Email!,
value.WebSiteUrl!,
Expand All @@ -14,7 +14,7 @@ public static class CompanyExtensions
value.PostCode,
value.CountryId);

public static CompanyEditCommand MapEditCommand(this CompanyCreateEditDto value, Guid id) =>
public static EditCompany.Command MapEditCommand(this CompanyCreateEditDto value, Guid id) =>
new(id,
value.Name!,
value.Email!,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using FluentAssertions;
using MockQueryable.Moq;
using Monaco.Template.Backend.Application.Features.Company.Commands;
using Monaco.Template.Backend.Application.Features.Company;
using Monaco.Template.Backend.Application.Infrastructure.Context;
using Monaco.Template.Backend.Common.Tests.Factories;
using Monaco.Template.Backend.Domain.Model;
Expand Down Expand Up @@ -30,16 +30,16 @@ public async Task CreateNewCompanySucceeds(Domain.Model.Country country)
var countryDbSetMock = new[] { country }.AsQueryable().BuildMockDbSet();
dbContextMock.Setup(x => x.Set<Domain.Model.Country>())
.Returns(countryDbSetMock.Object);
var commandMock = new Mock<CompanyCreateCommand>(It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<Guid>());
var commandMock = new Mock<CreateCompany.Command>(It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<Guid>());

var sut = new CompanyCommandsHandlers(dbContextMock.Object);
var sut = new CreateCompany.Handler(dbContextMock.Object);
var result = await sut.Handle(commandMock.Object, new CancellationToken());

companyDbSetMock.Verify(x => x.Attach(It.IsAny<Domain.Model.Company>()), Times.Once);
Expand All @@ -62,17 +62,17 @@ public async Task EditCompanySucceeds(Domain.Model.Country country)
var countryDbSetMock = new[] { country }.AsQueryable().BuildMockDbSet();
dbContextMock.Setup(x => x.Set<Domain.Model.Country>())
.Returns(countryDbSetMock.Object);
var commandMock = new Mock<CompanyEditCommand>(It.IsAny<Guid>(),
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<Guid>());
var commandMock = new Mock<EditCompany.Command>(It.IsAny<Guid>(),
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<Guid>());

var sut = new CompanyCommandsHandlers(dbContextMock.Object);
var sut = new EditCompany.Handler(dbContextMock.Object);
var result = await sut.Handle(commandMock.Object, new CancellationToken());

companyMock.Verify(x => x.Update(It.IsAny<string>(),
Expand Down
Loading