blob: 5c9fa39a6901739cbffab5a90670c0e54e9220aa (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
using Microsoft.AspNetCore.Mvc;
namespace HyperBooru;
[ApiController]
[Route("/api/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();
}
}
|