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()); }
public async Task<IActionResult> Redirect(int id) { var ad = AdsService.GetById(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 = AdsService.GetById(model.Id); if (entity != null) { Mapper.Map(model, entity); bool b1 = await AdsService.SaveChangesAsync() > 0; return ResultData(null, b1, b1 ? "修改成功" : "修改失败"); } bool b = AdsService.AddEntitySaved(model.Mapper<Advertisement>()) != null; 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)); }
public async Task <ActionResult> RandomGo() { var ad = AdsService.GetByWeightedPrice((AdvertiseType) new Random().Next(1, 4), Request.Location()); if (!Request.IsRobot() && string.IsNullOrEmpty(HttpContext.Session.Get <string>("ads" + ad.Id))) { HttpContext.Session.Set("ads" + ad.Id, ad.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(-1); await ClickRecordService.GetQuery(a => a.Time < start).DeleteFromQueryAsync(); } return(Redirect(ad.Url)); }