using Microsoft.AspNetCore.Mvc;
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 MongoDB.Bson;
namespace Dpz.Core.Web.Controllers
{
public class TimelineController : Controller
{
private readonly ITimelineService _timelineService;
//private readonly IPictureService _pictureService;
private readonly IObjectStorageOperation _objectStorageService;
public TimelineController(
ITimelineService timelineService,
//IPictureService pictureService,
IObjectStorageOperation objectStorageService)
{
_timelineService = timelineService;
//_pictureService = pictureService;
_objectStorageService = objectStorageService;
}
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.GetIdentity().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.GetIdentity();
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);
// var pictureInfo = new VmPictureInfo
// {
// Height = result.Height,
// Url = result.AccessUrl,
// Width = result.Width,
// Delete = result.AccessUrl
// };
// var picture = new VmPicture
// {
// Creator = userInfo,
// Description = "时间轴",
// Tag = new[] {"时间轴"},
// Type = PictureType.Timeline,
// Info = pictureInfo,
// UploadTime = DateTime.Now
// };
// await _pictureService.InsertAsync(picture);
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.GetIdentity();
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
}
}