chore: add csharpier to husky, format backend with csharpier

This commit is contained in:
sam 2024-10-02 00:28:07 +02:00
parent 5fab66444f
commit 7f971e8549
Signed by: sam
GPG key ID: B4EF20DDE721CAA1
73 changed files with 2098 additions and 1048 deletions

View file

@ -18,13 +18,16 @@ public class FlagsController(
UserRendererService userRenderer,
ObjectStorageService objectStorageService,
ISnowflakeGenerator snowflakeGenerator,
IQueue queue) : ApiControllerBase
IQueue queue
) : ApiControllerBase
{
private readonly ILogger _logger = logger.ForContext<FlagsController>();
[HttpGet]
[Authorize("identify")]
[ProducesResponseType<IEnumerable<UserRendererService.PrideFlagResponse>>(statusCode: StatusCodes.Status200OK)]
[ProducesResponseType<IEnumerable<UserRendererService.PrideFlagResponse>>(
statusCode: StatusCodes.Status200OK
)]
public async Task<IActionResult> GetFlagsAsync(CancellationToken ct = default)
{
var flags = await db.PrideFlags.Where(f => f.UserId == CurrentUser!.Id).ToListAsync(ct);
@ -34,7 +37,9 @@ public class FlagsController(
[HttpPost]
[Authorize("user.update")]
[ProducesResponseType<UserRendererService.PrideFlagResponse>(statusCode: StatusCodes.Status202Accepted)]
[ProducesResponseType<UserRendererService.PrideFlagResponse>(
statusCode: StatusCodes.Status202Accepted
)]
public IActionResult CreateFlag([FromBody] CreateFlagRequest req)
{
ValidationUtils.Validate(ValidateFlag(req.Name, req.Description, req.Image));
@ -42,7 +47,8 @@ public class FlagsController(
var id = snowflakeGenerator.GenerateSnowflake();
queue.QueueInvocableWithPayload<CreateFlagInvocable, CreateFlagPayload>(
new CreateFlagPayload(id, CurrentUser!.Id, req.Name, req.Image, req.Description));
new CreateFlagPayload(id, CurrentUser!.Id, req.Name, req.Image, req.Description)
);
return Accepted(new CreateFlagResponse(id, req.Name, req.Description));
}
@ -57,10 +63,14 @@ public class FlagsController(
{
ValidationUtils.Validate(ValidateFlag(req.Name, req.Description, null));
var flag = await db.PrideFlags.FirstOrDefaultAsync(f => f.Id == id && f.UserId == CurrentUser!.Id);
if (flag == null) throw new ApiError.NotFound("Unknown flag ID, or it's not your flag.");
var flag = await db.PrideFlags.FirstOrDefaultAsync(f =>
f.Id == id && f.UserId == CurrentUser!.Id
);
if (flag == null)
throw new ApiError.NotFound("Unknown flag ID, or it's not your flag.");
if (req.Name != null) flag.Name = req.Name;
if (req.Name != null)
flag.Name = req.Name;
if (req.HasProperty(nameof(req.Description)))
flag.Description = req.Description;
@ -83,8 +93,11 @@ public class FlagsController(
{
await using var tx = await db.Database.BeginTransactionAsync();
var flag = await db.PrideFlags.FirstOrDefaultAsync(f => f.Id == id && f.UserId == CurrentUser!.Id);
if (flag == null) throw new ApiError.NotFound("Unknown flag ID, or it's not your flag.");
var flag = await db.PrideFlags.FirstOrDefaultAsync(f =>
f.Id == id && f.UserId == CurrentUser!.Id
);
if (flag == null)
throw new ApiError.NotFound("Unknown flag ID, or it's not your flag.");
var hash = flag.Hash;
@ -96,7 +109,10 @@ public class FlagsController(
{
try
{
_logger.Information("Deleting flag file {Hash} as it is no longer used by any flags", hash);
_logger.Information(
"Deleting flag file {Hash} as it is no longer used by any flags",
hash
);
await objectStorageService.DeleteFlagAsync(hash);
}
catch (Exception e)
@ -104,14 +120,19 @@ public class FlagsController(
_logger.Error(e, "Error deleting flag file {Hash}", hash);
}
}
else _logger.Debug("Flag file {Hash} is used by other flags, not deleting", hash);
else
_logger.Debug("Flag file {Hash} is used by other flags, not deleting", hash);
await tx.CommitAsync();
return NoContent();
}
private static List<(string, ValidationError?)> ValidateFlag(string? name, string? description, string? imageData)
private static List<(string, ValidationError?)> ValidateFlag(
string? name,
string? description,
string? imageData
)
{
var errors = new List<(string, ValidationError?)>();
@ -120,10 +141,20 @@ public class FlagsController(
switch (name.Length)
{
case < 1:
errors.Add(("name", ValidationError.LengthError("Name is too short", 1, 100, name.Length)));
errors.Add(
(
"name",
ValidationError.LengthError("Name is too short", 1, 100, name.Length)
)
);
break;
case > 100:
errors.Add(("name", ValidationError.LengthError("Name is too long", 1, 100, name.Length)));
errors.Add(
(
"name",
ValidationError.LengthError("Name is too long", 1, 100, name.Length)
)
);
break;
}
}
@ -133,12 +164,30 @@ public class FlagsController(
switch (description.Length)
{
case < 1:
errors.Add(("description",
ValidationError.LengthError("Description is too short", 1, 100, description.Length)));
errors.Add(
(
"description",
ValidationError.LengthError(
"Description is too short",
1,
100,
description.Length
)
)
);
break;
case > 500:
errors.Add(("description",
ValidationError.LengthError("Description is too long", 1, 100, description.Length)));
errors.Add(
(
"description",
ValidationError.LengthError(
"Description is too long",
1,
100,
description.Length
)
)
);
break;
}
}
@ -148,14 +197,24 @@ public class FlagsController(
switch (imageData.Length)
{
case 0:
errors.Add(("image", ValidationError.GenericValidationError("Image cannot be empty", null)));
errors.Add(
(
"image",
ValidationError.GenericValidationError("Image cannot be empty", null)
)
);
break;
case > 1_500_000:
errors.Add(("image", ValidationError.GenericValidationError("Image is too large", null)));
errors.Add(
(
"image",
ValidationError.GenericValidationError("Image is too large", null)
)
);
break;
}
}
return errors;
}
}
}