using Dpz.Core.EnumLibrary;
using Dpz.Core.Service.ObjectStorage.Services;
namespace Dpz.Core.Web.Controllers;
public class TimelineController(
ITimelineService timelineService,
IObjectStorageOperation objectStorageService
) : Controller
{
[Route("timeline")]
[HttpGet]
public async Task<IActionResult> Index()
{
ViewBag.Menu = MenuItem.Timeline;
this.SetTitle("时间轴");
var list = await timelineService.GetTimelinesAsync("pengqian");
return View(list);
}
[Route("timeline/{account}.html")]
public async Task<IActionResult> Timeline2(string account)
{
ViewBag.Menu = MenuItem.Timeline;
this.SetTitle($"{account} - 时间轴");
var list = await timelineService.GetTimelinesAsync(account);
return View("Index", list);
}
[CheckAuthorize, HttpPost]
public async Task<IActionResult> Publish(VmTimeline viewModel)
{
if (!string.IsNullOrEmpty(viewModel.Id))
{
var timeline = await timelineService.FindAsync(viewModel.Id);
if (User.RequiredUserId != 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.RequiredUserInfo;
await timelineService.SaveAsync(viewModel);
return Json(new ResultInfo(true));
}
[CheckAuthorize, HttpPost]
public async Task<IActionResult> Delete(string id)
{
var timeline = await timelineService.FindAsync(id);
if (User.RequiredUserId != 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 source = await timelineService.GetPageAsync(
pageIndex,
pageSize,
content,
User.RequiredUserId
);
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
}
⚠⚠ 以下内容为AI分析的结果,请根据实际情况进行判断。
上述代码是一个 ASP.NET Core MVC 控制器,名为 TimelineController,用于处理与时间轴(Timeline)相关的操作。以下是对代码中各个部分功能的详细解释:
1. 控制器的构造函数
public TimelineController(ITimelineService timelineService, IObjectStorageOperation objectStorageService) : Controller
- 控制器依赖于两个服务:
ITimelineService和IObjectStorageOperation。前者用于处理时间轴相关的业务逻辑,后者用于处理对象存储(如文件上传)。
2. 发布时间轴
public async Task<IActionResult> Publish(string id = "")
- GET 方法:如果提供了
id,则查找并返回对应的时间轴模型(VmTimeline),否则返回一个空模型。 - POST 方法:接收一个时间轴视图模型(
VmTimeline),进行以下验证:- 检查用户是否有权限修改时间轴(如果是更新操作)。
- 检查标题和日期是否为空。
- 如果验证通过,设置创建时间和最后更新时间,保存时间轴并返回成功的 JSON 响应。
3. 删除时间轴
[CheckAuthorize, HttpPost]
public async Task<IActionResult> Delete(string id)
- 检查用户是否有权限删除指定的时间轴(只能删除自己的时间轴)。
- 如果有权限,调用服务删除时间轴并返回成功的 JSON 响应。
4. 上传图片
[CheckAuthorize]
[HttpPost]
public async Task<IActionResult> Upload()
- 处理文件上传,检查上传的文件是否存在且为图片类型。
- 生成一个新的文件名并将文件上传到对象存储。
- 返回上传结果的 JSON 响应,包括成功与否和文件的访问 URL。
5. 获取我的时间轴
[HttpGet, CheckAuthorize]
public async Task<IActionResult> MyTimeline(int pageIndex = 1, int pageSize = 10, string content = "")
- 获取当前用户的时间轴列表,支持分页和内容过滤。
- 返回分页后的时间轴数据的 JSON 响应。
6. 查看时间轴详情
public async Task<IActionResult> Detail(string id = "")
- 根据提供的
id查找时间轴。 - 如果时间轴存在,返回其详细信息;如果不存在,返回错误信息的 JSON 响应。
7. 其他
- 控制器中使用了
CheckAuthorize特性,确保用户在执行某些操作时已通过身份验证。 - 使用
ResultInfo类封装返回的结果,提供统一的响应格式。
总结
这个控制器提供了一个完整的时间轴管理功能,包括创建、更新、删除、上传图片、获取用户的时间轴列表和查看时间轴详情等操作。通过使用依赖注入和服务层,代码保持了良好的结构和可维护性。
评论加载中...