tycg_carviolation_BE/Traffic.Api/Controllers/AccountController.cs

306 lines
9.5 KiB
C#

using LiangLiSystem.Services.Helpers;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Traffic.Data.Enums;
using Traffic.Data.ViewModels;
using Traffic.Service.Filter;
using Traffic.Service.Interfaces;
namespace Traffic.Api.Controllers
{
[Route("api/user")]
[Authorize]
[ApiController]
[ServiceFilter(typeof(LogFilter))]
public class AccountController : ControllerBase
{
private readonly IAccountService _service;
private readonly ILogger _logger;
public AccountController(IAccountService service, ILogger<AccountController> logger)
{
_service = service;
_logger = logger;
}
/// <summary>
/// 取得 使用者資訊
/// </summary>
/// <param name="id">使用者 Id</param>
/// <returns></returns>
[HttpGet("{id}")]
public IActionResult GetAccountUser(int id)
{
try
{
var result = _service.GetAccountUser(id);
return Ok(result);
}
catch (Exception)
{
return BadRequest();
}
}
/// <summary>
/// 取得 UserData 列表
/// </summary>
/// <param name="searchModel"></param>
/// <param name="Id"></param>
/// <returns></returns>
[HttpGet("GetUserDataList")]
public IActionResult GetUserDataList([FromQuery] SearchModelViewModel searchModel, int? Id)
{
try
{
var result = new Response();
var model = Util.GetSearchModel(searchModel);
result.Result = _service.GetUserDataList(model, Id);
return Ok(result);
}
catch (Exception)
{
return BadRequest();
}
}
/// <summary>
/// 新增使用者
/// </summary>
/// <param name="account">新增的使用者資訊</param>
/// <returns></returns>
/// <remarks>管理者Admin RoleCodeId = 0, 一般User RoleCodeId = 1</remarks>
[HttpPost("PostUserDataList")]
public IActionResult InserAccountUser(UserData account)
{
try
{
var userIdentity = (ClaimsIdentity)User.Identity;
var mID = new ParserForUserFromClaim(userIdentity).GetUserId();
var result = _service.InserAccountUser(mID, account);
return result.Success ? Ok(result) : BadRequest(result);
}
catch (Exception)
{
return BadRequest();
}
}
/// <summary>
/// 管理者修改使用者狀態
/// </summary>
/// <param name="id"></param>
/// <param name="status">狀態</param>
/// <returns></returns>
/// <remarks> 0:Disable, 1:Enable</remarks>
[HttpPut("PutUserStatus/{id}")]
public IActionResult PutUserStatus(int id, int status)
{
try
{
var userIdentity = (ClaimsIdentity)User.Identity;
var byWho = new ParserForUserFromClaim(userIdentity).GetUserId();
var result = _service.PutUserbyAdmin(byWho, id, status);
return result.Success ? Ok(result) : BadRequest(result);
}
catch (Exception)
{
return BadRequest();
}
}
/// <summary>
/// 管理者修改使用者
/// </summary>
/// <param name="id"></param>
/// <param name="account">管理者欲修改的使用者資訊</param>
/// <returns></returns>
/// <remarks>管理者Admin RoleCodeId = 0, 一般User RoleCodeId = 1</remarks>
[HttpPut("PutUserData/{id}")]
public IActionResult UpdateAccountbyAdmin(int id, UserData account)
{
try
{
var userIdentity = (ClaimsIdentity)User.Identity;
var byWho = new ParserForUserFromClaim(userIdentity).GetUserId();
account.Id = id;
var result = _service.UpdateAccountbyAdmin(byWho, account);
return result.Success ? Ok(result) : BadRequest(result);
}
catch (Exception)
{
return BadRequest();
}
}
/// <summary>
/// 管理者修改使用者密碼
/// </summary>
/// <param name="id">使用者Id</param>
/// <returns></returns>
/// <remarks>管理者Admin RoleCodeId = 0, 一般User RoleCodeId = 1</remarks>
[HttpPut("PutUserPassword/{id}")]
public IActionResult UpdateAccountPasswordbyAdmin(int id)
{
try
{
var userIdentity = (ClaimsIdentity)User.Identity;
var byWho = new ParserForUserFromClaim(userIdentity).GetUserId();
var result = _service.UpdateAccountPasswordbyAdmin(id, byWho);
return result.Success ? Ok(result) : BadRequest(result);
}
catch (Exception)
{
return BadRequest();
}
}
///// <summary>
///// 使用者修改自己的帳號資訊
///// </summary>
///// <param name="account">使用者自己欲修改的資訊</param>
///// <returns></returns>
//[HttpPut("")]
//public IActionResult UpdateAccountbyUser(UserData account)
//{
// var userIdentity = (ClaimsIdentity)User.Identity;
// var byWho = new ParserForUserFromClaim(userIdentity).GetUserId();
// var result = _service.UpdateAccountbyUser(byWho, account);
// return result.Success ? Ok(result) : BadRequest(result);
//}
/// <summary>
/// 修改密碼
/// </summary>
/// <param name="account">修改前後的密碼</param>
/// <returns></returns>
[HttpPut("PutUserPassword")]
public IActionResult UpdateAccountPassword(UserData account)
{
try
{
var userIdentity = (ClaimsIdentity)User.Identity;
var byWho = new ParserForUserFromClaim(userIdentity).GetUserId();
var byWhoAccount = new ParserForUserFromClaim(userIdentity).GetAccount();
account.Id = byWho;
account.Account = byWhoAccount;
var result = _service.UpdateAccountPasswordByUser(byWho, account);
return result.Success ? Ok(result) : BadRequest(result);
}
catch (Exception ex)
{
return BadRequest(ex.ToString());
}
}
/// <summary>
/// 刪除使用者帳號
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpDelete("DeleteUserData")]
public IActionResult DeleteUserData(int id)
{
try
{
return Ok(_service.DeleteUserData(id));
}
catch (Exception)
{
return BadRequest();
}
}
/// <summary>
/// 是否存在此User
/// </summary>
/// <param name="account"></param>
/// <returns></returns>
[HttpGet("GetExistUser")]
public IActionResult GetExistUser(string account)
{
try
{
var result = _service.GetExistUser(account);
return Ok(new { Result = result });
}
catch (Exception)
{
return BadRequest();
}
}
/// <summary>
/// 取得使用者名稱列表
/// </summary>
/// <returns></returns>
[HttpGet("GetUserNameList")]
public IActionResult GetUserNameList()
{
try
{
var result = _service.GetUserNameList();
return Ok(new { Result = result });
}
catch (Exception)
{
return BadRequest();
}
}
/// <summary>
/// 取得使用者站點列表
/// </summary>
/// <returns></returns>
[HttpGet("GetUserSite")]
public IActionResult GetUserSite()
{
try
{
var userIdentity = (ClaimsIdentity)User.Identity;
var id = new ParserForUserFromClaim(userIdentity).GetUserId();
var result = _service.GetUserSite(id);
return Ok(new { Result = result });
}
catch (Exception)
{
return BadRequest();
}
}
/// <summary>
/// 取得使用者站點資訊列表
/// </summary>
/// <returns></returns>
[HttpGet("GetUserSiteInfo")]
public IActionResult GetUserSiteInfo()
{
try
{
var userIdentity = (ClaimsIdentity)User.Identity;
var id = new ParserForUserFromClaim(userIdentity).GetUserId();
var result = _service.GetUserSiteInfo(id);
return Ok(new { Result = result });
}
catch (Exception)
{
return BadRequest();
}
}
}
}