summaryrefslogtreecommitdiff
path: root/TagController.cs
diff options
context:
space:
mode:
Diffstat (limited to 'TagController.cs')
-rw-r--r--TagController.cs89
1 files changed, 89 insertions, 0 deletions
diff --git a/TagController.cs b/TagController.cs
new file mode 100644
index 0000000..b97b066
--- /dev/null
+++ b/TagController.cs
@@ -0,0 +1,89 @@
+using Microsoft.AspNetCore.Mvc;
+
+namespace HyperBooru;
+
+[ApiController]
+[Route("tag")]
+public class TagController : Controller {
+ private HyperBooruDbContext db;
+
+ public TagController(HyperBooruDbContext db) =>
+ this.db = db;
+
+ [HttpPost("{objectId}/{tagId}")]
+ public IActionResult AddTag([FromRoute] Guid objectId, [FromRoute] Guid tagId) {
+ var obj = db.Objects.First(o => o.Guid == objectId);
+ var def = db.TagDefinitions.First(d => d.Guid == tagId);
+
+ if(obj is null || def is null)
+ return NotFound();
+
+ bool alreadyTagged = obj.Tags
+ .Select(t => t.TagDefinition.Guid)
+ .Contains(def.Guid);
+
+ if(alreadyTagged)
+ return BadRequest();
+
+ obj.Tags.Add(new() {
+ TagDefinition = def,
+ Target = obj
+ });
+
+ db.SaveChanges();
+
+ return Ok();
+ }
+
+ [HttpDelete("{objectId}/{tagId}")]
+ public IActionResult RemoveTag([FromRoute] Guid objectId, [FromRoute] Guid tagId) {
+ var obj = db.Objects.First(o => o.Guid == objectId);
+
+ if(obj is null)
+ return NotFound();
+
+ var tag = obj.Tags
+ .First(t => t.Guid == tagId || t.TagDefinition.Guid == tagId);
+
+ if(tag is null)
+ return NotFound();
+
+ obj.Tags.Remove(tag);
+ db.SaveChanges();
+
+ return Ok();
+ }
+
+ [HttpPost("def")]
+ public void CreateTagDefinition(
+ [FromForm] string name,
+ [FromForm] string? @namespace) {
+
+ DbTagDefinition tagdef = new() {
+ Source = TagSource.UserTag,
+ Namespace = @namespace,
+ Name = name
+ };
+ if(!db.TagDefinitions.Contains(tagdef))
+ db.TagDefinitions.Add(tagdef);
+ db.SaveChanges();
+ }
+
+ [HttpDelete("def/{tagId}")]
+ public IActionResult DeleteTagDefinition([FromRoute] Guid tagId) {
+ var tagdef = db.TagDefinitions.First(td => td.Guid == tagId);
+ if(tagdef is null)
+ return NotFound();
+
+ using var transaction = db.Database.BeginTransaction();
+
+ db.Tags.RemoveRange(
+ db.Tags.Where(t => t.TagDefinition == tagdef));
+ db.TagDefinitions.Remove(tagdef);
+ db.SaveChanges();
+
+ transaction.Commit();
+
+ return Ok();
+ }
+} \ No newline at end of file