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 { /// /// 公佈欄管理 /// [Route("api/announce")] [Authorize] [ApiController] [ServiceFilter(typeof(LogFilter))] public class AnnounceController : ControllerBase { private readonly IAnnounceService _service; public AnnounceController(IAnnounceService service) { _service = service; } /// /// 取得公告列表 /// /// /// [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(); } } /// /// 取得公告 by id /// /// /// [HttpGet("{id}")] public IActionResult GetAnnounce(int id) { try { var result = new Response(); result.Result = _service.GetAnnounceById(id); return Ok(result); } catch (Exception) { return BadRequest(); } } /// /// 新增公告 /// /// /// [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(); } } /// /// 修改公告 /// /// 公告的流水號 /// /// [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(); } } /// /// 刪除公告 /// /// 公告的流水號 /// [HttpDelete("{id}")] public IActionResult DeleteAnnounce(int id) { try { var result = _service.DeleteAnnounce(id); return result.Success ? Ok(result) : BadRequest(result); } catch (Exception) { return BadRequest(); } } } }