This repository was archived by the owner on Apr 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathCRUDController.cs
210 lines (199 loc) · 7.27 KB
/
CRUDController.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
//******************************************************************************
// <copyright file="license.md" company="RawCMS project (https://github.com/arduosoft/RawCMS)">
// Copyright (c) 2019 RawCMS project (https://github.com/arduosoft/RawCMS)
// RawCMS project is released under GPL3 terms, see LICENSE file on repository root at https://github.com/arduosoft/RawCMS .
// </copyright>
// <author>Daniele Fontani, Emanuele Bucarelli, Francesco Mina</author>
// <autogenerated>true</autogenerated>
//******************************************************************************
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using RawCMS.Library.Core;
using RawCMS.Library.Core.Attributes;
using RawCMS.Library.Core.Exceptions;
using RawCMS.Library.DataModel;
using RawCMS.Library.Service;
using RawCMS.Plugins.Core.Model;
using System;
using System.Collections.Generic;
namespace RawCMS.Plugins.Core.Controllers
{
//[Authorize(AuthenticationSchemes = "Bearer")]
[Route("api/[controller]")]
[ParameterValidator("collection", "_(.*)", true)]
[ApiController]
public class CRUDController : ControllerBase
{
private readonly CRUDService service;
public CRUDController(AppEngine manager, CRUDService service)
{
this.service = service;
}
// GET api/CRUD/{collection}
[HttpGet("{collection}")]
public RestMessage<ItemList> Get(string collection, string rawQuery = null, string expando = "", int pageNumber = 1, int pageSize = 20, string sort = "")
{
var sortValue = Newtonsoft.Json.JsonConvert.DeserializeObject<List<SortOption>>(sort);
ItemList result = service.Query(collection, new Library.DataModel.DataQuery()
{
PageNumber = pageNumber,
PageSize = pageSize,
RawQuery = rawQuery,
Sort = sortValue,
Expando = new List<string>(expando?.Split(',', StringSplitOptions.RemoveEmptyEntries))
});
return new RestMessage<ItemList>(result);
}
// GET api/CRUD/{collection}/5
[HttpGet("{collection}/{id}")]
public RestMessage<JObject> Get(string collection, string id)
{
JObject data = service.Get(collection, id);
return new RestMessage<JObject>(data);
}
// POST api/CRUD/{collection}
[HttpPost("{collection}")]
public RestMessage<JObject> Post(string collection, [FromBody]JObject value, bool omitPayload = false)
{
RestMessage<JObject> response = new RestMessage<JObject>(new JObject());
try
{
var added = service.Insert(collection, value);
if (omitPayload)
{
response.Data["_id"] = added["_id"];
}
else
{
response.Data = added;
}
return response;
}
catch (ValidationException err)
{
response.Errors = err.Errors;
}
catch (Exception untrapped)
{
//TODO: log here
response.Errors.Add(new Library.Core.Error()
{
Code = "UNEXPEXTED",
Title = $"{collection} produces an unexpexted error",
Description = untrapped.Message,
});
}
return response;
}
// PUT api/CRUD/{collection}/5
[HttpPut("{collection}/{id}")]
public RestMessage<JObject> Put(string collection, string id, [FromBody]JObject value, bool omitPayload = false)
{
RestMessage<JObject> response = new RestMessage<JObject>(new JObject());
try
{
value["_id"] = id;
var updated = service.Update(collection, value, true);
if (!omitPayload)
{
response.Data = updated;
}
return response;
}
catch (ValidationException err)
{
response.Errors = err.Errors;
}
catch (Exception untrapped)
{
//TODO: log here
response.Errors.Add(new Library.Core.Error()
{
Code = "UNEXPEXTED",
Title = $"{collection} produces an unexpexted error",
Description = untrapped.Message,
});
}
return response;
}
// PUT api/CRUD/{collection}/5
[HttpPatch("{collection}/{id}")]
public RestMessage<JObject> Patch(string collection, string id, [FromBody]JObject value, bool omitPayload = false)
{
RestMessage<JObject> response = new RestMessage<JObject>(new JObject());
try
{
value["_id"] = id;
var updated = service.Update(collection, value, false);
if (!omitPayload)
{
response.Data = updated;
}
return response;
}
catch (ValidationException err)
{
response.Errors = err.Errors;
}
catch (Exception untrapped)
{
//TODO: log here
response.Errors.Add(new Library.Core.Error()
{
Code = "UNEXPEXTED",
Title = $"{collection} produces an unexpexted error",
Description = untrapped.Message,
});
}
return response;
}
// DELETE api/CRUD/{collection}/5
[HttpDelete("{collection}/{id}")]
public RestMessage<bool> Delete(string collection, string id)
{
RestMessage<bool> response = new RestMessage<bool>(false);
try
{
bool result = service.Delete(collection, id);
response.Data = true;
return response;
}
catch (ValidationException err)
{
response.Errors = err.Errors;
}
catch (Exception untrapped)
{
//TODO: log here
response.Errors.Add(new Library.Core.Error()
{
Code = "UNEXPEXTED",
Title = $"{collection} produces an unexpexted error",
Description = untrapped.Message,
});
}
return response;
}
//
[HttpGet("{collection}/count")]
public RestMessage<long> Count(string collection)
{
RestMessage<long> response = new RestMessage<long>(0);
try
{
var result = service.Count(collection);
response.Data = result;
}catch(Exception ex)
{
//TODO: log here
response.Errors.Add(new Library.Core.Error()
{
Code = "UNEXPEXTED",
Title = $"{collection} produces an unexpexted error",
Description = ex.Message,
});
}
return response;
}
}
}