From 08819c6738a4f82ccf07ae5ed60835b087f7bb34 Mon Sep 17 00:00:00 2001 From: HombreLaser Date: Sun, 6 Nov 2022 02:10:44 -0600 Subject: Añadidos grupos y usuarios MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Controllers/GroupsController.cs | 24 +++ Controllers/UserAccountsController.cs | 24 +++ LibraryAPI.csproj | 3 + Migrations/20221106035140_AddUser.Designer.cs | 132 +++++++++++++ Migrations/20221106035140_AddUser.cs | 146 ++++++++++++++ Migrations/20221106041635_AddGroup.Designer.cs | 179 +++++++++++++++++ Migrations/20221106041635_AddGroup.cs | 64 +++++++ .../20221106055650_FixGroupModel.Designer.cs | 179 +++++++++++++++++ Migrations/20221106055650_FixGroupModel.cs | 19 ++ .../20221106061015_ConfigureManyToMany.Designer.cs | 213 +++++++++++++++++++++ Migrations/20221106061015_ConfigureManyToMany.cs | 47 +++++ ...221106062230_AddJoinTableReferences.Designer.cs | 193 +++++++++++++++++++ .../20221106062230_AddJoinTableReferences.cs | 47 +++++ .../20221106062917_AddJoinTableId.Designer.cs | 196 +++++++++++++++++++ Migrations/20221106062917_AddJoinTableId.cs | 26 +++ ...221106063041_AddJoinTablePrimaryKey.Designer.cs | 196 +++++++++++++++++++ .../20221106063041_AddJoinTablePrimaryKey.cs | 19 ++ .../20221106063220_DeleteJoinTableId.Designer.cs | 193 +++++++++++++++++++ Migrations/20221106063220_DeleteJoinTableId.cs | 26 +++ .../20221106063641_AddNullableFields.Designer.cs | 193 +++++++++++++++++++ Migrations/20221106063641_AddNullableFields.cs | 19 ++ Migrations/LibraryContextModelSnapshot.cs | 94 ++++++++- Models/Book.cs | 2 +- Models/Group.cs | 12 ++ Models/GroupUserAccount.cs | 10 + Models/LibraryContext.cs | 9 +- Models/UserAccount.cs | 30 +++ Program.cs | 23 +++ Services/LoggerService.cs | 18 +- Validators/NameAttribute.cs | 20 ++ 30 files changed, 2337 insertions(+), 19 deletions(-) create mode 100644 Controllers/GroupsController.cs create mode 100644 Controllers/UserAccountsController.cs create mode 100644 Migrations/20221106035140_AddUser.Designer.cs create mode 100644 Migrations/20221106035140_AddUser.cs create mode 100644 Migrations/20221106041635_AddGroup.Designer.cs create mode 100644 Migrations/20221106041635_AddGroup.cs create mode 100644 Migrations/20221106055650_FixGroupModel.Designer.cs create mode 100644 Migrations/20221106055650_FixGroupModel.cs create mode 100644 Migrations/20221106061015_ConfigureManyToMany.Designer.cs create mode 100644 Migrations/20221106061015_ConfigureManyToMany.cs create mode 100644 Migrations/20221106062230_AddJoinTableReferences.Designer.cs create mode 100644 Migrations/20221106062230_AddJoinTableReferences.cs create mode 100644 Migrations/20221106062917_AddJoinTableId.Designer.cs create mode 100644 Migrations/20221106062917_AddJoinTableId.cs create mode 100644 Migrations/20221106063041_AddJoinTablePrimaryKey.Designer.cs create mode 100644 Migrations/20221106063041_AddJoinTablePrimaryKey.cs create mode 100644 Migrations/20221106063220_DeleteJoinTableId.Designer.cs create mode 100644 Migrations/20221106063220_DeleteJoinTableId.cs create mode 100644 Migrations/20221106063641_AddNullableFields.Designer.cs create mode 100644 Migrations/20221106063641_AddNullableFields.cs create mode 100644 Models/Group.cs create mode 100644 Models/GroupUserAccount.cs create mode 100644 Models/UserAccount.cs create mode 100644 Validators/NameAttribute.cs diff --git a/Controllers/GroupsController.cs b/Controllers/GroupsController.cs new file mode 100644 index 0000000..f81cc56 --- /dev/null +++ b/Controllers/GroupsController.cs @@ -0,0 +1,24 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using LibraryAPI.Models; +using LibraryAPI.Filters; + +namespace LibraryAPI.Controllers { + [Route("api/groups")] + [ApiController] + public class GroupsController : ControllerBase { + private readonly LibraryContext _context; + + public GroupsController(LibraryContext context) { + _context = context; + } + + [HttpPost] + public async Task PostGroup(Group g) { + _context.Add(g); + await _context.SaveChangesAsync(); + + return Ok(); + } + } +} diff --git a/Controllers/UserAccountsController.cs b/Controllers/UserAccountsController.cs new file mode 100644 index 0000000..d93ad31 --- /dev/null +++ b/Controllers/UserAccountsController.cs @@ -0,0 +1,24 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using LibraryAPI.Models; +using LibraryAPI.Filters; + +namespace LibraryAPI.Controllers { + [Route("api/users")] + [ApiController] + public class UserAccountsController : ControllerBase { + private readonly LibraryContext _context; + + public UserAccountsController(LibraryContext context) { + _context = context; + } + + [HttpPost("signup")] + public async Task PostUserAccount(UserAccount user) { + _context.Add(user); + await _context.SaveChangesAsync(); + + return Ok(); + } + } +} diff --git a/LibraryAPI.csproj b/LibraryAPI.csproj index 335e82a..42a0ec5 100644 --- a/LibraryAPI.csproj +++ b/LibraryAPI.csproj @@ -7,6 +7,9 @@ + + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/Migrations/20221106035140_AddUser.Designer.cs b/Migrations/20221106035140_AddUser.Designer.cs new file mode 100644 index 0000000..5144011 --- /dev/null +++ b/Migrations/20221106035140_AddUser.Designer.cs @@ -0,0 +1,132 @@ +// +using System; +using LibraryAPI.Models; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace LibraryAPI.Migrations +{ + [DbContext(typeof(LibraryContext))] + [Migration("20221106035140_AddUser")] + partial class AddUser + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "6.0.9") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("LibraryAPI.Models.Author", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Country") + .IsRequired() + .HasColumnType("text"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("character varying(64)"); + + b.Property("YearOfBirth") + .HasColumnType("integer"); + + b.Property("languages") + .IsRequired() + .HasColumnType("text[]"); + + b.HasKey("Id"); + + b.ToTable("Authors"); + }); + + modelBuilder.Entity("LibraryAPI.Models.Book", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("AuthorId") + .HasColumnType("bigint"); + + b.Property("Edition") + .HasColumnType("integer"); + + b.Property("Genres") + .IsRequired() + .HasColumnType("text[]"); + + b.Property("ISBN") + .IsRequired() + .HasColumnType("text"); + + b.Property("PublicationYear") + .HasColumnType("integer"); + + b.Property("Title") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("character varying(128)"); + + b.HasKey("Id"); + + b.HasIndex("AuthorId"); + + b.ToTable("Books"); + }); + + modelBuilder.Entity("LibraryAPI.Models.UserAccount", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Email") + .IsRequired() + .HasColumnType("text"); + + b.Property("Password") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Users"); + }); + + modelBuilder.Entity("LibraryAPI.Models.Book", b => + { + b.HasOne("LibraryAPI.Models.Author", "Author") + .WithMany("Books") + .HasForeignKey("AuthorId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Author"); + }); + + modelBuilder.Entity("LibraryAPI.Models.Author", b => + { + b.Navigation("Books"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Migrations/20221106035140_AddUser.cs b/Migrations/20221106035140_AddUser.cs new file mode 100644 index 0000000..de69f56 --- /dev/null +++ b/Migrations/20221106035140_AddUser.cs @@ -0,0 +1,146 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace LibraryAPI.Migrations +{ + public partial class AddUser : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AlterColumn( + name: "Title", + table: "Books", + type: "character varying(128)", + maxLength: 128, + nullable: false, + defaultValue: "", + oldClrType: typeof(string), + oldType: "text", + oldNullable: true); + + migrationBuilder.AlterColumn( + name: "ISBN", + table: "Books", + type: "text", + nullable: false, + defaultValue: "", + oldClrType: typeof(string), + oldType: "text", + oldNullable: true); + + migrationBuilder.AlterColumn( + name: "Genres", + table: "Books", + type: "text[]", + nullable: false, + defaultValue: new string[0], + oldClrType: typeof(string[]), + oldType: "text[]", + oldNullable: true); + + migrationBuilder.AlterColumn( + name: "languages", + table: "Authors", + type: "text[]", + nullable: false, + defaultValue: new string[0], + oldClrType: typeof(string[]), + oldType: "text[]", + oldNullable: true); + + migrationBuilder.AlterColumn( + name: "Name", + table: "Authors", + type: "character varying(64)", + maxLength: 64, + nullable: false, + defaultValue: "", + oldClrType: typeof(string), + oldType: "text", + oldNullable: true); + + migrationBuilder.AlterColumn( + name: "Country", + table: "Authors", + type: "text", + nullable: false, + defaultValue: "", + oldClrType: typeof(string), + oldType: "text", + oldNullable: true); + + migrationBuilder.CreateTable( + name: "Users", + columns: table => new + { + Id = table.Column(type: "bigint", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + Email = table.Column(type: "text", nullable: false), + Password = table.Column(type: "text", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Users", x => x.Id); + }); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Users"); + + migrationBuilder.AlterColumn( + name: "Title", + table: "Books", + type: "text", + nullable: true, + oldClrType: typeof(string), + oldType: "character varying(128)", + oldMaxLength: 128); + + migrationBuilder.AlterColumn( + name: "ISBN", + table: "Books", + type: "text", + nullable: true, + oldClrType: typeof(string), + oldType: "text"); + + migrationBuilder.AlterColumn( + name: "Genres", + table: "Books", + type: "text[]", + nullable: true, + oldClrType: typeof(string[]), + oldType: "text[]"); + + migrationBuilder.AlterColumn( + name: "languages", + table: "Authors", + type: "text[]", + nullable: true, + oldClrType: typeof(string[]), + oldType: "text[]"); + + migrationBuilder.AlterColumn( + name: "Name", + table: "Authors", + type: "text", + nullable: true, + oldClrType: typeof(string), + oldType: "character varying(64)", + oldMaxLength: 64); + + migrationBuilder.AlterColumn( + name: "Country", + table: "Authors", + type: "text", + nullable: true, + oldClrType: typeof(string), + oldType: "text"); + } + } +} diff --git a/Migrations/20221106041635_AddGroup.Designer.cs b/Migrations/20221106041635_AddGroup.Designer.cs new file mode 100644 index 0000000..3db1b6d --- /dev/null +++ b/Migrations/20221106041635_AddGroup.Designer.cs @@ -0,0 +1,179 @@ +// +using System; +using LibraryAPI.Models; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace LibraryAPI.Migrations +{ + [DbContext(typeof(LibraryContext))] + [Migration("20221106041635_AddGroup")] + partial class AddGroup + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "6.0.9") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("GroupUserAccount", b => + { + b.Property("GroupsId") + .HasColumnType("bigint"); + + b.Property("UsersId") + .HasColumnType("bigint"); + + b.HasKey("GroupsId", "UsersId"); + + b.HasIndex("UsersId"); + + b.ToTable("GroupUserAccount"); + }); + + modelBuilder.Entity("LibraryAPI.Models.Author", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Country") + .IsRequired() + .HasColumnType("text"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("character varying(64)"); + + b.Property("YearOfBirth") + .HasColumnType("integer"); + + b.Property("languages") + .IsRequired() + .HasColumnType("text[]"); + + b.HasKey("Id"); + + b.ToTable("Authors"); + }); + + modelBuilder.Entity("LibraryAPI.Models.Book", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("AuthorId") + .HasColumnType("bigint"); + + b.Property("Edition") + .HasColumnType("integer"); + + b.Property("Genres") + .IsRequired() + .HasColumnType("text[]"); + + b.Property("ISBN") + .IsRequired() + .HasColumnType("text"); + + b.Property("PublicationYear") + .HasColumnType("integer"); + + b.Property("Title") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("character varying(128)"); + + b.HasKey("Id"); + + b.HasIndex("AuthorId"); + + b.ToTable("Books"); + }); + + modelBuilder.Entity("LibraryAPI.Models.Group", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Groups"); + }); + + modelBuilder.Entity("LibraryAPI.Models.UserAccount", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Email") + .IsRequired() + .HasColumnType("text"); + + b.Property("Password") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Users"); + }); + + modelBuilder.Entity("GroupUserAccount", b => + { + b.HasOne("LibraryAPI.Models.Group", null) + .WithMany() + .HasForeignKey("GroupsId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("LibraryAPI.Models.UserAccount", null) + .WithMany() + .HasForeignKey("UsersId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("LibraryAPI.Models.Book", b => + { + b.HasOne("LibraryAPI.Models.Author", "Author") + .WithMany("Books") + .HasForeignKey("AuthorId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Author"); + }); + + modelBuilder.Entity("LibraryAPI.Models.Author", b => + { + b.Navigation("Books"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Migrations/20221106041635_AddGroup.cs b/Migrations/20221106041635_AddGroup.cs new file mode 100644 index 0000000..23f5fc9 --- /dev/null +++ b/Migrations/20221106041635_AddGroup.cs @@ -0,0 +1,64 @@ +using Microsoft.EntityFrameworkCore.Migrations; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace LibraryAPI.Migrations +{ + public partial class AddGroup : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Groups", + columns: table => new + { + Id = table.Column(type: "bigint", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + Name = table.Column(type: "text", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Groups", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "GroupUserAccount", + columns: table => new + { + GroupsId = table.Column(type: "bigint", nullable: false), + UsersId = table.Column(type: "bigint", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_GroupUserAccount", x => new { x.GroupsId, x.UsersId }); + table.ForeignKey( + name: "FK_GroupUserAccount_Groups_GroupsId", + column: x => x.GroupsId, + principalTable: "Groups", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_GroupUserAccount_Users_UsersId", + column: x => x.UsersId, + principalTable: "Users", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_GroupUserAccount_UsersId", + table: "GroupUserAccount", + column: "UsersId"); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "GroupUserAccount"); + + migrationBuilder.DropTable( + name: "Groups"); + } + } +} diff --git a/Migrations/20221106055650_FixGroupModel.Designer.cs b/Migrations/20221106055650_FixGroupModel.Designer.cs new file mode 100644 index 0000000..a474608 --- /dev/null +++ b/Migrations/20221106055650_FixGroupModel.Designer.cs @@ -0,0 +1,179 @@ +// +using System; +using LibraryAPI.Models; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace LibraryAPI.Migrations +{ + [DbContext(typeof(LibraryContext))] + [Migration("20221106055650_FixGroupModel")] + partial class FixGroupModel + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "6.0.9") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("GroupUserAccount", b => + { + b.Property("GroupsId") + .HasColumnType("bigint"); + + b.Property("UsersId") + .HasColumnType("bigint"); + + b.HasKey("GroupsId", "UsersId"); + + b.HasIndex("UsersId"); + + b.ToTable("GroupUserAccount"); + }); + + modelBuilder.Entity("LibraryAPI.Models.Author", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Country") + .IsRequired() + .HasColumnType("text"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("character varying(64)"); + + b.Property("YearOfBirth") + .HasColumnType("integer"); + + b.Property("languages") + .IsRequired() + .HasColumnType("text[]"); + + b.HasKey("Id"); + + b.ToTable("Authors"); + }); + + modelBuilder.Entity("LibraryAPI.Models.Book", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("AuthorId") + .HasColumnType("bigint"); + + b.Property("Edition") + .HasColumnType("integer"); + + b.Property("Genres") + .IsRequired() + .HasColumnType("text[]"); + + b.Property("ISBN") + .IsRequired() + .HasColumnType("text"); + + b.Property("PublicationYear") + .HasColumnType("integer"); + + b.Property("Title") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("character varying(128)"); + + b.HasKey("Id"); + + b.HasIndex("AuthorId"); + + b.ToTable("Books"); + }); + + modelBuilder.Entity("LibraryAPI.Models.Group", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Groups"); + }); + + modelBuilder.Entity("LibraryAPI.Models.UserAccount", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Email") + .IsRequired() + .HasColumnType("text"); + + b.Property("Password") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Users"); + }); + + modelBuilder.Entity("GroupUserAccount", b => + { + b.HasOne("LibraryAPI.Models.Group", null) + .WithMany() + .HasForeignKey("GroupsId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("LibraryAPI.Models.UserAccount", null) + .WithMany() + .HasForeignKey("UsersId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("LibraryAPI.Models.Book", b => + { + b.HasOne("LibraryAPI.Models.Author", "Author") + .WithMany("Books") + .HasForeignKey("AuthorId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Author"); + }); + + modelBuilder.Entity("LibraryAPI.Models.Author", b => + { + b.Navigation("Books"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Migrations/20221106055650_FixGroupModel.cs b/Migrations/20221106055650_FixGroupModel.cs new file mode 100644 index 0000000..391d6f3 --- /dev/null +++ b/Migrations/20221106055650_FixGroupModel.cs @@ -0,0 +1,19 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace LibraryAPI.Migrations +{ + public partial class FixGroupModel : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + + } + } +} diff --git a/Migrations/20221106061015_ConfigureManyToMany.Designer.cs b/Migrations/20221106061015_ConfigureManyToMany.Designer.cs new file mode 100644 index 0000000..79f0241 --- /dev/null +++ b/Migrations/20221106061015_ConfigureManyToMany.Designer.cs @@ -0,0 +1,213 @@ +// +using System; +using LibraryAPI.Models; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace LibraryAPI.Migrations +{ + [DbContext(typeof(LibraryContext))] + [Migration("20221106061015_ConfigureManyToMany")] + partial class ConfigureManyToMany + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "6.0.9") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("GroupUserAccount", b => + { + b.Property("GroupsId") + .HasColumnType("bigint"); + + b.Property("UsersId") + .HasColumnType("bigint"); + + b.HasKey("GroupsId", "UsersId"); + + b.HasIndex("UsersId"); + + b.ToTable("GroupUserAccount"); + }); + + modelBuilder.Entity("LibraryAPI.Models.Author", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Country") + .IsRequired() + .HasColumnType("text"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("character varying(64)"); + + b.Property("YearOfBirth") + .HasColumnType("integer"); + + b.Property("languages") + .IsRequired() + .HasColumnType("text[]"); + + b.HasKey("Id"); + + b.ToTable("Authors"); + }); + + modelBuilder.Entity("LibraryAPI.Models.Book", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("AuthorId") + .HasColumnType("bigint"); + + b.Property("Edition") + .HasColumnType("integer"); + + b.Property("Genres") + .IsRequired() + .HasColumnType("text[]"); + + b.Property("ISBN") + .IsRequired() + .HasColumnType("text"); + + b.Property("PublicationYear") + .HasColumnType("integer"); + + b.Property("Title") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("character varying(128)"); + + b.HasKey("Id"); + + b.HasIndex("AuthorId"); + + b.ToTable("Books"); + }); + + modelBuilder.Entity("LibraryAPI.Models.Group", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Groups"); + }); + + modelBuilder.Entity("LibraryAPI.Models.GroupUserAccount", b => + { + b.Property("UserAccountId") + .HasColumnType("bigint"); + + b.Property("GroupId") + .HasColumnType("bigint"); + + b.HasKey("UserAccountId", "GroupId"); + + b.HasIndex("GroupId"); + + b.ToTable("GroupUserAccounts"); + }); + + modelBuilder.Entity("LibraryAPI.Models.UserAccount", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Email") + .IsRequired() + .HasColumnType("text"); + + b.Property("Password") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Users"); + }); + + modelBuilder.Entity("GroupUserAccount", b => + { + b.HasOne("LibraryAPI.Models.Group", null) + .WithMany() + .HasForeignKey("GroupsId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("LibraryAPI.Models.UserAccount", null) + .WithMany() + .HasForeignKey("UsersId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("LibraryAPI.Models.Book", b => + { + b.HasOne("LibraryAPI.Models.Author", "Author") + .WithMany("Books") + .HasForeignKey("AuthorId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Author"); + }); + + modelBuilder.Entity("LibraryAPI.Models.GroupUserAccount", b => + { + b.HasOne("LibraryAPI.Models.Group", "Group") + .WithMany() + .HasForeignKey("GroupId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("LibraryAPI.Models.UserAccount", "UserAccount") + .WithMany() + .HasForeignKey("UserAccountId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Group"); + + b.Navigation("UserAccount"); + }); + + modelBuilder.Entity("LibraryAPI.Models.Author", b => + { + b.Navigation("Books"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Migrations/20221106061015_ConfigureManyToMany.cs b/Migrations/20221106061015_ConfigureManyToMany.cs new file mode 100644 index 0000000..9300ac2 --- /dev/null +++ b/Migrations/20221106061015_ConfigureManyToMany.cs @@ -0,0 +1,47 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace LibraryAPI.Migrations +{ + public partial class ConfigureManyToMany : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "GroupUserAccounts", + columns: table => new + { + GroupId = table.Column(type: "bigint", nullable: false), + UserAccountId = table.Column(type: "bigint", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_GroupUserAccounts", x => new { x.UserAccountId, x.GroupId }); + table.ForeignKey( + name: "FK_GroupUserAccounts_Groups_GroupId", + column: x => x.GroupId, + principalTable: "Groups", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_GroupUserAccounts_Users_UserAccountId", + column: x => x.UserAccountId, + principalTable: "Users", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_GroupUserAccounts_GroupId", + table: "GroupUserAccounts", + column: "GroupId"); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "GroupUserAccounts"); + } + } +} diff --git a/Migrations/20221106062230_AddJoinTableReferences.Designer.cs b/Migrations/20221106062230_AddJoinTableReferences.Designer.cs new file mode 100644 index 0000000..0b2d767 --- /dev/null +++ b/Migrations/20221106062230_AddJoinTableReferences.Designer.cs @@ -0,0 +1,193 @@ +// +using System; +using LibraryAPI.Models; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace LibraryAPI.Migrations +{ + [DbContext(typeof(LibraryContext))] + [Migration("20221106062230_AddJoinTableReferences")] + partial class AddJoinTableReferences + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "6.0.9") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("LibraryAPI.Models.Author", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Country") + .IsRequired() + .HasColumnType("text"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("character varying(64)"); + + b.Property("YearOfBirth") + .HasColumnType("integer"); + + b.Property("languages") + .IsRequired() + .HasColumnType("text[]"); + + b.HasKey("Id"); + + b.ToTable("Authors"); + }); + + modelBuilder.Entity("LibraryAPI.Models.Book", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("AuthorId") + .HasColumnType("bigint"); + + b.Property("Edition") + .HasColumnType("integer"); + + b.Property("Genres") + .IsRequired() + .HasColumnType("text[]"); + + b.Property("ISBN") + .IsRequired() + .HasColumnType("text"); + + b.Property("PublicationYear") + .HasColumnType("integer"); + + b.Property("Title") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("character varying(128)"); + + b.HasKey("Id"); + + b.HasIndex("AuthorId"); + + b.ToTable("Books"); + }); + + modelBuilder.Entity("LibraryAPI.Models.Group", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Groups"); + }); + + modelBuilder.Entity("LibraryAPI.Models.GroupUserAccount", b => + { + b.Property("UserAccountId") + .HasColumnType("bigint"); + + b.Property("GroupId") + .HasColumnType("bigint"); + + b.HasKey("UserAccountId", "GroupId"); + + b.HasIndex("GroupId"); + + b.ToTable("GroupUserAccounts"); + }); + + modelBuilder.Entity("LibraryAPI.Models.UserAccount", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Email") + .IsRequired() + .HasColumnType("text"); + + b.Property("Password") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Users"); + }); + + modelBuilder.Entity("LibraryAPI.Models.Book", b => + { + b.HasOne("LibraryAPI.Models.Author", "Author") + .WithMany("Books") + .HasForeignKey("AuthorId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Author"); + }); + + modelBuilder.Entity("LibraryAPI.Models.GroupUserAccount", b => + { + b.HasOne("LibraryAPI.Models.Group", "Group") + .WithMany("Users") + .HasForeignKey("GroupId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("LibraryAPI.Models.UserAccount", "UserAccount") + .WithMany("Groups") + .HasForeignKey("UserAccountId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Group"); + + b.Navigation("UserAccount"); + }); + + modelBuilder.Entity("LibraryAPI.Models.Author", b => + { + b.Navigation("Books"); + }); + + modelBuilder.Entity("LibraryAPI.Models.Group", b => + { + b.Navigation("Users"); + }); + + modelBuilder.Entity("LibraryAPI.Models.UserAccount", b => + { + b.Navigation("Groups"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Migrations/20221106062230_AddJoinTableReferences.cs b/Migrations/20221106062230_AddJoinTableReferences.cs new file mode 100644 index 0000000..d7f1f8e --- /dev/null +++ b/Migrations/20221106062230_AddJoinTableReferences.cs @@ -0,0 +1,47 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace LibraryAPI.Migrations +{ + public partial class AddJoinTableReferences : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "GroupUserAccount"); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "GroupUserAccount", + columns: table => new + { + GroupsId = table.Column(type: "bigint", nullable: false), + UsersId = table.Column(type: "bigint", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_GroupUserAccount", x => new { x.GroupsId, x.UsersId }); + table.ForeignKey( + name: "FK_GroupUserAccount_Groups_GroupsId", + column: x => x.GroupsId, + principalTable: "Groups", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_GroupUserAccount_Users_UsersId", + column: x => x.UsersId, + principalTable: "Users", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_GroupUserAccount_UsersId", + table: "GroupUserAccount", + column: "UsersId"); + } + } +} diff --git a/Migrations/20221106062917_AddJoinTableId.Designer.cs b/Migrations/20221106062917_AddJoinTableId.Designer.cs new file mode 100644 index 0000000..0c6f633 --- /dev/null +++ b/Migrations/20221106062917_AddJoinTableId.Designer.cs @@ -0,0 +1,196 @@ +// +using System; +using LibraryAPI.Models; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace LibraryAPI.Migrations +{ + [DbContext(typeof(LibraryContext))] + [Migration("20221106062917_AddJoinTableId")] + partial class AddJoinTableId + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "6.0.9") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("LibraryAPI.Models.Author", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Country") + .IsRequired() + .HasColumnType("text"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("character varying(64)"); + + b.Property("YearOfBirth") + .HasColumnType("integer"); + + b.Property("languages") + .IsRequired() + .HasColumnType("text[]"); + + b.HasKey("Id"); + + b.ToTable("Authors"); + }); + + modelBuilder.Entity("LibraryAPI.Models.Book", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("AuthorId") + .HasColumnType("bigint"); + + b.Property("Edition") + .HasColumnType("integer"); + + b.Property("Genres") + .IsRequired() + .HasColumnType("text[]"); + + b.Property("ISBN") + .IsRequired() + .HasColumnType("text"); + + b.Property("PublicationYear") + .HasColumnType("integer"); + + b.Property("Title") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("character varying(128)"); + + b.HasKey("Id"); + + b.HasIndex("AuthorId"); + + b.ToTable("Books"); + }); + + modelBuilder.Entity("LibraryAPI.Models.Group", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Groups"); + }); + + modelBuilder.Entity("LibraryAPI.Models.GroupUserAccount", b => + { + b.Property("UserAccountId") + .HasColumnType("bigint"); + + b.Property("GroupId") + .HasColumnType("bigint"); + + b.Property("Id") + .HasColumnType("bigint"); + + b.HasKey("UserAccountId", "GroupId"); + + b.HasIndex("GroupId"); + + b.ToTable("GroupUserAccounts"); + }); + + modelBuilder.Entity("LibraryAPI.Models.UserAccount", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Email") + .IsRequired() + .HasColumnType("text"); + + b.Property("Password") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Users"); + }); + + modelBuilder.Entity("LibraryAPI.Models.Book", b => + { + b.HasOne("LibraryAPI.Models.Author", "Author") + .WithMany("Books") + .HasForeignKey("AuthorId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Author"); + }); + + modelBuilder.Entity("LibraryAPI.Models.GroupUserAccount", b => + { + b.HasOne("LibraryAPI.Models.Group", "Group") + .WithMany("Users") + .HasForeignKey("GroupId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("LibraryAPI.Models.UserAccount", "UserAccount") + .WithMany("Groups") + .HasForeignKey("UserAccountId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Group"); + + b.Navigation("UserAccount"); + }); + + modelBuilder.Entity("LibraryAPI.Models.Author", b => + { + b.Navigation("Books"); + }); + + modelBuilder.Entity("LibraryAPI.Models.Group", b => + { + b.Navigation("Users"); + }); + + modelBuilder.Entity("LibraryAPI.Models.UserAccount", b => + { + b.Navigation("Groups"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Migrations/20221106062917_AddJoinTableId.cs b/Migrations/20221106062917_AddJoinTableId.cs new file mode 100644 index 0000000..b585b43 --- /dev/null +++ b/Migrations/20221106062917_AddJoinTableId.cs @@ -0,0 +1,26 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace LibraryAPI.Migrations +{ + public partial class AddJoinTableId : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AddColumn( + name: "Id", + table: "GroupUserAccounts", + type: "bigint", + nullable: false, + defaultValue: 0L); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropColumn( + name: "Id", + table: "GroupUserAccounts"); + } + } +} diff --git a/Migrations/20221106063041_AddJoinTablePrimaryKey.Designer.cs b/Migrations/20221106063041_AddJoinTablePrimaryKey.Designer.cs new file mode 100644 index 0000000..42930be --- /dev/null +++ b/Migrations/20221106063041_AddJoinTablePrimaryKey.Designer.cs @@ -0,0 +1,196 @@ +// +using System; +using LibraryAPI.Models; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace LibraryAPI.Migrations +{ + [DbContext(typeof(LibraryContext))] + [Migration("20221106063041_AddJoinTablePrimaryKey")] + partial class AddJoinTablePrimaryKey + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "6.0.9") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("LibraryAPI.Models.Author", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Country") + .IsRequired() + .HasColumnType("text"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("character varying(64)"); + + b.Property("YearOfBirth") + .HasColumnType("integer"); + + b.Property("languages") + .IsRequired() + .HasColumnType("text[]"); + + b.HasKey("Id"); + + b.ToTable("Authors"); + }); + + modelBuilder.Entity("LibraryAPI.Models.Book", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("AuthorId") + .HasColumnType("bigint"); + + b.Property("Edition") + .HasColumnType("integer"); + + b.Property("Genres") + .IsRequired() + .HasColumnType("text[]"); + + b.Property("ISBN") + .IsRequired() + .HasColumnType("text"); + + b.Property("PublicationYear") + .HasColumnType("integer"); + + b.Property("Title") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("character varying(128)"); + + b.HasKey("Id"); + + b.HasIndex("AuthorId"); + + b.ToTable("Books"); + }); + + modelBuilder.Entity("LibraryAPI.Models.Group", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Groups"); + }); + + modelBuilder.Entity("LibraryAPI.Models.GroupUserAccount", b => + { + b.Property("UserAccountId") + .HasColumnType("bigint"); + + b.Property("GroupId") + .HasColumnType("bigint"); + + b.Property("Id") + .HasColumnType("bigint"); + + b.HasKey("UserAccountId", "GroupId"); + + b.HasIndex("GroupId"); + + b.ToTable("GroupUserAccounts"); + }); + + modelBuilder.Entity("LibraryAPI.Models.UserAccount", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Email") + .IsRequired() + .HasColumnType("text"); + + b.Property("Password") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Users"); + }); + + modelBuilder.Entity("LibraryAPI.Models.Book", b => + { + b.HasOne("LibraryAPI.Models.Author", "Author") + .WithMany("Books") + .HasForeignKey("AuthorId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Author"); + }); + + modelBuilder.Entity("LibraryAPI.Models.GroupUserAccount", b => + { + b.HasOne("LibraryAPI.Models.Group", "Group") + .WithMany("Users") + .HasForeignKey("GroupId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("LibraryAPI.Models.UserAccount", "UserAccount") + .WithMany("Groups") + .HasForeignKey("UserAccountId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Group"); + + b.Navigation("UserAccount"); + }); + + modelBuilder.Entity("LibraryAPI.Models.Author", b => + { + b.Navigation("Books"); + }); + + modelBuilder.Entity("LibraryAPI.Models.Group", b => + { + b.Navigation("Users"); + }); + + modelBuilder.Entity("LibraryAPI.Models.UserAccount", b => + { + b.Navigation("Groups"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Migrations/20221106063041_AddJoinTablePrimaryKey.cs b/Migrations/20221106063041_AddJoinTablePrimaryKey.cs new file mode 100644 index 0000000..ea5f3d7 --- /dev/null +++ b/Migrations/20221106063041_AddJoinTablePrimaryKey.cs @@ -0,0 +1,19 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace LibraryAPI.Migrations +{ + public partial class AddJoinTablePrimaryKey : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + + } + } +} diff --git a/Migrations/20221106063220_DeleteJoinTableId.Designer.cs b/Migrations/20221106063220_DeleteJoinTableId.Designer.cs new file mode 100644 index 0000000..7ca0cb0 --- /dev/null +++ b/Migrations/20221106063220_DeleteJoinTableId.Designer.cs @@ -0,0 +1,193 @@ +// +using System; +using LibraryAPI.Models; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace LibraryAPI.Migrations +{ + [DbContext(typeof(LibraryContext))] + [Migration("20221106063220_DeleteJoinTableId")] + partial class DeleteJoinTableId + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "6.0.9") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("LibraryAPI.Models.Author", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Country") + .IsRequired() + .HasColumnType("text"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("character varying(64)"); + + b.Property("YearOfBirth") + .HasColumnType("integer"); + + b.Property("languages") + .IsRequired() + .HasColumnType("text[]"); + + b.HasKey("Id"); + + b.ToTable("Authors"); + }); + + modelBuilder.Entity("LibraryAPI.Models.Book", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("AuthorId") + .HasColumnType("bigint"); + + b.Property("Edition") + .HasColumnType("integer"); + + b.Property("Genres") + .IsRequired() + .HasColumnType("text[]"); + + b.Property("ISBN") + .IsRequired() + .HasColumnType("text"); + + b.Property("PublicationYear") + .HasColumnType("integer"); + + b.Property("Title") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("character varying(128)"); + + b.HasKey("Id"); + + b.HasIndex("AuthorId"); + + b.ToTable("Books"); + }); + + modelBuilder.Entity("LibraryAPI.Models.Group", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Groups"); + }); + + modelBuilder.Entity("LibraryAPI.Models.GroupUserAccount", b => + { + b.Property("UserAccountId") + .HasColumnType("bigint"); + + b.Property("GroupId") + .HasColumnType("bigint"); + + b.HasKey("UserAccountId", "GroupId"); + + b.HasIndex("GroupId"); + + b.ToTable("GroupUserAccounts"); + }); + + modelBuilder.Entity("LibraryAPI.Models.UserAccount", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Email") + .IsRequired() + .HasColumnType("text"); + + b.Property("Password") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Users"); + }); + + modelBuilder.Entity("LibraryAPI.Models.Book", b => + { + b.HasOne("LibraryAPI.Models.Author", "Author") + .WithMany("Books") + .HasForeignKey("AuthorId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Author"); + }); + + modelBuilder.Entity("LibraryAPI.Models.GroupUserAccount", b => + { + b.HasOne("LibraryAPI.Models.Group", "Group") + .WithMany("Users") + .HasForeignKey("GroupId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("LibraryAPI.Models.UserAccount", "UserAccount") + .WithMany("Groups") + .HasForeignKey("UserAccountId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Group"); + + b.Navigation("UserAccount"); + }); + + modelBuilder.Entity("LibraryAPI.Models.Author", b => + { + b.Navigation("Books"); + }); + + modelBuilder.Entity("LibraryAPI.Models.Group", b => + { + b.Navigation("Users"); + }); + + modelBuilder.Entity("LibraryAPI.Models.UserAccount", b => + { + b.Navigation("Groups"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Migrations/20221106063220_DeleteJoinTableId.cs b/Migrations/20221106063220_DeleteJoinTableId.cs new file mode 100644 index 0000000..20cea64 --- /dev/null +++ b/Migrations/20221106063220_DeleteJoinTableId.cs @@ -0,0 +1,26 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace LibraryAPI.Migrations +{ + public partial class DeleteJoinTableId : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropColumn( + name: "Id", + table: "GroupUserAccounts"); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.AddColumn( + name: "Id", + table: "GroupUserAccounts", + type: "bigint", + nullable: false, + defaultValue: 0L); + } + } +} diff --git a/Migrations/20221106063641_AddNullableFields.Designer.cs b/Migrations/20221106063641_AddNullableFields.Designer.cs new file mode 100644 index 0000000..c09fe61 --- /dev/null +++ b/Migrations/20221106063641_AddNullableFields.Designer.cs @@ -0,0 +1,193 @@ +// +using System; +using LibraryAPI.Models; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace LibraryAPI.Migrations +{ + [DbContext(typeof(LibraryContext))] + [Migration("20221106063641_AddNullableFields")] + partial class AddNullableFields + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "6.0.9") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("LibraryAPI.Models.Author", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Country") + .IsRequired() + .HasColumnType("text"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(64) + .HasColumnType("character varying(64)"); + + b.Property("YearOfBirth") + .HasColumnType("integer"); + + b.Property("languages") + .IsRequired() + .HasColumnType("text[]"); + + b.HasKey("Id"); + + b.ToTable("Authors"); + }); + + modelBuilder.Entity("LibraryAPI.Models.Book", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("AuthorId") + .HasColumnType("bigint"); + + b.Property("Edition") + .HasColumnType("integer"); + + b.Property("Genres") + .IsRequired() + .HasColumnType("text[]"); + + b.Property("ISBN") + .IsRequired() + .HasColumnType("text"); + + b.Property("PublicationYear") + .HasColumnType("integer"); + + b.Property("Title") + .IsRequired() + .HasMaxLength(128) + .HasColumnType("character varying(128)"); + + b.HasKey("Id"); + + b.HasIndex("AuthorId"); + + b.ToTable("Books"); + }); + + modelBuilder.Entity("LibraryAPI.Models.Group", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Groups"); + }); + + modelBuilder.Entity("LibraryAPI.Models.GroupUserAccount", b => + { + b.Property("UserAccountId") + .HasColumnType("bigint"); + + b.Property("GroupId") + .HasColumnType("bigint"); + + b.HasKey("UserAccountId", "GroupId"); + + b.HasIndex("GroupId"); + + b.ToTable("GroupUserAccounts"); + }); + + modelBuilder.Entity("LibraryAPI.Models.UserAccount", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Email") + .IsRequired() + .HasColumnType("text"); + + b.Property("Password") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Users"); + }); + + modelBuilder.Entity("LibraryAPI.Models.Book", b => + { + b.HasOne("LibraryAPI.Models.Author", "Author") + .WithMany("Books") + .HasForeignKey("AuthorId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Author"); + }); + + modelBuilder.Entity("LibraryAPI.Models.GroupUserAccount", b => + { + b.HasOne("LibraryAPI.Models.Group", "Group") + .WithMany("Users") + .HasForeignKey("GroupId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("LibraryAPI.Models.UserAccount", "UserAccount") + .WithMany("Groups") + .HasForeignKey("UserAccountId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Group"); + + b.Navigation("UserAccount"); + }); + + modelBuilder.Entity("LibraryAPI.Models.Author", b => + { + b.Navigation("Books"); + }); + + modelBuilder.Entity("LibraryAPI.Models.Group", b => + { + b.Navigation("Users"); + }); + + modelBuilder.Entity("LibraryAPI.Models.UserAccount", b => + { + b.Navigation("Groups"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Migrations/20221106063641_AddNullableFields.cs b/Migrations/20221106063641_AddNullableFields.cs new file mode 100644 index 0000000..a860b2f --- /dev/null +++ b/Migrations/20221106063641_AddNullableFields.cs @@ -0,0 +1,19 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace LibraryAPI.Migrations +{ + public partial class AddNullableFields : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + + } + } +} diff --git a/Migrations/LibraryContextModelSnapshot.cs b/Migrations/LibraryContextModelSnapshot.cs index 0fa3e9b..a523712 100644 --- a/Migrations/LibraryContextModelSnapshot.cs +++ b/Migrations/LibraryContextModelSnapshot.cs @@ -31,15 +31,19 @@ namespace LibraryAPI.Migrations NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); b.Property("Country") + .IsRequired() .HasColumnType("text"); b.Property("Name") - .HasColumnType("text"); + .IsRequired() + .HasMaxLength(64) + .HasColumnType("character varying(64)"); b.Property("YearOfBirth") .HasColumnType("integer"); b.Property("languages") + .IsRequired() .HasColumnType("text[]"); b.HasKey("Id"); @@ -62,16 +66,20 @@ namespace LibraryAPI.Migrations .HasColumnType("integer"); b.Property("Genres") + .IsRequired() .HasColumnType("text[]"); b.Property("ISBN") + .IsRequired() .HasColumnType("text"); b.Property("PublicationYear") .HasColumnType("integer"); b.Property("Title") - .HasColumnType("text"); + .IsRequired() + .HasMaxLength(128) + .HasColumnType("character varying(128)"); b.HasKey("Id"); @@ -80,6 +88,59 @@ namespace LibraryAPI.Migrations b.ToTable("Books"); }); + modelBuilder.Entity("LibraryAPI.Models.Group", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Groups"); + }); + + modelBuilder.Entity("LibraryAPI.Models.GroupUserAccount", b => + { + b.Property("UserAccountId") + .HasColumnType("bigint"); + + b.Property("GroupId") + .HasColumnType("bigint"); + + b.HasKey("UserAccountId", "GroupId"); + + b.HasIndex("GroupId"); + + b.ToTable("GroupUserAccounts"); + }); + + modelBuilder.Entity("LibraryAPI.Models.UserAccount", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("bigint"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Email") + .IsRequired() + .HasColumnType("text"); + + b.Property("Password") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Users"); + }); + modelBuilder.Entity("LibraryAPI.Models.Book", b => { b.HasOne("LibraryAPI.Models.Author", "Author") @@ -91,10 +152,39 @@ namespace LibraryAPI.Migrations b.Navigation("Author"); }); + modelBuilder.Entity("LibraryAPI.Models.GroupUserAccount", b => + { + b.HasOne("LibraryAPI.Models.Group", "Group") + .WithMany("Users") + .HasForeignKey("GroupId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("LibraryAPI.Models.UserAccount", "UserAccount") + .WithMany("Groups") + .HasForeignKey("UserAccountId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Group"); + + b.Navigation("UserAccount"); + }); + modelBuilder.Entity("LibraryAPI.Models.Author", b => { b.Navigation("Books"); }); + + modelBuilder.Entity("LibraryAPI.Models.Group", b => + { + b.Navigation("Users"); + }); + + modelBuilder.Entity("LibraryAPI.Models.UserAccount", b => + { + b.Navigation("Groups"); + }); #pragma warning restore 612, 618 } } diff --git a/Models/Book.cs b/Models/Book.cs index 49bd527..4a05865 100644 --- a/Models/Book.cs +++ b/Models/Book.cs @@ -19,4 +19,4 @@ namespace LibraryAPI.Models { [StringLength(128, ErrorMessage = "Title too long")] public string? Title { get; set; } } -} \ No newline at end of file +} diff --git a/Models/Group.cs b/Models/Group.cs new file mode 100644 index 0000000..f5ed59d --- /dev/null +++ b/Models/Group.cs @@ -0,0 +1,12 @@ +using System.ComponentModel.DataAnnotations; +using LibraryAPI.Validators; + +namespace LibraryAPI.Models { + public class Group { + public long Id { get; set; } + public ICollection? Users { get; set; } + [Required] + [Name] + public string? Name {get; set; } + } +} diff --git a/Models/GroupUserAccount.cs b/Models/GroupUserAccount.cs new file mode 100644 index 0000000..706c2e9 --- /dev/null +++ b/Models/GroupUserAccount.cs @@ -0,0 +1,10 @@ +using System.ComponentModel.DataAnnotations; + +namespace LibraryAPI.Models { + public class GroupUserAccount { + public long GroupId { get; set; } + public Group? Group { get; set; } + public long UserAccountId { get; set; } + public UserAccount? UserAccount { get; set; } + } +} diff --git a/Models/LibraryContext.cs b/Models/LibraryContext.cs index b07ba54..3f56b0c 100644 --- a/Models/LibraryContext.cs +++ b/Models/LibraryContext.cs @@ -4,6 +4,13 @@ namespace LibraryAPI.Models { public class LibraryContext : DbContext { public DbSet? Authors { get; set; } public DbSet? Books { get; set; } + public DbSet? Users { get; set; } + public DbSet? Groups { get; set; } + public DbSet? GroupUserAccounts { get; set; } public LibraryContext(DbContextOptions options) : base(options) {} + + protected override void OnModelCreating(ModelBuilder modelBuilder) { + modelBuilder.Entity().HasKey(sc => new { sc.UserAccountId, sc.GroupId }); + } } -} \ No newline at end of file +} diff --git a/Models/UserAccount.cs b/Models/UserAccount.cs new file mode 100644 index 0000000..e3184ed --- /dev/null +++ b/Models/UserAccount.cs @@ -0,0 +1,30 @@ +using System.ComponentModel.DataAnnotations; +using Microsoft.AspNetCore.Identity; + +namespace LibraryAPI.Models { + public class UserAccount { + public long Id { get; set; } + [Required] + public string? Email { get; set; } + private string? _password; + [Required] + public string? Password { get { return _password; } set { _password = HashPassword(value); } } + public ICollection? Groups { get; set; } + private PasswordHasher _hasher; + + public UserAccount() { + _hasher = new PasswordHasher(); + } + + public PasswordVerificationResult VerifyPassword(string to_verify) { + return _hasher.VerifyHashedPassword(this, Password, to_verify); + } + + private string? HashPassword(string? unhashed_password) { + if(unhashed_password != null) + return _hasher.HashPassword(this, unhashed_password); + + return null; + } + } +} diff --git a/Program.cs b/Program.cs index d717af7..2678a94 100644 --- a/Program.cs +++ b/Program.cs @@ -1,4 +1,10 @@ using Microsoft.EntityFrameworkCore; +using Microsoft.AspNetCore.Authentication.JwtBearer; +using Microsoft.AspNetCore.Identity; +using Microsoft.IdentityModel.Tokens; +using Microsoft.OpenApi.Models; +using System.IdentityModel.Tokens.Jwt; +using System.Text; using LibraryAPI.Models; using LibraryAPI.Filters; using LibraryAPI.Services; @@ -11,6 +17,21 @@ builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); builder.Services.AddHostedService(); builder.Services.AddScoped(); +// JWT Config. +builder.Services.AddAuthentication(options => { + options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; + options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; + options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme; +}).AddJwtBearer(options => + options.TokenValidationParameters = new TokenValidationParameters { + ValidateIssuer = false, + ValidateAudience = false, + ValidateLifetime = true, + ValidateIssuerSigningKey = true, + IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["keyjwt"])), + ClockSkew = TimeSpan.Zero + }); +builder.Services.AddAuthorization(); var app = builder.Build(); @@ -22,6 +43,8 @@ if (app.Environment.IsDevelopment()) app.UseHttpsRedirection(); +app.UseAuthentication(); + app.UseAuthorization(); app.MapControllers(); diff --git a/Services/LoggerService.cs b/Services/LoggerService.cs index dd78e23..64eecb1 100644 --- a/Services/LoggerService.cs +++ b/Services/LoggerService.cs @@ -1,15 +1,11 @@ -using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Hosting; +// Singleton service. namespace LibraryAPI.Services { - public class LoggerService : IHostedService { - private readonly IWebHostEnvironment env; - private readonly string log_file = "Log.txt"; + public class LoggerService : BaseService, IHostedService { private Timer timer; - public LoggerService(IWebHostEnvironment env) { - this.env = env; - } + public LoggerService(IWebHostEnvironment env) : base(env) { } public Task StartAsync(CancellationToken cancel_token) { timer = new Timer(DoWork, null, TimeSpan.Zero, TimeSpan.FromMinutes(5)); @@ -25,13 +21,5 @@ namespace LibraryAPI.Services { timer.Dispose(); return Task.CompletedTask; } - - private void LogToFile(string message) { - var path = $@"{env.ContentRootPath}/wwwroot/{log_file}"; - - using (StreamWriter w = new StreamWriter(path, append: true)) { - w.WriteLine(message); - } - } } } diff --git a/Validators/NameAttribute.cs b/Validators/NameAttribute.cs new file mode 100644 index 0000000..2f1a5ae --- /dev/null +++ b/Validators/NameAttribute.cs @@ -0,0 +1,20 @@ +using System.ComponentModel.DataAnnotations; +using LibraryAPI.Models; + +namespace LibraryAPI.Validators { + public class NameAttribute : ValidationAttribute { + public string[] valid_names = { "regular", "librarian" }; + public string GetErrorMessage() { + return "Group name must be \"regular\" or \"librarian\""; + } + + protected override ValidationResult? IsValid(object? value, ValidationContext validationContext) { + var group = (Group) validationContext.ObjectInstance; + + if(Array.Exists(valid_names, s => s == group.Name)) + return ValidationResult.Success; + + return new ValidationResult(GetErrorMessage()); + } + } +} -- cgit v1.2.3