Advertisement
Guest User

Untitled

a guest
Nov 16th, 2018
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. public class User
  2. {
  3. public Guid Id { get; }
  4. public string Name { get; }
  5.  
  6. public User(Guid _Id, string _Name) => (Id, Name) = (_Id, _Name);
  7. }
  8.  
  9. public abstract class Request { }
  10.  
  11. public class CreateUser : Request
  12. {
  13. public User User { get; }
  14. public CreateUser(User _User) => (User) = (_User);
  15. }
  16.  
  17. public class GetUser : Request
  18. {
  19. public User User { get; }
  20. public GetUser(User _User) => (User) = (_User);
  21. }
  22.  
  23. public class DeleteUser : Request
  24. {
  25. public User User { get; }
  26. public DeleteUser(User _User) => (User) = (_User);
  27. }
  28.  
  29. public class GetAllUser : Request { }
  30.  
  31. public abstract class Response { }
  32.  
  33. public class UserCreated: Response { }
  34.  
  35. public class UserDeleted: Response { }
  36.  
  37. public class UserData: Response
  38. {
  39. public User User { get; }
  40. public UserData(User _User) => (User) = (_User);
  41. }
  42.  
  43. public class AllUsers : Response
  44. {
  45. public User[] Users { get; }
  46. public AllUsers(User[] _Users) => (Users) = (_Users);
  47. }
  48.  
  49. public static class RequestApi {
  50. public static Task<Response> ProcessRequest(this Request Request)
  51. => Request.Match()
  52. .With<CreateUser>(req => from create in UserService.CreateUser(req.User)
  53. select UserCreated())
  54.  
  55. .With<GetUser>(req => from user in UserService.GetUser(req.User.Id)
  56. select user)
  57.  
  58. .With<DeleteUser>(req => from delete in UserService.DeleteUser(req.User.Id)
  59. select UserDeleted())
  60.  
  61. .With<GetAllUser>(req => from users in UserService.GetAllUsers()
  62. select users)
  63. .Final();
  64.  
  65. public static Task<HzResponse> TryProcess(this Request req)
  66. => (HttpFunc next, HttpContext ctx) =>
  67. {
  68. try
  69. {
  70. return from resp in req.ProcessRequest()
  71. select Successful.OK(resp, next, ctx).Return();
  72. }
  73. catch (Exception e)
  74. {
  75. var resp = RequestFailed(req, e);
  76. return ServerErrors.internalError(json, resp, next, ctx).Return();
  77. }
  78. };
  79.  
  80. public static Task<HzResponse> CreateUser(HttpFunc next)
  81. => (HttpContext ctx)
  82. => from user in ctx.BindJsonAsync<User>()
  83. select TryProcess(CreateUser(user), next ctx).Return();
  84.  
  85. public static Hz webApi()
  86. => choose([
  87. arg => GET(arg).Bind(routef("/user/%O")(x => GetUser(x).TryProcess())),
  88. arg => GET(arg).Bind(route("/user/")).Bind(GetAllUsers().TryProcess()),
  89. arg => POST(arg).Bind(route("/user/")).Bind(CreateUser),
  90. arg => Post(arg).Bind(routef("/user/%O/delete")(x => DeleteUser().TryProcess())),
  91. ]);
  92. }
  93.  
  94. class Program
  95. {
  96. public static void configureCors(CorsPolicyBuilder builder)
  97. {
  98. builder.WithOrigins("http://localhost:8080")
  99. .AllowAnyMethod()
  100. .AllowAnyHeader();
  101. }
  102.  
  103. public static IApplicationBuilder configureApp(IApplicationBuilder app) =>
  104. app.UseHttpsRedirection()
  105. .UseCors(configureCors)
  106. .UseGiraffe(webApi);
  107.  
  108. public static void configureServices(IServiceCollection services)
  109. {
  110. services.AddCors();
  111. services.AddGiraffe();
  112. }
  113.  
  114. public static void configureLogging(this ILoggingBuilder builder)
  115. {
  116. var filter = (LogLevel l) => l.Equals LogLevel.Error;
  117. builder.AddFilter(filter).AddConsole().AddDebug();
  118. }
  119.  
  120.  
  121. static void Main()
  122. {
  123. var contentRoot = Directory.GetCurrentDirectory()
  124. var webRoot = Path.Combine(contentRoot, "WebRoot")
  125. WebHostBuilder()
  126. .UseKestrel()
  127. .UseContentRoot(contentRoot).UseIISIntegration()
  128. .UseWebRoot(webRoot)
  129. .Configure(Action < IApplicationBuilder > configureApp)
  130. .ConfigureLogging(configureLogging)
  131. .Build()
  132. .Run();
  133. }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement