summaryrefslogtreecommitdiff
path: root/Services
diff options
context:
space:
mode:
authorJake Mannens <jake@asger.xyz>2023-09-05 01:01:24 +1000
committerJake Mannens <jake@asger.xyz>2025-08-20 00:48:45 +1000
commit988f68191bbd93ce225205ae2a0ebcdf9df45655 (patch)
treee4e173ffc226b879dacd65efd85ecfee0529e644 /Services
parentac7f9cca27f5a1a1839bfe5be5a53853f53b7b17 (diff)
Tag names and aliases are now verified to be unique
Diffstat (limited to 'Services')
-rw-r--r--Services/TagService.cs45
1 files changed, 42 insertions, 3 deletions
diff --git a/Services/TagService.cs b/Services/TagService.cs
index 2648bb3..56aba04 100644
--- a/Services/TagService.cs
+++ b/Services/TagService.cs
@@ -126,14 +126,34 @@ public class TagService : ITagService {
public void CreateTagDefinition(string name, string? @namespace = null, string? alias = null) {
using var db = dbFactory.CreateDbContext();
- TagDefinition tagdef = new() {
+ if(string.IsNullOrEmpty(@namespace))
+ @namespace = null;
+ if(string.IsNullOrEmpty(alias))
+ alias = null;
+
+ // Remove leading and trailing whitespace
+ name = name.Trim();
+ @namespace = @namespace?.Trim();
+ alias = alias?.Trim();
+
+ TagDefinition tagDef = new() {
Source = TagSource.UserTag,
Namespace = @namespace,
Name = name,
Alias = alias
};
- if(!db.TagDefinitions.Contains(tagdef))
- db.TagDefinitions.Add(tagdef);
+
+ bool nameExists = db.TagDefinitions.Any(td => td.Name.ToLower() == name.ToLower());
+ bool aliasExists = false;
+ if(alias is not null)
+ aliasExists = db.TagDefinitions
+ .Where(td => td.Alias != null)
+ .Any(td => td.Alias!.ToLower() == alias.ToLower());
+ if(nameExists || aliasExists)
+ throw new TagDuplicateException(nameExists, aliasExists);
+
+ if(!db.TagDefinitions.Contains(tagDef))
+ db.TagDefinitions.Add(tagDef);
db.SaveChanges();
}
@@ -162,8 +182,27 @@ public class TagService : ITagService {
if(string.IsNullOrEmpty(@namespace))
@namespace = null;
+ if(string.IsNullOrEmpty(alias))
+ alias = null;
+
+ // Remove leading and trailing whitespace
+ name = name.Trim();
+ @namespace = @namespace?.Trim();
+ alias = alias?.Trim();
var tag = db.TagDefinitions.First(td => td.Guid == tagDef);
+
+ TagDefinition? nameExisting = db.TagDefinitions.FirstOrDefault(td => td.Name.ToLower() == name.ToLower());
+ TagDefinition? aliasExisting = null;
+ if(alias is not null)
+ aliasExisting = db.TagDefinitions
+ .Where(td => td.Alias != null)
+ .FirstOrDefault(td => td.Alias!.ToLower() == alias.ToLower());
+ bool nameExists = nameExisting is not null && nameExisting != tag;
+ bool aliasExists = aliasExisting is not null && aliasExisting != tag;
+ if(nameExists || aliasExists)
+ throw new TagDuplicateException(nameExists, aliasExists);
+
tag.Name = name;
tag.Namespace = @namespace;
tag.Alias = alias;