public async Task <ActionResult> ChangeState(int id)
        {
            var ad = await AdsService.GetByIdAsync(id) ?? throw new NotFoundException("广告不存在!");

            ad.Status = ad.Status == Status.Available ? Status.Unavailable : Status.Available;
            return(ResultData(null, await AdsService.SaveChangesAsync() > 0, ad.Status == Status.Available ? $"【{ad.Title}】已上架!" : $"【{ad.Title}】已下架!"));
        }
        public async Task <ActionResult> SetCategories(int id, [FromBodyOrDefault] string cids)
        {
            var entity = await AdsService.GetByIdAsync(id) ?? throw new NotFoundException("广告未找到");

            entity.CategoryIds = cids;
            await AdsService.SaveChangesAsync();

            return(Ok());
        }
Exemple #3
0
        public async Task <IActionResult> Redirect(int id)
        {
            var ad = await AdsService.GetByIdAsync(id) ?? throw new NotFoundException("推广链接不存在");

            if (!HttpContext.Request.IsRobot() && string.IsNullOrEmpty(HttpContext.Session.Get <string>("ads" + id)))
            {
                HttpContext.Session.Set("ads" + id, id.ToString());
                ad.ViewCount++;
                await AdsService.SaveChangesAsync();
            }

            return(RedirectPermanent(ad.Url));
        }
        public async Task <IActionResult> Save(AdvertisementDto model)
        {
            model.CategoryId = model.CategoryId?.Replace("null", "");
            var entity = await AdsService.GetByIdAsync(model.Id);

            if (entity != null)
            {
                Mapper.Map(model, entity);
                bool b1 = await AdsService.SaveChangesAsync() > 0;

                return(ResultData(null, b1, b1 ? "修改成功" : "修改失败"));
            }

            bool b = await AdsService.AddEntitySavedAsync(model.Mapper <Advertisement>()) > 0;

            return(ResultData(null, b, b ? "添加成功" : "添加失败"));
        }
        public async Task <IActionResult> Redirect(int id)
        {
            var ad = await AdsService.GetByIdAsync(id) ?? throw new NotFoundException("推广链接不存在");

            if (!Request.IsRobot() && string.IsNullOrEmpty(HttpContext.Session.Get <string>("ads" + id)))
            {
                HttpContext.Session.Set("ads" + id, id.ToString());
                ad.ClickRecords.Add(new AdvertisementClickRecord()
                {
                    IP       = ClientIP,
                    Location = ClientIP.GetIPLocation(),
                    Referer  = Request.Headers[HeaderNames.Referer].ToString(),
                    Time     = DateTime.Now
                });
                await AdsService.SaveChangesAsync();

                var start = DateTime.Today.AddMonths(-6);
                await ClickRecordService.GetQuery(a => a.Time < start).DeleteFromQueryAsync();
            }

            return(Redirect(ad.Url));
        }