Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
tickets.db
tickets.db-shm
tickets.db-wal
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.10.1" />
<PackageReference Include="NetArchTest.Rules" Version="1.3.2" />
<PackageReference Include="xunit.v3.mtp-off" Version="4.0.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="4.0.0" />
</ItemGroup>

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

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using EventTicketing.Data;
using EventTicketing.Features.Reservations;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;

namespace EventTicketing.Tests;

public sealed class ReserveTicketsTests : IDisposable
{
private readonly SqliteConnection _connection = new("Data Source=:memory:");

public ReserveTicketsTests() => _connection.Open();

public void Dispose() => _connection.Dispose();

private TicketingDbContext CreateDbContext()
{
var options = new DbContextOptionsBuilder<TicketingDbContext>()
.UseSqlite(_connection)
.Options;

var dbContext = new TicketingDbContext(options);
dbContext.Database.EnsureCreated();

return dbContext;
}

[Fact]
public async Task HandleAsync_WhenTicketsAreAvailable_SavesTheReservation()
{
var handler = new ReserveTickets.Handler(CreateDbContext());

var result = await handler.HandleAsync(eventId: 1, quantity: 3, TestContext.Current.CancellationToken);

Assert.True(result.IsSuccess);
Assert.Equal(97, result.Value.TicketsLeft);

var saved = await CreateDbContext().Events.SingleAsync(e => e.Id == 1, TestContext.Current.CancellationToken);
Assert.Equal(3, saved.TicketsSold);
}

[Fact]
public async Task HandleAsync_WhenTheEventIsSoldOut_SavesNothing()
{
var handler = new ReserveTickets.Handler(CreateDbContext());

var result = await handler.HandleAsync(eventId: 2, quantity: 3, TestContext.Current.CancellationToken);

Assert.False(result.IsSuccess);
Assert.Equal("Event.SoldOut", result.Error!.Code);

var saved = await CreateDbContext().Events.SingleAsync(e => e.Id == 2, TestContext.Current.CancellationToken);
Assert.Equal(0, saved.TicketsSold);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using EventTicketing.Features.Reservations;
using NetArchTest.Rules;

namespace EventTicketing.Tests;

public class SliceTests
{
[Fact]
public void Features_DoNotDependOnEachOther()
{
var assembly = typeof(ReserveTickets).Assembly;

var features = assembly.GetTypes()
.Select(t => t.Namespace)
.OfType<string>()
.Where(ns => ns.StartsWith("EventTicketing.Features."))
.Distinct()
.ToArray();

foreach (var feature in features)
{
var otherFeatures = features.Where(f => f != feature).ToArray();

var result = Types.InAssembly(assembly)
.That()
.ResideInNamespace(feature)
.ShouldNot()
.HaveDependencyOnAny(otherFeatures)
.GetResult();

Assert.True(result.IsSuccessful,
$"{feature} depends on another feature: " + string.Join(", ", result.FailingTypeNames ?? []));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EventTicketing", "EventTicketing\EventTicketing.csproj", "{5FAF63F8-89C3-4EEE-A3A0-DCB365CA69B6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EventTicketing.Tests", "EventTicketing.Tests\EventTicketing.Tests.csproj", "{4B09EE23-0672-4FE0-8078-430028A5351E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{5FAF63F8-89C3-4EEE-A3A0-DCB365CA69B6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5FAF63F8-89C3-4EEE-A3A0-DCB365CA69B6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5FAF63F8-89C3-4EEE-A3A0-DCB365CA69B6}.Debug|x64.ActiveCfg = Debug|Any CPU
{5FAF63F8-89C3-4EEE-A3A0-DCB365CA69B6}.Debug|x64.Build.0 = Debug|Any CPU
{5FAF63F8-89C3-4EEE-A3A0-DCB365CA69B6}.Debug|x86.ActiveCfg = Debug|Any CPU
{5FAF63F8-89C3-4EEE-A3A0-DCB365CA69B6}.Debug|x86.Build.0 = Debug|Any CPU
{5FAF63F8-89C3-4EEE-A3A0-DCB365CA69B6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5FAF63F8-89C3-4EEE-A3A0-DCB365CA69B6}.Release|Any CPU.Build.0 = Release|Any CPU
{5FAF63F8-89C3-4EEE-A3A0-DCB365CA69B6}.Release|x64.ActiveCfg = Release|Any CPU
{5FAF63F8-89C3-4EEE-A3A0-DCB365CA69B6}.Release|x64.Build.0 = Release|Any CPU
{5FAF63F8-89C3-4EEE-A3A0-DCB365CA69B6}.Release|x86.ActiveCfg = Release|Any CPU
{5FAF63F8-89C3-4EEE-A3A0-DCB365CA69B6}.Release|x86.Build.0 = Release|Any CPU
{4B09EE23-0672-4FE0-8078-430028A5351E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4B09EE23-0672-4FE0-8078-430028A5351E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4B09EE23-0672-4FE0-8078-430028A5351E}.Debug|x64.ActiveCfg = Debug|Any CPU
{4B09EE23-0672-4FE0-8078-430028A5351E}.Debug|x64.Build.0 = Debug|Any CPU
{4B09EE23-0672-4FE0-8078-430028A5351E}.Debug|x86.ActiveCfg = Debug|Any CPU
{4B09EE23-0672-4FE0-8078-430028A5351E}.Debug|x86.Build.0 = Debug|Any CPU
{4B09EE23-0672-4FE0-8078-430028A5351E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4B09EE23-0672-4FE0-8078-430028A5351E}.Release|Any CPU.Build.0 = Release|Any CPU
{4B09EE23-0672-4FE0-8078-430028A5351E}.Release|x64.ActiveCfg = Release|Any CPU
{4B09EE23-0672-4FE0-8078-430028A5351E}.Release|x64.Build.0 = Release|Any CPU
{4B09EE23-0672-4FE0-8078-430028A5351E}.Release|x86.ActiveCfg = Release|Any CPU
{4B09EE23-0672-4FE0-8078-430028A5351E}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using EventTicketing.Domain;
using Microsoft.EntityFrameworkCore;

namespace EventTicketing.Data;

public sealed class TicketingDbContext(DbContextOptions<TicketingDbContext> options)
: DbContext(options)
{
public DbSet<Event> Events => Set<Event>();

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Event>(builder =>
{
builder.Property(e => e.Name).HasMaxLength(200);

builder.HasData(
new { Id = 1, Name = "Vertical Slice Live", Capacity = 100, TicketsSold = 0 },
new { Id = 2, Name = "Tiny Jazz Club Night", Capacity = 2, TicketsSold = 0 });
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
namespace EventTicketing.Domain;

public sealed class Event
{
private Event(string name, int capacity)
{
Name = name;
Capacity = capacity;
}

public int Id { get; private set; }
public string Name { get; private set; }
public int Capacity { get; private set; }
public int TicketsSold { get; private set; }

public int TicketsLeft => Capacity - TicketsSold;

public static Event Create(string name, int capacity)
{
ArgumentException.ThrowIfNullOrWhiteSpace(name);
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(capacity);

return new Event(name, capacity);
}

public Result Reserve(int quantity)
{
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(quantity);

if (quantity > TicketsLeft)
return EventErrors.SoldOut(TicketsLeft, quantity);

TicketsSold += quantity;

return Result.Success();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace EventTicketing.Domain;

public static class EventErrors
{
public static Error NotFound(int eventId) =>
new("Event.NotFound", $"Event {eventId} was not found.", ErrorType.NotFound);

public static Error SoldOut(int ticketsLeft, int requested) =>
new("Event.SoldOut", $"Only {ticketsLeft} tickets left, {requested} requested.", ErrorType.Conflict);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
namespace EventTicketing.Domain;

public enum ErrorType
{
NotFound,
Conflict
}

public sealed record Error(string Code, string Description, ErrorType Type);

public class Result
{
protected Result(Error? error) => Error = error;

public Error? Error { get; }
public bool IsSuccess => Error is null;

public static Result Success() => new(null);

public static implicit operator Result(Error error) => new(error);
}

public sealed class Result<TValue> : Result
{
private readonly TValue? _value;

private Result(TValue? value, Error? error)
: base(error) => _value = value;

public TValue Value => IsSuccess
? _value!
: throw new InvalidOperationException("A failed result has no value.");

public static implicit operator Result<TValue>(TValue value) =>
new(value, null);

public static implicit operator Result<TValue>(Error error) =>
new(default, error);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="10.0.12" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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

###

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

{ "quantity": 2 }

###

GET http://localhost:5000/api/events
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using EventTicketing.Data;
using EventTicketing.Domain;
using Microsoft.EntityFrameworkCore;

namespace EventTicketing.Features.Events;

public static class GetEventAvailability
{
public sealed record Response(int EventId, string Name, int TicketsLeft);

public sealed class Handler(TicketingDbContext dbContext)
{
public async Task<Result<Response>> HandleAsync(
int eventId, CancellationToken cancellationToken = default)
{
var availability = await dbContext.Events
.AsNoTracking()
.Where(e => e.Id == eventId)
.Select(e => new Response(e.Id, e.Name, e.Capacity - e.TicketsSold))
.FirstOrDefaultAsync(cancellationToken);

if (availability is null)
return EventErrors.NotFound(eventId);

return availability;
}
}

public static void MapEndpoint(IEndpointRouteBuilder app) =>
app.MapGet("/{eventId:int}", async (
int eventId,
Handler handler,
CancellationToken cancellationToken) =>
{
var result = await handler.HandleAsync(eventId, cancellationToken);

return result.IsSuccess ? Results.Ok(result.Value) : result.ToProblem();
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using EventTicketing.Data;
using Microsoft.EntityFrameworkCore;

namespace EventTicketing.Features.Events;

public static class ListAvailableEvents
{
public sealed record AvailableEvent(int EventId, string Name, int TicketsLeft);

public static void MapEndpoint(IEndpointRouteBuilder app) =>
app.MapGet("/", async (TicketingDbContext dbContext, CancellationToken cancellationToken) =>
await dbContext.Events
.AsNoTracking()
.Where(e => e.TicketsSold < e.Capacity)
.Select(e => new AvailableEvent(e.Id, e.Name, e.Capacity - e.TicketsSold))
.ToListAsync(cancellationToken));
}
Loading
Loading