Adicionado as principais estruturas e regras.

This commit is contained in:
Adriano Serighelli 2025-09-11 18:36:12 -03:00
parent 92e9d274dc
commit b375fdb1ce
6 changed files with 139 additions and 16 deletions

View File

@ -0,0 +1,79 @@
using BackupPipefy.Domain.Entities;
using BackupPipefy.Infrastructure.Data;
using BackupPipefy.Infrastructure.Services;
using Microsoft.EntityFrameworkCore;
public class BackupService
{
private readonly PipefyClient _client;
private readonly BackupContext _context;
private readonly int _requestsPerMinute;
public BackupService(PipefyClient client, BackupContext context, int requestsPerMinute)
{
_client = client;
_context = context;
_requestsPerMinute = requestsPerMinute;
}
public async Task RunBackup(int pipeId)
{
var control = await _context.BackupControls.FirstOrDefaultAsync();
if (control == null)
{
control = new BackupControl { LastBackupTime = DateTime.MinValue };
_context.BackupControls.Add(control);
await _context.SaveChangesAsync();
}
DateTime startTime = DateTime.Now;
string json = await _client.GetCardsAsync(pipeId, control.LastBackupTime);
var doc = System.Text.Json.JsonDocument.Parse(json);
var edges = doc.RootElement.GetProperty("data")
.GetProperty("allCards")
.GetProperty("edges");
int count = 0;
int delayMs = (int)(60000 / _requestsPerMinute);
foreach (var edge in edges.EnumerateArray())
{
var node = edge.GetProperty("node");
long id = long.Parse(node.GetProperty("id").GetString());
var card = await _context.PipefyCards.FindAsync(id);
if (card != null)
{
card.JsonData = node.GetRawText();
}
else
{
await _context.PipefyCards.AddAsync(new PipefyCard
{
Id = id,
JsonData = node.GetRawText()
});
}
count++;
await Task.Delay(delayMs);
}
await _context.SaveChangesAsync();
// Atualiza o controle de backup
control.LastBackupTime = startTime;
await _context.SaveChangesAsync();
// Grava log
_context.BackupLogs.Add(new BackupLog
{
ExecutionTime = startTime,
Status = "SUCCESS",
RecordsProcessed = count
});
await _context.SaveChangesAsync();
}
}

View File

@ -6,4 +6,8 @@
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.9" />
</ItemGroup>
</Project> </Project>

View File

@ -1,12 +1,13 @@
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BackupPipefy.Domain.Entities namespace BackupPipefy.Domain.Entities
{ {
internal class BackupLog public class BackupLog
{ {
public int LogId { get; set; }
public DateTime ExecutionTime { get; set; } = DateTime.Now;
public string Status { get; set; }
public int RecordsProcessed { get; set; }
public string ErrorMessage { get; set; }
} }
} }

View File

@ -1,12 +1,10 @@
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BackupPipefy.Domain.Entities namespace BackupPipefy.Domain.Entities
{ {
internal class PipefyCard public class PipefyCard
{ {
public long Id { get; set; } // ID do card
public string JsonData { get; set; } // JSON bruto retornado pelo GraphQL
} }
} }

View File

@ -1,12 +1,26 @@
using System; using Microsoft.EntityFrameworkCore;
using System.Collections.Generic; using BackupPipefy.Domain.Entities;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BackupPipefy.Infrastructure.Data namespace BackupPipefy.Infrastructure.Data
{ {
internal class BackupContext public class BackupContext : DbContext
{ {
public DbSet<PipefyCard> PipefyCards { get; set; }
public DbSet<BackupLog> BackupLogs { get; set; }
public DbSet<BackupControl> BackupControls { get; set; }
public BackupContext(DbContextOptions<BackupContext> options) : base(options) { }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<PipefyCard>()
.HasKey(c => c.Id);
modelBuilder.Entity<BackupLog>()
.HasKey(l => l.LogId);
modelBuilder.Entity<BackupControl>()
.HasKey(c => c.Id);
}
} }
} }

View File

@ -0,0 +1,27 @@
namespace BackupPipefy.Infrastructure.Services
{
public class PipefyClient
{
private readonly HttpClient _httpClient;
public PipefyClient(string apiToken)
{
_httpClient = new HttpClient();
_httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiToken}");
}
public async Task<string> GetCardsAsync(int pipeId, DateTime? lastUpdated = null)
{
// Aqui você vai montar sua query GraphQL real
string query = "{ allCards { edges { node { id updated_at } } } }";
var payload = new { query };
var content = new StringContent(System.Text.Json.JsonSerializer.Serialize(payload), System.Text.Encoding.UTF8, "application/json");
var response = await _httpClient.PostAsync("https://api.pipefy.com/graphql", content);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
}
}