Exemplo n.º 1
0
        public HttpStatusCodeResult LaunchCampaign(LaunchCampaignData data)
        {
            if (string.IsNullOrWhiteSpace(data.CampaignTitle) && string.IsNullOrWhiteSpace(data.Description) && string.IsNullOrWhiteSpace(data.Alias))
            {
                string error = "name|" + T("Campiagn Title can't be empty").ToString() + "|campaign_description_text|" + T("Campiagn Description can't be empty").ToString() + "|url|" + T("Campiagn URL can't be empty").ToString();
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest, error);
            }

            if (string.IsNullOrWhiteSpace(data.CampaignTitle) && string.IsNullOrWhiteSpace(data.Description))
            {
                string error = "name|" + T("Campiagn Title can't be empty").ToString() + "|campaign_description_text|" + T("Campiagn Description can't be empty").ToString();
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest, error);
            }

            if (string.IsNullOrWhiteSpace(data.CampaignTitle) && string.IsNullOrWhiteSpace(data.Alias))
            {
                string error = "name|" + T("Campiagn Title can't be empty").ToString() + "|url|" + T("Campiagn URL can't be empty").ToString();
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest, error);
            }

            if (string.IsNullOrWhiteSpace(data.Description) && string.IsNullOrWhiteSpace(data.Alias))
            {
                string error = "campaign_description_text|" + T("Campiagn Description can't be empty").ToString() + "|url|" + T("Campiagn URL can't be empty").ToString();
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest, error);
            }

            if (string.IsNullOrWhiteSpace(data.CampaignTitle))
            {
                string error = "name|" + T("Campiagn Title can't be empty").ToString();
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest, error);
            }

            if (string.IsNullOrWhiteSpace(data.Description))
            {
                string error = "campaign_description_text|" + T("Campiagn Description can't be empty").ToString();
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest, error);
            }

            if (string.IsNullOrWhiteSpace(data.Alias))
            {
                string error = "url|" + T("Campiagn URL can't be empty").ToString();
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest, error);
            }

            data.Alias = data.Alias.Trim();

            if (data.Alias.Any(ch => Char.IsWhiteSpace(ch)))
            {
                string error = "url|" + T("Campiagn URL can't contain whitespaces").ToString();
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest, error);
            }

            if (data.Alias.Contains('&') || data.Alias.Contains('?') || data.Alias.Contains('/') || data.Alias.Contains('\\'))
            {
                string error = "url|" + T("Campiagn URL has wrong format").ToString();
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest, error);
            }

            if (_campaignService.GetCampaignByAlias(data.Alias) != null)
            {
                string error = "url|" + T("Campiagn with this URL already exists").ToString();
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest, error);
            }
            if (data.Alias.Length < 4)
            {
                string error = "url|" + T("Campiagn URL must be at least 4 characters long").ToString();
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest, error);
            }

            if (string.IsNullOrWhiteSpace(data.Design))
            {
                string error = "Design|" + T("No design found for your campaign").ToString();
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest, error);
            }

            if (_orchardServices.WorkContext.CurrentUser == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.Forbidden);
            }

            try
            {
                foreach (var prod in data.Products)
                {
                    double price = 0;
                    if (!double.TryParse(prod.Price, out price))
                    {
                        double.TryParse(prod.Price.Replace('.', ','), out price);
                    }
                    double cost = 0;
                    if (!double.TryParse(prod.BaseCost, out cost))
                    {
                        double.TryParse(prod.BaseCost.Replace('.', ','), out cost);
                    }

                    if (price < cost)
                    {
                        prod.Price = prod.BaseCost;
                    }
                }

                data.CampaignCulture = cultureUsed; ////TODO: (auth:keinlekan) После удаления поля в таблице/моделе - удалить данный код

                var campaign = _campaignService.CreateNewCampiagn(data);
                CreateImagesForCampaignProducts(campaign);
                var pathToTemplates = Server.MapPath("/Modules/Teeyoot.Module/Content/message-templates/");
                var pathToMedia = Request.Url.Scheme + "://" + Request.Url.Authority + Request.ApplicationPath.TrimEnd('/');
                _teeyootMessagingService.SendNewCampaignAdminMessage(pathToTemplates, pathToMedia, campaign.Id);
               
                return new HttpStatusCodeResult(HttpStatusCode.OK);
            }
            catch (Exception e)
            {
                Logger.Error("Error occured when trying to create new campaign ---------------> " + e.ToString());
                return new HttpStatusCodeResult(HttpStatusCode.InternalServerError, T("Error occured when trying to create new campaign").ToString());
            }
        }
