Skip to content

Commit

Permalink
#125 Added Budget REST API
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonGeering committed Aug 30, 2020
1 parent da4defe commit 454329b
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using AdminAssistant.DomainModel.Modules.BudgetModule.CQRS;
using AdminAssistant.Framework.Providers;
using AutoMapper;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations;

namespace AdminAssistant.WebAPI.v1
{
[ApiController]
[Route("api/v1/core/[controller]")]
[ApiExplorerSettings(GroupName = "Budgets - Budget")]
public class BudgetController : WebAPIControllerBase
{
public BudgetController(IMapper mapper, IMediator mediator, ILoggingProvider loggingProvider)
: base(mapper, mediator, loggingProvider)
{
}

[HttpGet]
[SwaggerOperation("Lists all budgets.", OperationId = "GetBudget")]
[SwaggerResponse(StatusCodes.Status200OK, "Ok - returns a list of BudgetResponseDto", type: typeof(IEnumerable<BudgetResponseDto>))]
public async Task<ActionResult<IEnumerable<BudgetResponseDto>>> GetBudgets()
{
Log.Start();

var result = await Mediator.Send(new BudgetQuery()).ConfigureAwait(false);
var response = Mapper.Map<IEnumerable<BudgetResponseDto>>(result.Value);

return Log.Finish(Ok(response));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using AdminAssistant.DomainModel.Modules.BudgetModule;
using AdminAssistant.Framework.TypeMapping;

namespace AdminAssistant.WebAPI.v1
{
public class BudgetResponseDto : IMapFrom<Budget>
{
public int BudgetID { get; set; }
public string BudgetName { get; set; } = string.Empty;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.Diagnostics.CodeAnalysis;
using AdminAssistant.DomainModel.Modules.AccountsModule;
using AdminAssistant.DomainModel.Modules.BudgetModule;
using AdminAssistant.DomainModel.Modules.DocumentsModule;
using AutoMapper;
using FluentAssertions;
Expand Down Expand Up @@ -41,6 +42,7 @@ public void HaveValidConfiguration()
[InlineData(typeof(BankAccountInfo), typeof(BankAccountInfoResponseDto))]
[InlineData(typeof(BankAccountType), typeof(BankAccountTypeResponseDto))]
[InlineData(typeof(BankAccountTransaction), typeof(BankAccountTransactionResponseDto))]
[InlineData(typeof(Budget), typeof(BudgetResponseDto))]
[InlineData(typeof(Document), typeof(DocumentResponseDto))]
public void ShouldSupportMappingFromSourceToDestination(Type source, Type destination)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#pragma warning disable CA1707 // Identifiers should not contain underscores
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AdminAssistant.DomainModel;
using AdminAssistant.DomainModel.Modules.BudgetModule;
using AdminAssistant.DomainModel.Modules.BudgetModule.CQRS;
using Ardalis.Result;
using AutoMapper;
using FluentAssertions;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using Xunit;

namespace AdminAssistant.WebAPI.v1.BudgetModule
{
public class BudgetController_UnitTest_Should
{
[Fact]
[Trait("Category", "Unit")]
public async Task Return_Status200OK_With_AListOfBudgets_Given_NoArguments()
{
// Arrange
var budgets = new List<Budget>()
{
Factory.Budget.WithTestData(10).Build(),
Factory.Budget.WithTestData(20).Build()
};

var services = new ServiceCollection();
services.AddMockServerSideLogging();
services.AddAutoMapper(typeof(MappingProfile));

var mockMediator = new Mock<IMediator>();
mockMediator.Setup(x => x.Send(It.IsAny<BudgetQuery>(), It.IsAny<CancellationToken>()))
.Returns(Task.FromResult(Result<IEnumerable<Budget>>.Success(budgets)));

services.AddTransient((sp) => mockMediator.Object);
services.AddTransient<BudgetController>();

// Act
var response = await services.BuildServiceProvider().GetRequiredService<BudgetController>().GetBudgets().ConfigureAwait(false);

// Assert
response.Result.Should().BeOfType<OkObjectResult>();
response.Value.Should().BeNull();

var result = (OkObjectResult)response.Result;
result.Value.Should().BeAssignableTo<IEnumerable<BudgetResponseDto>>();

//var value = ((IEnumerable<CurrencyResponseDto>)result.Value).ToArray();
//value.Should().HaveCount(currencies.Count);

//var expected = currencies.ToArray();
//for (int i = 0; i < expected.Length; i++)
//{
// value[i].CurrencyID.Should().Be(expected[i].CurrencyID);
// value[i].Symbol.Should().Be(expected[i].Symbol);
// value[i].DecimalFormat.Should().Be(expected[i].DecimalFormat);
//}
}
}
}
#pragma warning restore CA1707 // Identifiers should not contain underscores
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
using AdminAssistant.Infra.DAL;

namespace AdminAssistant.DomainModel.Modules.BudgetModule
{
public class Budget
public class Budget : IDatabasePersistable
{
public const int BudgetNameMaxLength = Constants.NameMaxLength;

public int BudgetID { get; set; }
public string BudgetName { get; set; } = string.Empty;

public int PrimaryKey => BudgetID;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Collections.Generic;
using Ardalis.Result;
using MediatR;

namespace AdminAssistant.DomainModel.Modules.BudgetModule.CQRS
{
public class BudgetQuery : IRequest<Result<IEnumerable<Budget>>>
{
}
}

0 comments on commit 454329b

Please sign in to comment.