441 lines
22 KiB
C#
441 lines
22 KiB
C#
using System.Text.Json;
|
|
using AyCode.Core.Loggers;
|
|
using AyCode.Services.SignalRs;
|
|
using AyCode.Utils.Extensions;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
using Newtonsoft.Json.Serialization;
|
|
using TIAM.Core.Enums;
|
|
using TIAM.Database.DataLayers.Admins;
|
|
using TIAM.Entities.Emails;
|
|
using TIAM.Entities.Transfers;
|
|
using TIAM.Services;
|
|
using TIAM.Services.Server;
|
|
using TIAMWebApp.Shared.Application.Models;
|
|
using TIAMWebApp.Shared.Application.Models.ClientSide.Messages;
|
|
using TIAMWebApp.Server.Services;
|
|
using TIAMWebApp.Shared.Application.Models.ClientSide;
|
|
using AyCode.Core.Extensions;
|
|
|
|
namespace TIAMWebApp.Server.Controllers
|
|
{
|
|
[ApiController]
|
|
[Route("api/v1/[controller]")]
|
|
public class TransferDataAPIController : ControllerBase
|
|
{
|
|
//private static readonly TransferDestination[] Names = new TransferDestination[]
|
|
//{
|
|
// /*"Castle of Buda", "Hungarian National Museum", "Parliament of Hungary", "Heroes square", "Gellert Hill", "Margaret Island"*/
|
|
// new() { DestinationId = 1, DestinationName = "Airport", DestinationDescription = "International airport of Budapest", DestinationLatitude = 42.234444f, DestinationLongitude = 39.100010f },
|
|
// new() { DestinationId = 2, DestinationName = "Castle of Buda", DestinationDescription = "International airport of Budapest", DestinationLatitude = 42.234444f, DestinationLongitude = 39.100010f },
|
|
// new() { DestinationId = 3, DestinationName = "Hungarian National Museum", DestinationDescription = "International airport of Budapest", DestinationLatitude = 42.234444f, DestinationLongitude = 39.100010f },
|
|
// new() { DestinationId = 4, DestinationName = "Parliament of Hungary", DestinationDescription = "International airport of Budapest", DestinationLatitude = 42.234444f, DestinationLongitude = 39.100010f },
|
|
// new() { DestinationId = 5, DestinationName = "Heroes square", DestinationDescription = "International airport of Budapest", DestinationLatitude = 42.234444f, DestinationLongitude = 39.100010f },
|
|
// new() { DestinationId = 6, DestinationName = "Gellert Hill", DestinationDescription = "International airport of Budapest", DestinationLatitude = 42.234444f, DestinationLongitude = 39.100010f },
|
|
// new() { DestinationId = 6, DestinationName = "Margaret Island", DestinationDescription = "International airport of Budapest", DestinationLatitude = 42.234444f, DestinationLongitude = 39.100010f }
|
|
|
|
//};
|
|
|
|
private readonly AdminDal _adminDal;
|
|
private readonly TIAM.Core.Loggers.ILogger _logger;
|
|
private readonly TransferBackendService _transferBackendService;
|
|
private readonly IMessageSenderService _messageSenderService;
|
|
private readonly AuthService _authService;
|
|
|
|
public TransferDataAPIController(AdminDal adminDal, TransferBackendService transferBackendService, IMessageSenderService messageSenderService, IEnumerable<IAcLogWriterBase> logWriters, AuthService authService)
|
|
{
|
|
_adminDal = adminDal;
|
|
_transferBackendService = transferBackendService;
|
|
|
|
_logger = new TIAM.Core.Loggers.Logger<TransferDataAPIController>(logWriters.ToArray());
|
|
_messageSenderService = messageSenderService;
|
|
_authService = authService;
|
|
}
|
|
|
|
|
|
|
|
[AllowAnonymous]
|
|
[HttpGet]
|
|
[Route(APIUrls.GetTransferDestinationsRouteName)]
|
|
public async Task<IEnumerable<TransferDestination>> GetTransferDestinations()
|
|
{
|
|
return await _adminDal.Context.TransferDestinations.ToListAsync();
|
|
}
|
|
|
|
[AllowAnonymous]
|
|
[HttpGet]
|
|
[Route("GetTransferDestinationByCoordinates")]
|
|
public async Task<TransferDestination?> GetTransferDestinationByCoordinates(double latitude, double longitude)
|
|
{
|
|
return null;// await _transferDestinationDal.Context.TransferDestinations.FirstOrDefaultAsync(x => x.Latitude == latitude && x.Longitude == longitude);
|
|
}
|
|
|
|
[AllowAnonymous]
|
|
[HttpGet]
|
|
[Route("GetTransferDestinationByAddress")]
|
|
public async Task<TransferDestination?> GetTransferDestinationByAddress(string address)
|
|
{
|
|
return null;//await _transferDestinationDal.Context.TransferDestinations.FirstOrDefaultAsync(x => x.Address == address);
|
|
}
|
|
|
|
[AllowAnonymous]
|
|
[HttpPost]
|
|
[Route(APIUrls.CreateTransferDestinationRouteName)]
|
|
public async Task<IActionResult> CreateTransferDestination([FromBody] JsonElement serializedTransferDestinationModel)
|
|
{
|
|
_logger.Info(@"CreateTransferDestination called!");
|
|
if (string.IsNullOrEmpty(serializedTransferDestinationModel.GetRawText()))
|
|
{
|
|
return BadRequest("SerializedTramsferDestinationWizardModel is required");
|
|
}
|
|
else
|
|
{
|
|
TransferDestination? transferDestination = JObject.Parse(serializedTransferDestinationModel.GetRawText()).ToObject<TransferDestination>();
|
|
|
|
if (transferDestination != null)
|
|
{
|
|
|
|
var id = Guid.NewGuid();
|
|
//TransferDestination transferDestination = new TransferDestination(id, transferDestinationModel.Name, transferDestinationModel.Description, transferDestinationModel.AddressString);
|
|
|
|
|
|
if (string.IsNullOrEmpty(transferDestination.Name) || string.IsNullOrEmpty(transferDestination.AddressString))
|
|
{
|
|
return BadRequest("Invalid request");
|
|
}
|
|
else
|
|
{
|
|
_logger.Debug(transferDestination.ToString());
|
|
|
|
//await _transferDestinationDal.Context.TransferDestinations.AddAsync(transferDestination);
|
|
await _adminDal.AddTransferDestinationAsync(transferDestination);
|
|
return Ok(transferDestination);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
return BadRequest("Invalid request");
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
[AllowAnonymous]
|
|
[HttpPost]
|
|
[Route(APIUrls.UpdateTransferDestinationRouteName)]
|
|
public async Task<IActionResult> UpdateTransferDestination([FromBody] JsonElement serializedTransferDestination)
|
|
{
|
|
_logger.Info(@"UpdateTransferDestination called!");
|
|
if (string.IsNullOrEmpty(serializedTransferDestination.GetRawText()))
|
|
{
|
|
_logger.Error(@"Bad request!");
|
|
return BadRequest("SerializedTramsferDestinationWizardModel is required");
|
|
}
|
|
else
|
|
{
|
|
_logger.Info(@"Serialized model not empty!");
|
|
TransferDestination? transferDestination = JObject.Parse(serializedTransferDestination.GetRawText()).ToObject<TransferDestination>();
|
|
_logger.Info($@"TransferDestination to be updated: {serializedTransferDestination.GetRawText()}");
|
|
_logger.Info($@"TransferDestination to be updated: {transferDestination.AddressString}");
|
|
|
|
|
|
if (transferDestination != null)
|
|
{
|
|
|
|
|
|
//TransferDestination transferDestination = new TransferDestination(id, transferDestinationModel.Name, transferDestinationModel.Description, transferDestinationModel.AddressString);
|
|
|
|
if (transferDestination.Id == Guid.Empty || string.IsNullOrEmpty(transferDestination.Name) || string.IsNullOrEmpty(transferDestination.AddressString))
|
|
{
|
|
_logger.Error(@"Serialized model not empty, but bad request!");
|
|
return BadRequest("Invalid request");
|
|
}
|
|
else
|
|
{
|
|
|
|
_logger.Info($@"TransferDestination to be updated: {transferDestination.Id}");
|
|
_logger.Info($@"TransferDestination to be updated new name: {transferDestination.Name}");
|
|
_logger.Info($@"TransferDestination to be updated new price: {transferDestination.Price}");
|
|
//Console.WriteLine($"TransferDestination to be updated new price: {transferDestination.Price2}");
|
|
//Console.WriteLine($"TransferDestination to be updated new price: {transferDestination.Price3}");
|
|
//Console.WriteLine($"TransferDestination to be updated new priceType: {transferDestination.PriceType}");
|
|
_logger.Info($@"TransferDestination to be updated new address: {transferDestination.AddressString}");
|
|
_logger.Info($@"TransferDestination to be updated new description: {transferDestination.Description}");
|
|
|
|
//var dbTransferDestinationJson = _adminDal.GetTransferDestinationJsonById(transferDestination.Id);
|
|
//_logger.Info($"TransferDestination JSON to be updated: {dbTransferDestinationJson}");
|
|
|
|
//var dbTransferDestination = JObject.Parse(dbTransferDestinationJson).ToObject<TransferDestination>();
|
|
|
|
//var dbTransferDestination = _adminDal.GetTransferDestinationById(transferDestination.Id, true);
|
|
//if (dbTransferDestination.Id != Guid.Empty)
|
|
//{
|
|
// dbTransferDestination.AddressId = transferDestination.AddressId;
|
|
// dbTransferDestination.Price = transferDestination.Price;
|
|
// dbTransferDestination.PriceType = transferDestination.PriceType;
|
|
// dbTransferDestination.Name = transferDestination.Name;
|
|
// dbTransferDestination.Description = transferDestination.Description;
|
|
// dbTransferDestination.AddressString = transferDestination.AddressString;
|
|
// dbTransferDestination.Address = transferDestination.Address;
|
|
|
|
//}
|
|
|
|
//await _transferDestinationDal.Context.TransferDestinations.AddAsync(transferDestination);
|
|
await _adminDal.UpdateTransferDestinationAsync(transferDestination);
|
|
return Ok(transferDestination);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
return BadRequest("Invalid request");
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
[AllowAnonymous]
|
|
[HttpPost]
|
|
[Route(APIUrls.CreateTransferRouteName)]
|
|
public async Task<IActionResult> CreateTransfer([FromBody] JsonElement serializedTransferModel)
|
|
{
|
|
_logger.Info(@"CreateTransfer called!");
|
|
if (string.IsNullOrEmpty(serializedTransferModel.GetRawText()))
|
|
{
|
|
return BadRequest("SerializedTramsferDestinationWizardModel is required");
|
|
}
|
|
else
|
|
{
|
|
Transfer? transfer = JObject.Parse(serializedTransferModel.GetRawText()).ToObject<Transfer>();
|
|
|
|
if (transfer != null)
|
|
{
|
|
|
|
var id = Guid.NewGuid();
|
|
//TransferDestination transferDestination = new TransferDestination(id, transferDestinationModel.Name, transferDestinationModel.Description, transferDestinationModel.AddressString);
|
|
|
|
|
|
if (string.IsNullOrEmpty(transfer.FromAddress) || string.IsNullOrEmpty(transfer.ToAddress))
|
|
{
|
|
return BadRequest("Invalid request");
|
|
}
|
|
else
|
|
{
|
|
_logger.Info($@"TransferDestination to be created: {id}");
|
|
_logger.Info($@"TransferDestination to be created: {transfer.FromAddress}");
|
|
_logger.Info($@"TransferDestination to be created: {transfer.ToAddress}");
|
|
_logger.Info($@"TransferDestination to be created: {transfer.ProductId}");
|
|
_logger.Info($@"TransferDestination to be created: {transfer.Price}");
|
|
|
|
var from = await _adminDal.Context.TransferDestinations.FirstOrDefaultAsync(x => x.AddressString == transfer.FromAddress);
|
|
var to = await _adminDal.Context.TransferDestinations.FirstOrDefaultAsync(x => x.AddressString == transfer.ToAddress);
|
|
|
|
//TODO
|
|
if (!transfer.ProductId.IsNullOrEmpty())
|
|
transfer.Price = _transferBackendService.GetTransferPrice(transfer.ProductId.Value, from, to, transfer.PassengerCount);
|
|
|
|
transfer.TransferStatusType = TransferStatusType.OrderSubmitted;
|
|
|
|
await _adminDal.AddTransferAsync(transfer);
|
|
|
|
|
|
_logger.Info($"Created transfer, send emailMessage!!!");
|
|
var message = new MessageSenderModel<EmailMessage>();
|
|
message.Message.Id = Guid.NewGuid();
|
|
message.Message.Subject = "[Tour I Am] New transfer in Budapest";
|
|
message.Message.ContextId = transfer.Id;
|
|
message.Message.SenderId = Guid.Empty;
|
|
message.Message.Recipients.Add(new EmailRecipient(Guid.NewGuid(), transfer.UserId, Guid.NewGuid(), transfer.ContactEmail));
|
|
|
|
string FormatEmailContent()
|
|
{
|
|
return $@"
|
|
<html>
|
|
<body>
|
|
<p>Dear {transfer.FullName},</p>
|
|
<p>We are pleased to inform you that a transfer order has been placed. Below are the details of the transfer:</p>
|
|
<p>{transfer.FromAddress} - {transfer.ToAddress}</p>
|
|
<p>{transfer.Appointment}</p>
|
|
<p>{transfer.FullName}</p>
|
|
<p>{transfer.PassengerCount}</p>
|
|
<p>Please confirm the transfer by clicking on the following link:</p>
|
|
<p><a href=""{Setting.BaseUrl}/mytransfers/{transfer.Id}"">Confirm Transfer</a></p>
|
|
<p>If you did not request this transfer, please disregard this email.</p>
|
|
<p>Thank you,<br/>Tour I Am team</p>
|
|
</body>
|
|
</html>";
|
|
}
|
|
message.Message.Text = FormatEmailContent();
|
|
_logger.Info(message.Message.Text);
|
|
//message.Message.Text = $"Dear {transfer.FullName}! /n We have received an order from you, please confirm the details here: https://www.touriam.com/mytransfer?{transfer.Id}";
|
|
var messageElement = message.Message;
|
|
var result = await _messageSenderService.SendMessageAsync(messageElement, (int)message.MessageType);
|
|
await _adminDal.AddEmailMessageAsync(messageElement);
|
|
_logger.Info("SendEmail result: " + result);
|
|
|
|
|
|
return Ok(transfer);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
return BadRequest("Invalid request");
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
[AllowAnonymous]
|
|
[HttpPost]
|
|
[Route(APIUrls.CreateTransfersRouteName)]
|
|
public async Task<IActionResult> CreateTransfers([FromBody] JsonElement serializedTransferList)
|
|
{
|
|
_logger.Info(@"CreateTransfers called!");
|
|
if (string.IsNullOrEmpty(serializedTransferList.GetRawText()))
|
|
{
|
|
return BadRequest("SerializedTramsferDestinationWizardModel is required");
|
|
}
|
|
else
|
|
{
|
|
_logger.Info($@"Serialized model: {serializedTransferList.GetRawText()}");
|
|
|
|
var settings = new JsonSerializerSettings
|
|
{
|
|
ContractResolver = new CamelCasePropertyNamesContractResolver()
|
|
};
|
|
|
|
List<Transfer>? transfers = JsonConvert.DeserializeObject<List<Transfer>>(serializedTransferList.GetRawText(), settings);
|
|
|
|
//List<Transfer>? transfers = JObject.Parse(serializedTransferModel.GetRawText()).ToObject<List<Transfer>>();
|
|
List<Transfer> createdTransfers = new List<Transfer>();
|
|
if (transfers != null)
|
|
{
|
|
foreach (var transfer in transfers)
|
|
{
|
|
var id = Guid.NewGuid();
|
|
var result = await _adminDal.AddTransferAsync(transfer);
|
|
if (result)
|
|
{
|
|
createdTransfers.Add(transfer);
|
|
}
|
|
}
|
|
|
|
foreach (var createdTransfer in createdTransfers)
|
|
{
|
|
_logger.Info($"Created transfer, send emailMessage!!!");
|
|
var message = new MessageSenderModel<EmailMessage>();
|
|
message.Message = new EmailMessage();
|
|
message.Message.Id = Guid.NewGuid();
|
|
message.MessageType = AyCode.Models.Enums.MessageTypesEnum.email;
|
|
message.Message.Subject = "[Tour I Am] New transfer in Budapest";
|
|
message.Message.ContextId = createdTransfer.Id;
|
|
message.Message.SenderId = Guid.Empty;
|
|
message.Message.Recipients.Add(new EmailRecipient(Guid.NewGuid(), createdTransfer.UserId, Guid.NewGuid(), createdTransfer.ContactEmail));
|
|
string FormatEmailContent()
|
|
{
|
|
return $@"
|
|
<html>
|
|
<body>
|
|
<p>Dear {createdTransfer.FullName},</p>
|
|
<p>We are pleased to inform you that a transfer order has been placed. Below are the details of the transfer:</p>
|
|
<p>
|
|
{createdTransfer.FromAddress} - {createdTransfer.ToAddress}</p>
|
|
<p>{createdTransfer.Appointment}</p>
|
|
<p>{createdTransfer.FullName}</p>
|
|
<p>{createdTransfer.PassengerCount}</p>
|
|
<p>Please confirm the transfer by clicking on the following link:</p>
|
|
<p><a href=""{Setting.BaseUrl}/mytransfers/{createdTransfer.Id}"">Confirm Transfer</a></p>
|
|
<p>If you did not request this transfer, please disregard this email.</p>
|
|
<p>Thank you,<br/>Tour I Am team</p>
|
|
</body>
|
|
</html>";
|
|
}
|
|
message.Message.Text = FormatEmailContent();
|
|
_logger.Info(message.Message.Text);
|
|
var messageElement = message.Message;
|
|
Console.WriteLine(message.Message);
|
|
var result = await _messageSenderService.SendMessageAsync(messageElement, (int)message.MessageType);
|
|
await _adminDal.AddEmailMessageAsync(messageElement);
|
|
_logger.Info("SendEmail result: " + result);
|
|
}
|
|
|
|
return Ok(createdTransfers);
|
|
}
|
|
else
|
|
{
|
|
return BadRequest("Invalid request");
|
|
}
|
|
}
|
|
}
|
|
|
|
[Authorize]
|
|
[HttpGet]
|
|
[Route(APIUrls.GetTransfersRouteName)]
|
|
[SignalR(SignalRTags.GetTransfers)]
|
|
public async Task<string> GetTransfers()
|
|
{
|
|
//var token = _authService.GetAuthTokenFromRequest(Request);
|
|
//_logger.Detail(token);
|
|
|
|
var result = await _adminDal.GetTransfersJsonAsync();
|
|
return result;
|
|
}
|
|
|
|
[Authorize]
|
|
[HttpGet]
|
|
[Route(APIUrls.GetTransfersByUserIdRouteName)]
|
|
[SignalR(SignalRTags.GetTransfersByUserId)]
|
|
public async Task<string> GetTransfersByUserId(Guid userId)
|
|
{
|
|
var result = await _adminDal.GetTransfersByUserIdJsonAsync(userId);
|
|
return result;
|
|
}
|
|
|
|
[Authorize]
|
|
[HttpGet]
|
|
[Route(APIUrls.GetTransfersByDriverRouteName)]
|
|
[SignalR(SignalRTags.GetTransfersByDriverId)]
|
|
public Task<string> GetDriverTransfers(Guid transferId, Guid userProductMappingId)
|
|
{
|
|
throw new NotImplementedException();
|
|
//var result = await _adminDal.GetTransfersByUserIdJsonAsync(userId);
|
|
//return result;
|
|
}
|
|
|
|
[AllowAnonymous]
|
|
[HttpPost]
|
|
[Route(APIUrls.GetTransferByIdRouteName)]
|
|
[SignalR(SignalRTags.GetTransfer)]
|
|
public string? GetTransferById([FromBody] Guid transferId)
|
|
{
|
|
_logger.Info($"Get transfer by id called; transferId: {transferId}");
|
|
|
|
return _adminDal.GetTransferJsonById(transferId);
|
|
}
|
|
|
|
[AllowAnonymous]
|
|
[HttpPost]
|
|
[Route(APIUrls.UpdateTransferRouteName)]
|
|
[SignalR(SignalRTags.UpdateTransfer)]
|
|
public async Task<Transfer> UpdateTransfer(Transfer transferToModify)
|
|
{
|
|
_logger.Info($"UpdateTransfer called! + {transferToModify.Id}");
|
|
await _adminDal.UpdateTransferAsync(transferToModify);
|
|
|
|
return transferToModify;
|
|
}
|
|
|
|
}
|
|
} |