Files
BuYuHouDuan/UserSrc/UserApi/Notices/NoticeController.cs
T

117 lines
3.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Common;
using CommonInfrastructure;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using UserDomain.Entity;
using UserService.Notices;
using UserService.Notices.Param;
namespace UserApi.Notices
{
[ApiController, Route("api/[controller]")]
[Authorize]
public class NoticeController : BaseController
{
private readonly INoticeService _noticeService;
public NoticeController(INoticeService noticeService)
{
_noticeService = noticeService;
}
/// <summary>
/// 获取系统公告列表(分页)
/// </summary>
[HttpPost("list")]
public async Task<PageResponse<NoticeDto>> GetNoticeList(GetNoticeListParam param)
{
var result = await _noticeService.GetList(param);
var list = result.list ?? new List<Notice>();
var dtos = list.Select(n => new NoticeDto
{
Id = n.Id,
Title = n.Title,
Content = n.Content,
Recommend = n.Recommend,
TopImage = n.TopImage,
ContentImages = n.ContentImages,
CreateTime = n.CreateTime
});
return this.ToPageResult(dtos, (int)result.count);
}
/// <summary>
/// 根据ID获取公告详情
/// </summary>
[HttpGet("get")]
[AllowAnonymous]
public async Task<BaseResponse<NoticeDto>> GetNotice(string id)
{
var result = await _noticeService.Get(id);
if (result == null)
{
return this.ToFailResult<NoticeDto>("公告不存在");
}
var dto = new NoticeDto
{
Id = result.Id,
Title = result.Title,
Content = result.Content,
Recommend = result.Recommend,
TopImage = result.TopImage,
ContentImages = result.ContentImages,
CreateTime = result.CreateTime
};
return this.ToResult(dto);
}
/// <summary>
/// 获取一个最新的公告
/// </summary>
[HttpGet("get/newone")]
[AllowAnonymous]
public async Task<BaseResponse<NoticeDto>> GetNewOne()
{
var result = await _noticeService.GetNewOne();
if (result == null)
{
return this.ToFailResult<NoticeDto>("暂无公告");
}
var dto = new NoticeDto
{
Id = result.Id,
Title = result.Title,
Content = result.Content,
Recommend = result.Recommend,
TopImage = result.TopImage,
ContentImages = result.ContentImages,
CreateTime = result.CreateTime
};
return this.ToResult(dto);
}
}
public class NoticeDto
{
public string Id { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public string Recommend { get; set; }
public string TopImage { get; set; }
public List<string> ContentImages { get; set; }
public long CreateTime { get; set; }
}
}