Beispiel #1
0
        public ActorsModule(IActorProvider actorProvider)
        {
            Get("/actors", async(req, res, routeData) =>
            {
                var people = actorProvider.Get();
                await res.Negotiate(people);
            });

            Post("/actors", async(req, res, routeData) =>
            {
                var result = req.BindAndValidate <Actor>();

                if (!result.ValidationResult.IsValid)
                {
                    res.StatusCode = 422;
                    await res.Negotiate(result.ValidationResult.GetFormattedErrors());
                    return;
                }

                actorProvider.Add(result.Data);

                res.StatusCode = 201;
                await res.Negotiate(result.Data);
            });

            Get("/actors/{id:int}", async(req, res, routeData) =>
            {
                try
                {
                    var person = actorProvider.Get(routeData.As <int>("id"));
                    await res.Negotiate(person);
                }
                catch (InvalidOperationException)
                {
                    res.StatusCode = 404;
                }
            });

            Put("/actors/{id:int}", async(req, res, routeData) =>
            {
                var result = req.BindAndValidate <Actor>();

                if (!result.ValidationResult.IsValid)
                {
                    res.StatusCode = 422;
                    await res.Negotiate(result.ValidationResult.GetFormattedErrors());
                    return;
                }

                try
                {
                    actorProvider.Update(result.Data);

                    res.StatusCode = 204;
                }
                catch (InvalidOperationException)
                {
                    res.StatusCode = 404;
                }
            });

            Delete("/actors/{id:int}", (req, res, routeData) =>
            {
                try
                {
                    var actor = actorProvider.Get(routeData.As <int>("id"));
                    actorProvider.Delete(actor);

                    res.StatusCode = 204;
                }
                catch (InvalidOperationException)
                {
                    res.StatusCode = 404;
                }

                return(Task.CompletedTask);
            });
        }
Beispiel #2
0
        public ActorModule(IActorProvider actorProvider)
        {
            Get("/actors", (req, res) =>
            {
                var actors = actorProvider.Get();
                return(res.Negotiate(actors));
            });

            Get("/actors/{id:int}", (req, res) =>
            {
                try
                {
                    var actor = actorProvider.Get(req.RouteValues.As <int>("id"));
                    return(res.Negotiate(actor));
                }
                catch (KeyNotFoundException)
                {
                    res.StatusCode = 404;
                    return(Task.CompletedTask);
                }
            });

            Post("/actors", async(req, res) =>
            {
                var result = await req.BindAndValidate <Actor>();

                if (!result.ValidationResult.IsValid)
                {
                    res.StatusCode = 422;
                    await res.Negotiate(result.ValidationResult.GetFormattedErrors());
                    return;
                }

                var actor = actorProvider.Create(result.Data);

                res.StatusCode = 201;
                await res.Negotiate(actor);
            });

            Put("/actors/{id:int}", async(req, res) =>
            {
                var result = await req.BindAndValidate <Actor>();

                if (!result.ValidationResult.IsValid)
                {
                    res.StatusCode = 422;
                    await res.Negotiate(result.ValidationResult.GetFormattedErrors());
                    return;
                }

                result.Data.Id = req.RouteValues.As <int>("id");
                actorProvider.Update(result.Data);
                res.StatusCode = 204;
            });

            Post("/actors/{id:int}/avatar", async(req, res) =>
            {
                try
                {
                    var actor = actorProvider.Get(req.RouteValues.As <int>("id"));
                    var file  = await req.BindFile();

                    actor.Avatar = file.FileName;

                    actorProvider.Update(actor);

                    await res.Negotiate(actor);
                }
                catch (KeyNotFoundException)
                {
                    res.StatusCode = 404;
                }
            });

            Delete("/actors/{id:int}", (req, res) =>
            {
                actorProvider.Delete(req.RouteValues.As <int>("id"));
                res.StatusCode = 204;
                return(Task.CompletedTask);
            });
        }
Beispiel #3
0
        public ActorModule(IActorProvider actorProvider)
        {
            this.Get <ListAllActors>("/actors", (req, res) =>
            {
                var actors = actorProvider.Get();
                return(res.Negotiate(actors));
            });

            Get <GetActorById>("/actors/{id:int}", (req, res) =>
            {
                try
                {
                    var actor = actorProvider.Get(req.RouteValues.As <int>("id"));
                    return(res.Negotiate(actor));
                }
                catch (KeyNotFoundException)
                {
                    res.StatusCode = 404;
                    return(Task.CompletedTask);
                }
            });

            Post <CreateActor>("/actors", async(req, res) =>
            {
                var result = await req.BindAndValidate <Actor>();

                if (!result.ValidationResult.IsValid)
                {
                    res.StatusCode = 422;
                    await res.Negotiate(result.ValidationResult.GetFormattedErrors());
                    return;
                }

                var actor = actorProvider.Create(result.Data);

                res.StatusCode = 201;
                await res.Negotiate(actor);
            });

            Put <UpdateActor>("/actors/{id:int}", async(req, res) =>
            {
                var result = await req.BindAndValidate <Actor>();

                if (!result.ValidationResult.IsValid)
                {
                    res.StatusCode = 422;
                    await res.Negotiate(result.ValidationResult.GetFormattedErrors());
                    return;
                }

                actorProvider.Update(result.Data);
                res.StatusCode = 204;
            });

            Delete <DeleteActor>("/actors/{id:int}", (req, res) =>
            {
                actorProvider.Delete(req.RouteValues.As <int>("id"));
                res.StatusCode = 204;
                return(Task.CompletedTask);
            });
        }