/// <summary>
        /// Zwraca listę kampanii
        /// </summary>
        /// <param name="request"></param>
        public CampaignsResponse Campaigns([FromBody] CampaignsRequest request)
        {
            var response = new CampaignsResponse();

            try
            {
                var adminRole = User.IsInRole("Admin");
                var id        = adminRole && request.Id != 0 ? (int)objectRepository.MultimediaObjects.FirstOrDefault(it => it.Id == request.Id).UserId : User.GetUserIDInt();

                var allCampaigns = campaignRepository.Campaigns
                                   .Where(it => it.UserId == id || (adminRole && request.Id == 0))
                                   .Select(it => new CampToMM {
                    Id = it.Id, ClickValue = it.ClickValue, EndDate = it.EndDate, IsActive = it.IsActive, Name = it.Name, ViewValue = it.ViewValue, StartDate = it.StartDate
                });

                if (adminRole)
                {
                    allCampaigns = campaignRepository.Campaigns.Select(it => new CampToMM {
                        Id = it.Id, ClickValue = it.ClickValue, EndDate = it.EndDate, IsActive = it.IsActive, Name = it.Name, ViewValue = it.ViewValue, StartDate = it.StartDate
                    });
                }

                var connectedCampaigns = new List <int>();

                if (request.Id > 0)
                {
                    var campaignsToObject = campaignRepository.CampaignsToObject(request.Id);
                    if (campaignsToObject.Any())
                    {
                        connectedCampaigns.AddRange(campaignsToObject);
                    }
                }

                response.AvailableCampaigns = new List <CampToMM>();
                response.ConnectedCampaigns = new List <CampToMM>();

                foreach (var item in allCampaigns)
                {
                    if (connectedCampaigns.Contains(item.Id))
                    {
                        response.ConnectedCampaigns.Add(item);
                    }
                    else
                    {
                        response.AvailableCampaigns.Add(item);
                    }
                }
            }
            catch (Exception ex)
            {
                response.Errors.Add(new ApiValidationErrorItem
                {
                    Message = ex.Message
                });
            }

            response.Accepted = response.Errors.Count == 0;

            return(response);
        }
        /// <summary>
        /// Pobranie listy kampanii dla zadanej kategorii
        /// </summary>
        /// <param name="request"></param>
        public CampaignsResponse Campaigns([FromBody] CampaignsRequest request)
        {
            var response = new CampaignsResponse();

            try
            {
                var allCampaigns = campaignRepository.Campaigns.Select(it => new CampToCat {
                    Id = it.Id, ClickValue = it.ClickValue, EndDate = it.EndDate, IsActive = it.IsActive, Name = it.Name, ViewValue = it.ViewValue, StartDate = it.StartDate
                });

                var connectedCampaigns = new List <int>();

                if (request.ObjectId > 0)
                {
                    var campaignsToCategory = categoryRepository.Categories
                                              .Single(it => it.Id == request.ObjectId)
                                              .Campaigns.Select(it => it.Id);

                    if (campaignsToCategory.Any())
                    {
                        connectedCampaigns.AddRange(campaignsToCategory);
                    }
                }

                response.AvailableCampaigns = new List <CampToCat>();
                response.ConnectedCampaigns = new List <CampToCat>();

                foreach (var item in allCampaigns)
                {
                    if (connectedCampaigns.Contains(item.Id))
                    {
                        response.ConnectedCampaigns.Add(item);
                    }
                    else
                    {
                        response.AvailableCampaigns.Add(item);
                    }
                }
            }
            catch (Exception ex)
            {
                response.Errors.Add(new ApiValidationErrorItem
                {
                    Message = ex.Message
                });
            }

            response.Accepted = response.Errors.Count == 0;

            return(response);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Loads the campaign with the specified id.
        /// </summary>
        /// <param name="campaignId">The campaign id.</param>
        /// <returns>Returns the campaign.</returns>
        private Campaign LoadCampaign(Guid?campaignId = null)
        {
            // Build up the request.
            CampaignsRequest campaignRequest = new CampaignsRequest()
            {
                SecurityContext = this.securityContext,
                Type            = CampaignType.InWork,
                Language        = "EN"
            };

            if (campaignId.HasValue)               // If there is a campaign id given, then load the campaign by its id.

            {
                campaignRequest.Id = campaignId.Value;

                CampaignsResponse response = this.serviceAgent.GetCampaigns(campaignRequest);

                if (response == null)
                {
                    return(null);
                }
                else
                {
                    return(response.Campaigns.FirstOrDefault());
                }
            }
            else               // If there is no campaign id given, then load the campaign by its name.

            {
                Campaign[] loadedCampaigns  = this.serviceAgent.GetCampaigns(campaignRequest).Campaigns;
                Campaign   existingCampaign = loadedCampaigns.FirstOrDefault(c => c.Name.Equals("My first campaign", StringComparison.OrdinalIgnoreCase));

                if (existingCampaign == null)
                {
                    return(loadedCampaigns.FirstOrDefault(c => c.Name.Equals(CAMPAIGN_NAME, StringComparison.OrdinalIgnoreCase)));
                }
                else
                {
                    return(existingCampaign);
                }
            }
        }