public IActionResult Prepare()
        {
            CampaignContext context = cache.Get <CampaignContext>("context");

            context.Process();
            return(View("Run", context.Campaign));
        }
Example #2
0
 public override string Execute(List <string> parameters)
 {
     if (parameters.Count != 6)
     {
         return(ErrorType.PARAMETER_IS_NOT_SUFFICIENT.ToString());
     }
     try
     {
         string          name             = parameters[1];
         string          productCode      = parameters[2];
         int             duration         = int.Parse(parameters[3]);
         int             pmLimit          = int.Parse(parameters[4]);
         int             targetSalesCount = int.Parse(parameters[5]);
         CampaignContext context          = new CampaignContext();
         Campaign        campaign         = new Campaign(name, productCode, Time.GetTime(), duration, pmLimit, targetSalesCount);
         bool            result           = context.Add(campaign);
         if (!result)
         {
             return(ErrorType.CAMPAIGN_ALREADY_EXISTS.ToString());
         }
         else
         {
             return($"Campaign created; name {name}, product {productCode}, duration {duration}, limit {pmLimit}, target sales count {targetSalesCount}");
         };
     }
     catch (System.Exception)
     {
         return(ErrorType.UNKNOWN_EXCEPTION.ToString());
     }
 }
        public override string Execute(List <string> parameters)
        {
            if (parameters.Count != 2)
            {
                return(ErrorType.PARAMETER_IS_NOT_SUFFICIENT.ToString());
            }
            CampaignContext campaignContext = new CampaignContext();
            Campaign        campaign        = campaignContext.GetByName(parameters[1]);

            if (campaign == null)
            {
                return(ErrorType.CAMPAIGN_NOT_FOUND.ToString());
            }
            OrderContext orderContext = new OrderContext();
            List <Order> orders       = orderContext.GetOrdersByCampaign(campaign);
            string       statusDesc   = "";

            if (campaign.GetActive() == true)
            {
                statusDesc = "Not Ended";
            }
            else
            {
                statusDesc = "Ended";
            }
            double totalSales   = orders.Sum(x => x.Quantity);
            double totalRevenue = orders.Sum(x => x.Price * x.Quantity);
            double averagePrice = totalRevenue / totalSales;
            double turnover     = totalSales / campaign.TargetSalesCount;

            return($"Campaign {campaign.Name} info; Status {statusDesc}, Target Sales {campaign.TargetSalesCount},Total Sales {totalSales}, Turnover {turnover}, Average Item Price {averagePrice}");
        }
        public IActionResult Run()
        {
            CampaignContext context = cache.Get <CampaignContext>("context");

            context.Process();
            return(View("Index", context.Campaign));
        }
 public override string Execute(List <string> parameters)
 {
     if (parameters.Count != 3)
     {
         return(ErrorType.PARAMETER_IS_NOT_SUFFICIENT.ToString());
     }
     try
     {
         string         productCode    = parameters[1];
         double         quantity       = double.Parse(parameters[2]);
         ProductContext productContext = new ProductContext();
         Product        product        = productContext.Get(productCode);
         if (product == null)
         {
             return(ErrorType.PRODUCT_NOT_FOUND.ToString());
         }
         if (product.GetStock() < quantity)
         {
             return(ErrorType.PRODUCT_STOCK_IS_NOT_SUFFICIENT.ToString());
         }
         product.DecreaseStock(quantity);
         CampaignContext campaignContext = new CampaignContext();
         var             response        = campaignContext.PriceByProduct(product);
         Order           order           = new Order(productCode, response.Item1, quantity, response.Item2);
         OrderContext    orderContext    = new OrderContext();
         orderContext.Add(order);
         return($"Order created; product {productCode}, quantity {quantity}");
     }
     catch (System.Exception)
     {
         return(ErrorType.UNKNOWN_EXCEPTION.ToString());
     }
 }
