From acb74202f5391272c2e1823dfe04a044c7f7a9a7 Mon Sep 17 00:00:00 2001 From: Jake Mannens Date: Tue, 5 Sep 2023 01:01:24 +1000 Subject: Tag names and aliases are now verified to be unique --- Services/TagService.cs | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) (limited to 'Services') diff --git a/Services/TagService.cs b/Services/TagService.cs index 2648bb3..a3c4b29 100644 --- a/Services/TagService.cs +++ b/Services/TagService.cs @@ -126,14 +126,29 @@ 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; + + 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 +177,22 @@ public class TagService : ITagService { if(string.IsNullOrEmpty(@namespace)) @namespace = null; + if(string.IsNullOrEmpty(alias)) + alias = null; 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; -- cgit v1.3