public JsonNetResult EditResource(CreateResource res, string editID) { try { var itemID = Guid.Parse(editID); //Update ResoureItem ResourceItem originalResource = new ResourceItem() { ItemID = itemID, Title = res.Title, ItemDescription = res.ItemDescription, Url = res.Url, UrlThumbnail = res.UrlThumbnail, TypeID = res.TypeID, StatusID = res.StatusID, PostDate = res.PostDate ?? DateTime.Now.ToCST(), Language = res.Language, CreatedDate = DateTime.Now.ToCST(), }; ExigoDAL.ModifyResourceItem(originalResource); //Current Availabilities List <ResourceAvailability> currentAvailability = ExigoDAL.GetResourceAvailabilities(new GetResourceAvailabilitiesRequest() { ItemID = originalResource.ItemID }).ToList(); //Wanted Availabilities List <ResourceAvailability> keepAvailabilities = new List <ResourceAvailability>(); List <Available> neededAvailabilities = new List <Available>(); foreach (var market in res.Markets) { var availability = new Available(); availability.Language = res.Language; availability.Market = market; var exist = currentAvailability.Where(ca => ca.Language == availability.Language && ca.Market == availability.Market).FirstOrDefault(); if (exist == null) { neededAvailabilities.Add(availability); } else { keepAvailabilities.Add(exist); } } //Add NEW Availabilities foreach (var avail in neededAvailabilities) { ResourceAvailability availableresource = new ResourceAvailability() { AvailabilityID = Guid.NewGuid(), ItemID = originalResource.ItemID, Market = avail.Market, Language = avail.Language }; ExigoDAL.CreateResourceAvailabilities(availableresource); } //Delete Availabilities no longer wanted List <Guid> deleteAvailabilities = currentAvailability.Except(keepAvailabilities).Select(t => t.AvailabilityID).ToList(); ExigoDAL.DeleteResourceAvailabilities(deleteAvailabilities); //Modify ResourceCategoryItems // Current ResourceCategoryItems CategoryIDs var RCI = ExigoDAL.GetResourceCategoryItems(new GetResourceCategoryItemsRequest() { ItemID = originalResource.ItemID }); var currentRCI = RCI.Select(rci => rci.CategoryID); // Wanted CategoryIDs var wantedRCI = res.CategoryID; // Needed CategoryIDs var neededRCI = wantedRCI.Except(currentRCI); //Add New CategoryItems foreach (var category in neededRCI) { var lastResource = ExigoDAL.GetResourceCategoryItems(new GetResourceCategoryItemsRequest() { CategoryID = category }) .OrderByDescending(r => r.ItemOrder) .FirstOrDefault(); int lastItemOrder = InitialOrderValue - 1; if (lastResource != null) { lastItemOrder = lastResource.ItemOrder; } ResourceCategoryItem categoryItem = new ResourceCategoryItem() { ItemID = originalResource.ItemID, CategoryID = category, ItemOrder = lastItemOrder }; ExigoDAL.CreateResourceCategoryItem(categoryItem); } //Delete Unwanted CategoryItems var unwantedRCICategoryIDs = currentRCI.Except(wantedRCI).ToList(); if (unwantedRCICategoryIDs.Any()) { ExigoDAL.DeleteResourceCategoryItems(itemID, unwantedRCICategoryIDs); } List <Tag> tags = new List <Tag>(); if (res.Keywords.Any()) { // check to see if tag existis with these keywords. if not add it //Get A List Of Tags Using Provided Keywords var existingTags = ExigoDAL.GetTagsForResources(new GetTagsForResourcesRequest() { Names = res.Keywords }); var existingTagNames = existingTags.Select(et => et.Name); //Get a List of Keywords we need to make tags for var newTagsneeded = res.Keywords.Except(existingTagNames); //Create The New Tags List <Tag> NewlyCreatedTags = new List <Tag>(); foreach (var keyword in newTagsneeded) { Tag newTag = new Tag() { TagID = Guid.NewGuid(), Name = keyword }; NewlyCreatedTags.Add(newTag); ExigoDAL.CreateTag(newTag); } //Combine existing and New Tags var completeTagList = existingTags.Union(NewlyCreatedTags).ToList(); var completeTagIDs = completeTagList.Select(v => v.TagID); //Get a list of ItemTags currently associated with the Item var currentItemTagIDs = ExigoDAL.GetResourceItemTags(new GetResourceItemTagsRequest() { ItemID = originalResource.ItemID }).Select(v => v.TagID); //Get a list of Tags we need to make ItemTags for var newItemTagsneeded = completeTagList.Where(t => !currentItemTagIDs.Contains(t.TagID)); // Update ResourceItemTags Table with new Tags foreach (var tagneeded in newItemTagsneeded) { ResourceItemTag rit = new ResourceItemTag() { TagID = tagneeded.TagID, ItemID = itemID, }; ExigoDAL.CreateResourceItemTag(rit); } // remove any pivot table tags that are no longer being used for this resource var nolongerwanted = currentItemTagIDs.Except(completeTagIDs).ToList(); if (nolongerwanted.Count > 0) { ExigoDAL.DeleteResourceItemTags(nolongerwanted); } } return(new JsonNetResult(new { success = true, })); } catch (Exception ex) { return(new JsonNetResult(new { success = false, message = ex.Message })); } }