Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using EventTicketing.Api.Contracts;
using EventTicketing.Api.Extensions;
using EventTicketing.Application.Events;
using System.ComponentModel.DataAnnotations;
using EventTicketing.Application;

namespace EventTicketing.Api.Endpoints;
namespace EventTicketing.Api;

public sealed record ReserveTicketsRequest([property: Range(1, 20)] int Quantity);

public static class EventEndpoints
{
public static IEndpointRouteBuilder MapEventEndpoints(this IEndpointRouteBuilder app)
public static void MapEventEndpoints(this IEndpointRouteBuilder app)
{
var events = app.MapGroup("/api/events");

Expand All @@ -31,7 +32,5 @@ public static IEndpointRouteBuilder MapEventEndpoints(this IEndpointRouteBuilder

return result.IsSuccess ? Results.Ok(result.Value) : result.ToProblem();
});

return app;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
<ProjectReference Include="..\EventTicketing.Infrastructure\EventTicketing.Infrastructure.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.12" />
</ItemGroup>

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
GET http://localhost:5000/api/events/1

###

POST http://localhost:5000/api/events/1/reservations
Content-Type: application/json

{ "quantity": 3 }

###

POST http://localhost:5000/api/events/2/reservations
Content-Type: application/json

{ "quantity": 3 }

###

POST http://localhost:5000/api/events/1/reservations
Content-Type: application/json

{ "quantity": 0 }

###

GET http://localhost:5000/api/events/99

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using EventTicketing.Api.Endpoints;
using EventTicketing.Api;
using EventTicketing.Application;
using EventTicketing.Infrastructure;
using EventTicketing.Infrastructure.Persistence;

var builder = WebApplication.CreateBuilder(args);

Expand All @@ -10,16 +9,15 @@

builder.Services.AddProblemDetails();
builder.Services.AddValidation();
builder.Services.AddOpenApi();

var app = builder.Build();

app.UseExceptionHandler();

if (app.Environment.IsDevelopment())
using (var scope = app.Services.CreateScope())
{
app.MapOpenApi();
await app.Services.SeedDatabaseAsync();
var dbContext = scope.ServiceProvider.GetRequiredService<TicketingDbContext>();
dbContext.Database.EnsureCreated();
}

app.MapEventEndpoints();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,20 @@
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"launchBrowser": true,
"applicationUrl": "http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:7052;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using EventTicketing.Domain;

namespace EventTicketing.Api;

public static class ResultExtensions
{
public static IResult ToProblem(this Result result)
{
var error = result.Error
?? throw new InvalidOperationException("A successful result is not a problem.");

var statusCode = error.Type switch
{
ErrorType.NotFound => StatusCodes.Status404NotFound,
ErrorType.Conflict => StatusCodes.Status409Conflict,
_ => StatusCodes.Status500InternalServerError
};

return TypedResults.Problem(statusCode: statusCode, title: error.Code, detail: error.Description);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using EventTicketing.Application.Events;
using Microsoft.Extensions.DependencyInjection;

namespace EventTicketing.Application;
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using EventTicketing.Domain;

namespace EventTicketing.Application;

public sealed record GetEventAvailabilityQuery(int EventId);

public sealed record EventAvailability(int EventId, string Name, int TicketsLeft);

public sealed class GetEventAvailabilityHandler(IEventRepository events)
{
public async Task<Result<EventAvailability>> HandleAsync(
GetEventAvailabilityQuery query, CancellationToken cancellationToken = default)
{
var availability = await events.GetAvailabilityAsync(query.EventId, cancellationToken);
if (availability is null)
return EventErrors.NotFound(query.EventId);

return availability;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using EventTicketing.Domain;

namespace EventTicketing.Application;

public interface IEventRepository
{
Task<Event?> GetByIdAsync(int eventId, CancellationToken cancellationToken = default);

Task<EventAvailability?> GetAvailabilityAsync(int eventId, CancellationToken cancellationToken = default);

Task SaveChangesAsync(CancellationToken cancellationToken = default);
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
using EventTicketing.Application.Abstractions;
using EventTicketing.Domain.Common;
using EventTicketing.Domain.Events;
using EventTicketing.Domain;

namespace EventTicketing.Application.Events;
namespace EventTicketing.Application;

public sealed record ReserveTicketsCommand(int EventId, int Quantity);

public sealed record ReservationResponse(int EventId, int TicketsReserved, int TicketsLeft);

public sealed class ReserveTicketsHandler(IEventRepository events, IUnitOfWork unitOfWork)
public sealed class ReserveTicketsHandler(IEventRepository events)
{
public async Task<Result<ReservationResponse>> HandleAsync(
ReserveTicketsCommand command, CancellationToken cancellationToken = default)
Expand All @@ -18,10 +16,10 @@ public async Task<Result<ReservationResponse>> HandleAsync(
return EventErrors.NotFound(command.EventId);

var reservation = ev.Reserve(command.Quantity);
if (reservation.IsFailure)
return reservation.Error;
if (!reservation.IsSuccess)
return reservation.Error!;

await unitOfWork.SaveChangesAsync(cancellationToken);
await events.SaveChangesAsync(cancellationToken);

return new ReservationResponse(ev.Id, command.Quantity, ev.TicketsLeft);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,50 +1,31 @@
using EventTicketing.Application.Events;
using EventTicketing.Domain.Events;
using EventTicketing.Infrastructure.Persistence;
using EventTicketing.Application;
using EventTicketing.Domain;
using NetArchTest.Rules;
using TestResult = NetArchTest.Rules.TestResult;

namespace EventTicketing.ArchitectureTests;

public class DependencyRuleTests
{
private const string Application = "EventTicketing.Application";
private const string Infrastructure = "EventTicketing.Infrastructure";
private const string Api = "EventTicketing.Api";

[Fact]
public void Domain_DependsOnNoOtherLayer()
{
var result = Types.InAssembly(typeof(Event).Assembly)
.ShouldNot()
.HaveDependencyOnAny(Application, Infrastructure, Api)
.HaveDependencyOnAny("EventTicketing.Application", "EventTicketing.Infrastructure", "EventTicketing.Api")
.GetResult();

Assert.True(result.IsSuccessful, Describe(result));
Assert.True(result.IsSuccessful, "Offending types: " + string.Join(", ", result.FailingTypeNames ?? []));
}

[Fact]
public void Application_DoesNotDependOnInfrastructureOrTheWeb()
{
var result = Types.InAssembly(typeof(ReserveTicketsHandler).Assembly)
.ShouldNot()
.HaveDependencyOnAny(Infrastructure, Api, "Microsoft.EntityFrameworkCore", "Microsoft.AspNetCore")
.HaveDependencyOnAny("EventTicketing.Infrastructure", "EventTicketing.Api",
"Microsoft.EntityFrameworkCore", "Microsoft.AspNetCore")
.GetResult();

Assert.True(result.IsSuccessful, Describe(result));
Assert.True(result.IsSuccessful, "Offending types: " + string.Join(", ", result.FailingTypeNames ?? []));
}

[Fact]
public void Infrastructure_DoesNotDependOnTheApi()
{
var result = Types.InAssembly(typeof(TicketingDbContext).Assembly)
.ShouldNot()
.HaveDependencyOn(Api)
.GetResult();

Assert.True(result.IsSuccessful, Describe(result));
}

private static string Describe(TestResult result) =>
"Offending types: " + string.Join(", ", result.FailingTypeNames ?? []);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\EventTicketing.Api\EventTicketing.Api.csproj" />
<ProjectReference Include="..\EventTicketing.Application\EventTicketing.Application.csproj" />
</ItemGroup>

</Project>

This file was deleted.

This file was deleted.

Loading
Loading