Example #6
0
        public CampaignContext GetCurrentStats(Campaign campaign)
        {
            this.TryToLogin();
            for (; ;)
            {
                DateTime today = DateTime.Today; DateTime tomorrow = today.AddDays(2);
                dynamic  json = this.Get($"https://ssp-api.propellerads.com/v5/adv/statistics/campaigns?date_from={today.Year}-{today.Month}-{today.Day}&date_to={tomorrow.Year}-{tomorrow.Month}-{tomorrow.Day}&page_size=10000&campaign_id[]=" + campaign.ID);
                if (json == null)
                {
                    continue;
                }

                if (json.result.ToString().Equals("[]"))
                {
                    return(new CampaignContext());
                }

                CampaignContext result = new CampaignContext()
                {
                    Clicks      = int.Parse(json.result[0].impressions.ToString()),
                    Conversions = int.Parse(json.result[0].conversions.ToString()),
                    Payout      = 0.0,
                    Spent       = double.Parse(json.result[0].money.ToString())
                };
                return(result);
            }
        }
Example #7
0
 public override string Execute(List <string> parameters)
 {
     if (parameters.Count != 2)
     {
         return(ErrorType.PARAMETER_IS_NOT_SUFFICIENT.ToString());;
     }
     try
     {
         string         productCode = parameters[1];
         ProductContext context     = new ProductContext();
         Product        product     = context.Get(productCode);
         if (product == null)
         {
             return(ErrorType.PRODUCT_NOT_FOUND.ToString());
         }
         else
         {
             CampaignContext          campaignContext = new CampaignContext();
             Tuple <double, Campaign> response        = campaignContext.PriceByProduct(product);
             return($"Product {product.ProductCode} info; price {response.Item1}, stock {product.GetStock()}");
         }
     }
     catch (System.Exception)
     {
         return(ErrorType.UNKNOWN_EXCEPTION.ToString());
     }
 }
        public IActionResult Create(Campaign obj)
        {
            CampaignContext context = cache.Get <CampaignContext>("context");

            context.Campaign = obj;
            context.Process();
            return(View("Approve", obj));
        }
        public IActionResult Index()
        {
            ICampaignState  initialState = new CreateState();
            CampaignContext context      = new CampaignContext(initialState);

            cache.Set("context", context);
            return(View());
        }
        // GET: /Campaign/
        public ActionResult Index(string sortOrder, string currentFilter, string searchString, int?page)
        {
            db = new CampaignContext();

            ViewBag.CurrentSort  = sortOrder;
            ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
            ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";

            if (searchString != null)
            {
                page = 1;
            }
            else
            {
                searchString = currentFilter;
            }

            ViewBag.CurrentFilter = searchString;

            var campaigns = from c in db.Campaigns
                            select c;

            if (!String.IsNullOrEmpty(searchString))
            {
                if (searchString == "0")
                {
                    campaigns = campaigns.Where(s => s.StartDate > DateTime.Now || s.EndDate < DateTime.Now);
                }
                else if (searchString == "1")
                {
                    campaigns = campaigns.Where(s => s.StartDate <= DateTime.Now && s.EndDate >= DateTime.Now);
                }
            }

            switch (sortOrder)
            {
            case "name_desc":
                campaigns = campaigns.OrderByDescending(s => s.CampaignName);
                break;

            case "Date":
                campaigns = campaigns.OrderBy(s => s.StartDate);
                break;

            case "date_desc":
                campaigns = campaigns.OrderByDescending(s => s.StartDate);
                break;

            default:
                campaigns = campaigns.OrderBy(s => s.CampaignName);
                break;
            }

            int pageSize   = 3;
            int pageNumber = (page ?? 1);

            return(View(campaigns.ToPagedList(pageNumber, pageSize)));
        }
Example #11
0
        public static string IncreaseTime(int param)
        {
            current += param;
            CampaignContext campaignContext = new CampaignContext();
            List <Campaign> list            = campaignContext.ActiveCampaign();

            List <Campaign> updateList = list.Where(x => x.StartTime + x.Duration <= current).ToList();

            campaignContext.DeactiveAll(updateList);
            return($"Time is {current}:00");
        }
        private CampaignContext CreateDataContext()
        {
            DbContextOptions = new DbContextOptionsBuilder()
                               .UseInMemoryDatabase(nameof(BonusEngineContext))
                               .Options;

            var context = new CampaignContext(DbContextOptions);

            context.Database.EnsureDeleted();
            context.Database.EnsureCreated();

            return(context);
        }
        public Tuple <double, Campaign> PriceByProduct(Product product)
        {
            CampaignContext campaignContext = new CampaignContext();
            double          price           = product.Price;
            Campaign        campaign        = campaignContext.List().Where(x => x.GetActive() == true && x.ProductCode.Equals(product.ProductCode)).SingleOrDefault();

            if (campaign != null)
            {
                int    currentTime  = Time.GetTime();
                int    diff         = currentTime - campaign.StartTime;
                double discountRate = campaign.PmLimit / campaign.Duration * diff;
                if (discountRate > 0)
                {
                    if (discountRate > campaign.PmLimit)
                    {
                        discountRate = campaign.PmLimit;
                    }
                    price = (int)(price * (1 - discountRate / 100));
                }
            }

            return(Tuple.Create(price, campaign));
        }
