Exemplo n.º 1
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
                }));
            }
        }