-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#131 Added Contact controller REST API
- Loading branch information
1 parent
79eecc0
commit ba0531d
Showing
10 changed files
with
199 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
...nAssistant.Blazor/Server/WebAPI/v1/ContactsModule/ContactController.ContactResponseDto.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using AdminAssistant.DomainModel.Modules.ContactsModule; | ||
using AdminAssistant.Framework.TypeMapping; | ||
|
||
namespace AdminAssistant.WebAPI.v1.ContactsModule | ||
{ | ||
public class ContactResponseDto : IMapFrom<Contact> | ||
{ | ||
public int ContactID { get; set; } | ||
public string FirstName { get; set; } = string.Empty; | ||
public string LastName { get; set; } = string.Empty; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/AdminAssistant.Blazor/Server/WebAPI/v1/ContactsModule/ContactController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.ContactsModule.CQRS; | ||
using AdminAssistant.Infra.Providers; | ||
using AutoMapper; | ||
using MediatR; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Swashbuckle.AspNetCore.Annotations; | ||
|
||
namespace AdminAssistant.WebAPI.v1.ContactsModule | ||
{ | ||
[ApiController] | ||
[Route("api/v1/core/[controller]")] | ||
[ApiExplorerSettings(GroupName = "Contacts - Contact")] | ||
public class ContactController : WebAPIControllerBase | ||
{ | ||
public ContactController(IMapper mapper, IMediator mediator, ILoggingProvider loggingProvider) | ||
: base(mapper, mediator, loggingProvider) | ||
{ | ||
} | ||
|
||
[HttpGet] | ||
[SwaggerOperation("Lists all contacts", OperationId = "GetContact")] | ||
[SwaggerResponse(StatusCodes.Status200OK, "Ok - returns a list of ContactResponseDto", type: typeof(IEnumerable<ContactResponseDto>))] | ||
public async Task<ActionResult<IEnumerable<ContactResponseDto>>> GetContacts() | ||
{ | ||
Log.Start(); | ||
|
||
var result = await Mediator.Send(new ContactQuery()).ConfigureAwait(false); | ||
var response = Mapper.Map<IEnumerable<ContactResponseDto>>(result.Value); | ||
|
||
return Log.Finish(Ok(response)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
src/AdminAssistant.Test/WebAPI/v1/ContactsModule/ContactController_UnitTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.ContactsModule; | ||
using AdminAssistant.DomainModel.Modules.ContactsModule.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.ContactsModule | ||
{ | ||
public class ContactController_GetContacts | ||
{ | ||
[Fact] | ||
[Trait("Category", "Unit")] | ||
public async Task Returns_Status200OK_With_AListOfContacts_Given_NoArguments() | ||
{ | ||
// Arrange | ||
var documents = new List<Contact>() | ||
{ | ||
Factory.Contact.WithTestData(10).Build(), | ||
Factory.Contact.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<ContactQuery>(), It.IsAny<CancellationToken>())) | ||
.Returns(Task.FromResult(Result<IEnumerable<Contact>>.Success(documents))); | ||
|
||
services.AddTransient((sp) => mockMediator.Object); | ||
services.AddTransient<ContactController>(); | ||
|
||
// Act | ||
var response = await services.BuildServiceProvider().GetRequiredService<ContactController>().GetContacts().ConfigureAwait(false); | ||
|
||
// Assert | ||
response.Result.Should().BeOfType<OkObjectResult>(); | ||
response.Value.Should().BeNull(); | ||
|
||
var result = (OkObjectResult)response.Result; | ||
result.Value.Should().BeAssignableTo<IEnumerable<ContactResponseDto>>(); | ||
|
||
//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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/AdminAssistant/DomainModel/Modules/ContactsModule/Builders/ContactBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
namespace AdminAssistant.DomainModel.Modules.ContactsModule.Builders | ||
{ | ||
public interface IContactBuilder | ||
{ | ||
Contact Build(); | ||
IContactBuilder WithTestData(int assetID = Constants.UnknownRecordID); | ||
IContactBuilder WithFirstName(string firstName); | ||
IContactBuilder WithLastName(string lastName); | ||
} | ||
internal class ContactBuilder : Contact, IContactBuilder | ||
{ | ||
public static Contact Default(IContactBuilder builder) => builder.Build(); | ||
public static Contact Default(ContactBuilder builder) => builder.Build(); | ||
|
||
public Contact Build() => this; | ||
|
||
public IContactBuilder WithTestData(int assetID = Constants.UnknownRecordID) | ||
{ | ||
ContactID = assetID; | ||
FirstName = "Fred"; | ||
LastName = "Blogs"; | ||
return this; | ||
} | ||
|
||
public IContactBuilder WithFirstName(string firstName) | ||
{ | ||
FirstName = firstName; | ||
return this; | ||
} | ||
|
||
public IContactBuilder WithLastName(string lastName) | ||
{ | ||
LastName = lastName; | ||
return this; | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/AdminAssistant/DomainModel/Modules/ContactsModule/CQRS/ContactQuery.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.ContactsModule.CQRS | ||
{ | ||
public class ContactQuery : IRequest<Result<IEnumerable<Contact>>> | ||
{ | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/AdminAssistant/DomainModel/Modules/ContactsModule/Contact.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using AdminAssistant.Infra.DAL; | ||
|
||
namespace AdminAssistant.DomainModel.Modules.ContactsModule | ||
{ | ||
public class Contact : IDatabasePersistable | ||
{ | ||
public const int ContactFirstNameMaxLength = Constants.NameMaxLength; | ||
public const int ContactLastNameMaxLength = Constants.NameMaxLength; | ||
|
||
public int ContactID { get; set; } | ||
public string FirstName { get; set; } = string.Empty; | ||
public string LastName { get; set; } = string.Empty; | ||
|
||
public int PrimaryKey => ContactID; | ||
} | ||
} |