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;
}
///
/// 添加系统公告
///
[HttpPost("add")]
public async Task> AddNotice([FromBody] AddNoticeParam param)
{
var result = await _noticeService.Add(param);
return result.isok
? this.ToResult(result.msg)
: this.ToFailResult(result.msg);
}
///
/// 修改系统公告
///
[HttpPost("update")]
public async Task> UpdateNotice([FromBody] UpdateNoticeParam param)
{
var result = await _noticeService.Update(param);
return result.isok
? this.ToResult(result.msg)
: this.ToFailResult(result.msg);
}
///
/// 删除公告
///
[HttpGet("delete")]
public async Task> DeleteNotice([FromQuery] string id)
{
var result = await _noticeService.Delete(id);
return result.isok
? this.ToResult(result.msg)
: this.ToFailResult(result.msg);
}
///
/// 获取系统公告列表
///
[HttpPost("list")]
public async Task> GetNoticeList([FromBody] GetNoticeListParam param)
{
var (list, count) = await _noticeService.GetList(param);
if (list == null)
return this.ToPageResult(new List(), 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);
}
///
/// 根据ID获取公告详情
///
[HttpGet("get")]
[AllowAnonymous]
public async Task> GetNotice([FromQuery] string id)
{
var notice = await _noticeService.Get(id);
if (notice == null)
{
return this.ToFailResult("公告不存在");
}
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);
}
}