Skip to content

Commit

Permalink
update code to latest style
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Hathcock committed Mar 4, 2024
1 parent 75ac25c commit 1a46b75
Show file tree
Hide file tree
Showing 53 changed files with 331 additions and 723 deletions.
10 changes: 2 additions & 8 deletions build/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@
ForEach("publish", "**/bin", "**/obj"),
dir =>
{
IEnumerable<string> GetDirectories(string d)
{
return Glob.Directories(".", d);
}
IEnumerable<string> GetDirectories(string d) => Glob.Directories(".", d);

void RemoveDirectory(string d)
{
Expand Down Expand Up @@ -53,10 +50,7 @@ void RemoveDirectory(string d)
DependsOn(Build),
() =>
{
IEnumerable<string> GetFiles(string d)
{
return Glob.Files(".", d);
}
IEnumerable<string> GetFiles(string d) => Glob.Files(".", d);

foreach (var file in GetFiles("tests/**/*.csproj"))
{
Expand Down
16 changes: 8 additions & 8 deletions src/Conduit/Domain/Article.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Conduit.Domain;
public class Article
{
[JsonIgnore]
public int ArticleId { get; set; }
public int ArticleId { get; init; }

public string? Slug { get; set; }

Expand All @@ -19,12 +19,12 @@ public class Article

public string? Body { get; set; }

public Person? Author { get; set; }
public Person? Author { get; init; }

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

[NotMapped]
public bool Favorited => ArticleFavorites?.Any() ?? false;
public bool Favorited => ArticleFavorites.Count != 0;

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

[JsonIgnore]
public List<ArticleTag> ArticleTags { get; set; } = new();
public List<ArticleTag> ArticleTags { get; init; } = new();

[JsonIgnore]
public List<ArticleFavorite> ArticleFavorites { get; set; } = new();
public List<ArticleFavorite> ArticleFavorites { get; init; } = new();

public DateTime CreatedAt { get; set; }
public DateTime CreatedAt { get; init; }

public DateTime UpdatedAt { get; set; }
}
}
8 changes: 4 additions & 4 deletions src/Conduit/Domain/ArticleFavorite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ namespace Conduit.Domain;

public class ArticleFavorite
{
public int ArticleId { get; set; }
public Article? Article { get; set; }
public int ArticleId { get; init; }
public Article? Article { get; init; }

public int PersonId { get; set; }
public Person? Person { get; set; }
public int PersonId { get; init; }
public Person? Person { get; init; }
}
8 changes: 4 additions & 4 deletions src/Conduit/Domain/ArticleTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ namespace Conduit.Domain;

public class ArticleTag
{
public int ArticleId { get; set; }
public Article? Article { get; set; }
public int ArticleId { get; init; }
public Article? Article { get; init; }

public string? TagId { get; set; }
public Tag? Tag { get; set; }
public string? TagId { get; init; }
public Tag? Tag { get; init; }
}
16 changes: 8 additions & 8 deletions src/Conduit/Domain/Comment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ namespace Conduit.Domain;
public class Comment
{
[JsonPropertyName("id")]
public int CommentId { get; set; }
public int CommentId { get; init; }

public string? Body { get; set; }
public string? Body { get; init; }

public Person? Author { get; set; }
public Person? Author { get; init; }

[JsonIgnore]
public int AuthorId { get; set; }
public int AuthorId { get; init; }

[JsonIgnore]
public Article? Article { get; set; }
public Article? Article { get; init; }

[JsonIgnore]
public int ArticleId { get; set; }
public int ArticleId { get; init; }

public DateTime CreatedAt { get; set; }
public DateTime CreatedAt { get; init; }

public DateTime UpdatedAt { get; set; }
public DateTime UpdatedAt { get; init; }
}
8 changes: 4 additions & 4 deletions src/Conduit/Domain/FollowedPeople.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ namespace Conduit.Domain;

public class FollowedPeople
{
public int ObserverId { get; set; }
public Person? Observer { get; set; }
public int ObserverId { get; init; }
public Person? Observer { get; init; }

public int TargetId { get; set; }
public Person? Target { get; set; }
public int TargetId { get; init; }
public Person? Target { get; init; }
}
14 changes: 7 additions & 7 deletions src/Conduit/Domain/Person.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Conduit.Domain;
public class Person
{
[JsonIgnore]
public int PersonId { get; set; }
public int PersonId { get; init; }

public string? Username { get; set; }

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

[JsonIgnore]
public List<ArticleFavorite> ArticleFavorites { get; set; } = new();
public List<ArticleFavorite> ArticleFavorites { get; init; } = new();

[JsonIgnore]
public List<FollowedPeople> Following { get; set; } = new();
public List<FollowedPeople> Following { get; init; } = new();

[JsonIgnore]
public List<FollowedPeople> Followers { get; set; } = new();
public List<FollowedPeople> Followers { get; init; } = new();

[JsonIgnore]
public byte[] Hash { get; set; } = Array.Empty<byte>();
public byte[] Hash { get; set; } = [];

[JsonIgnore]
public byte[] Salt { get; set; } = Array.Empty<byte>();
}
public byte[] Salt { get; set; } = [];
}
4 changes: 2 additions & 2 deletions src/Conduit/Domain/Tag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Conduit.Domain;

public class Tag
{
public string? TagId { get; set; }
public string? TagId { get; init; }

public List<ArticleTag> ArticleTags { get; set; } = new();
public List<ArticleTag> ArticleTags { get; init; } = new();
}
48 changes: 13 additions & 35 deletions src/Conduit/Features/Articles/ArticlesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,8 @@
namespace Conduit.Features.Articles;

[Route("articles")]
public class ArticlesController : Controller
public class ArticlesController(IMediator mediator) : Controller
{
private readonly IMediator _mediator;

public ArticlesController(IMediator mediator)
{
_mediator = mediator;
}

[HttpGet]
public Task<ArticlesEnvelope> Get(
[FromQuery] string tag,
Expand All @@ -25,13 +18,11 @@ public Task<ArticlesEnvelope> Get(
[FromQuery] int? limit,
[FromQuery] int? offset,
CancellationToken cancellationToken
)
{
return _mediator.Send(
) =>
mediator.Send(
new List.Query(tag, author, favorited, limit, offset),
cancellationToken
);
}

[HttpGet("feed")]
public Task<ArticlesEnvelope> GetFeed(
Expand All @@ -41,44 +32,31 @@ public Task<ArticlesEnvelope> GetFeed(
[FromQuery] int? limit,
[FromQuery] int? offset,
CancellationToken cancellationToken
)
{
return _mediator.Send(
new List.Query(tag, author, favorited, limit, offset) { IsFeed = true }
);
}
) =>
mediator.Send(
new List.Query(tag, author, favorited, limit, offset) { IsFeed = true }, cancellationToken);

[HttpGet("{slug}")]
public Task<ArticleEnvelope> Get(string slug, CancellationToken cancellationToken)
{
return _mediator.Send(new Details.Query(slug), cancellationToken);
}
public Task<ArticleEnvelope> Get(string slug, CancellationToken cancellationToken) => mediator.Send(new Details.Query(slug), cancellationToken);

[HttpPost]
[Authorize(AuthenticationSchemes = JwtIssuerOptions.Schemes)]
public Task<ArticleEnvelope> Create(
[FromBody] Create.Command command,
CancellationToken cancellationToken
)
{
return _mediator.Send(command, cancellationToken);
}
) =>
mediator.Send(command, cancellationToken);

[HttpPut("{slug}")]
[Authorize(AuthenticationSchemes = JwtIssuerOptions.Schemes)]
public Task<ArticleEnvelope> Edit(
string slug,
[FromBody] Edit.Model model,
CancellationToken cancellationToken
)
{
return _mediator.Send(new Edit.Command(model, slug), cancellationToken);
}
) =>
mediator.Send(new Edit.Command(model, slug), cancellationToken);

[HttpDelete("{slug}")]
[Authorize(AuthenticationSchemes = JwtIssuerOptions.Schemes)]
public Task Delete(string slug, CancellationToken cancellationToken)
{
return _mediator.Send(new Delete.Command(slug), cancellationToken);
}
}
public Task Delete(string slug, CancellationToken cancellationToken) => mediator.Send(new Delete.Command(slug), cancellationToken);
}
43 changes: 16 additions & 27 deletions src/Conduit/Features/Articles/Create.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ public class Create
{
public class ArticleData
{
public string? Title { get; set; }
public string? Title { get; init; }

public string? Description { get; set; }
public string? Description { get; init; }

public string? Body { get; set; }
public string? Body { get; init; }

public string[]? TagList { get; set; }
public string[]? TagList { get; init; }
}

public class ArticleDataValidator : AbstractValidator<ArticleData>
Expand All @@ -38,42 +38,31 @@ public record Command(ArticleData Article) : IRequest<ArticleEnvelope>;

public class CommandValidator : AbstractValidator<Command>
{
public CommandValidator()
{
RuleFor(x => x.Article).NotNull().SetValidator(new ArticleDataValidator());
}
public CommandValidator() => RuleFor(x => x.Article).NotNull().SetValidator(new ArticleDataValidator());
}

public class Handler : IRequestHandler<Command, ArticleEnvelope>
public class Handler(ConduitContext context, ICurrentUserAccessor currentUserAccessor)
: IRequestHandler<Command, ArticleEnvelope>
{
private readonly ConduitContext _context;
private readonly ICurrentUserAccessor _currentUserAccessor;

public Handler(ConduitContext context, ICurrentUserAccessor currentUserAccessor)
{
_context = context;
_currentUserAccessor = currentUserAccessor;
}

public async Task<ArticleEnvelope> Handle(
Command message,
CancellationToken cancellationToken
)
{
var author = await _context.Persons.FirstAsync(
x => x.Username == _currentUserAccessor.GetCurrentUsername(),
var author = await context.Persons.FirstAsync(
x => x.Username == currentUserAccessor.GetCurrentUsername(),
cancellationToken
);
var tags = new List<Tag>();
foreach (var tag in (message.Article.TagList ?? Enumerable.Empty<string>()))
{
var t = await _context.Tags.FindAsync(tag);
var t = await context.Tags.FindAsync(tag);
if (t == null)
{
t = new Tag() { TagId = tag };
await _context.Tags.AddAsync(t, cancellationToken);
await context.Tags.AddAsync(t, cancellationToken);
//save immediately for reuse
await _context.SaveChangesAsync(cancellationToken);
await context.SaveChangesAsync(cancellationToken);
}
tags.Add(t);
}
Expand All @@ -88,16 +77,16 @@ CancellationToken cancellationToken
Title = message.Article.Title,
Slug = message.Article.Title.GenerateSlug()
};
await _context.Articles.AddAsync(article, cancellationToken);
await context.Articles.AddAsync(article, cancellationToken);

await _context.ArticleTags.AddRangeAsync(
await context.ArticleTags.AddRangeAsync(
tags.Select(x => new ArticleTag() { Article = article, Tag = x }),
cancellationToken
);

await _context.SaveChangesAsync(cancellationToken);
await context.SaveChangesAsync(cancellationToken);

return new ArticleEnvelope(article);
}
}
}
}
Loading

0 comments on commit 1a46b75

Please sign in to comment.