-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKommentarController.cs
121 lines (97 loc) · 4.34 KB
/
KommentarController.cs
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using Microsoft.AspNetCore.Mvc;
using WSI.KommentarService.Data;
using WSI.KommentarService.Models;
using WSI.WortFilter;
using Microsoft.EntityFrameworkCore;
namespace WSI.KommentarService.Controllers;
[ApiController]
[Route("v1.0/Kommentar")]
public class KommentarController : ControllerBase {
private readonly IWortFilterService _wortFilterService;
private readonly DataContext _dataContext;
public KommentarController(IWortFilterService wortFilterService, DataContext dataContext) {
_wortFilterService = wortFilterService;
_dataContext = dataContext;
}
[HttpGet("{id}")]
[ProducesResponseType(typeof(KommentarModel), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
public async Task<ActionResult<KommentarModel>> Get(Guid id) {
var kommentar = await _dataContext.Kommentare
.Include(k => k.WeitereKommentare)!.ThenInclude(k => k.WeitereKommentare)!.ThenInclude(k => k.WeitereKommentare)
.FirstOrDefaultAsync(f => f.Id == id);
var kopf = kommentar?.KopfKommentar;
if (kommentar == null) {
return NoContent();
} else {
return Ok(kommentar);
}
}
[HttpPost]
[ProducesResponseType(typeof(KommentarModel), StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status409Conflict)]
public async Task<ActionResult<KommentarModel>> Post([FromBody] KommentarPOSTModel kommentar) {
var kommentarNeu = kommentar.ZuKommentar();
if (_dataContext.Kommentare.Any(f => f.Id == kommentarNeu.Id)) {
return Conflict();
}
if (kommentarNeu.ÜbergeordneterKommentarId != null) {
var übergeordnetKommentar = await _dataContext.Kommentare
.FirstOrDefaultAsync(f => f.Id == kommentarNeu.ÜbergeordneterKommentarId);
if (übergeordnetKommentar == null)
return BadRequest();
kommentarNeu.KopfId = übergeordnetKommentar.KopfId;
}
kommentarNeu.Kommentar = _wortFilterService.FilterWörter(kommentarNeu.Kommentar);
kommentarNeu.Title = _wortFilterService.FilterWörter(kommentarNeu.Title ?? "");
await _dataContext.Kommentare.AddAsync(kommentarNeu);
await _dataContext.SaveChangesAsync();
return CreatedAtAction(nameof(Get), new { id = kommentarNeu.Id}, kommentarNeu);
}
[HttpPut]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status405MethodNotAllowed)]
public async Task<IActionResult> Put([FromBody] KommentarPUTModel kommentar) {
var saved = await _dataContext.Kommentare
.AsTracking()
.FirstOrDefaultAsync(f => f.Id == kommentar.Id);
if (saved == null) {
return BadRequest();
}
if (saved.SchreibSchlüssel != kommentar.SchreibSchlüssel || saved.Gelöscht) {
return StatusCode(405);
}
saved.Update(kommentar);
saved.Kommentar = _wortFilterService.FilterWörter(saved.Kommentar);
saved.Title = _wortFilterService.FilterWörter(saved.Title ?? "");
await _dataContext.SaveChangesAsync();
return NoContent();
}
[HttpDelete]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status405MethodNotAllowed)]
public async Task<IActionResult> Delete([FromBody] KommentarDELETEModel kommentar) {
var saved = await _dataContext.Kommentare
.AsTracking()
.Include(i => i.WeitereKommentare)
.FirstOrDefaultAsync(f => f.Id == kommentar.Id);
if (saved == null) {
return BadRequest();
} else if (saved.SchreibSchlüssel != kommentar.SchreibSchlüssel) {
return StatusCode(405);
}
if (saved.WeitereKommentare != null && saved.WeitereKommentare.Any()) {
saved.Name = "deleted";
saved.Kommentar = "deleted";
saved.Title = "deleted";
saved.Gelöscht = true;
} else {
_dataContext.Kommentare.Remove(saved);
}
await _dataContext.SaveChangesAsync();
return NoContent();
}
}