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.Interfaces;
namespace Traffic.Api.Controllers
{
///
/// 行政區管理
///
[Route("api/siteInformation")]
[Authorize]
[ApiController]
public class SiteInformationController : ControllerBase
{
private readonly ISiteInformationService _service;
public SiteInformationController(ISiteInformationService service)
{
_service = service;
}
///
/// 取得所有行政區列表 by 員警
///
[HttpGet("user")]
public IActionResult GetSiteInformationsByUser()
{
try
{
var userIdentity = (ClaimsIdentity)User.Identity;
var userId = new ParserForUserFromClaim(userIdentity).GetUserId();
var result = new Response();
result.Result = _service.GetSiteInformationsByUser(userId);
return Ok(result);
}
catch (Exception)
{
return BadRequest();
}
}
///
/// 取得所有行政區列表
///
[AllowAnonymous]
[HttpGet("emailNoNull")]
public IActionResult GetSiteInformationsEmailNoNull()
{
try
{
var result = new Response();
result.Result = _service.GetSiteInformations().Where(i => !string.IsNullOrWhiteSpace(i.CompanyEmail));
return Ok(result);
}
catch (Exception)
{
return BadRequest();
}
}
///
/// 取得所有行政區列表
///
[AllowAnonymous]
[HttpGet("all")]
public IActionResult GetSiteInformations()
{
try
{
var result = new Response();
result.Result = _service.GetSiteInformations();
return Ok(result);
}
catch (Exception)
{
return BadRequest();
}
}
///
/// 取得行政區列表 by EventType
///
///
///
[HttpPost("eventType")]
public IActionResult GetSiteInformationsByEventTypes(List eventTypes)
{
try
{
var result = new Response
{
Result = _service.GetSiteInformationsByEventTypes(eventTypes)
};
return Ok(result);
}
catch (Exception)
{
return BadRequest();
}
}
///
/// 取得行政區列表 by 條件
///
///
///
[HttpGet("")]
public IActionResult GetSiteInformations([FromQuery] SearchModelViewModel searchModel)
{
try
{
var result = new Response();
var model = Util.GetSearchModel(searchModel);
result.Result = _service.GetSiteInformations(model);
return Ok(result);
}
catch (Exception)
{
return BadRequest();
}
}
///
/// 取得行政區 by id
///
///
///
[HttpGet("{id}")]
public IActionResult GetSiteInformation(int id)
{
try
{
var result = new Response();
result.Result = _service.GetSiteInformationById(id);
return Ok(result);
}
catch (Exception)
{
return BadRequest();
}
}
///
/// 新增行政區
///
///
///
[HttpPost("")]
public IActionResult InsertSiteInformation(SiteInformationViewModel vm)
{
try
{
var result = _service.InsertSiteInformation(vm);
return result.Success ? Ok(result) : BadRequest(result);
}
catch (Exception)
{
return BadRequest();
}
}
///
/// 修改行政區
///
/// 行政區的流水號
///
///
[HttpPut("{id}")]
public IActionResult UpdateSiteInformation(int id, SiteInformationViewModel vm)
{
try
{
var result = _service.UpdatSiteInformation(id, vm);
return result.Success ? Ok(result) : BadRequest(result);
}
catch (Exception)
{
return BadRequest();
}
}
///
/// 刪除行政區
///
/// 行政區的流水號
///
[HttpDelete("{id}")]
public IActionResult DeleteSiteInformation(int id)
{
try
{
var result = _service.DeleteSiteInformation(id);
return result.Success ? Ok(result) : BadRequest(result);
}
catch (Exception)
{
return BadRequest();
}
}
}
}