public async Task <ActionResult> ChangeState(int id) { var notice = await NoticeService.GetByIdAsync(id) ?? throw new NotFoundException("公告未找到"); notice.NoticeStatus = notice.NoticeStatus == NoticeStatus.Normal ? NoticeStatus.Expired : NoticeStatus.Normal; var b = await NoticeService.SaveChangesAsync() > 0; return(ResultData(null, b, notice.NoticeStatus == NoticeStatus.Normal ? $"【{notice.Title}】已上架!" : $"【{notice.Title}】已下架!")); }
public async Task <ActionResult> Edit(Notice notice) { var entity = await NoticeService.GetByIdAsync(notice.Id) ?? throw new NotFoundException("公告已经被删除!"); entity.ModifyDate = DateTime.Now; entity.Title = notice.Title; entity.Content = await ImagebedClient.ReplaceImgSrc(notice.Content.ClearImgAttributes()); bool b = await NoticeService.SaveChangesAsync() > 0; return(ResultData(null, b, b ? "修改成功" : "修改失败")); }
public async Task <ActionResult> Details(int id) { var notice = await NoticeService.GetByIdAsync(id) ?? throw new NotFoundException("页面未找到"); notice.ModifyDate = notice.ModifyDate.ToTimeZone(HttpContext.Session.Get <string>(SessionKey.TimeZone)); notice.PostDate = notice.PostDate.ToTimeZone(HttpContext.Session.Get <string>(SessionKey.TimeZone)); if (!HttpContext.Session.TryGetValue("notice" + id, out _)) { notice.ViewCount += 1; await NoticeService.SaveChangesAsync(); HttpContext.Session.Set("notice" + id, notice.Title); } return(View(notice)); }
public async Task <ActionResult> Details(int id) { var notice = await NoticeService.GetByIdAsync(id) ?? throw new NotFoundException("页面未找到"); if (!HttpContext.Session.TryGetValue("notice" + id, out _)) { notice.ViewCount += 1; await NoticeService.SaveChangesAsync(); HttpContext.Session.Set("notice" + id, notice.Title); } notice.ModifyDate = notice.ModifyDate.ToTimeZone(HttpContext.Session.Get <string>(SessionKey.TimeZone)); notice.PostDate = notice.PostDate.ToTimeZone(HttpContext.Session.Get <string>(SessionKey.TimeZone)); notice.Content = ReplaceVariables(notice.Content); ViewBag.Ads = AdsService.GetByWeightedPrice(AdvertiseType.InPage, Request.Location()); return(View(notice)); }
public async Task <ActionResult> Delete(int id) { var notice = await NoticeService.GetByIdAsync(id) ?? throw new NotFoundException("公告已经被删除!"); var srcs = notice.Content.MatchImgSrcs().Where(s => s.StartsWith("/")); foreach (var path in srcs) { try { System.IO.File.Delete(HostEnvironment.WebRootPath + path); } catch { } } bool b = await NoticeService.DeleteByIdSavedAsync(id) > 0; return(ResultData(null, b, b ? "删除成功" : "删除失败")); }
public async Task <ActionResult> Edit(NoticeDto notice, CancellationToken cancellationToken) { var entity = await NoticeService.GetByIdAsync(notice.Id) ?? throw new NotFoundException("公告已经被删除!"); if (notice.StartTime.HasValue && notice.EndTime.HasValue && notice.StartTime >= notice.EndTime) { return(ResultData(null, false, "开始时间不能小于结束时间")); } if (DateTime.Now < notice.StartTime) { entity.NoticeStatus = NoticeStatus.UnStart; } entity.ModifyDate = DateTime.Now; entity.StartTime = notice.StartTime; entity.EndTime = notice.EndTime; entity.Title = notice.Title; entity.Content = await ImagebedClient.ReplaceImgSrc(await notice.Content.ClearImgAttributes(), cancellationToken); bool b = await NoticeService.SaveChangesAsync() > 0; return(ResultData(null, b, b ? "修改成功" : "修改失败")); }
public void GetEventById() { var result = noticeService.GetByIdAsync(1).Result; Assert.AreEqual(true, result.Success); }