new migration

This commit is contained in:
2025-10-13 14:22:43 +02:00
parent e359084293
commit 08b862318d
9 changed files with 460 additions and 2 deletions

View File

@@ -0,0 +1,10 @@
namespace ApiEfCoreLibrary.DTO.Login.Request;
public class UpdateLoginDto
{
public int Id { get; set; }
public string? Username { get; set; }
public string? FullName { get; set; }
public string? Password { get; set; }
public string? Salt { get; set; }
}

View File

@@ -0,0 +1,37 @@
using ApiEfCoreLibrary.DTO.Author.Request;
using ApiEfCoreLibrary.DTO.Author.Response;
namespace ApiEfCoreLibrary.Endpoints.Author;
using FastEndpoints;
public class CreateLoginEndpoint(LibraryDbContext database) : Endpoint<CreateAuthorDto, GetAuthorDto>
{
public override void Configure()
{
Post("/api/authors");
AllowAnonymous();
}
public override async Task HandleAsync(CreateAuthorDto req, CancellationToken ct)
{
var author = new Models.Author()
{
Name = req.Name,
FirstName = req.FirstName
};
database.Authors.Add(author);
await database.SaveChangesAsync(ct);
// Pour renvoyer une erreur : Send.StringAsync("Le message d'erreur", 400);
GetAuthorDto responseDto = new()
{
Id = author.Id,
Name = author.Name,
FirstName = author.FirstName
};
await Send.OkAsync(responseDto, ct);
}
}

View File

@@ -0,0 +1,36 @@
using ApiEfCoreLibrary.DTO.Author.Request;
using ApiEfCoreLibrary.DTO.Author.Response;
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
namespace ApiEfCoreLibrary.Endpoints.Author;
public class DeleteAuthorRequest
{
public int Id { get; set; }
}
public class DeleteLoginEndpoint(LibraryDbContext database) : Endpoint<DeleteAuthorRequest>
{
public override void Configure()
{
Delete("/api/authors/{@Id}", x => new {x.Id});
AllowAnonymous();
}
public override async Task HandleAsync(DeleteAuthorRequest req, CancellationToken ct)
{
var author = await database.Authors.SingleOrDefaultAsync(x => x.Id == req.Id, ct);
if (author == null)
{
await Send.NotFoundAsync(ct);
return;
}
database.Authors.Remove(author);
await database.SaveChangesAsync(ct);
await Send.NoContentAsync(ct);
}
}

View File

@@ -0,0 +1,40 @@
using ApiEfCoreLibrary.DTO.Author.Response;
using ApiEfCoreLibrary.DTO.Book.Response;
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
namespace ApiEfCoreLibrary.Endpoints.Author;
public class GetAllLoginEndpoint(LibraryDbContext database) : EndpointWithoutRequest<List<GetAuthorDto>>
{
public override void Configure()
{
Get("/api/authors");
AllowAnonymous();
}
public override async Task HandleAsync(CancellationToken ct)
{
var authors = await database.Authors
.Include(x => x.Books)
.Select(author => new GetAuthorDto()
{
Id = author.Id,
Name = author.Name,
FirstName = author.FirstName,
Books = author.Books.Select(book => new GetBookDto
{
Id = book.Id,
Title = book.Title,
AuthorId = book.AuthorId,
BookAuthorName = book.Author.Name,
BookAuthorFirstName = book.Author.FirstName,
ReleaseYear = book.ReleaseYear,
Isbn = book.Isbn
}).ToList()
})
.ToListAsync(ct);
await Send.OkAsync(authors, ct);
}
}

View File

@@ -0,0 +1,53 @@
using ApiEfCoreLibrary.DTO.Author.Request;
using ApiEfCoreLibrary.DTO.Author.Response;
using ApiEfCoreLibrary.DTO.Book.Response;
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
namespace ApiEfCoreLibrary.Endpoints.Author;
public class GetAuthorRequest
{
public int Id { get; set; }
}
public class GetLoginEndpoint(LibraryDbContext database) : Endpoint<GetAuthorRequest, GetAuthorDto>
{
public override void Configure()
{
Get("/api/authors/{@Id}", x => new {x.Id});
AllowAnonymous();
}
public override async Task HandleAsync(GetAuthorRequest req, CancellationToken ct)
{
var author = await database.Authors
.Include(x => x.Books)
.SingleOrDefaultAsync(x => x.Id == req.Id, ct);
if (author == null)
{
await Send.NotFoundAsync(ct);
return;
}
GetAuthorDto responseDto = new()
{
Id = author.Id,
Name = author.Name,
FirstName = author.FirstName,
Books = author.Books.Select(book => new GetBookDto
{
Id = book.Id,
Title = book.Title,
AuthorId = book.AuthorId,
BookAuthorName = book.Author.Name,
BookAuthorFirstName = book.Author.FirstName,
ReleaseYear = book.ReleaseYear,
Isbn = book.Isbn
}).ToList()
};
await Send.OkAsync(responseDto, ct);
}
}

View File

@@ -0,0 +1,40 @@
using ApiEfCoreLibrary.DTO.Author.Request;
using ApiEfCoreLibrary.DTO.Author.Response;
using ApiEfCoreLibrary.DTO.Book.Response;
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
namespace ApiEfCoreLibrary.Endpoints.Author;
public class UpdateLoginEndpoint(LibraryDbContext database) : Endpoint<UpdateAuthorDto, GetAuthorDto>
{
public override void Configure()
{
Put("/api/authors/{@Id}", x => new {x.Id});
AllowAnonymous();
}
public override async Task HandleAsync(UpdateAuthorDto req, CancellationToken ct)
{
var author = await database.Authors.SingleOrDefaultAsync(x => x.Id == req.Id, ct);
if (author == null)
{
await Send.NotFoundAsync(ct);
return;
}
author.Name = req.Name;
author.FirstName = req.FirstName;
await database.SaveChangesAsync(ct);
GetAuthorDto responseDto = new()
{
Id = author.Id,
Name = author.Name,
FirstName = author.FirstName,
};
await Send.OkAsync(responseDto, ct);
}
}

View File

@@ -0,0 +1,220 @@
// <auto-generated />
using System;
using ApiEfCoreLibrary;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace ApiEfCoreLibrary.Migrations
{
[DbContext(typeof(LibraryDbContext))]
[Migration("20251013121137_EditingDatabase")]
partial class EditingDatabase
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "8.0.20")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
modelBuilder.Entity("ApiEfCoreLibrary.Models.Author", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("FirstName")
.IsRequired()
.HasMaxLength(100)
.HasColumnType("nvarchar(100)");
b.Property<string>("Name")
.IsRequired()
.HasMaxLength(100)
.HasColumnType("nvarchar(100)");
b.HasKey("Id");
b.ToTable("Authors");
});
modelBuilder.Entity("ApiEfCoreLibrary.Models.Book", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("AuthorId")
.HasColumnType("int");
b.Property<string>("Isbn")
.IsRequired()
.HasMaxLength(20)
.HasColumnType("nvarchar(20)");
b.Property<int?>("ReleaseYear")
.HasColumnType("int");
b.Property<string>("Title")
.IsRequired()
.HasMaxLength(255)
.HasColumnType("nvarchar(255)");
b.HasKey("Id");
b.HasIndex("AuthorId");
b.ToTable("Books");
});
modelBuilder.Entity("ApiEfCoreLibrary.Models.Loan", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("BookId")
.HasColumnType("int");
b.Property<DateOnly>("Date")
.HasColumnType("date");
b.Property<DateOnly?>("EffectiveReturningDate")
.HasColumnType("date");
b.Property<DateOnly>("PlannedReturningDate")
.HasColumnType("date");
b.Property<int>("UserId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("BookId");
b.HasIndex("UserId");
b.ToTable("Loans");
});
modelBuilder.Entity("ApiEfCoreLibrary.Models.Login", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("FullName")
.IsRequired()
.HasMaxLength(200)
.HasColumnType("nvarchar(200)");
b.Property<string>("Password")
.IsRequired()
.HasMaxLength(255)
.HasColumnType("nvarchar(255)");
b.Property<string>("Salt")
.IsRequired()
.HasMaxLength(24)
.HasColumnType("nvarchar(24)");
b.Property<string>("Username")
.IsRequired()
.HasMaxLength(100)
.HasColumnType("nvarchar(100)");
b.HasKey("Id");
b.ToTable("Logins");
});
modelBuilder.Entity("ApiEfCoreLibrary.Models.User", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<DateOnly?>("BirthDate")
.HasColumnType("date");
b.Property<string>("Email")
.IsRequired()
.HasMaxLength(255)
.HasColumnType("nvarchar(255)");
b.Property<string>("FirstName")
.IsRequired()
.HasMaxLength(100)
.HasColumnType("nvarchar(100)");
b.Property<string>("Name")
.IsRequired()
.HasMaxLength(100)
.HasColumnType("nvarchar(100)");
b.HasKey("Id");
b.ToTable("Users");
});
modelBuilder.Entity("ApiEfCoreLibrary.Models.Book", b =>
{
b.HasOne("ApiEfCoreLibrary.Models.Author", "Author")
.WithMany("Books")
.HasForeignKey("AuthorId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Author");
});
modelBuilder.Entity("ApiEfCoreLibrary.Models.Loan", b =>
{
b.HasOne("ApiEfCoreLibrary.Models.Book", "Book")
.WithMany()
.HasForeignKey("BookId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("ApiEfCoreLibrary.Models.User", "User")
.WithMany("Loans")
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Book");
b.Navigation("User");
});
modelBuilder.Entity("ApiEfCoreLibrary.Models.Author", b =>
{
b.Navigation("Books");
});
modelBuilder.Entity("ApiEfCoreLibrary.Models.User", b =>
{
b.Navigation("Loans");
});
#pragma warning restore 612, 618
}
}
}

View File

@@ -0,0 +1,22 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace ApiEfCoreLibrary.Migrations
{
/// <inheritdoc />
public partial class EditingDatabase : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
}
}
}

View File

@@ -6,6 +6,6 @@ public class Login
[Key] public int Id { get; set; }
[Required, MaxLength(100)] public string? Username { get; set; }
[Required, MaxLength(200)] public string? FullName { get; set; }
[Required, MaxLength(255)] public string? Password { get; set; }
[Required, MinLength(24), MaxLength(24)] public string? Salt { get; set; }
[Required, Length(60, 60)] public string? Password { get; set; }
[Required, Length(24, 24)] public string? Salt { get; set; }
}