aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2022-12-17 19:07:23 +0800
committercrupest <crupest@outlook.com>2022-12-20 20:32:53 +0800
commit7407875fb75bcd90f6f7ef54573483fe2f3cfb84 (patch)
treeed55de998706e6805f71614d20d2602d1b3b599f
parent4595650f6e36ff413bcb65f5419daf8f9bfee9de (diff)
downloadcrupest-7407875fb75bcd90f6f7ef54573483fe2f3cfb84.tar.gz
crupest-7407875fb75bcd90f6f7ef54573483fe2f3cfb84.tar.bz2
crupest-7407875fb75bcd90f6f7ef54573483fe2f3cfb84.zip
Develop secret api. v41
-rw-r--r--docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs13
-rw-r--r--docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudWebApplicationExtensions.cs32
-rw-r--r--docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/EntityJsonHelper.cs51
-rw-r--r--docker/crupest-api/CrupestApi/CrupestApi.Commons/Json.cs1
4 files changed, 75 insertions, 22 deletions
diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs
index 65085fd..d371c84 100644
--- a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs
+++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudService.cs
@@ -46,7 +46,7 @@ public class CrudService<TEntity> : IDisposable where TEntity : class
return result;
}
- public TEntity GetByKey(string key)
+ public TEntity GetByKey(object key)
{
var result = _table.Select<TEntity>(_dbConnection, null, WhereClause.Create().Eq(_table.KeyColumn.ColumnName, key));
return result.Single();
@@ -58,4 +58,15 @@ public class CrudService<TEntity> : IDisposable where TEntity : class
var key = _table.Insert(_dbConnection, insertClauses);
return (string)key;
}
+
+ public void Update(object key, JsonElement jsonElement)
+ {
+ var updateClauses = _jsonHelper.ConvertJsonElementToUpdateClause(jsonElement);
+ _table.Update(_dbConnection, WhereClause.Create().Eq(_table.KeyColumn.ColumnName, key), updateClauses);
+ }
+
+ public void DeleteByKey(object key)
+ {
+ _table.Delete(_dbConnection, WhereClause.Create().Eq(_table.KeyColumn.ColumnName, key));
+ }
}
diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudWebApplicationExtensions.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudWebApplicationExtensions.cs
index 8df444c..7331273 100644
--- a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudWebApplicationExtensions.cs
+++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/CrudWebApplicationExtensions.cs
@@ -17,7 +17,7 @@ public static class CrudWebApplicationExtensions
var key = context.Request.RouteValues["key"]?.ToString();
if (key == null)
{
- await context.ResponseMessageAsync("Please specify a key.");
+ await context.ResponseMessageAsync("Please specify a key in path.");
return;
}
@@ -33,6 +33,36 @@ public static class CrudWebApplicationExtensions
await context.ResponseJsonAsync(crudService.JsonHelper.ConvertEntityToDictionary(crudService.GetByKey(key)));
});
+ app.MapPatch(path + "/{key}", async (context) =>
+ {
+ var crudService = context.RequestServices.GetRequiredService<CrudService<TEntity>>();
+ var key = context.Request.RouteValues["key"]?.ToString();
+ if (key == null)
+ {
+ await context.ResponseMessageAsync("Please specify a key in path.");
+ return;
+ }
+
+ var jsonDocument = await context.Request.ReadJsonAsync();
+ crudService.Update(key, jsonDocument.RootElement);
+
+ await context.ResponseJsonAsync(crudService.JsonHelper.ConvertEntityToDictionary(crudService.GetByKey(key)));
+ });
+
+ app.MapDelete(path + "/{key}", async (context) =>
+ {
+ var crudService = context.RequestServices.GetRequiredService<CrudService<TEntity>>();
+ var key = context.Request.RouteValues["key"]?.ToString();
+ if (key == null)
+ {
+ await context.ResponseMessageAsync("Please specify a key in path.");
+ return;
+ }
+
+ crudService.DeleteByKey(key);
+ await context.ResponseMessageAsync("Deleted.", StatusCodes.Status200OK);
+ });
+
return app;
}
}
diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/EntityJsonHelper.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/EntityJsonHelper.cs
index e893638..1265fe9 100644
--- a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/EntityJsonHelper.cs
+++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Crud/EntityJsonHelper.cs
@@ -93,36 +93,20 @@ public class EntityJsonHelper<TEntity> where TEntity : class
return ConvertJsonElementToInsertClauses(document.RootElement);
}
- public IUpdateClause ConvertJsonElementToUpdateClause(JsonDocument json)
+ public IUpdateClause ConvertJsonElementToUpdateClause(JsonElement rootElement, bool saveNull)
{
var updateClause = UpdateClause.Create();
- if (json.RootElement.ValueKind != JsonValueKind.Object)
+ if (rootElement.ValueKind != JsonValueKind.Object)
{
throw new UserException("The root element must be an object.");
}
- bool saveNull = false;
-
- if (json.RootElement.TryGetProperty("$saveNull", out var propertyElement))
- {
- if (propertyElement.ValueKind is not JsonValueKind.True or JsonValueKind.False)
- {
- throw new UserException("$saveNull can only be true or false.");
- }
-
- if (propertyElement.ValueKind is JsonValueKind.True)
- {
- saveNull = true;
- }
- }
-
-
foreach (var column in _table.PropertyColumns)
{
object? value = null;
- if (json.RootElement.TryGetProperty(column.ColumnName, out propertyElement))
+ if (rootElement.TryGetProperty(column.ColumnName, out var propertyElement))
{
value = propertyElement.ValueKind switch
{
@@ -151,9 +135,36 @@ public class EntityJsonHelper<TEntity> where TEntity : class
return updateClause;
}
+ public IUpdateClause ConvertJsonElementToUpdateClause(JsonElement rootElement)
+ {
+ var updateClause = UpdateClause.Create();
+
+ if (rootElement.ValueKind != JsonValueKind.Object)
+ {
+ throw new UserException("The root element must be an object.");
+ }
+
+ bool saveNull = false;
+
+ if (rootElement.TryGetProperty("$saveNull", out var propertyElement))
+ {
+ if (propertyElement.ValueKind is not JsonValueKind.True or JsonValueKind.False)
+ {
+ throw new UserException("$saveNull can only be true or false.");
+ }
+
+ if (propertyElement.ValueKind is JsonValueKind.True)
+ {
+ saveNull = true;
+ }
+ }
+
+ return ConvertJsonElementToUpdateClause(rootElement, saveNull);
+ }
+
public IUpdateClause ConvertJsonToUpdateClause(string json)
{
var document = JsonSerializer.Deserialize<JsonDocument>(json, _jsonSerializerOptions.CurrentValue)!;
- return ConvertJsonElementToUpdateClause(document);
+ return ConvertJsonElementToUpdateClause(document.RootElement);
}
}
diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Json.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Json.cs
index dbf020f..61bcc61 100644
--- a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Json.cs
+++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Json.cs
@@ -1,5 +1,6 @@
using System.Text.Json;
using Microsoft.Extensions.Options;
+using Microsoft.Net.Http.Headers;
namespace CrupestApi.Commons;