summaryrefslogtreecommitdiff
path: root/Controllers/ApiTagController.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Controllers/ApiTagController.cs')
-rw-r--r--Controllers/ApiTagController.cs81
1 files changed, 42 insertions, 39 deletions
diff --git a/Controllers/ApiTagController.cs b/Controllers/ApiTagController.cs
index f48cc05..d1e49ee 100644
--- a/Controllers/ApiTagController.cs
+++ b/Controllers/ApiTagController.cs
@@ -34,7 +34,10 @@ public class ApiTagController : Controller {
.Include(td => td.ImplicitTags)
.FirstOrDefaultAsync(td => td.Guid == tagDefinitionId);
- return tagDefinition is not null ? Ok(tagDefinition) : NotFound();
+ if(tagDefinition is null)
+ throw new ObjectNotFoundException([ tagDefinitionId ]);
+
+ return Ok(tagDefinition);
}
[HttpPost("definition")]
@@ -42,12 +45,12 @@ public class ApiTagController : Controller {
using var db = dbFactory.CreateDbContext();
using var transaction = await db.Database.BeginTransactionAsync();
- if(db.TagDefinitions.Any(td => td.Name == request.Name))
- return BadRequest("Name already exists");
+ var nameExists = db.TagDefinitions.Any(td => td.Name == request.Name);
+ var aliasExists =
+ request.Alias is not null && db.TagDefinitions.Any(td => td.Alias == request.Alias);
- if(request.Alias is not null)
- if(db.TagDefinitions.Any(td => td.Alias == request.Alias))
- return BadRequest("Alias already exists");
+ if(nameExists || aliasExists)
+ throw new TagDuplicateException(nameExists, aliasExists);
List<TagDefinition> implicitTags = new();
if(request.ImplicitTags is not null) {
@@ -81,10 +84,10 @@ public class ApiTagController : Controller {
.FirstOrDefaultAsync(td => td.Guid == tagDefinitionId);
if(tagDefinition is null)
- return NotFound("Tag definition not found");
+ throw new ObjectNotFoundException([ tagDefinitionId ]);
if(tagDefinition.ObjectId < 0)
- return BadRequest("Cannot delete built-in tag definition");
+ throw new ApiModels.ArgumentException("Cannot delete built-in tag definition");
db.TagDefinitions.Remove(tagDefinition);
@@ -106,18 +109,18 @@ public class ApiTagController : Controller {
.FirstOrDefaultAsync(td => td.Guid == tagDefinitionId);
if(tagDefinition is null)
- return NotFound("Tag definition not found");
+ throw new ObjectNotFoundException([ tagDefinitionId ]);
if(tagDefinition.ObjectId < 0)
- return BadRequest("Cannot update built-in tag definition");
+ throw new ApiModels.ArgumentException("Cannot update built-in tag definition");
- if(request.Name is not null)
- if(db.TagDefinitions.Any(td => td.Name == request.Name))
- return BadRequest("Name already exists");
+ var nameExists =
+ request.Name is not null && db.TagDefinitions.Any(td => td.Name == request.Name);
+ var aliasExists =
+ request.Alias is not null && db.TagDefinitions.Any(td => td.Alias == request.Alias);
- if(request.Alias is not null)
- if(db.TagDefinitions.Any(td => td.Alias == request.Alias))
- return BadRequest("Alias already exists");
+ if(nameExists || aliasExists)
+ throw new TagDuplicateException(nameExists, aliasExists);
tagDefinition.Namespace = request.Namespace ?? tagDefinition.Namespace;
tagDefinition.Name = request.Name ?? tagDefinition.Name;
@@ -137,15 +140,14 @@ public class ApiTagController : Controller {
using var db = dbFactory.CreateDbContext();
using var transaction = await db.Database.BeginTransactionAsync();
+ var missing = new List<Guid>();
+
var tagDefinition = await db.TagDefinitions
.Include(td => td.ImplicitTags)
.FirstOrDefaultAsync(td => td.Guid == tagDefinitionId);
if(tagDefinition is null)
- return NotFound("Tag definition not found");
-
- if(tagDefinition.ObjectId < 0)
- return BadRequest("Cannot update built-in tag definition");
+ missing.Add(tagDefinitionId);
implicitTagIds = implicitTagIds.Distinct().ToArray();
@@ -153,10 +155,12 @@ public class ApiTagController : Controller {
.Where(td => implicitTagIds.Contains(td.Guid))
.ToArray();
- var missingTags = implicitTagIds.Except(implicitTags.Select(td => td.Guid));
- var missingTagsString = string.Join(", ", missingTags.Select(td => td.ToString()));
- if(missingTags.Any())
- return BadRequest($"Invalid tag IDs specified: {missingTagsString}");
+ missing.AddRange(implicitTagIds.Except(implicitTags.Select(td => td.Guid)));
+ if(missing.Any())
+ throw new ObjectNotFoundException(missing);
+
+ if(tagDefinition!.ObjectId < 0)
+ throw new ApiModels.ArgumentException("Cannot update built-in tag definition");
tagDefinition.ImplicitTags.AddRange(
implicitTags.ExceptBy(tagDefinition.ImplicitTags.Select(td => td.Guid), td => td.Guid));
@@ -175,15 +179,14 @@ public class ApiTagController : Controller {
using var db = dbFactory.CreateDbContext();
using var transaction = await db.Database.BeginTransactionAsync();
+ var missing = new List<Guid>();
+
var tagDefinition = await db.TagDefinitions
.Include(td => td.ImplicitTags)
.FirstOrDefaultAsync(td => td.Guid == tagDefinitionId);
if(tagDefinition is null)
- return NotFound("Tag definition not found");
-
- if(tagDefinition.ObjectId < 0)
- return BadRequest("Cannot update built-in tag definition");
+ missing.Add(tagDefinitionId);
implicitTagIds = implicitTagIds.Distinct().ToArray();
@@ -191,10 +194,12 @@ public class ApiTagController : Controller {
.Where(td => implicitTagIds.Contains(td.Guid))
.ToArray();
- var missingTags = implicitTagIds.Except(implicitTags.Select(td => td.Guid));
- var missingTagsString = string.Join(", ", missingTags.Select(td => td.ToString()));
- if(missingTags.Any())
- return BadRequest($"Invalid tag IDs specified: {missingTagsString}");
+ missing.AddRange(implicitTagIds.Except(implicitTags.Select(td => td.Guid)));
+ if(missing.Any())
+ throw new ObjectNotFoundException(missing);
+
+ if(tagDefinition!.ObjectId < 0)
+ throw new ApiModels.ArgumentException("Cannot update built-in tag definition");
tagDefinition.ImplicitTags.AddRange(
implicitTags.ExceptBy(tagDefinition.ImplicitTags.Select(td => td.Guid), td => td.Guid));
@@ -225,18 +230,16 @@ public class ApiTagController : Controller {
.FirstOrDefaultAsync(td => td.Guid == tagDefinitionId);
if(tagDefinition is null)
- return NotFound("Tag definition not found");
+ throw new ObjectNotFoundException([ tagDefinitionId ]);
if(tagDefinition.ObjectId < 0)
- return BadRequest("Cannot update built-in tag definition");
+ throw new ApiModels.ArgumentException("Cannot update built-in tag definition");
implicitTagIds = implicitTagIds.Distinct().ToArray();
- var missingTagIds = implicitTagIds
- .Except(tagDefinition.ImplicitTags.Select(td => td.Guid));
- var missingTagsString = string.Join(", ", missingTagIds.Select(td => td.ToString()));
- if(missingTagIds.Any())
- return BadRequest($"Invalid tag IDs specified: {missingTagsString}");
+ var missingTags = implicitTagIds.Except(tagDefinition.ImplicitTags.Select(td => td.Guid));
+ if(missingTags.Any())
+ throw new ObjectNotFoundException(missingTags);
var toRemove = tagDefinition.ImplicitTags
.Where(td => !implicitTagIds.Contains(td.Guid))