Exemplo n.º 2
0
        public CampaignRecord CreateNewCampiagn(LaunchCampaignData data)
        {
            var user = Services.WorkContext.CurrentUser;
            var teeyootUser = user.ContentItem.Get(typeof(TeeyootUserPart));
            int? userId = null;

            if (teeyootUser != null)
            {
                userId = teeyootUser.ContentItem.Record.Id;
            }

            try
            {
                var newCampaign = new CampaignRecord
                {
                    Alias = data.Alias,
                    BackSideByDefault = data.BackSideByDefault,
                    Description = data.Description,
                    Design = data.Design,
                    EndDate = DateTime.UtcNow.AddDays(data.CampaignLength),
                    IsForCharity = data.IsForCharity,
                    StartDate = DateTime.UtcNow,
                    ProductCountGoal = data.ProductCountGoal,
                    ProductCountSold = 0,
                    TeeyootUserId = userId,
                    Title = data.CampaignTitle,
                    IsActive = true,
                    IsApproved = false,
                    CampaignStatusRecord = _statusRepository.Table.First(s => s.Name == CampaignStatus.Unpaid.ToString()),
                    CampaignProfit = data.CampaignProfit ?? string.Empty,
                    ProductMinimumGoal = data.ProductMinimumGoal == 0 ? 1 : data.ProductMinimumGoal,
                    CampaignCulture = (data.CampaignCulture == null || string.IsNullOrEmpty(data.CampaignCulture)) ? "en-MY" : data.CampaignCulture.Trim(), //TODO: (auth:keinlekan) Удалить код после удаления поля из таблицы/модели
                    CntBackColor = data.CntBackColor,
                    CntFrontColor = data.CntFrontColor,
                    //CountryRecord = _countryService.GetCountryByCulture(_workContextAccessor.GetContext().CurrentCulture.Trim()),
                    CountryRecord = user.ContentItem.As<TeeyootUserPart>().CountryRecord,
                    CurrencyRecord = user.ContentItem.As<TeeyootUserPart>().CurrencyRecord
                };
                _campaignRepository.Create(newCampaign);

                //TODO: (auth:keinlekan) Удалить данный код после локализации
                var culture = _workContextAccessor.GetContext().CurrentCulture.Trim();
                string cultureUsed = culture == "en-SG" ? "en-SG" : (culture == "id-ID" ? "id-ID" : "en-MY");
                var currencyId = _countryService.GetCurrencyByCulture(_workContextAccessor.GetContext().CurrentCulture.Trim());//_currencyRepository.Table.Where(c => c.CurrencyCulture == cultureUsed).First();

                if (data.Tags != null)
                {
                    foreach (var tag in data.Tags)
                    {
                        if (_campaignCategories.Table.Where(c => c.Name.ToLower() == tag).FirstOrDefault() != null)
                        {
                            var cat = _campaignCategories.Table.Where(c => c.Name.ToLower() == tag).FirstOrDefault();
                            var link = new LinkCampaignAndCategoriesRecord
                            {
                                CampaignRecord = newCampaign,
                                CampaignCategoriesPartRecord = cat
                            };
                            _linkCampaignAndCategories.Create(link);
                        }
                        else
                        {
                            var cat = new CampaignCategoriesRecord
                            {
                                Name = tag,
                                IsVisible = false,
                                CategoriesCulture = cultureUsed,
                                CountryRecord = _countryService.GetCountryByCulture(_workContextAccessor.GetContext().CurrentCulture.Trim())
                            };
                            _campaignCategories.Create(cat);
                            var link = new LinkCampaignAndCategoriesRecord
                            {
                                CampaignRecord = newCampaign,
                                CampaignCategoriesPartRecord = cat
                            };
                            _linkCampaignAndCategories.Create(link);
                        }
                    }
                }

                foreach (var prod in data.Products)
                {
                    double baseCost = 0;
                    if (!double.TryParse(prod.BaseCost, out baseCost))
                    {
                        double.TryParse(prod.BaseCost.Replace('.', ','), out baseCost);
                    }

                    double price = 0;
                    if (!double.TryParse(prod.Price, out price))
                    {
                        double.TryParse(prod.Price.Replace('.', ','), out price);
                    }

                    var campProduct = new CampaignProductRecord
                    {
                        CampaignRecord_Id = newCampaign.Id,
                        BaseCost = baseCost,
                        CurrencyRecord = currencyId,
                        Price = price,
                        ProductColorRecord = _colorRepository.Get(prod.ColorId),
                        ProductRecord = _productRepository.Get(prod.ProductId),
                        SecondProductColorRecord = prod.SecondColorId == 0 ? null : _colorRepository.Get(prod.SecondColorId),
                        ThirdProductColorRecord = prod.ThirdColorId == 0 ? null : _colorRepository.Get(prod.ThirdColorId),
                        FourthProductColorRecord = prod.FourthColorId == 0 ? null : _colorRepository.Get(prod.FourthColorId),
                        FifthProductColorRecord = prod.FifthColorId == 0 ? null : _colorRepository.Get(prod.FifthColorId)
                    };

                    _campProdRepository.Create(campProduct);

                    newCampaign.Products.Add(campProduct);
                }

                return newCampaign;
            }
            catch
            {
                throw;
            }
        }