@@ -15,13 +15,13 @@ public class Create
1515{
1616 public class ArticleData
1717 {
18- public string ? Title { get ; set ; }
18+ public string ? Title { get ; init ; }
1919
20- public string ? Description { get ; set ; }
20+ public string ? Description { get ; init ; }
2121
22- public string ? Body { get ; set ; }
22+ public string ? Body { get ; init ; }
2323
24- public string [ ] ? TagList { get ; set ; }
24+ public string [ ] ? TagList { get ; init ; }
2525 }
2626
2727 public class ArticleDataValidator : AbstractValidator < ArticleData >
@@ -38,42 +38,31 @@ public record Command(ArticleData Article) : IRequest<ArticleEnvelope>;
3838
3939 public class CommandValidator : AbstractValidator < Command >
4040 {
41- public CommandValidator ( )
42- {
43- RuleFor ( x => x . Article ) . NotNull ( ) . SetValidator ( new ArticleDataValidator ( ) ) ;
44- }
41+ public CommandValidator ( ) => RuleFor ( x => x . Article ) . NotNull ( ) . SetValidator ( new ArticleDataValidator ( ) ) ;
4542 }
4643
47- public class Handler : IRequestHandler < Command , ArticleEnvelope >
44+ public class Handler ( ConduitContext context , ICurrentUserAccessor currentUserAccessor )
45+ : IRequestHandler < Command , ArticleEnvelope >
4846 {
49- private readonly ConduitContext _context ;
50- private readonly ICurrentUserAccessor _currentUserAccessor ;
51-
52- public Handler ( ConduitContext context , ICurrentUserAccessor currentUserAccessor )
53- {
54- _context = context ;
55- _currentUserAccessor = currentUserAccessor ;
56- }
57-
5847 public async Task < ArticleEnvelope > Handle (
5948 Command message ,
6049 CancellationToken cancellationToken
6150 )
6251 {
63- var author = await _context . Persons . FirstAsync (
64- x => x . Username == _currentUserAccessor . GetCurrentUsername ( ) ,
52+ var author = await context . Persons . FirstAsync (
53+ x => x . Username == currentUserAccessor . GetCurrentUsername ( ) ,
6554 cancellationToken
6655 ) ;
6756 var tags = new List < Tag > ( ) ;
6857 foreach ( var tag in ( message . Article . TagList ?? Enumerable . Empty < string > ( ) ) )
6958 {
70- var t = await _context . Tags . FindAsync ( tag ) ;
59+ var t = await context . Tags . FindAsync ( tag ) ;
7160 if ( t == null )
7261 {
7362 t = new Tag ( ) { TagId = tag } ;
74- await _context . Tags . AddAsync ( t , cancellationToken ) ;
63+ await context . Tags . AddAsync ( t , cancellationToken ) ;
7564 //save immediately for reuse
76- await _context . SaveChangesAsync ( cancellationToken ) ;
65+ await context . SaveChangesAsync ( cancellationToken ) ;
7766 }
7867 tags . Add ( t ) ;
7968 }
@@ -88,16 +77,16 @@ CancellationToken cancellationToken
8877 Title = message . Article . Title ,
8978 Slug = message . Article . Title . GenerateSlug ( )
9079 } ;
91- await _context . Articles . AddAsync ( article , cancellationToken ) ;
80+ await context . Articles . AddAsync ( article , cancellationToken ) ;
9281
93- await _context . ArticleTags . AddRangeAsync (
82+ await context . ArticleTags . AddRangeAsync (
9483 tags . Select ( x => new ArticleTag ( ) { Article = article , Tag = x } ) ,
9584 cancellationToken
9685 ) ;
9786
98- await _context . SaveChangesAsync ( cancellationToken ) ;
87+ await context . SaveChangesAsync ( cancellationToken ) ;
9988
10089 return new ArticleEnvelope ( article ) ;
10190 }
10291 }
103- }
92+ }
0 commit comments