138 lines
3.8 KiB
C#
138 lines
3.8 KiB
C#
using LiangLiSystem.Services.Helpers;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Security.Claims;
|
|
using System.Threading.Tasks;
|
|
using Traffic.Data.ViewModels;
|
|
using Traffic.Service.Filter;
|
|
using Traffic.Service.Interfaces;
|
|
|
|
namespace Traffic.Api.Controllers
|
|
{
|
|
/// <summary>
|
|
/// 公佈欄管理
|
|
/// </summary>
|
|
[Route("api/announce")]
|
|
[Authorize]
|
|
[ApiController]
|
|
[ServiceFilter(typeof(LogFilter))]
|
|
public class AnnounceController : ControllerBase
|
|
{
|
|
private readonly IAnnounceService _service;
|
|
|
|
public AnnounceController(IAnnounceService service)
|
|
{
|
|
_service = service;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 取得公告列表
|
|
/// </summary>
|
|
/// <param name="searchModel"></param>
|
|
/// <returns></returns>
|
|
[HttpGet("")]
|
|
public IActionResult GetAnnounces([FromQuery] SearchModelViewModel searchModel)
|
|
{
|
|
try
|
|
{
|
|
var result = new Response();
|
|
var model = Util.GetSearchModel(searchModel);
|
|
result.Result = _service.GetAnnounces(model);
|
|
return Ok(result);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return BadRequest();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 取得公告 by id
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
[HttpGet("{id}")]
|
|
public IActionResult GetAnnounce(int id)
|
|
{
|
|
try
|
|
{
|
|
var result = new Response();
|
|
result.Result = _service.GetAnnounceById(id);
|
|
return Ok(result);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return BadRequest();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 新增公告
|
|
/// </summary>
|
|
/// <param name="vm"></param>
|
|
/// <returns></returns>
|
|
[HttpPost("")]
|
|
public IActionResult InsertAnnounce(AnnounceInsertViewModel vm)
|
|
{
|
|
try
|
|
{
|
|
var userIdentity = (ClaimsIdentity)User.Identity;
|
|
var byWho = new ParserForUserFromClaim(userIdentity).GetUserId();
|
|
|
|
var result = _service.InsertAnnounce(byWho, vm);
|
|
return result.Success ? Ok(result) : BadRequest(result);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return BadRequest();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 修改公告
|
|
/// </summary>
|
|
/// <param name="id">公告的流水號</param>
|
|
/// <param name="vm"></param>
|
|
/// <returns></returns>
|
|
[HttpPut("{id}")]
|
|
public IActionResult UpdateAnnounce(int id, AnnounceInsertViewModel vm)
|
|
{
|
|
try
|
|
{
|
|
var userIdentity = (ClaimsIdentity)User.Identity;
|
|
var byWho = new ParserForUserFromClaim(userIdentity).GetUserId();
|
|
|
|
var result = _service.UpdatAnnounce(id, vm, byWho);
|
|
return result.Success ? Ok(result) : BadRequest(result);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return BadRequest();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 刪除公告
|
|
/// </summary>
|
|
/// <param name="id">公告的流水號</param>
|
|
/// <returns></returns>
|
|
[HttpDelete("{id}")]
|
|
public IActionResult DeleteAnnounce(int id)
|
|
{
|
|
try
|
|
{
|
|
var result = _service.DeleteAnnounce(id);
|
|
return result.Success ? Ok(result) : BadRequest(result);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return BadRequest();
|
|
}
|
|
}
|
|
}
|
|
}
|