网站首页 网站源码
using System;
using System.Linq;
using System.Threading.Tasks;
using Dpz.Core.Public.ViewModel;
using Dpz.Core.Service.ObjectStorage.Services;
using Dpz.Core.Service.RepositoryService;
using Dpz.Core.Web.Library;
using Dpz.Core.Web.Library.Filter;
using Dpz.Core.Web.Models;
using Microsoft.AspNetCore.Mvc;
using MongoDB.Bson;
namespace Dpz.Core.Web.Controllers;
public class TimelineController(
ITimelineService timelineService,
IObjectStorageOperation objectStorageService
) : Controller
{
public async Task<IActionResult> Publish(string id = "")
{
VmTimeline? model = null;
if (!string.IsNullOrEmpty(id))
{
model = await timelineService.FindAsync(id);
}
return View(model);
}
[CheckAuthorize, HttpPost]
public async Task<IActionResult> Publish(VmTimeline viewModel)
{
if (!string.IsNullOrEmpty(viewModel.Id))
{
var timeline = await timelineService.FindAsync(viewModel.Id);
if (User.GetStrictIdentity().Id != timeline?.Author.Id)
{
return Json(new ResultInfo("没有权限修改其他用户的时间轴!"));
}
}
if (string.IsNullOrEmpty(viewModel.Title))
{
return Json(new ResultInfo("标题不能为空!"));
}
if (viewModel.Date == DateTime.MinValue)
{
return Json(new ResultInfo("时间不能为空!"));
}
viewModel.CreateTime = DateTime.Now;
viewModel.LastUpdateTime = DateTime.Now;
viewModel.Author = User.GetIdentity();
await timelineService.SaveAsync(viewModel);
return Json(new ResultInfo(true));
}
[CheckAuthorize, HttpPost]
public async Task<IActionResult> Delete(string id)
{
var userInfo = User.GetStrictIdentity();
var timeline = await timelineService.FindAsync(id);
if (userInfo.Id != timeline?.Author.Id)
{
return Json(new ResultInfo("不能删除别人的时间轴!"));
}
await timelineService.DeleteAsync(id);
return Json(new ResultInfo(true));
}
[CheckAuthorize]
[HttpPost]
public async Task<IActionResult> Upload()
{
var file = Request.Form.Files.FirstOrDefault();
if (file is { Length: > 0 } && file.ContentType.Contains("image"))
{
//var userInfo = User.GetIdentity();
var path = new[] { "images", "timeline", DateTime.Now.ToString("yyyy-MM-dd") };
var fileName =
ObjectId.GenerateNewId() + file.FileName[file.FileName.LastIndexOf('.')..];
var result = await objectStorageService.UploadAsync(
file.OpenReadStream(),
path,
fileName
);
return Json(
new
{
success = 1,
message = "",
url = result.AccessUrl,
}
);
}
return Json(
new
{
success = 0,
message = "请选择一张图片!",
url = "",
}
);
}
#region memble mobile
[HttpGet, CheckAuthorize]
public async Task<IActionResult> MyTimeline(
int pageIndex = 1,
int pageSize = 10,
string content = ""
)
{
var userInfo = User.GetStrictIdentity();
var source = await timelineService.GetPageAsync(pageIndex, pageSize, content, userInfo.Id);
var data = Pagination<VmTimeline>.Create(source);
return Json(new ResultInfo(data));
}
public async Task<IActionResult> Detail(string id = "")
{
if (string.IsNullOrEmpty(id))
{
return Json(new ResultInfo("缺失参数"));
}
var timeline = await timelineService.FindAsync(id);
if (timeline == null)
{
return Json(new ResultInfo("时间轴不存在"));
}
return Json(new ResultInfo(timeline));
}
#endregion
}
上述代码是一个 ASP.NET Core MVC 控制器,名为 TimelineController
,用于处理与时间轴(Timeline)相关的操作。以下是对代码中各个部分功能的详细解释:
public TimelineController(ITimelineService timelineService, IObjectStorageOperation objectStorageService) : Controller
ITimelineService
和 IObjectStorageOperation
。前者用于处理时间轴相关的业务逻辑,后者用于处理对象存储(如文件上传)。public async Task<IActionResult> Publish(string id = "")
id
,则查找并返回对应的时间轴模型(VmTimeline
),否则返回一个空模型。VmTimeline
),进行以下验证:[CheckAuthorize, HttpPost]
public async Task<IActionResult> Delete(string id)
[CheckAuthorize]
[HttpPost]
public async Task<IActionResult> Upload()
[HttpGet, CheckAuthorize]
public async Task<IActionResult> MyTimeline(int pageIndex = 1, int pageSize = 10, string content = "")
public async Task<IActionResult> Detail(string id = "")
id
查找时间轴。CheckAuthorize
特性,确保用户在执行某些操作时已通过身份验证。ResultInfo
类封装返回的结果,提供统一的响应格式。这个控制器提供了一个完整的时间轴管理功能,包括创建、更新、删除、上传图片、获取用户的时间轴列表和查看时间轴详情等操作。通过使用依赖注入和服务层,代码保持了良好的结构和可维护性。