Skip to content

Commit 1a46b75

Browse files
author
Adam Hathcock
committed
update code to latest style
1 parent 75ac25c commit 1a46b75

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+331
-723
lines changed

build/Program.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@
1616
ForEach("publish", "**/bin", "**/obj"),
1717
dir =>
1818
{
19-
IEnumerable<string> GetDirectories(string d)
20-
{
21-
return Glob.Directories(".", d);
22-
}
19+
IEnumerable<string> GetDirectories(string d) => Glob.Directories(".", d);
2320

2421
void RemoveDirectory(string d)
2522
{
@@ -53,10 +50,7 @@ void RemoveDirectory(string d)
5350
DependsOn(Build),
5451
() =>
5552
{
56-
IEnumerable<string> GetFiles(string d)
57-
{
58-
return Glob.Files(".", d);
59-
}
53+
IEnumerable<string> GetFiles(string d) => Glob.Files(".", d);
6054

6155
foreach (var file in GetFiles("tests/**/*.csproj"))
6256
{

src/Conduit/Domain/Article.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Conduit.Domain;
99
public class Article
1010
{
1111
[JsonIgnore]
12-
public int ArticleId { get; set; }
12+
public int ArticleId { get; init; }
1313

1414
public string? Slug { get; set; }
1515

@@ -19,12 +19,12 @@ public class Article
1919

2020
public string? Body { get; set; }
2121

22-
public Person? Author { get; set; }
22+
public Person? Author { get; init; }
2323

24-
public List<Comment> Comments { get; set; } = new();
24+
public List<Comment> Comments { get; init; } = new();
2525

2626
[NotMapped]
27-
public bool Favorited => ArticleFavorites?.Any() ?? false;
27+
public bool Favorited => ArticleFavorites.Count != 0;
2828

2929
[NotMapped]
3030
public int FavoritesCount => ArticleFavorites?.Count ?? 0;
@@ -34,12 +34,12 @@ public class Article
3434
ArticleTags.Where(x => x.TagId is not null).Select(x => x.TagId!).ToList();
3535

3636
[JsonIgnore]
37-
public List<ArticleTag> ArticleTags { get; set; } = new();
37+
public List<ArticleTag> ArticleTags { get; init; } = new();
3838

3939
[JsonIgnore]
40-
public List<ArticleFavorite> ArticleFavorites { get; set; } = new();
40+
public List<ArticleFavorite> ArticleFavorites { get; init; } = new();
4141

42-
public DateTime CreatedAt { get; set; }
42+
public DateTime CreatedAt { get; init; }
4343

4444
public DateTime UpdatedAt { get; set; }
45-
}
45+
}

src/Conduit/Domain/ArticleFavorite.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ namespace Conduit.Domain;
22

33
public class ArticleFavorite
44
{
5-
public int ArticleId { get; set; }
6-
public Article? Article { get; set; }
5+
public int ArticleId { get; init; }
6+
public Article? Article { get; init; }
77

8-
public int PersonId { get; set; }
9-
public Person? Person { get; set; }
8+
public int PersonId { get; init; }
9+
public Person? Person { get; init; }
1010
}

src/Conduit/Domain/ArticleTag.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ namespace Conduit.Domain;
22

33
public class ArticleTag
44
{
5-
public int ArticleId { get; set; }
6-
public Article? Article { get; set; }
5+
public int ArticleId { get; init; }
6+
public Article? Article { get; init; }
77

8-
public string? TagId { get; set; }
9-
public Tag? Tag { get; set; }
8+
public string? TagId { get; init; }
9+
public Tag? Tag { get; init; }
1010
}

src/Conduit/Domain/Comment.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,22 @@ namespace Conduit.Domain;
66
public class Comment
77
{
88
[JsonPropertyName("id")]
9-
public int CommentId { get; set; }
9+
public int CommentId { get; init; }
1010

11-
public string? Body { get; set; }
11+
public string? Body { get; init; }
1212

13-
public Person? Author { get; set; }
13+
public Person? Author { get; init; }
1414

1515
[JsonIgnore]
16-
public int AuthorId { get; set; }
16+
public int AuthorId { get; init; }
1717

1818
[JsonIgnore]
19-
public Article? Article { get; set; }
19+
public Article? Article { get; init; }
2020

2121
[JsonIgnore]
22-
public int ArticleId { get; set; }
22+
public int ArticleId { get; init; }
2323

24-
public DateTime CreatedAt { get; set; }
24+
public DateTime CreatedAt { get; init; }
2525

26-
public DateTime UpdatedAt { get; set; }
26+
public DateTime UpdatedAt { get; init; }
2727
}

src/Conduit/Domain/FollowedPeople.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ namespace Conduit.Domain;
22

33
public class FollowedPeople
44
{
5-
public int ObserverId { get; set; }
6-
public Person? Observer { get; set; }
5+
public int ObserverId { get; init; }
6+
public Person? Observer { get; init; }
77

8-
public int TargetId { get; set; }
9-
public Person? Target { get; set; }
8+
public int TargetId { get; init; }
9+
public Person? Target { get; init; }
1010
}

src/Conduit/Domain/Person.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Conduit.Domain;
77
public class Person
88
{
99
[JsonIgnore]
10-
public int PersonId { get; set; }
10+
public int PersonId { get; init; }
1111

1212
public string? Username { get; set; }
1313

@@ -18,17 +18,17 @@ public class Person
1818
public string? Image { get; set; }
1919

2020
[JsonIgnore]
21-
public List<ArticleFavorite> ArticleFavorites { get; set; } = new();
21+
public List<ArticleFavorite> ArticleFavorites { get; init; } = new();
2222

2323
[JsonIgnore]
24-
public List<FollowedPeople> Following { get; set; } = new();
24+
public List<FollowedPeople> Following { get; init; } = new();
2525

2626
[JsonIgnore]
27-
public List<FollowedPeople> Followers { get; set; } = new();
27+
public List<FollowedPeople> Followers { get; init; } = new();
2828

2929
[JsonIgnore]
30-
public byte[] Hash { get; set; } = Array.Empty<byte>();
30+
public byte[] Hash { get; set; } = [];
3131

3232
[JsonIgnore]
33-
public byte[] Salt { get; set; } = Array.Empty<byte>();
34-
}
33+
public byte[] Salt { get; set; } = [];
34+
}

src/Conduit/Domain/Tag.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace Conduit.Domain;
44

55
public class Tag
66
{
7-
public string? TagId { get; set; }
7+
public string? TagId { get; init; }
88

9-
public List<ArticleTag> ArticleTags { get; set; } = new();
9+
public List<ArticleTag> ArticleTags { get; init; } = new();
1010
}

src/Conduit/Features/Articles/ArticlesController.cs

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,8 @@
88
namespace Conduit.Features.Articles;
99

1010
[Route("articles")]
11-
public class ArticlesController : Controller
11+
public class ArticlesController(IMediator mediator) : Controller
1212
{
13-
private readonly IMediator _mediator;
14-
15-
public ArticlesController(IMediator mediator)
16-
{
17-
_mediator = mediator;
18-
}
19-
2013
[HttpGet]
2114
public Task<ArticlesEnvelope> Get(
2215
[FromQuery] string tag,
@@ -25,13 +18,11 @@ public Task<ArticlesEnvelope> Get(
2518
[FromQuery] int? limit,
2619
[FromQuery] int? offset,
2720
CancellationToken cancellationToken
28-
)
29-
{
30-
return _mediator.Send(
21+
) =>
22+
mediator.Send(
3123
new List.Query(tag, author, favorited, limit, offset),
3224
cancellationToken
3325
);
34-
}
3526

3627
[HttpGet("feed")]
3728
public Task<ArticlesEnvelope> GetFeed(
@@ -41,44 +32,31 @@ public Task<ArticlesEnvelope> GetFeed(
4132
[FromQuery] int? limit,
4233
[FromQuery] int? offset,
4334
CancellationToken cancellationToken
44-
)
45-
{
46-
return _mediator.Send(
47-
new List.Query(tag, author, favorited, limit, offset) { IsFeed = true }
48-
);
49-
}
35+
) =>
36+
mediator.Send(
37+
new List.Query(tag, author, favorited, limit, offset) { IsFeed = true }, cancellationToken);
5038

5139
[HttpGet("{slug}")]
52-
public Task<ArticleEnvelope> Get(string slug, CancellationToken cancellationToken)
53-
{
54-
return _mediator.Send(new Details.Query(slug), cancellationToken);
55-
}
40+
public Task<ArticleEnvelope> Get(string slug, CancellationToken cancellationToken) => mediator.Send(new Details.Query(slug), cancellationToken);
5641

5742
[HttpPost]
5843
[Authorize(AuthenticationSchemes = JwtIssuerOptions.Schemes)]
5944
public Task<ArticleEnvelope> Create(
6045
[FromBody] Create.Command command,
6146
CancellationToken cancellationToken
62-
)
63-
{
64-
return _mediator.Send(command, cancellationToken);
65-
}
47+
) =>
48+
mediator.Send(command, cancellationToken);
6649

6750
[HttpPut("{slug}")]
6851
[Authorize(AuthenticationSchemes = JwtIssuerOptions.Schemes)]
6952
public Task<ArticleEnvelope> Edit(
7053
string slug,
7154
[FromBody] Edit.Model model,
7255
CancellationToken cancellationToken
73-
)
74-
{
75-
return _mediator.Send(new Edit.Command(model, slug), cancellationToken);
76-
}
56+
) =>
57+
mediator.Send(new Edit.Command(model, slug), cancellationToken);
7758

7859
[HttpDelete("{slug}")]
7960
[Authorize(AuthenticationSchemes = JwtIssuerOptions.Schemes)]
80-
public Task Delete(string slug, CancellationToken cancellationToken)
81-
{
82-
return _mediator.Send(new Delete.Command(slug), cancellationToken);
83-
}
84-
}
61+
public Task Delete(string slug, CancellationToken cancellationToken) => mediator.Send(new Delete.Command(slug), cancellationToken);
62+
}

src/Conduit/Features/Articles/Create.cs

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)