public HttpResponseMessage<SyndicationFeed> Get(string id, HttpRequestMessage request)
        {
            Encounter encounter;
            try
            {
                encounter = encounters.Get(int.Parse(id));
            }
            catch (KeyNotFoundException)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            var feed = new SyndicationFeed
                           {
                               BaseUri = BaseUri,
                               Title = SyndicationContent.CreatePlaintextContent(encounter.Title),
                               Description = SyndicationContent.CreatePlaintextContent(encounter.Description)
                           };
            feed.Categories.Add(new SyndicationCategory("encounter"));
            feed.Authors.Add(new SyndicationPerson {Name = "Dungeon Master", Email = "*****@*****.**"});

            if (encounter.IsResolved)
            {
                feed.Links.Add(new SyndicationLink {RelationshipType = "continue", Uri = new Uri("/rooms/" + encounter.GuardedRoomId, UriKind.Relative)});
            }
            else
            {
                feed.Links.Add(new SyndicationLink {RelationshipType = "flee", Uri = new Uri("/rooms/" + encounter.FleeRoomId, UriKind.Relative)});
                var xhtml = new FormWriter(new Uri("/encounters/" + encounter.Id, UriKind.RelativeOrAbsolute), HttpMethod.Post, new TextInput("endurance")).ToXhtml();
                feed.ElementExtensions.Add(XmlReader.Create(new StringReader(xhtml)));
            }

            feed.Items = encounter.GetAllRounds()
                .Reverse()
                .Select(round =>
                            {
                                var entry = new SyndicationItem
                                                {
                                                    Title = SyndicationContent.CreatePlaintextContent("Round " + round.Id),
                                                    Summary = SyndicationContent.CreatePlaintextContent(string.Format("The {0} has {1} Endurance Points", encounter.Title, round.Endurance))
                                                };
                                entry.Links.Add(SyndicationLink.CreateSelfLink(new Uri(string.Format("http://{0}:8081/encounters/{1}/rounds/{2}", Environment.MachineName, encounter.Id, round.Id))));
                                entry.Categories.Add(new SyndicationCategory("round"));
                                return entry;
                            });

            var response = new HttpResponseMessage<SyndicationFeed>(feed) {StatusCode = HttpStatusCode.OK};
            response.Headers.CacheControl = new CacheControlHeaderValue {NoCache = true, NoStore = true};
            response.Content.Headers.ContentType = AtomMediaType.Feed;

            return response;
        }
        public void ShouldReturnXhtmlRepresentationOfForm()
        {
            const string expectedXhtml = @"<div xmlns=""http://www.w3.org/1999/xhtml"">
              <form action=""/encounters/1"" method=""POST"" enctype=""application/x-www-form-urlencoded"">
            <input type=""text"" name=""field1"" value=""field1value"" />
            <input type=""text"" name=""field2"" />
            <input type=""text"" name=""field3"" value="""" />
            <input type=""submit"" value=""Submit"" />
              </form>
            </div>";

            var writer = new FormWriter(new Uri("/encounters/1", UriKind.Relative), HttpMethod.Post, new TextInput("field1", "field1value"), new TextInput("field2"), new TextInput("field3", string.Empty));

            Assert.AreEqual(expectedXhtml, writer.ToXhtml());
        }
 public EntryBuilder WithForm(FormWriter form)
 {
     entry.Content = SyndicationContent.CreateXhtmlContent(form.ToXhtml());
     return this;
 }
 public FeedBuilder WithForm(FormWriter form)
 {
     feed.ElementExtensions.Add(XmlReader.Create(new StringReader(form.ToXhtml())));
     return this;
 }
        public HttpResponseMessage<SyndicationItem> Post(string id, HttpRequestMessage request)
        {
            Encounter encounter;
            try
            {
                encounter = encounters.Get(int.Parse(id));
            }
            catch (KeyNotFoundException)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            if (encounter.IsResolved)
            {
                var methodNotAllowedResponse = new HttpResponseMessage {StatusCode = HttpStatusCode.MethodNotAllowed, Content = new ByteArrayContent(new byte[] {})};
                methodNotAllowedResponse.Content.Headers.Allow.Add("GET");
                throw new HttpResponseException(methodNotAllowedResponse);
            }

            var form = request.Content.ReadAs<JsonValue>(new[] {new FormUrlEncodedMediaTypeFormatter()});
            var clientEndurance = form["endurance"].ReadAs<int>();

            var result = encounter.Action(clientEndurance);
            var round = result.Round;

            var xhtml = new FormWriter(new Uri("/encounters/" + encounter.Id, UriKind.RelativeOrAbsolute), HttpMethod.Post, new TextInput("endurance", result.ClientEndurance.ToString())).ToXhtml();

            var entry = new SyndicationItem
                            {
                                BaseUri = BaseUri,
                                Title = SyndicationContent.CreatePlaintextContent("Round " + round.Id),
                                Summary = SyndicationContent.CreatePlaintextContent(string.Format("The {0} has {1} Endurance Point{2}", encounter.Title, round.Endurance, Math.Abs(round.Endurance).Equals(1) ? "" : "s")),
                                Content = SyndicationContent.CreateXhtmlContent(xhtml),
                            };
            entry.Links.Add(SyndicationLink.CreateSelfLink(new Uri(string.Format("http://{0}:8081/encounters/{1}/rounds/{2}", Environment.MachineName, encounter.Id, round.Id))));
            entry.Categories.Add(new SyndicationCategory("round"));

            if (encounter.IsResolved)
            {
                entry.Links.Add(new SyndicationLink {RelationshipType = "continue", Uri = new Uri("/rooms/" + encounter.GuardedRoomId, UriKind.Relative)});
            }
            else
            {
                entry.Content = SyndicationContent.CreateXhtmlContent(xhtml);
                entry.Links.Add(new SyndicationLink {RelationshipType = "flee", Uri = new Uri("/rooms/" + encounter.FleeRoomId, UriKind.Relative)});
            }

            var response = new HttpResponseMessage<SyndicationItem>(entry) {StatusCode = HttpStatusCode.Created};
            response.Headers.Location = new Uri(string.Format("http://{0}:8081/encounters/{1}/rounds/{2}", Environment.MachineName, encounter.Id, result.Round.Id));
            response.Content.Headers.ContentType = AtomMediaType.Entry;

            return response;
        }