Adiciona campo JsonBody em ProcessedInvoices (jsonb)

Inclui suporte para armazenar dados JSON na tabela ProcessedInvoices usando o tipo jsonb do PostgreSQL. Implementa conversão entre JsonDocument e string JSON no modelo. Cria migração para adicionar/remover a coluna. Atualiza versão do EF Core para 9.0.0. Ajusta gravação de faturas para salvar o corpo JSON recebido.
This commit is contained in:
Giuliano Paschoalino 2026-02-27 14:03:52 -03:00
parent 001c467a7c
commit df6810922b
6 changed files with 110 additions and 3 deletions

View File

@ -0,0 +1,55 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
using Webhook_4docs;
#nullable disable
namespace Webhook_4docs.Migrations
{
[DbContext(typeof(WebhookDbContext))]
[Migration("20260227164052_AddJsonBodyToProcessedInvoices")]
partial class AddJsonBodyToProcessedInvoices
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "9.0.0")
.HasAnnotation("Relational:MaxIdentifierLength", 63);
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
modelBuilder.Entity("Webhook_4docs.ProcessedInvoices", b =>
{
b.Property<int>("InvoiceId")
.ValueGeneratedOnAdd()
.HasColumnType("integer");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("InvoiceId"));
b.Property<DateTime>("DateTimeProcessed")
.HasColumnType("timestamp with time zone");
b.Property<string>("InvoicePath")
.HasColumnType("text");
b.Property<string>("JsonBody")
.HasColumnType("jsonb");
b.Property<string>("Status")
.HasColumnType("text");
b.HasKey("InvoiceId");
b.ToTable("ProcessedInvoices");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,28 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Webhook_4docs.Migrations
{
/// <inheritdoc />
public partial class AddJsonBodyToProcessedInvoices : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "JsonBody",
table: "ProcessedInvoices",
type: "jsonb",
nullable: true);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "JsonBody",
table: "ProcessedInvoices");
}
}
}

View File

@ -4,6 +4,7 @@ using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
using Webhook_4docs;
#nullable disable
@ -16,12 +17,12 @@ namespace Webhook_4docs.Migrations
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "7.0.17")
.HasAnnotation("ProductVersion", "9.0.0")
.HasAnnotation("Relational:MaxIdentifierLength", 63);
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
modelBuilder.Entity("ProcessedInvoices", b =>
modelBuilder.Entity("Webhook_4docs.ProcessedInvoices", b =>
{
b.Property<int>("InvoiceId")
.ValueGeneratedOnAdd()
@ -35,6 +36,9 @@ namespace Webhook_4docs.Migrations
b.Property<string>("InvoicePath")
.HasColumnType("text");
b.Property<string>("JsonBody")
.HasColumnType("jsonb");
b.Property<string>("Status")
.HasColumnType("text");

View File

@ -1,4 +1,5 @@
using System.ComponentModel.DataAnnotations;
using System.Text.Json;
namespace Webhook_4docs
{
@ -9,5 +10,6 @@ namespace Webhook_4docs
public DateTime DateTimeProcessed { get; set; }
public string? Status { get; set; }
public string? InvoicePath { get; set; }
public JsonDocument? JsonBody { get; set; }
}
}

View File

@ -104,6 +104,7 @@ namespace Webhook_4docs
}
var JsonBody = JsonDocument.Parse(requestBody).RootElement;
var JsonBodyDocument = JsonDocument.Parse(requestBody);
string CaminhoDB = "X:/Middle/Informativo Setorial/Modelo Word/BD1_dados cadastrais e faturas.accdb";
if (JsonBody.TryGetProperty("requestID", out JsonElement fatura_ID_json))
@ -195,7 +196,8 @@ namespace Webhook_4docs
InvoiceId = Int32.Parse(fatura_ID),
DateTimeProcessed = DateTime.UtcNow,
Status = status,
InvoicePath = fatura_arquivo
InvoicePath = fatura_arquivo,
JsonBody = JsonBodyDocument
};
logger.LogInformation("Fatura incluída no BD");

View File

@ -1,9 +1,25 @@
using Microsoft.EntityFrameworkCore;
using System.Text.Json;
namespace Webhook_4docs
{
public class WebhookDbContext(DbContextOptions<WebhookDbContext> options) : DbContext(options)
{
public DbSet<ProcessedInvoices> ProcessedInvoices { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ProcessedInvoices>()
.Property(p => p.JsonBody)
.HasColumnType("jsonb")
.HasConversion(
v => v == null ? null : v.RootElement.GetRawText(),
v => v == null ? null : ParseJsonDocument(v));
}
private static JsonDocument ParseJsonDocument(string json)
=> JsonDocument.Parse(json);
}
}