forked from Code-Sharp/uHttpSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDemoController.cs
More file actions
90 lines (71 loc) · 2.84 KB
/
Copy pathDemoController.cs
File metadata and controls
90 lines (71 loc) · 2.84 KB
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
using System;
using System.Linq;
using System.Threading.Tasks;
using uhttpsharp.Attributes;
using uhttpsharp.Controllers;
using uhttpsharp.Handlers;
namespace uhttpsharp.Demo.Controllers {
public class EmptyPipeline : IPipeline {
public Task<IControllerResponse> Go(Func<Task<IControllerResponse>> injectedTask, IHttpContext context) {
return injectedTask();
}
}
public class JsonController : IController {
public JsonController(int id) { }
public IPipeline Pipeline => new EmptyPipeline();
[HttpMethod(HttpMethods.Post)]
public IControllerResponse Post([FromBody] Question question) {
return Response.Render(HttpResponseCode.Ok, question).Result;
}
public class Question {
public string TheQuestion { get; set; }
}
}
public class MyController {
private readonly int _id;
public MyController(int id) {
_id = id;
}
public MyController() { }
[HttpMethod(HttpMethods.Post)]
public Task<IControllerResponse> Post([FromPost("a")] MyRequest request, [FromHeaders("header")] string hello, [FromQuery("query")] string world) {
return Response.Render(HttpResponseCode.Ok, null);
}
[Indexer]
public Task<object> Get(IHttpContext context, int id) {
return Task.FromResult<object>(new MyController(id));
}
}
public class MyRequest : IValidate {
public int A { get; set; }
public void Validate(IErrorContainer container) {
if (A == 0) container.Log("A cannot be zero");
}
}
internal class BaseController : IController {
public IController Derived => new DerivedController();
public virtual IPipeline Pipeline => new EmptyPipeline();
[HttpMethod(HttpMethods.Get)]
public Task<IControllerResponse> Get() {
return Response.Render(HttpResponseCode.Ok, new {Hello = "Base!", Kaki = Enumerable.Range(0, 10000)});
}
[HttpMethod(HttpMethods.Post)]
public Task<IControllerResponse> Post([FromBody] MyRequest a) {
return Response.Render(HttpResponseCode.Ok, a);
}
}
internal class DerivedController : BaseController {
[HttpMethod(HttpMethods.Get)]
public new Task<IControllerResponse> Get() {
return Response.Render(HttpResponseCode.Ok, new {Hello = "Derived!"});
}
[Indexer]
public Task<IController> Indexer(IHttpContext context, int hey) {
return Task.FromResult<IController>(this);
}
[Indexer]
public Task<IController> Indexer(IHttpContext context, string hey) {
return Task.FromResult<IController>(this);
}
}
}