add duty-time and poi (จาก google maps ไปก่อน)
This commit is contained in:
parent
43932f742b
commit
78f8c0813c
30 changed files with 3395 additions and 4 deletions
114
BMA.EHR.Leave.Service/Controllers/CheckInController.cs
Normal file
114
BMA.EHR.Leave.Service/Controllers/CheckInController.cs
Normal file
|
|
@ -0,0 +1,114 @@
|
|||
using BMA.EHR.Application.Repositories;
|
||||
using BMA.EHR.Application.Repositories.Leaves;
|
||||
using BMA.EHR.Command.Service.DTOs.POI;
|
||||
using BMA.EHR.Domain.Common;
|
||||
using BMA.EHR.Infrastructure.Persistence;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Newtonsoft.Json;
|
||||
using Swashbuckle.AspNetCore.Annotations;
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace BMA.EHR.Command.Service.Controllers
|
||||
{
|
||||
[Route("api/v{version:apiVersion}/leave/check-in")]
|
||||
[ApiVersion("1.0")]
|
||||
[ApiController]
|
||||
[Produces("application/json")]
|
||||
[Authorize]
|
||||
[SwaggerTag("API ระบบลงเวลาทำงาน")]
|
||||
public class CheckInController : BaseController
|
||||
{
|
||||
#region " Fields "
|
||||
|
||||
private readonly DutyTimeRepository _repository;
|
||||
private readonly LeaveDbContext _context;
|
||||
private readonly IHttpContextAccessor _httpContextAccessor;
|
||||
private readonly IWebHostEnvironment _hostingEnvironment;
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly UserProfileRepository _userProfileRepository;
|
||||
|
||||
#endregion
|
||||
|
||||
#region " Constuctor and Destructor "
|
||||
|
||||
public CheckInController(DutyTimeRepository repository,
|
||||
LeaveDbContext context,
|
||||
IHttpContextAccessor httpContextAccessor,
|
||||
IWebHostEnvironment hostingEnvironment,
|
||||
IConfiguration configuration,
|
||||
UserProfileRepository userProfileRepository)
|
||||
{
|
||||
_repository = repository;
|
||||
_context = context;
|
||||
_httpContextAccessor = httpContextAccessor;
|
||||
_hostingEnvironment = hostingEnvironment;
|
||||
_configuration = configuration;
|
||||
_userProfileRepository = userProfileRepository;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region " Properties "
|
||||
|
||||
private string? UserId => _httpContextAccessor?.HttpContext?.User?.FindFirst(ClaimTypes.NameIdentifier)?.Value;
|
||||
|
||||
private string? FullName => _httpContextAccessor?.HttpContext?.User?.FindFirst("name")?.Value;
|
||||
|
||||
private bool? PlacementAdmin => _httpContextAccessor?.HttpContext?.User?.IsInRole("placement1");
|
||||
|
||||
private Guid OcId
|
||||
{
|
||||
get
|
||||
{
|
||||
if (UserId != null || UserId != "")
|
||||
return _userProfileRepository.GetUserOCId(Guid.Parse(UserId!));
|
||||
else
|
||||
return Guid.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region " Methods "
|
||||
|
||||
[HttpPost("locations")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
[AllowAnonymous]
|
||||
public async Task<ActionResult<ResponseObject>> ListPOIAsync([FromBody] GetPOIDto data)
|
||||
{
|
||||
var api_url = $"https://maps.googleapis.com/maps/api/place/nearbysearch/json?location={data.Lat},{data.Lon}&types=point_of_interest&radius=50000&sensor=false&language=th&key=AIzaSyDXKvpU4hinlCKGOEJUgLDbx9yCSZe3woc";
|
||||
|
||||
using (var client = new HttpClient())
|
||||
{
|
||||
var req = new HttpRequestMessage(HttpMethod.Get, api_url);
|
||||
|
||||
var res = await client.SendAsync(req);
|
||||
var result = await res.Content.ReadAsStringAsync();
|
||||
var poi_result = JsonConvert.DeserializeObject<GetPOIResultDto>(result);
|
||||
|
||||
var poi_data = new List<POIResultDto>();
|
||||
|
||||
if (poi_result != null)
|
||||
{
|
||||
foreach (var r in poi_result.results.Take(5))
|
||||
{
|
||||
poi_data.Add(new POIResultDto
|
||||
{
|
||||
Id = r.place_id,
|
||||
Name = r.name,
|
||||
Latitude = r.geometry.location.lat,
|
||||
Longitude = r.geometry.location.lng,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return Success(poi_data);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
269
BMA.EHR.Leave.Service/Controllers/DutyTimeController.cs
Normal file
269
BMA.EHR.Leave.Service/Controllers/DutyTimeController.cs
Normal file
|
|
@ -0,0 +1,269 @@
|
|||
using BMA.EHR.Application.Repositories;
|
||||
using BMA.EHR.Application.Repositories.Leaves;
|
||||
using BMA.EHR.Command.Service.DTOs.DutyTime;
|
||||
using BMA.EHR.Domain.Common;
|
||||
using BMA.EHR.Domain.Models.Leave;
|
||||
using BMA.EHR.Domain.Shared;
|
||||
using BMA.EHR.Infrastructure.Persistence;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Swashbuckle.AspNetCore.Annotations;
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace BMA.EHR.Command.Service.Controllers
|
||||
{
|
||||
[Route("api/v{version:apiVersion}/leave/duty-time")]
|
||||
[ApiVersion("1.0")]
|
||||
[ApiController]
|
||||
[Produces("application/json")]
|
||||
[Authorize]
|
||||
[SwaggerTag("API ระบบจัดการรอบการลงเวลาทำงาน")]
|
||||
public class DutyTimeController : BaseController
|
||||
{
|
||||
#region " Fields "
|
||||
|
||||
private readonly DutyTimeRepository _repository;
|
||||
private readonly LeaveDbContext _context;
|
||||
private readonly IHttpContextAccessor _httpContextAccessor;
|
||||
private readonly IWebHostEnvironment _hostingEnvironment;
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly UserProfileRepository _userProfileRepository;
|
||||
|
||||
#endregion
|
||||
|
||||
#region " Constuctor and Destructor "
|
||||
|
||||
public DutyTimeController(DutyTimeRepository repository,
|
||||
LeaveDbContext context,
|
||||
IHttpContextAccessor httpContextAccessor,
|
||||
IWebHostEnvironment hostingEnvironment,
|
||||
IConfiguration configuration,
|
||||
UserProfileRepository userProfileRepository)
|
||||
{
|
||||
_repository = repository;
|
||||
_context = context;
|
||||
_httpContextAccessor = httpContextAccessor;
|
||||
_hostingEnvironment = hostingEnvironment;
|
||||
_configuration = configuration;
|
||||
_userProfileRepository = userProfileRepository;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region " Properties "
|
||||
|
||||
private string? UserId => _httpContextAccessor?.HttpContext?.User?.FindFirst(ClaimTypes.NameIdentifier)?.Value;
|
||||
|
||||
private string? FullName => _httpContextAccessor?.HttpContext?.User?.FindFirst("name")?.Value;
|
||||
|
||||
private bool? PlacementAdmin => _httpContextAccessor?.HttpContext?.User?.IsInRole("placement1");
|
||||
|
||||
private Guid OcId
|
||||
{
|
||||
get
|
||||
{
|
||||
if (UserId != null || UserId != "")
|
||||
return _userProfileRepository.GetUserOCId(Guid.Parse(UserId!));
|
||||
else
|
||||
return Guid.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region " Methods "
|
||||
|
||||
/// <summary>
|
||||
/// LV1_004 - ข้อมูลทั้งหมดของรอบการปฏิบัติงาน (ADMIN)
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// </returns>
|
||||
/// <response code="200">เมื่อทำรายการสำเร็จ</response>
|
||||
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
|
||||
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
|
||||
[HttpGet]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public async Task<ActionResult<ResponseObject>> GetAllAsync()
|
||||
{
|
||||
var data = await _repository.GetAllAsync();
|
||||
|
||||
return Success(data);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ข้อมูลของรอบการปฏิบัติงาน (ADMIN)
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// </returns>
|
||||
/// <response code="200">เมื่อทำรายการสำเร็จ</response>
|
||||
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
|
||||
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
|
||||
[HttpGet("{id:guid}")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public async Task<ActionResult<ResponseObject>> GetByIdAsync(Guid id)
|
||||
{
|
||||
var data = await _repository.GetByIdAsync(id);
|
||||
|
||||
return Success(data);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// LV1_001 - สร้างรอบการปฏิบัติงาน (ADMIN)
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// </returns>
|
||||
/// <response code="200">เมื่อทำรายการสำเร็จ</response>
|
||||
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
|
||||
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
|
||||
[HttpPost]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public async Task<ActionResult<ResponseObject>> PostAsync([FromBody] CreateDutyTimeDto data)
|
||||
{
|
||||
// validate
|
||||
var startMorning = TimeOnly.Parse(data.StartTimeMorning);
|
||||
var endMorning = TimeOnly.Parse(data.EndTimeMorning);
|
||||
var startAfternoon = TimeOnly.Parse(data.StartTimeAfternoon);
|
||||
var endAfternoon = TimeOnly.Parse(data.EndTimeAfternoon);
|
||||
|
||||
if (startMorning >= endMorning)
|
||||
{
|
||||
throw new Exception(GlobalMessages.StartTimeGreaterEnd);
|
||||
}
|
||||
|
||||
if (startAfternoon >= endAfternoon)
|
||||
{
|
||||
throw new Exception(GlobalMessages.StartTimeGreaterEnd);
|
||||
}
|
||||
|
||||
var oldData = await _repository.GetAllAsync();
|
||||
if (oldData == null || oldData.Count == 0)
|
||||
{
|
||||
var inserted = new DutyTime
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Description = data.Description,
|
||||
StartTimeMorning = data.StartTimeMorning,
|
||||
EndTimeMorning = data.EndTimeMorning,
|
||||
StartTimeAfternoon = data.StartTimeAfternoon,
|
||||
EndTimeAfternoon = data.EndTimeAfternoon,
|
||||
IsActive = true,
|
||||
IsDefault = true,
|
||||
};
|
||||
|
||||
var ret = await _repository.AddAsync(inserted);
|
||||
|
||||
return Success(ret);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (data.IsDefault)
|
||||
{
|
||||
foreach (var d in oldData)
|
||||
{
|
||||
d.IsDefault = false;
|
||||
await _repository.UpdateAsync(d);
|
||||
}
|
||||
}
|
||||
|
||||
var inserted = new DutyTime
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Description = data.Description,
|
||||
StartTimeMorning = data.StartTimeMorning,
|
||||
EndTimeMorning = data.EndTimeMorning,
|
||||
StartTimeAfternoon = data.StartTimeAfternoon,
|
||||
EndTimeAfternoon = data.EndTimeAfternoon,
|
||||
IsActive = true,
|
||||
IsDefault = data.IsDefault,
|
||||
};
|
||||
|
||||
var ret = await _repository.AddAsync(inserted);
|
||||
|
||||
return Success(ret);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// LV1_002 - แก้ไขรอบการปฏิบัติงาน (ADMIN)
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// </returns>
|
||||
/// <response code="200">เมื่อทำรายการสำเร็จ</response>
|
||||
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
|
||||
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
|
||||
[HttpPut("{id:guid}")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public async Task<ActionResult<ResponseObject>> PutAsync(Guid id, [FromBody] UpdateDutyTimeDto data)
|
||||
{
|
||||
var oldData = await _repository.GetByIdAsync(id);
|
||||
if (oldData == null)
|
||||
{
|
||||
throw new Exception(GlobalMessages.DataNotFound);
|
||||
}
|
||||
else
|
||||
{
|
||||
var oldDataList = await _repository.GetAllAsync();
|
||||
if (data.IsDefault)
|
||||
{
|
||||
foreach (var d in oldDataList)
|
||||
{
|
||||
d.IsDefault = false;
|
||||
await _repository.UpdateAsync(d);
|
||||
}
|
||||
}
|
||||
|
||||
oldData.Description = data.Description;
|
||||
oldData.IsDefault = data.IsDefault;
|
||||
oldData.IsActive = data.IsActive;
|
||||
|
||||
await _repository.UpdateAsync(oldData);
|
||||
|
||||
return Success(oldData);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// LV1_003 - ลบรอบการปฏิบัติงาน (ADMIN)
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// </returns>
|
||||
/// <response code="200">เมื่อทำรายการสำเร็จ</response>
|
||||
/// <response code="401">ไม่ได้ Login เข้าระบบ</response>
|
||||
/// <response code="500">เมื่อเกิดข้อผิดพลาดในการทำงาน</response>
|
||||
[HttpDelete("{id:guid}")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
||||
public async Task<ActionResult<ResponseObject>> DeleteAsync(Guid id)
|
||||
{
|
||||
var oldData = await _repository.GetByIdAsync(id);
|
||||
if (oldData == null)
|
||||
{
|
||||
throw new Exception(GlobalMessages.DataNotFound);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (oldData.IsActive || oldData.IsDefault)
|
||||
{
|
||||
throw new Exception("ไม่สามารถลบรอบการปฏิบัติงานที่ยังใช้งานอยู่ได้");
|
||||
}
|
||||
|
||||
await _repository.DeleteAsync(oldData);
|
||||
return Success();
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue