Files
BuYuHouDuan/AdminSrc/AdminApi/Notices/NoticeController.cs
T

104 lines
2.9 KiB
C#

using Domain.Admin.Notices;
using AdminService.Notices;
using AdminService.Notices.Param;
using Common;
using CommonInfrastructure;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace AdminApi.Notices;
[ApiController, Route("adminapi/[controller]")]
[Authorize]
public class NoticeController : BaseController
{
private readonly INoticeService _noticeService;
public NoticeController(INoticeService noticeService)
{
_noticeService = noticeService;
}
/// <summary>
/// 添加系统公告
/// </summary>
[HttpPost("add")]
public async Task<BaseResponse<string>> AddNotice([FromBody] AddNoticeParam param)
{
var result = await _noticeService.Add(param);
return result.isok
? this.ToResult(result.msg)
: this.ToFailResult<string>(result.msg);
}
/// <summary>
/// 修改系统公告
/// </summary>
[HttpPost("update")]
public async Task<BaseResponse<string>> UpdateNotice([FromBody] UpdateNoticeParam param)
{
var result = await _noticeService.Update(param);
return result.isok
? this.ToResult(result.msg)
: this.ToFailResult<string>(result.msg);
}
/// <summary>
/// 删除公告
/// </summary>
[HttpGet("delete")]
public async Task<BaseResponse<string>> DeleteNotice([FromQuery] string id)
{
var result = await _noticeService.Delete(id);
return result.isok
? this.ToResult(result.msg)
: this.ToFailResult<string>(result.msg);
}
/// <summary>
/// 获取系统公告列表
/// </summary>
[HttpPost("list")]
public async Task<PageResponse<NoticeListItemDto>> GetNoticeList([FromBody] GetNoticeListParam param)
{
var (list, count) = await _noticeService.GetList(param);
if (list == null)
return this.ToPageResult(new List<NoticeListItemDto>(), 0);
var dtos = list.Select(n => new NoticeListItemDto
{
Id = n.Id,
Title = n.Title,
Recommend = n.Recommend,
TopImage = n.TopImage,
CreateTime = n.CreateTime
});
return this.ToPageResult(dtos, (int)count);
}
/// <summary>
/// 根据ID获取公告详情
/// </summary>
[HttpGet("get")]
[AllowAnonymous]
public async Task<BaseResponse<NoticeDto>> GetNotice([FromQuery] string id)
{
var notice = await _noticeService.Get(id);
if (notice == null)
{
return this.ToFailResult<NoticeDto>("公告不存在");
}
var dto = new NoticeDto
{
Id = notice.Id,
Title = notice.Title,
Content = notice.Content,
Recommend = notice.Recommend,
TopImage = notice.TopImage,
ContentImages = notice.ContentImages,
CreateTime = notice.CreateTime
};
return this.ToResult(dto);
}
}