public Resource Create(string selfUri, GameInfo gameInfo) { // Create the HAL document with the given self URI. var halDocument = new Resource(new Link(selfUri)); // Add the curies link. var curiesLink = new Link(_uriHelper.GetRelationsUri()) { Templated = true }; halDocument.AddLink("curies", curiesLink); // The manager's current team. if (gameInfo != null) { var currentTeamLink = new Link(_uriHelper.GetTeamUri(gameInfo.Id, gameInfo.CurrentTeamId)); halDocument.AddLink("rel:managers-team", currentTeamLink); } // var mainLinks = new List<Link>(); // Add the home link to the main links. // var homeLink = new Link(_uriHelper.GetHomeUri()) { Title = "Home" }; // mainLinks.Add(homeLink); // if (_loggedIn) // { // Add the Games link. // var gamesLink = new Link(_uriHelper.GetGamesUri()) { Title = "Games" }; // mainLinks.Add(gamesLink); // Add game specific links. // if (gameInfo != null) // { // CreateGameMenu(gameInfo, halDocument); // } // } // halDocument.AddLink("main", mainLinks); return(halDocument); }
private void CreateGameMenu(GameInfo gameInfo, Resource halDocument) { var serviceFactory = new ServiceFactory(); // Create the game menu. var gameLinks = new List <Link>(); // The game dashboard. var gameLink = new Link(UriHelper.GetGameUri(gameInfo.Id)) { Title = "Dashboard" }; gameLinks.Add(gameLink); /* * * * * // The manager's squad. * var teamPlayersLink = new Link(UriHelper.GetTeamPlayersUri(gameInfo.Id, gameInfo.CurrentTeamId)) { Title = "Players" }; * gameLinks.Add(teamPlayersLink); * * // League tables for the current season. * var seasonService = serviceFactory.CreateSeasonService(gameInfo); * var currentSeason = seasonService.GetCurrentSeason(); * var leagueTableLink = new Link(UriHelper.GetSeasonLeagueTablesUri(gameInfo.Id, currentSeason.Id)) { Title = "League Tables" }; * gameLinks.Add(leagueTableLink); * * // Matches of the manager's team in the current season. * var teamMatchesLink = new Link(UriHelper.GetSeasonTeamMatchesUri(gameInfo.Id, currentSeason.Id, gameInfo.CurrentTeamId)) { Title = "Matches" }; * gameLinks.Add(teamMatchesLink); * * // Other teams, starting with the manager's team. * var otherTeamsLink = new Link(UriHelper.GetTeamUri(gameInfo.Id, gameInfo.CurrentTeamId)) { Title = "Other Teams" }; * gameLinks.Add(otherTeamsLink); * * // Seasons, starting with the current season. * var seasonLink = new Link(UriHelper.GetSeasonUri(gameInfo.Id, currentSeason.Id)) { Title = "Seasons" }; * gameLinks.Add(seasonLink); * * */ halDocument.AddLink("game", gameLinks); }
private void AddPlayNextMatchDayForm(GameInfo gameInfo, Resource halDocument, DateTime matchDate) { var seasonService = ServiceFactory.CreateSeasonService(gameInfo); var matchService = ServiceFactory.CreateMatchService(gameInfo); var currentSeason = seasonService.GetCurrentSeason(); var nextMatchDate = matchService.GetNextMatchDate(currentSeason.Id); if (nextMatchDate.HasValue && matchDate == nextMatchDate) { // Add a form to play the match day. var matchDayResourceFactory = new MatchDayResourceFactory(UriHelper, gameInfo.Id, nextMatchDate.Value); var form = matchDayResourceFactory.GetForm(); halDocument.AddForm(form); // Also add a link to the match day. var link = matchDayResourceFactory.GetLink(); halDocument.AddLink("rel:next-match-day", link); } }
public void TestHalDocumentFromTheSpecification() { var halDocument = new Resource(new Link("/orders")); halDocument.AddLink("curies", new[] { new Link("http://example.com/docs/rels/{rel}") { Name = "ea", Templated = true } }); halDocument.AddLink("next", new Link("/orders?page=2")); halDocument.AddLink("ea:find", new Link("/orders{?id}") { Templated = true }); halDocument.AddLink("ea:admin", new[] { new Link("/admins/2") { Title = "Fred" }, new Link("/admins/5") { Title = "Kate" } }); halDocument.AddProperty("currentlyProcessing", 14); halDocument.AddProperty("shippedToday", 20); var order1 = new Resource(new Link("/orders/123")); order1.AddLink("ea:basket", new Link("/baskets/98712")); order1.AddLink("ea:customer", new Link("/customers/7809")); order1.AddProperty("total", 30); order1.AddProperty("currency", "USD"); order1.AddProperty("status", "shipped"); var order2 = new Resource(new Link("/orders/124")); order2.AddLink("ea:basket", new Link("/baskets/97213")); order2.AddLink("ea:customer", new Link("/customers/12369")); order2.AddProperty("total", 20); order2.AddProperty("currency", "USD"); order2.AddProperty("status", "processing"); halDocument.AddResource("ea:order", new[] { order1, order2 }); var settings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, Formatting = Formatting.Indented, Converters = new List <JsonConverter> { new DecimalJsonConverter() } }; File.WriteAllText(@"D:\Temp\hal.json", halDocument.Json.ToString()); // The document contains 4 properties: _links, currentlyProcessing, shippedToday and _embedded. Assert.AreEqual(4, halDocument.Json.Count); // The _links object contains 5 links. Assert.AreEqual(5, halDocument.Json["_links"].Count()); // The self link only contains a href property. Assert.AreEqual(1, halDocument.Json["_links"]["self"].Count()); Assert.AreEqual("/orders", halDocument.Json["_links"]["self"]["href"]); // The curies link array contains one link with 3 properties, name, href and templated. Assert.AreEqual(1, halDocument.Json["_links"]["curies"].Count()); Assert.AreEqual(3, halDocument.Json["_links"]["curies"][0].Count()); Assert.AreEqual("ea", halDocument.Json["_links"]["curies"][0]["name"]); Assert.AreEqual("http://example.com/docs/rels/{rel}", halDocument.Json["_links"]["curies"][0]["href"]); Assert.AreEqual(true, halDocument.Json["_links"]["curies"][0]["templated"]); // The next link only contains a href property. Assert.AreEqual(1, halDocument.Json["_links"]["next"].Count()); Assert.AreEqual("/orders?page=2", halDocument.Json["_links"]["next"]["href"]); // The ea:find link contains an href and templated property. Assert.AreEqual(2, halDocument.Json["_links"]["ea:find"].Count()); Assert.AreEqual("/orders{?id}", halDocument.Json["_links"]["ea:find"]["href"]); // The ea:admin link array contains two links, with 2 properties each: href and title. Assert.AreEqual(2, halDocument.Json["_links"]["ea:admin"].Count()); Assert.AreEqual(2, halDocument.Json["_links"]["ea:admin"][0].Count()); Assert.AreEqual("/admins/2", halDocument.Json["_links"]["ea:admin"][0]["href"]); Assert.AreEqual("Fred", halDocument.Json["_links"]["ea:admin"][0]["title"]); Assert.AreEqual(2, halDocument.Json["_links"]["ea:admin"][1].Count()); Assert.AreEqual("/admins/5", halDocument.Json["_links"]["ea:admin"][1]["href"]); Assert.AreEqual("Kate", halDocument.Json["_links"]["ea:admin"][1]["title"]); // This resource contains two properties: currentlyProcessing and shippedToday. Assert.AreEqual(14, halDocument.Json["currentlyProcessing"]); Assert.AreEqual(20, halDocument.Json["shippedToday"]); // The _embedded object contains one property: ea:order, which is an array of two orders with 4 properties each. Assert.AreEqual(1, halDocument.Json["_embedded"].Count()); Assert.AreEqual(2, halDocument.Json["_embedded"]["ea:order"].Count()); Assert.AreEqual(4, halDocument.Json["_embedded"]["ea:order"][0].Count()); Assert.AreEqual(3, halDocument.Json["_embedded"]["ea:order"][0]["_links"].Count()); Assert.AreEqual("/orders/123", halDocument.Json["_embedded"]["ea:order"][0]["_links"]["self"]["href"]); Assert.AreEqual("/baskets/98712", halDocument.Json["_embedded"]["ea:order"][0]["_links"]["ea:basket"]["href"]); Assert.AreEqual("/customers/7809", halDocument.Json["_embedded"]["ea:order"][0]["_links"]["ea:customer"]["href"]); Assert.AreEqual(30, halDocument.Json["_embedded"]["ea:order"][0]["total"]); Assert.AreEqual("USD", halDocument.Json["_embedded"]["ea:order"][0]["currency"]); Assert.AreEqual("shipped", halDocument.Json["_embedded"]["ea:order"][0]["status"]); Assert.AreEqual(4, halDocument.Json["_embedded"]["ea:order"][1].Count()); Assert.AreEqual(3, halDocument.Json["_embedded"]["ea:order"][1]["_links"].Count()); Assert.AreEqual("/orders/124", halDocument.Json["_embedded"]["ea:order"][1]["_links"]["self"]["href"]); Assert.AreEqual("/baskets/97213", halDocument.Json["_embedded"]["ea:order"][1]["_links"]["ea:basket"]["href"]); Assert.AreEqual("/customers/12369", halDocument.Json["_embedded"]["ea:order"][1]["_links"]["ea:customer"]["href"]); Assert.AreEqual(20, halDocument.Json["_embedded"]["ea:order"][1]["total"]); Assert.AreEqual("USD", halDocument.Json["_embedded"]["ea:order"][1]["currency"]); Assert.AreEqual("processing", halDocument.Json["_embedded"]["ea:order"][1]["status"]); }