fix leave

This commit is contained in:
Suphonchai Phoonsawat 2024-08-19 16:00:00 +07:00
parent f827ed558c
commit f0fe68d851
9 changed files with 554 additions and 63 deletions

View file

@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.0" />
<PackageReference Include="RabbitMQ.Client" Version="6.8.1" />
</ItemGroup>
<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

View file

@ -0,0 +1,28 @@
# See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.
# This stage is used when running from VS in fast mode (Default for Debug configuration)
FROM mcr.microsoft.com/dotnet/runtime:8.0 AS base
USER app
WORKDIR /app
# This stage is used to build the service project
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["BMA.EHR.CheckInConsumer/BMA.EHR.CheckInConsumer.csproj", "BMA.EHR.CheckInConsumer/"]
RUN dotnet restore "./BMA.EHR.CheckInConsumer/BMA.EHR.CheckInConsumer.csproj"
COPY . .
WORKDIR "/src/BMA.EHR.CheckInConsumer"
RUN dotnet build "./BMA.EHR.CheckInConsumer.csproj" -c $BUILD_CONFIGURATION -o /app/build
# This stage is used to publish the service project to be copied to the final stage
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./BMA.EHR.CheckInConsumer.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
# This stage is used in production or when running from VS in regular mode (Default when not using the Debug configuration)
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "BMA.EHR.CheckInConsumer.dll"]

View file

@ -0,0 +1,140 @@
using Microsoft.Extensions.Configuration;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
var basePath = Directory.GetCurrentDirectory();
var configuration = new ConfigurationBuilder()
.SetBasePath(basePath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.Build();
WriteToConsole("Consumer Start!");
var host = configuration["Rabbit:Host"] ?? "";
var user = configuration["Rabbit:User"] ?? "";
var pass = configuration["Rabbit:Password"] ?? "";
// create connection
var factory = new ConnectionFactory()
{
//Uri = new Uri("amqp://admin:P@ssw0rd@192.168.4.11:5672")
HostName = host,// หรือ hostname ของ RabbitMQ Server ที่คุณใช้
UserName = user, // ใส่ชื่อผู้ใช้ของคุณ
Password = pass // ใส่รหัสผ่านของคุณ
};
using var connection = factory.CreateConnection();
using var channel = connection.CreateModel();
channel.QueueDeclare(queue: "checkin-queue", durable: false, exclusive: false, autoDelete: false, arguments: null);
var consumer = new EventingBasicConsumer(channel);
consumer.Received += async (model, ea) =>
{
var body = ea.Body.ToArray();
var message = Encoding.UTF8.GetString(body);
await CallRestApi(message);
// convert string into object
//var request = JsonConvert.DeserializeObject<CheckInRequest>(message);
//using (var db = new ApplicationDbContext())
//{
// var item = new AttendantItem
// {
// Name = request.Name,
// CheckInDateTime = request.CheckInDateTime,
// };
// db.AttendantItems.Add(item);
// db.SaveChanges();
// WriteToConsole($"ได้รับคำขอจาก Queue: {message}");
// WriteToConsole($"ตอบกลับจาก REST API: {JsonConvert.SerializeObject(item)}");
//}
WriteToConsole($"ได้รับคำขอจาก Queue: {message}");
//WriteToConsole($"ตอบกลับจาก REST API: {JsonConvert.SerializeObject(item)}");
};
channel.BasicConsume(queue: "checkin-queue", autoAck: true, consumer: consumer);
Console.WriteLine("\nPress 'Enter' to exit the process...");
// another use of "Console.ReadKey()" method
// here it asks to press the enter key to exit
while (Console.ReadKey().Key != ConsoleKey.Enter)
{
}
static void WriteToConsole(string message)
{
Console.WriteLine($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")} : {message}");
}
async Task CallRestApi(string requestData)
{
using var client = new HttpClient();
var apiPath = $"{configuration["API"]}/leave/process-check-in";
var content = new StringContent(requestData, Encoding.UTF8, "application/json");
var response = await client.PostAsync(apiPath, content);
if (response.IsSuccessStatusCode)
{
var responseContent = await response.Content.ReadAsStringAsync();
WriteToConsole(responseContent);
}
else
{
var errorMessage = await response.Content.ReadAsStringAsync();
var res = JsonSerializer.Deserialize<ResponseObject>(errorMessage);
WriteToConsole($"Error: {res.Message}");
}
}
public class ResponseObject
{
[JsonPropertyName("status")]
public int Status { get; set; }
[JsonPropertyName("message")]
public string? Message { get; set; }
[JsonPropertyName("result")]
public object? Result { get; set; }
}
public class CheckTimeDtoRB
{
public Guid? CheckInId { get; set; }
public double Lat { get; set; } = 0;
public double Lon { get; set; } = 0;
public string POI { get; set; } = string.Empty;
public bool IsLocation { get; set; } = true;
public string? LocationName { get; set; } = string.Empty;
public string? Remark { get; set; } = string.Empty;
public Guid? UserId { get; set; }
public DateTime? CurrentDate { get; set; }
public string? CheckInFileName { get; set; }
public byte[]? CheckInFileBytes { get; set; }
}

View file

@ -0,0 +1,10 @@
{
"profiles": {
"BMA.EHR.CheckInConsumer": {
"commandName": "Project"
},
"Container (Dockerfile)": {
"commandName": "Docker"
}
}
}

View file

@ -0,0 +1,8 @@
{
"Rabbit": {
"Host": "192.168.1.40",
"User": "admin",
"Password": "Test123456"
},
"API": "https://localhost:7283/api/v1"
}