Example #14
0
 public CampaignRepository()
 {
     _db = new CampaignContext();
     _db.Campaigns.Load();
 }
Example #15
0
 public CampaignsController(CampaignContext context)
 {
     _context       = context;
     SucessMeassage = new SucessMeassage();
     ErrorMessage   = new ErrorMessage();
 }
Example #16
0
 public CampaignServices(CampaignContext appDb, IMapper mapper)
 {
     _appDb  = appDb;
     _mapper = mapper;
 }
Example #17
0
 public CampRepository(CampaignContext context)
 {
     _context = context;
 }
Example #18
0
        public static void Seed(CampaignContext context)
        {
            var today        = DateTime.UtcNow;
            var creationDate = DateTime.UtcNow.AddMonths(-1);

            if (!context.Campaigns.Any())
            {
                CampaignEntities = new List <CampaignEntity>
                {
                    new CampaignEntity
                    {
                        Id                = DeletedCampaignId,
                        Name              = "DeletedCampaign",
                        Reward            = 1500,
                        CreationDate      = creationDate,
                        CompletionCount   = 1,
                        IsDeleted         = true,
                        IsEnabled         = true,
                        FromDate          = creationDate,
                        ToDate            = today.AddDays(2),
                        ConditionEntities = new List <ConditionEntity>
                        {
                            new ConditionEntity
                            {
                                BonusTypeName = BonusTypeSignUp
                            }
                        }
                    },
                    new CampaignEntity
                    {
                        Id                = ActiveCampaignId,
                        Name              = "ActiveCampaign",
                        Reward            = 1500,
                        CreationDate      = creationDate.AddDays(1),
                        FromDate          = today,
                        ToDate            = today.AddDays(2),
                        CompletionCount   = 1,
                        IsDeleted         = false,
                        IsEnabled         = true,
                        ConditionEntities = new List <ConditionEntity>
                        {
                            new ConditionEntity
                            {
                                BonusTypeName = BonusTypeSignUp
                            }
                        }
                    },
                    new CampaignEntity
                    {
                        Name            = "ActiveWithoutEndDate",
                        Reward          = 1500,
                        CreationDate    = creationDate,
                        FromDate        = today,
                        CompletionCount = 1,
                        IsDeleted       = false,
                        IsEnabled       = true
                    },
                    new CampaignEntity
                    {
                        Name            = "PendingCampaign",
                        Reward          = 1500,
                        CreationDate    = creationDate,
                        FromDate        = today.AddDays(10),
                        ToDate          = today.AddDays(15),
                        CompletionCount = 1,
                        IsDeleted       = false,
                        IsEnabled       = true
                    },
                    new CampaignEntity
                    {
                        Id              = NotEnabledCampaignId,
                        Name            = "NotEnabledCampaign",
                        Reward          = 1500,
                        CreationDate    = creationDate,
                        FromDate        = creationDate,
                        CompletionCount = 1,
                        IsDeleted       = false,
                        IsEnabled       = false
                    },
                    new CampaignEntity
                    {
                        Name            = "CompletedCampaign",
                        Reward          = 1500,
                        CreationDate    = creationDate,
                        FromDate        = creationDate,
                        ToDate          = creationDate.AddDays(2),
                        CompletionCount = 1,
                        IsDeleted       = false,
                        IsEnabled       = true
                    }
                };

                context.Campaigns.AddRange(CampaignEntities);
                context.SaveChangesAsync();
            }
        }
 public Campaign1Controller(CampaignContext context)
 {
     _context = context;
 }
 public CampaignsController(CampaignContext appDb, ICampaignServices campaignServices)
 {
     _appDb            = appDb;
     _campaignServices = campaignServices;
 }