/// <summary> /// Adds references specific to news items /// </summary> /// <param name="HyperMediaModel">news item dto or news item details dto to add references to</param> /// <param name="Id">Id of news item resource to add reference to</param> private static void AddNewsItemReferences(this HyperMediaModel HyperMediaModel, int Id, IEnumerable <NewsItemCategoryRelation> categoryRelations, IEnumerable <AuthorNewsItemRelation> authorRelations) { // routes needed for links string newsItemRoute = Routes.BASE + Routes.NEWS_ITEM; string authorRoute = Routes.BASE + Routes.AUTHORS; string categoriesRoute = Routes.BASE + Routes.CATEGORIES; // add get, edit and update links for actions on this resource HyperMediaModel.AddBaseReferences(Id, newsItemRoute); // For each author associated with resouce, add link for getting detail info on it Func <int, string> linkAuthorByNewsItem = authorId => authorRoute + "/" + authorId; foreach (var item in authorRelations) { HyperMediaModel.Links.AddToReferenceList("authors", linkAuthorByNewsItem(item.AuthorId)); } // For each category associated with resouce, add link for getting detail info on it Func <int, string> linkCategoryDetailByNewsItem = categoryId => categoriesRoute + "/" + categoryId; foreach (var item in categoryRelations) { HyperMediaModel.Links.AddToReferenceList("categories", linkCategoryDetailByNewsItem(item.CategoryId)); } }
/// <summary> /// Adds self, edit and delete links for resources /// (All these actions are supported for all types of DTOs in system) /// </summary> /// <param name="HyperMediaModel">Hypermedia model to add reference to self, edit and delete</param> /// <param name="Id">Id of resource to add reference to</param> private static void AddBaseReferences(this HyperMediaModel HyperMediaModel, int Id, string ResourseBaseRoute) { string RouteToResource = ResourseBaseRoute + "/" + Id.ToString(); HyperMediaModel.Links.AddReference("self", RouteToResource); HyperMediaModel.Links.AddReference("edit", RouteToResource); HyperMediaModel.Links.AddReference("delete", RouteToResource); }
/// <summary> /// Adds appropriate references to categories resources /// </summary> /// <param name="HyperMediaModel">category dto or category details dto to add references to</param> /// <param name="Id">Id of category resource to add reference to</param> private static void AddCategoryReferences(this HyperMediaModel HyperMediaModel, int Id) { // routes needed for links string categoriesRoute = Routes.BASE + Routes.CATEGORIES; // add get, edit and update links for actions on this resource HyperMediaModel.AddBaseReferences(Id, categoriesRoute); }
private void AddLinksToAuthorDto(HyperMediaModel a, int Id) { a.Links.AddReference("self", new { href = $"/api/authors/{Id}" }); a.Links.AddReference("edit", new { href = $"/api/authors/{Id}" }); a.Links.AddReference("delete", new { href = $"/api/authors/{Id}" }); a.Links.AddReference("newsItems", new { href = $"/api/authors/{Id}/newsItems" }); a.Links.AddListReference("newsItemsDetailed", _newsItemRepository.GetNewsItemsByAuthorId(Id).Select(n => new { href = $"/api/{n.Id}" })); }
private void AddLinksToNewsItems(HyperMediaModel n, int id) { n.Links.AddReference("self", new { href = $"/api/{id}" }); n.Links.AddReference("edit", new { href = $"/api/{id}" }); n.Links.AddReference("delete", new { href = $"/api/{id}" }); n.Links.AddListReference("authors", _authorRepository.GetAuthorsByNewsItemId(id).Select(a => new { href = $"/api/authors/{a.AuthorId}" })); n.Links.AddListReference("categories", _categoryRepository.GetCategoryByNewsItemId(id).Select(c => new { href = $"/api/categories/{c.CategoryId}" })); }
/// <summary> /// Adds references to authors resources /// Needs list on author news item relations so that news items for authors are correctly referenced /// </summary> /// <param name="HyperMediaModel">author dto or author details dto to add references to</param> /// <param name="Id">Id of author resource to add reference to</param> /// <param name="newsItems">All news items associated with author</param> private static void AddAuthorReferences(this HyperMediaModel HyperMediaModel, int Id, IEnumerable <AuthorNewsItemRelation> newsItems) { // routes needed for links string newsItemRoute = Routes.BASE + Routes.NEWS_ITEM; string authorRoute = Routes.BASE + Routes.AUTHORS; // add get, edit and update links for actions on this resource HyperMediaModel.AddBaseReferences(Id, authorRoute); // add links to get list of all authored items for this resource string linkToAllAuthoredNewsItems = authorRoute + "/" + Id + "/newsItems"; HyperMediaModel.Links.AddReference("newsItems", linkToAllAuthoredNewsItems); // add links to get a detail info of news item related to this resource Func <int, string> linkNewsItemDetailByAuthor = newsItemId => newsItemRoute + "/" + newsItemId; foreach (var item in newsItems) { HyperMediaModel.Links.AddToReferenceList("newsItemsDetailed", linkNewsItemDetailByAuthor(item.NewsItemId)); } }
private void AddLinksToCategory(HyperMediaModel category, int id) { category.Links.AddReference("self", new { href = $"/api/categories/{id})" }); category.Links.AddReference("edit", new { href = $"/api/categories/{id})" }); category.Links.AddReference("delete", new { href = $"/api/categories/{id})" }); }