From df6810922b8e90332e3e5a21b6949e092d3b3a3a Mon Sep 17 00:00:00 2001 From: Giuliano Paschoalino Date: Fri, 27 Feb 2026 14:03:52 -0300 Subject: [PATCH] Adiciona campo JsonBody em ProcessedInvoices (jsonb) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- ...AddJsonBodyToProcessedInvoices.Designer.cs | 55 +++++++++++++++++++ ...27164052_AddJsonBodyToProcessedInvoices.cs | 28 ++++++++++ .../WebhookDbContextModelSnapshot.cs | 8 ++- Webhook 4docs/ProcessedInvoices.cs | 2 + Webhook 4docs/Program.cs | 4 +- Webhook 4docs/WebhookDbContext.cs | 16 ++++++ 6 files changed, 110 insertions(+), 3 deletions(-) create mode 100644 Webhook 4docs/Migrations/20260227164052_AddJsonBodyToProcessedInvoices.Designer.cs create mode 100644 Webhook 4docs/Migrations/20260227164052_AddJsonBodyToProcessedInvoices.cs diff --git a/Webhook 4docs/Migrations/20260227164052_AddJsonBodyToProcessedInvoices.Designer.cs b/Webhook 4docs/Migrations/20260227164052_AddJsonBodyToProcessedInvoices.Designer.cs new file mode 100644 index 0000000..82b9488 --- /dev/null +++ b/Webhook 4docs/Migrations/20260227164052_AddJsonBodyToProcessedInvoices.Designer.cs @@ -0,0 +1,55 @@ +// +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 + { + /// + 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("InvoiceId") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("InvoiceId")); + + b.Property("DateTimeProcessed") + .HasColumnType("timestamp with time zone"); + + b.Property("InvoicePath") + .HasColumnType("text"); + + b.Property("JsonBody") + .HasColumnType("jsonb"); + + b.Property("Status") + .HasColumnType("text"); + + b.HasKey("InvoiceId"); + + b.ToTable("ProcessedInvoices"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Webhook 4docs/Migrations/20260227164052_AddJsonBodyToProcessedInvoices.cs b/Webhook 4docs/Migrations/20260227164052_AddJsonBodyToProcessedInvoices.cs new file mode 100644 index 0000000..9ee4bd9 --- /dev/null +++ b/Webhook 4docs/Migrations/20260227164052_AddJsonBodyToProcessedInvoices.cs @@ -0,0 +1,28 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace Webhook_4docs.Migrations +{ + /// + public partial class AddJsonBodyToProcessedInvoices : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AddColumn( + name: "JsonBody", + table: "ProcessedInvoices", + type: "jsonb", + nullable: true); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropColumn( + name: "JsonBody", + table: "ProcessedInvoices"); + } + } +} diff --git a/Webhook 4docs/Migrations/WebhookDbContextModelSnapshot.cs b/Webhook 4docs/Migrations/WebhookDbContextModelSnapshot.cs index 803e749..3a9bf00 100644 --- a/Webhook 4docs/Migrations/WebhookDbContextModelSnapshot.cs +++ b/Webhook 4docs/Migrations/WebhookDbContextModelSnapshot.cs @@ -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("InvoiceId") .ValueGeneratedOnAdd() @@ -35,6 +36,9 @@ namespace Webhook_4docs.Migrations b.Property("InvoicePath") .HasColumnType("text"); + b.Property("JsonBody") + .HasColumnType("jsonb"); + b.Property("Status") .HasColumnType("text"); diff --git a/Webhook 4docs/ProcessedInvoices.cs b/Webhook 4docs/ProcessedInvoices.cs index e14b4f1..353a135 100644 --- a/Webhook 4docs/ProcessedInvoices.cs +++ b/Webhook 4docs/ProcessedInvoices.cs @@ -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; } } } \ No newline at end of file diff --git a/Webhook 4docs/Program.cs b/Webhook 4docs/Program.cs index 52d21d1..2965e6f 100644 --- a/Webhook 4docs/Program.cs +++ b/Webhook 4docs/Program.cs @@ -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"); diff --git a/Webhook 4docs/WebhookDbContext.cs b/Webhook 4docs/WebhookDbContext.cs index a76cd03..8e313a8 100644 --- a/Webhook 4docs/WebhookDbContext.cs +++ b/Webhook 4docs/WebhookDbContext.cs @@ -1,9 +1,25 @@ using Microsoft.EntityFrameworkCore; +using System.Text.Json; namespace Webhook_4docs { public class WebhookDbContext(DbContextOptions options) : DbContext(options) { public DbSet ProcessedInvoices { get; set; } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + base.OnModelCreating(modelBuilder); + + modelBuilder.Entity() + .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); } } \ No newline at end of file