Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class User
- {
- public Guid Id { get; }
- public string Name { get; }
- public User(Guid _Id, string _Name) => (Id, Name) = (_Id, _Name);
- }
- public abstract class Request { }
- public class CreateUser : Request
- {
- public User User { get; }
- public CreateUser(User _User) => (User) = (_User);
- }
- public class GetUser : Request
- {
- public User User { get; }
- public GetUser(User _User) => (User) = (_User);
- }
- public class DeleteUser : Request
- {
- public User User { get; }
- public DeleteUser(User _User) => (User) = (_User);
- }
- public class GetAllUser : Request { }
- public abstract class Response { }
- public class UserCreated: Response { }
- public class UserDeleted: Response { }
- public class UserData: Response
- {
- public User User { get; }
- public UserData(User _User) => (User) = (_User);
- }
- public class AllUsers : Response
- {
- public User[] Users { get; }
- public AllUsers(User[] _Users) => (Users) = (_Users);
- }
- public static class RequestApi {
- public static Task<Response> ProcessRequest(this Request Request)
- => Request.Match()
- .With<CreateUser>(req => from create in UserService.CreateUser(req.User)
- select UserCreated())
- .With<GetUser>(req => from user in UserService.GetUser(req.User.Id)
- select user)
- .With<DeleteUser>(req => from delete in UserService.DeleteUser(req.User.Id)
- select UserDeleted())
- .With<GetAllUser>(req => from users in UserService.GetAllUsers()
- select users)
- .Final();
- public static Task<HzResponse> TryProcess(this Request req)
- => (HttpFunc next, HttpContext ctx) =>
- {
- try
- {
- return from resp in req.ProcessRequest()
- select Successful.OK(resp, next, ctx).Return();
- }
- catch (Exception e)
- {
- var resp = RequestFailed(req, e);
- return ServerErrors.internalError(json, resp, next, ctx).Return();
- }
- };
- public static Task<HzResponse> CreateUser(HttpFunc next)
- => (HttpContext ctx)
- => from user in ctx.BindJsonAsync<User>()
- select TryProcess(CreateUser(user), next ctx).Return();
- public static Hz webApi()
- => choose([
- arg => GET(arg).Bind(routef("/user/%O")(x => GetUser(x).TryProcess())),
- arg => GET(arg).Bind(route("/user/")).Bind(GetAllUsers().TryProcess()),
- arg => POST(arg).Bind(route("/user/")).Bind(CreateUser),
- arg => Post(arg).Bind(routef("/user/%O/delete")(x => DeleteUser().TryProcess())),
- ]);
- }
- class Program
- {
- public static void configureCors(CorsPolicyBuilder builder)
- {
- builder.WithOrigins("http://localhost:8080")
- .AllowAnyMethod()
- .AllowAnyHeader();
- }
- public static IApplicationBuilder configureApp(IApplicationBuilder app) =>
- app.UseHttpsRedirection()
- .UseCors(configureCors)
- .UseGiraffe(webApi);
- public static void configureServices(IServiceCollection services)
- {
- services.AddCors();
- services.AddGiraffe();
- }
- public static void configureLogging(this ILoggingBuilder builder)
- {
- var filter = (LogLevel l) => l.Equals LogLevel.Error;
- builder.AddFilter(filter).AddConsole().AddDebug();
- }
- static void Main()
- {
- var contentRoot = Directory.GetCurrentDirectory()
- var webRoot = Path.Combine(contentRoot, "WebRoot")
- WebHostBuilder()
- .UseKestrel()
- .UseContentRoot(contentRoot).UseIISIntegration()
- .UseWebRoot(webRoot)
- .Configure(Action < IApplicationBuilder > configureApp)
- .ConfigureLogging(configureLogging)
- .Build()
- .Run();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement