add: ef core
This commit is contained in:
25
Infraestructure/Data/WorkshopDBContext.cs
Normal file
25
Infraestructure/Data/WorkshopDBContext.cs
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using CampusWorkshops.Api.Models;
|
||||||
|
|
||||||
|
namespace CampusWorkshops.Api.Infrastructure.Data;
|
||||||
|
|
||||||
|
public class WorkshopsDbContext : DbContext
|
||||||
|
{
|
||||||
|
public WorkshopsDbContext(DbContextOptions<WorkshopsDbContext> options) : base(options) {}
|
||||||
|
|
||||||
|
public DbSet<Workshop> Workshops => Set<Workshop>();
|
||||||
|
|
||||||
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||||
|
{
|
||||||
|
modelBuilder.Entity<Workshop>(e =>
|
||||||
|
{
|
||||||
|
e.ToTable("Workshops");
|
||||||
|
e.HasKey(w => w.Id);
|
||||||
|
e.Property(w => w.Title).IsRequired().HasMaxLength(120);
|
||||||
|
e.Property(w => w.Description).HasMaxLength(2000);
|
||||||
|
e.Property(w => w.Location).HasMaxLength(200);
|
||||||
|
e.Property(w => w.Capacity).HasDefaultValue(1);
|
||||||
|
e.HasIndex(w => w.StartAt);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
65
Migrations/20250903175616_InitialCreate.Designer.cs
generated
Normal file
65
Migrations/20250903175616_InitialCreate.Designer.cs
generated
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
// <auto-generated />
|
||||||
|
using System;
|
||||||
|
using CampusWorkshops.Api.Infrastructure.Data;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace Workshop10_API.Migrations
|
||||||
|
{
|
||||||
|
[DbContext(typeof(WorkshopsDbContext))]
|
||||||
|
[Migration("20250903175616_InitialCreate")]
|
||||||
|
partial class InitialCreate
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||||
|
{
|
||||||
|
#pragma warning disable 612, 618
|
||||||
|
modelBuilder.HasAnnotation("ProductVersion", "9.0.8");
|
||||||
|
|
||||||
|
modelBuilder.Entity("CampusWorkshops.Api.Models.Workshop", b =>
|
||||||
|
{
|
||||||
|
b.Property<Guid>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("TEXT");
|
||||||
|
|
||||||
|
b.Property<int>("Capacity")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("INTEGER")
|
||||||
|
.HasDefaultValue(1);
|
||||||
|
|
||||||
|
b.Property<string>("Description")
|
||||||
|
.HasMaxLength(2000)
|
||||||
|
.HasColumnType("TEXT");
|
||||||
|
|
||||||
|
b.Property<DateTimeOffset>("EndAt")
|
||||||
|
.HasColumnType("TEXT");
|
||||||
|
|
||||||
|
b.Property<bool>("IsOnline")
|
||||||
|
.HasColumnType("INTEGER");
|
||||||
|
|
||||||
|
b.Property<string>("Location")
|
||||||
|
.HasMaxLength(200)
|
||||||
|
.HasColumnType("TEXT");
|
||||||
|
|
||||||
|
b.Property<DateTimeOffset>("StartAt")
|
||||||
|
.HasColumnType("TEXT");
|
||||||
|
|
||||||
|
b.Property<string>("Title")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(120)
|
||||||
|
.HasColumnType("TEXT");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.HasIndex("StartAt");
|
||||||
|
|
||||||
|
b.ToTable("Workshops", (string)null);
|
||||||
|
});
|
||||||
|
#pragma warning restore 612, 618
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
45
Migrations/20250903175616_InitialCreate.cs
Normal file
45
Migrations/20250903175616_InitialCreate.cs
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
using System;
|
||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace Workshop10_API.Migrations
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
public partial class InitialCreate : Migration
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void Up(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.CreateTable(
|
||||||
|
name: "Workshops",
|
||||||
|
columns: table => new
|
||||||
|
{
|
||||||
|
Id = table.Column<Guid>(type: "TEXT", nullable: false),
|
||||||
|
Title = table.Column<string>(type: "TEXT", maxLength: 120, nullable: false),
|
||||||
|
Description = table.Column<string>(type: "TEXT", maxLength: 2000, nullable: true),
|
||||||
|
StartAt = table.Column<DateTimeOffset>(type: "TEXT", nullable: false),
|
||||||
|
EndAt = table.Column<DateTimeOffset>(type: "TEXT", nullable: false),
|
||||||
|
Location = table.Column<string>(type: "TEXT", maxLength: 200, nullable: true),
|
||||||
|
Capacity = table.Column<int>(type: "INTEGER", nullable: false, defaultValue: 1),
|
||||||
|
IsOnline = table.Column<bool>(type: "INTEGER", nullable: false)
|
||||||
|
},
|
||||||
|
constraints: table =>
|
||||||
|
{
|
||||||
|
table.PrimaryKey("PK_Workshops", x => x.Id);
|
||||||
|
});
|
||||||
|
|
||||||
|
migrationBuilder.CreateIndex(
|
||||||
|
name: "IX_Workshops_StartAt",
|
||||||
|
table: "Workshops",
|
||||||
|
column: "StartAt");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void Down(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.DropTable(
|
||||||
|
name: "Workshops");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
62
Migrations/WorkshopsDbContextModelSnapshot.cs
Normal file
62
Migrations/WorkshopsDbContextModelSnapshot.cs
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
// <auto-generated />
|
||||||
|
using System;
|
||||||
|
using CampusWorkshops.Api.Infrastructure.Data;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||||
|
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace Workshop10_API.Migrations
|
||||||
|
{
|
||||||
|
[DbContext(typeof(WorkshopsDbContext))]
|
||||||
|
partial class WorkshopsDbContextModelSnapshot : ModelSnapshot
|
||||||
|
{
|
||||||
|
protected override void BuildModel(ModelBuilder modelBuilder)
|
||||||
|
{
|
||||||
|
#pragma warning disable 612, 618
|
||||||
|
modelBuilder.HasAnnotation("ProductVersion", "9.0.8");
|
||||||
|
|
||||||
|
modelBuilder.Entity("CampusWorkshops.Api.Models.Workshop", b =>
|
||||||
|
{
|
||||||
|
b.Property<Guid>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("TEXT");
|
||||||
|
|
||||||
|
b.Property<int>("Capacity")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("INTEGER")
|
||||||
|
.HasDefaultValue(1);
|
||||||
|
|
||||||
|
b.Property<string>("Description")
|
||||||
|
.HasMaxLength(2000)
|
||||||
|
.HasColumnType("TEXT");
|
||||||
|
|
||||||
|
b.Property<DateTimeOffset>("EndAt")
|
||||||
|
.HasColumnType("TEXT");
|
||||||
|
|
||||||
|
b.Property<bool>("IsOnline")
|
||||||
|
.HasColumnType("INTEGER");
|
||||||
|
|
||||||
|
b.Property<string>("Location")
|
||||||
|
.HasMaxLength(200)
|
||||||
|
.HasColumnType("TEXT");
|
||||||
|
|
||||||
|
b.Property<DateTimeOffset>("StartAt")
|
||||||
|
.HasColumnType("TEXT");
|
||||||
|
|
||||||
|
b.Property<string>("Title")
|
||||||
|
.IsRequired()
|
||||||
|
.HasMaxLength(120)
|
||||||
|
.HasColumnType("TEXT");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.HasIndex("StartAt");
|
||||||
|
|
||||||
|
b.ToTable("Workshops", (string)null);
|
||||||
|
});
|
||||||
|
#pragma warning restore 612, 618
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,11 +2,16 @@
|
|||||||
using Microsoft.AspNetCore.Diagnostics;
|
using Microsoft.AspNetCore.Diagnostics;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using CampusWorkshops.Api.Repositories;
|
using CampusWorkshops.Api.Repositories;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using CampusWorkshops.Api.Infrastructure.Data;
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
// Add services
|
// Add services
|
||||||
builder.Services.AddSingleton<IWorkshopRepository, InMemoryWorkshopRepository>();
|
builder.Services.AddDbContext<WorkshopsDbContext>(options =>
|
||||||
|
options.UseSqlite(builder.Configuration.GetConnectionString("WorkshopsDb")));
|
||||||
|
|
||||||
|
builder.Services.AddScoped<IWorkshopRepository, EfWorkshopRepository>();
|
||||||
builder.Services.AddControllers();
|
builder.Services.AddControllers();
|
||||||
builder.Services.AddEndpointsApiExplorer();
|
builder.Services.AddEndpointsApiExplorer();
|
||||||
builder.Services.AddSwaggerGen(o =>
|
builder.Services.AddSwaggerGen(o =>
|
||||||
|
|||||||
71
Repositories/EfWorkshopRepository.cs
Normal file
71
Repositories/EfWorkshopRepository.cs
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
using CampusWorkshops.Api.Models;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using CampusWorkshops.Api.Infrastructure.Data;
|
||||||
|
|
||||||
|
namespace CampusWorkshops.Api.Repositories;
|
||||||
|
|
||||||
|
public class EfWorkshopRepository : IWorkshopRepository
|
||||||
|
{
|
||||||
|
private readonly WorkshopsDbContext _db;
|
||||||
|
public EfWorkshopRepository(WorkshopsDbContext db) => _db = db;
|
||||||
|
|
||||||
|
public async Task<IReadOnlyList<Workshop>> GetAllAsync(DateTimeOffset? from, DateTimeOffset? to, string? q, CancellationToken ct)
|
||||||
|
{
|
||||||
|
var query = _db.Workshops.AsNoTracking().AsQueryable();
|
||||||
|
if (from.HasValue)
|
||||||
|
{
|
||||||
|
query = query.Where(w => w.StartAt >= from.Value);
|
||||||
|
}
|
||||||
|
if (to.HasValue)
|
||||||
|
{
|
||||||
|
query = query.Where(w => w.EndAt >= to.Value);
|
||||||
|
}
|
||||||
|
if (!string.IsNullOrWhiteSpace(q))
|
||||||
|
{
|
||||||
|
var termo = q.ToLowerInvariant();
|
||||||
|
query = query.Where(w => w.Title.Contains(termo)|| w.Description.Contains(termo));
|
||||||
|
}
|
||||||
|
Console.WriteLine(query.ToQueryString());
|
||||||
|
return await query.ToListAsync(ct);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<Workshop?> GetByIdAsync(Guid id, CancellationToken ct)
|
||||||
|
=> await _db.Workshops.AsNoTracking().FirstOrDefaultAsync(w => w.Id == id, ct);
|
||||||
|
|
||||||
|
public async Task<Workshop> AddAsync(Workshop workshop, CancellationToken ct)
|
||||||
|
{
|
||||||
|
_db.Workshops.Add(workshop);
|
||||||
|
await _db.SaveChangesAsync();
|
||||||
|
return workshop;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<Workshop?> UpdateAsync(Workshop workshop, CancellationToken ct)
|
||||||
|
{
|
||||||
|
_db.Workshops.Update(workshop);
|
||||||
|
await _db.SaveChangesAsync(ct);
|
||||||
|
return workshop;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<bool> DeleteAsync(Guid id, CancellationToken ct)
|
||||||
|
{
|
||||||
|
var encontrado = await _db.Workshops.FindAsync([id]);
|
||||||
|
if (encontrado == null)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
_db.Workshops.Remove(encontrado);
|
||||||
|
await _db.SaveChangesAsync();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task<bool> ExistsAsync(Guid id, CancellationToken ct)
|
||||||
|
=> _db.Workshops.AnyAsync(w => w.Id == id, ct);
|
||||||
|
|
||||||
|
public async Task<Workshop?> UpdatePartialAsync(Guid id, Action<Workshop> updateAction, CancellationToken ct)
|
||||||
|
{
|
||||||
|
var w = await _db.Workshops.FirstOrDefaultAsync(x => x.Id == id, ct);
|
||||||
|
// TODO: implementar o UpdatePartial
|
||||||
|
return w;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,6 +9,16 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.2" />
|
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.2" />
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.8" />
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.8">
|
||||||
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
</PackageReference>
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.8" />
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="9.0.8">
|
||||||
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
</PackageReference>
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="9.0.3" />
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="9.0.3" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,7 @@
|
|||||||
{
|
{
|
||||||
|
"ConnectionStrings": {
|
||||||
|
"WorkshopsDb": "Data Source=workshops.db"
|
||||||
|
},
|
||||||
"Logging": {
|
"Logging": {
|
||||||
"LogLevel": {
|
"LogLevel": {
|
||||||
"Default": "Information",
|
"Default": "Information",
|
||||||
|
|||||||
BIN
workshops.db
Normal file
BIN
workshops.db
Normal file
Binary file not shown.
Reference in New Issue
Block a user