hrms-api-backend/BMA.EHR.Leave.Service/Controllers/CheckInController.cs

114 lines
4.1 KiB
C#

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
}
}