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
                }));
            }
        }
        public JsonNetResult CreateResource(CreateResource res)
        {
            try
            {
                // Create Resource Item
                ResourceItem resource = new ResourceItem()
                {
                    ItemID          = Guid.NewGuid(),
                    Title           = res.Title,
                    ItemDescription = res.ItemDescription,
                    TypeID          = res.TypeID,
                    Url             = res.Url,
                    UrlThumbnail    = res.UrlThumbnail,
                    CreatedDate     = DateTime.Now.ToCST(),
                    PostDate        = res.PostDate >= DateTime.Now.AddHours(1) ? res.PostDate : null,
                    StatusID        = res.StatusID,
                    LanguageID      = Convert.ToInt32(res.Language)
                };
                ExigoDAL.CreateResourceItem(resource);

                //Create Resource Category Item
                foreach (var categoryID in res.CategoryID)
                {
                    //2015-09-08
                    //Ivan S.
                    //66
                    //Sets the initial order for the new resource to the maximum number for that category
                    var lastResource = ExigoDAL.GetResourceCategoryItems(new GetResourceCategoryItemsRequest()
                    {
                        CategoryID = categoryID
                    })
                                       .OrderByDescending(r => r.ItemOrder)
                                       .FirstOrDefault();
                    int lastCategoryOrder = InitialOrderValue - 1;
                    if (lastResource != null)
                    {
                        lastCategoryOrder = lastResource.ItemOrder;
                    }
                    var NewOrder = ++lastCategoryOrder;

                    ResourceCategoryItem categoryItem = new ResourceCategoryItem()
                    {
                        ItemID     = resource.ItemID,
                        CategoryID = categoryID,
                        ItemOrder  = NewOrder
                    };
                    ExigoDAL.CreateResourceCategoryItem(categoryItem);
                }

                // Create the ResourceAvailability Entries
                if (res.Markets != null && res.Markets.Count() > 0)
                {
                    foreach (var CountryCode in res.Markets)
                    {
                        ResourceAvailability availableresource = new ResourceAvailability()
                        {
                            AvailabilityID = Guid.NewGuid(),
                            ItemID         = resource.ItemID,
                            CountryCode    = CountryCode,
                            LanguageID     = Convert.ToInt32(res.Language)
                        };
                        ExigoDAL.CreateResourceAvailabilities(availableresource);
                    }
                }

                //Create Tags
                List <Tag> Tags = new List <Tag>();
                if (res.Keywords.Count() > 0)
                {
                    var existingtags = ExigoDAL.GetTagsForResources(new GetTagsForResourcesRequest()
                    {
                        Names = res.Keywords
                    });
                    var existingtagnames = existingtags.Select(t => t.Name).ToList();
                    var needtocreatetags = res.Keywords.Except(existingtagnames);
                    foreach (var name in needtocreatetags)
                    {
                        //See if Tag exist in DB if not create one

                        Tag newTag = new Tag()
                        {
                            TagID = Guid.NewGuid(),
                            Name  = name
                        };
                        ExigoDAL.CreateTag(newTag);
                        Tags.Add(newTag);
                    }
                    Tags.AddRange(existingtags);
                }
                // Create the ResourceItemTag Entries
                if (Tags.Any())
                {
                    foreach (var tag in Tags)
                    {
                        ResourceItemTag rTag = new ResourceItemTag()
                        {
                            TagID  = tag.TagID,
                            ItemID = resource.ItemID
                        };
                        ExigoDAL.CreateResourceItemTag(rTag);
                    }
                }

                return(new JsonNetResult(new
                {
                    success = true,
                }));
            }
            catch (Exception ex)
            {
                return(new JsonNetResult(new
                {
                    success = false,
                    message = ex.Message
                }));
            }
        }