public Task <object> ConvertAsync(StateConverterContext context)
        {
            var urlHelper = context.HttpContext.RequestServices.GetRequiredService <IUrlHelper>();
            var root      = context.Object as Root;
            var path      = context.HttpContext.Request.GetDisplayUrl();

            var document = new Document
            {
                Class = new Class {
                    "root"
                },
                Properties = root,
                Href       = path,
                Links      = new Links()
                {
                    new Link()
                    {
                        Rel = new Rel()
                        {
                            "self"
                        }, Href = path
                    },
                    new Link()
                    {
                        Rel = new Rel()
                        {
                            "customers"
                        }, Href = urlHelper.AbsoluteRouteUrl("GetCustomers", new RouteValueDictionary())
                    },
                }
            };

            return(Task.FromResult <object>(document));
        }
        public override async Task WriteResponseBodyAsync(OutputFormatterWriteContext context,
                                                          Encoding selectedEncoding)
        {
            var response   = context.HttpContext.Response;
            var converters = StateConverters;

            var converterProviderContext = new StateConverterProviderContext
            {
                ObjectType = context.ObjectType
            };

            var selectedConverter = SelectConverter(converterProviderContext, converters);

            if (selectedConverter == null)
            {
                context.HttpContext.Response.StatusCode = StatusCodes.Status406NotAcceptable;
                return;
            }

            var converterContext = new StateConverterContext
            {
                HttpContext = context.HttpContext,
                Object      = context.Object,
                ObjectType  = context.ObjectType
            };

            var document = await selectedConverter.ConvertAsync(converterContext);

            using (var writer = context.WriterFactory(response.Body, selectedEncoding))
            {
                WriteObject(writer, document);

                await writer.FlushAsync();
            }
        }
        public Task <object> ConvertAsync(StateConverterContext context)
        {
            var customers = (context.Object as IEnumerable <Customer>);
            var path      = context.HttpContext.Request.GetDisplayUrl();

            var properties = new
            {
                count = customers.Count()
            };

            var document = new Document
            {
                Class = new Class {
                    "customer", "collection"
                },
                Properties = properties,
                Href       = path,
            };

            var entities = customers.Select(x => new Entity
            {
                Class = new Class {
                    "customer"
                },
                Properties = x,
                Href       = $"{path}/{x.Id}"
            });

            document.Entities.Add(entities);

            return(Task.FromResult <object>(document));
        }
        public Task <object> ConvertAsync(StateConverterContext context)
        {
            var urlHelper = context.HttpContext.RequestServices.GetRequiredService <IUrlHelper>();
            var staticIp  = context.Object as StaticIp;

            var document = new Document
            {
                Class = new Class {
                    "staticIp"
                },
                Properties = staticIp
            };

            return(Task.FromResult <object>(document));
        }
Exemple #5
0
        public Task <object> ConvertAsync(StateConverterContext context)
        {
            var urlHelper = context.HttpContext.RequestServices.GetRequiredService <IUrlHelper>();
            var customer  = context.Object as PhoneLine;

            var document = new Document
            {
                Class = new Class {
                    "phoneline"
                },
                Properties = customer
            };

            return(Task.FromResult <object>(document));
        }
Exemple #6
0
        public Task <object> ConvertAsync(StateConverterContext context)
        {
            var urlHelper = context.HttpContext.RequestServices.GetRequiredService <IUrlHelper>();
            var customer  = context.Object as Customer;

            var actions = new List <Action>();

            actions.Add(new Action
            {
                Name   = "delete",
                Title  = "Delete Customer",
                Method = "DELETE",
                Href   = new Href(urlHelper.AbsoluteRouteUrl("DeleteCustomer", new RouteValueDictionary()
                {
                    { "id", customer.Id }
                }), System.UriKind.Absolute),
            });

            if (customer.PhoneLine == null)
            {
                actions.Add(new Action
                {
                    Name   = "add phoneline",
                    Title  = "Add PhoneLine",
                    Method = "POST",
                    Href   = new Href(urlHelper.AbsoluteRouteUrl("AddPhoneLine", new RouteValueDictionary()
                    {
                        { "id", customer.Id }
                    }), System.UriKind.Absolute),
                    Fields = new Fields(new[] { new Field()
                                                {
                                                    Name = "PhoneNumber", Type = "text"
                                                } })
                });
            }
            else
            {
                actions.Add(new Action
                {
                    Name   = "delete phoneline",
                    Title  = "Delete PhoneLine",
                    Method = "DELETE",
                    Href   = new Href(urlHelper.AbsoluteRouteUrl("DeletePhoneLine", new RouteValueDictionary()
                    {
                        { "id", customer.Id }, { "phoneLineId", customer.PhoneLine.Id }
                    }), System.UriKind.Absolute),
                });

                if (customer.Broadband == null)
                {
                    actions.Add(new Action
                    {
                        Name   = "add broadband",
                        Title  = "Add Broadband",
                        Method = "POST",
                        Href   = new Href(urlHelper.AbsoluteRouteUrl("AddBroadband", new RouteValueDictionary()
                        {
                            { "id", customer.Id }
                        }), System.UriKind.Absolute),
                    });
                }
                else
                {
                    actions.Add(new Action
                    {
                        Name   = "delete broadband",
                        Title  = "Delete broadband",
                        Method = "DELETE",
                        Href   = new Href(urlHelper.AbsoluteRouteUrl("DeleteBroadband", new RouteValueDictionary()
                        {
                            { "id", customer.Id }, { "broadbandId", customer.Broadband.Id }
                        }), System.UriKind.Absolute),
                    });

                    if (customer.StaticIp == null)
                    {
                        actions.Add(new Action
                        {
                            Name   = "add static ip",
                            Title  = "Add Static IP",
                            Method = "POST",
                            Href   = new Href(urlHelper.AbsoluteRouteUrl("AddStaticIp", new RouteValueDictionary()
                            {
                                { "id", customer.Id }
                            }), System.UriKind.Absolute),
                        });
                    }
                    else
                    {
                        actions.Add(new Action
                        {
                            Name   = "delete static ip",
                            Title  = "Delete Static IP",
                            Method = "DELETE",
                            Href   = new Href(urlHelper.AbsoluteRouteUrl("DeleteStaticIp", new RouteValueDictionary()
                            {
                                { "id", customer.Id }, { "staticIpId", customer.StaticIp.Id }
                            }), System.UriKind.Absolute)
                        });
                    }
                }
            }

            var document = new Document
            {
                Class = new Class {
                    "customer"
                },
                Properties = customer,
                Href       = new Href(urlHelper.AbsoluteRouteUrl("GetCustomer", new RouteValueDictionary()
                {
                    { "id", customer.Id }
                }), System.UriKind.Absolute),
                Actions = new Actions(actions)
            };

            return(Task.FromResult <object>(document));
        }