11using DistributedCache . Extensions ;
2- using DistributedCache . Options ;
32using FluentMinimalApiMapper ;
4- using Microsoft . AspNetCore . HttpLogging ;
3+ using FluentValidation ;
4+ using MediatR ;
55using Microsoft . AspNetCore . Mvc ;
66using SharedKernel . Demo2 ;
77using ResponseCrafter . Enums ;
4040 . AddOutboundLoggingHandler ( )
4141 . AddHealthChecks ( ) ;
4242
43+
4344builder . Services . ConfigureHttpJsonOptions ( options =>
4445{
4546 options . SerializerOptions . PropertyNamingPolicy = null ;
7172 . UseOpenApi ( )
7273 . MapControllers ( ) ;
7374
75+ app . MapPost ( "user" , async ( [ FromBody ] UserCommand user , ISender sender ) =>
76+ {
77+ await sender . Send ( user ) ;
78+ return Results . Ok ( ) ;
79+ } ) ;
7480
7581app . MapPost ( "/receive-file" , ( [ FromForm ] IFormFile file ) => TypedResults . Ok ( ) )
7682 . DisableAntiforgery ( ) ;
@@ -141,4 +147,38 @@ public enum AnimalType
141147 Cat ,
142148 Fish
143149 }
150+ }
151+
152+ public record UserCommand ( string Name , string Email ) : ICommand < string > ;
153+
154+ public class UserCommandHandler : ICommandHandler < UserCommand , string >
155+ {
156+ public Task < string > Handle ( UserCommand request , CancellationToken cancellationToken )
157+ {
158+ return Task . FromResult ( $ "User { request . Name } with email { request . Email } created successfully.") ;
159+ }
160+ }
161+
162+ public class User
163+ {
164+ public string Name { get ; set ; } = string . Empty ;
165+ public string Email { get ; set ; } = string . Empty ;
166+ }
167+
168+ public class UserValidator : AbstractValidator < UserCommand >
169+ {
170+ public UserValidator ( )
171+ {
172+ RuleFor ( x => x . Name )
173+ . NotEmpty ( )
174+ . WithMessage ( "Name is required." )
175+ . MaximumLength ( 100 )
176+ . WithMessage ( "Name cannot exceed 100 characters." ) ;
177+
178+ RuleFor ( x => x . Email )
179+ . NotEmpty ( )
180+ . WithMessage ( "Email is required." )
181+ . EmailAddress ( )
182+ . WithMessage ( "Invalid email format." ) ;
183+ }
144184}
0 commit comments