Esempio n. 1
0
        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(),
                };

                Exigo.ModifyResourceItem(originalResource);

                //Current Availabilities
                List <ResourceAvailability> currentAvailability = Exigo.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
                    };
                    Exigo.CreateResourceAvailabilities(availableresource);
                }

                //Delete Availabilities no longer wanted
                List <Guid> deleteAvailabilities = currentAvailability.Except(keepAvailabilities).Select(t => t.AvailabilityID).ToList();
                Exigo.DeleteResourceAvailabilities(deleteAvailabilities);

                //Modify ResourceCategoryItems

                // Current ResourceCategoryItems CategoryIDs
                var RCI = Exigo.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 = Exigo.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
                    };
                    Exigo.CreateResourceCategoryItem(categoryItem);
                }
                //Delete Unwanted CategoryItems
                var unwantedRCICategoryIDs = currentRCI.Except(wantedRCI).ToList();
                if (unwantedRCICategoryIDs.Any())
                {
                    Exigo.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 = Exigo.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);
                        Exigo.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 = Exigo.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,
                        };
                        Exigo.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)
                    {
                        Exigo.DeleteResourceItemTags(nolongerwanted);
                    }
                }

                return(new JsonNetResult(new
                {
                    success = true,
                }));
            }
            catch (Exception ex)
            {
                return(new JsonNetResult(new
                {
                    success = false,
                    message = ex.Message
                }));
            }
        }
Esempio n. 2
0
        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,
                    Language        = res.Language
                };
                Exigo.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 = Exigo.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
                    };
                    Exigo.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,
                            Market         = CountryCode,
                            Language       = res.Language
                        };
                        Exigo.CreateResourceAvailabilities(availableresource);
                    }
                }

                //Create Tags
                List <Tag> Tags = new List <Tag>();
                if (res.Keywords.Count() > 0)
                {
                    var existingtags = Exigo.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
                        };
                        Exigo.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
                        };
                        Exigo.CreateResourceItemTag(rTag);
                    }
                }

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