Example #1
0
        // Converts list of domain objects into C+J media type DOM
        // Static so it can be re-used by multiple controllers ( e.g. Sessions?topicid={topicid}  and topic/{id}/sessions)

        public static Collection GetSessionsCollection(IEnumerable <Session> sessions, DataService dataService, HttpRequestMessage request)
        {
            var eventsCollection = new Collection();

            foreach (var session in sessions)
            {
                var item = new Item();
                item.Href = SessionLinkHelper.CreateLink(request, session).Target;
                item.Data.Add(new Data {
                    Name = "Title", Value = session.Title
                });
                item.Data.Add(new Data {
                    Name = "Timeslot", Value = session.TimeslotDescription
                });
                if (session.SpeakerId != 0)
                {
                    item.Data.Add(new Data
                    {
                        Name  = "Speaker",
                        Value = dataService.SpeakerRepository.Get(session.SpeakerId).Name
                    });
                    item.Links.Add(SpeakerLinkHelper.CreateLink(request, dataService.SpeakerRepository.Get(session.SpeakerId)).ToCJLink());
                }

                item.Links.Add(TopicsLinkHelper.CreateLink(request, session).ToCJLink());
                eventsCollection.Items.Add(item);
            }
            return(eventsCollection);
        }
Example #2
0
        public static HttpResponseMessage CreateResponse(Session session, HttpRequestMessage request)
        {
            var response = request.RespondOk();

            if (request.Headers.Accept.Contains(new MediaTypeWithQualityHeaderValue("application/hal+json")))
            {
                response.Content = CreateHalContent(session, request);
            }
            else
            {
                response.Content = new StringContent(session.Title + Environment.NewLine + session.Description);
                response.Headers.AddLinkHeader(SpeakerLinkHelper.CreateLink(request, session.SpeakerId));
            }
            return(response);
        }
        public static IHttpActionResult CreateResponse(Session session, HttpRequestMessage request)
        {
            IHttpActionResult response = new OkResult(request);

            if (request.Headers.Accept.Contains(new MediaTypeWithQualityHeaderValue("application/hal+json")))
            {
                response = response.WithContent(CreateHalContent(session, request));
            }
            else
            {
                response = response
                           .WithContent(new StringContent(session.Title + Environment.NewLine + session.Description))
                           .WithLinkHeaders(new List <ILink> {
                    SpeakerLinkHelper.CreateLink(request, session.SpeakerId)
                });
            }
            return(response);
        }
        private static HttpContent CreateHalContent(Session session, HttpRequestMessage request)
        {
            dynamic jsession = new JObject();

            jsession.title       = session.Title;
            jsession.description = session.Description;
            jsession.timeslot    = session.TimeslotDescription;

            dynamic links = new JObject();

            dynamic speakerLink = new JObject();

            speakerLink.href = SpeakerLinkHelper.CreateLink(request, session.SpeakerId).Target;
            links[LinkHelper.GetLinkRelationTypeName <SpeakerLink>()] = speakerLink;

            jsession["_links"] = links;

            return(new DynamicHalContent(jsession));
        }
Example #5
0
        public static IHttpActionResult CreateResponse(IEnumerable <Speaker> speakers, HttpRequestMessage request)
        {
            var collection = new Collection();

            foreach (var speaker in speakers)
            {
                var item = new Item();
                item.Href = SpeakerLinkHelper.CreateLink(request, speaker).Target;
                item.Data.Add(new Data {
                    Name = "Name", Value = speaker.Name
                });
                item.Links.Add(SessionsLinkHelper.CreateLink(request, speaker).ToCJLink());
                collection.Items.Add(item);
            }
            collection.Href = request.RequestUri;

            return(new OkResult(request)
                   .WithContent(new CollectionJsonContent(collection)));
        }