Skip to content

Commit 1104afc

Browse files
committed
feat: create server operations project
1 parent 6295ff8 commit 1104afc

25 files changed

+1154
-0
lines changed

core/KendoCoreService.sln

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.2.32630.192
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KendoCoreService", "KendoCoreService\KendoCoreService.csproj", "{428FECF6-2150-4B40-9A0A-3F5CF542CBD2}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{428FECF6-2150-4B40-9A0A-3F5CF542CBD2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{428FECF6-2150-4B40-9A0A-3F5CF542CBD2}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{428FECF6-2150-4B40-9A0A-3F5CF542CBD2}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{428FECF6-2150-4B40-9A0A-3F5CF542CBD2}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {AC4A30EC-62C2-432C-9EAF-C83833D24075}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using KendoCoreService.Extensions;
2+
using KendoCoreService.Interfaces;
3+
using KendoCoreService.Models.Request;
4+
using KendoCoreService.Models.Response;
5+
using Microsoft.AspNetCore.Mvc;
6+
using System.Collections;
7+
8+
namespace KendoCoreService.Controllers
9+
{
10+
[ApiController]
11+
[Route("[controller]/[action]")]
12+
public class ProductsController : ControllerBase
13+
{
14+
private readonly IProductRepository _products;
15+
16+
public ProductsController(IProductRepository products)
17+
{
18+
_products = products;
19+
}
20+
21+
[HttpPost]
22+
public IActionResult Read([FromBody]Request request)
23+
{
24+
var data = this._products.All().AsQueryable();
25+
int total = data.Count();
26+
IList resultData;
27+
bool isGrouped = false;
28+
29+
var aggregates = new Dictionary<string, Dictionary<string, string>>();
30+
31+
if (request.Sorts != null)
32+
{
33+
data = data.Sort(request.Sorts);
34+
}
35+
36+
if (request.Filter != null)
37+
{
38+
data = data.Filter(request.Filter);
39+
total = data.Count();
40+
}
41+
42+
if (request.Aggregates != null)
43+
{
44+
aggregates = data.CalculateAggregates(request.Aggregates);
45+
}
46+
47+
if (request.Take > 0)
48+
{
49+
data = data.Page(request.Skip, request.Take);
50+
}
51+
52+
if (request.Groups != null && request.Groups.Count > 0)
53+
{
54+
resultData = data.Group(request.Groups).Cast<Group>().ToList();
55+
isGrouped = true;
56+
}
57+
else
58+
{
59+
resultData = data.ToList();
60+
}
61+
62+
var result = new Response(resultData, aggregates, total, isGrouped).ToResult();
63+
return Ok(result);
64+
}
65+
}
66+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using KendoCoreService.Models.Request;
2+
3+
namespace KendoCoreService.Extensions
4+
{
5+
public static class AggregateExtension
6+
{
7+
public static Dictionary<string, Dictionary<string, string>> CalculateAggregates<T>(this IQueryable<T> data, List<AggregateRequest> aggregates)
8+
{
9+
if (aggregates == null)
10+
{
11+
return new Dictionary<string, Dictionary<string, string>>();
12+
}
13+
14+
int count = aggregates.Count;
15+
var result = new Dictionary<string, Dictionary<string, string>>();
16+
17+
if (count > 0)
18+
{
19+
foreach (var aggregate in aggregates)
20+
{
21+
string field = aggregate.Field;
22+
string functionName = aggregate.Aggregate;
23+
double aggregateResult = 0;
24+
25+
switch (functionName)
26+
{
27+
case "sum":
28+
aggregateResult = data.Sum(x => CommonExtension.ParseObjectToDouble(x, field));
29+
break;
30+
case "average":
31+
aggregateResult = data.Average(x => CommonExtension.ParseObjectToDouble(x, field));
32+
break;
33+
case "max":
34+
aggregateResult = data.Max(x => CommonExtension.ParseObjectToDouble(x, field));
35+
break;
36+
case "min":
37+
aggregateResult = data.Min(x => CommonExtension.ParseObjectToDouble(x, field));
38+
break;
39+
case "count":
40+
aggregateResult = data.Select(x => CommonExtension.GetValueByPropertyName(x, field)).Count();
41+
break;
42+
}
43+
44+
string functionResult = aggregateResult.ToString();
45+
46+
if (!result.ContainsKey(field))
47+
{
48+
result.Add(field, new Dictionary<string, string> { { functionName, functionResult } });
49+
}
50+
else
51+
{
52+
result[field].Add(functionName, functionResult);
53+
}
54+
}
55+
56+
return result;
57+
}
58+
59+
return new Dictionary<string, Dictionary<string, string>>();
60+
}
61+
}
62+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
using System.Linq;
3+
using System.Reflection;
4+
5+
namespace KendoCoreService.Extensions
6+
{
7+
public static class CommonExtension
8+
{
9+
public static object GetValueByPropertyName(object obj, string propertyName)
10+
{
11+
return obj.GetType().GetProperty(propertyName).GetValue(obj, null) ?? "null";
12+
}
13+
14+
public static PropertyInfo GetPropertyInfo(Type objType, string propertyName)
15+
{
16+
return objType.GetProperties().FirstOrDefault(p => p.Name == propertyName);
17+
}
18+
19+
public static double ParseObjectToDouble(object data, string field)
20+
{
21+
return double.Parse(GetValueByPropertyName(data, field).ToString());
22+
}
23+
24+
public static bool IsNumber(this object value)
25+
{
26+
return double.TryParse(value.ToString(), out double r);
27+
}
28+
29+
public static bool IsNull(this string str)
30+
{
31+
return str == "null";
32+
}
33+
34+
public static bool IsNotNull(this string str)
35+
{
36+
return str != "null";
37+
}
38+
39+
public static bool IsEmpty(this string str)
40+
{
41+
return str.Length == 0;
42+
}
43+
44+
public static bool IsNotEmpty(this string str)
45+
{
46+
return str.Length != 0;
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)