using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Traffic.Data.ViewModels;
using Traffic.Service.Interfaces;
namespace Traffic.Api.Controllers
{
[Route("api/group")]
[Authorize]
[ApiController]
public class PoliceStationController : ControllerBase
{
private readonly IPoliceStationService _service;
public PoliceStationController(IPoliceStationService service)
{
_service = service;
}
///
/// 取得 PoliceStation 列表
///
///
///
[HttpGet("GetGroupDataList")]
public IActionResult GetPoliceStations([FromQuery] SearchModelViewModel searchModel)
{
try
{
var result = new Response();
var model = new SearchModel
{
Term = searchModel.Term,
Page = Convert.ToInt32(string.IsNullOrWhiteSpace(searchModel.Page) ? "1" : searchModel.Page),
PageSize = Convert.ToInt32(string.IsNullOrWhiteSpace(searchModel.PageSize) ? "1" : searchModel.PageSize),
IsAsc = searchModel.IsAsc == "true" ? true : false,
Order = string.IsNullOrWhiteSpace(searchModel.Order) ? "id" : searchModel.Order,
};
result.Result = _service.GetPoliceStations(model);
return Ok(result);
}
catch (Exception)
{
return BadRequest();
}
}
///
/// 取得 PoliceStation by id
///
///
///
[HttpGet("GetGroupData/{id}")]
public IActionResult GetPoliceStation(int id)
{
try
{
var result = new Response();
result.Result = _service.GetPoliceStationById(id);
return Ok(result);
}
catch (Exception)
{
return BadRequest();
}
}
[HttpGet("GetGroupNameList")]
public IActionResult GetGroupNameList()
{
try
{
var result = new Response();
result.Result = _service.GetGroupNameList();
return Ok(result);
}
catch (Exception)
{
return BadRequest();
}
}
///
/// 新增 PoliceStation
///
///
///
[HttpPost("PostGroupData")]
public IActionResult InsertPoliceStation(PoliceStationViewModel vm)
{
try
{
var result = _service.InsertPoliceStation(vm);
return result.Success ? Ok(result) : BadRequest(result);
}
catch (Exception)
{
return BadRequest();
}
}
///
/// 修改 PoliceStation
///
///
///
[HttpPut("PutGroupData")]
public IActionResult UpdatePoliceStation(PoliceStationViewModel vm)
{
try
{
var result = _service.UpdatPoliceStation(vm);
return result.Success ? Ok(result) : BadRequest(result);
}
catch (Exception)
{
return BadRequest();
}
}
///
/// 刪除 PoliceStation
///
/// PoliceStation 的流水號
///
[HttpDelete("DeleteGroupData/{id}")]
public IActionResult DeletePoliceStation(int id)
{
try
{
var result = _service.DeletePoliceStation(id);
return result.Success ? Ok(result) : BadRequest(result);
}
catch (Exception)
{
return BadRequest();
}
}
}
}