diff options
| author | Paweł Bernaciak <pawelbernaciak@zohomail.eu> | 2023-10-21 11:55:33 +0200 |
|---|---|---|
| committer | Paweł Bernaciak <pawelbernaciak@zohomail.eu> | 2023-10-21 11:55:33 +0200 |
| commit | 12fef7cbaf2073f9cc349ed765ea140be0259d8e (patch) | |
| tree | 83412d050139bcdbd11eedffa61e1d0c9862dbed /backend/Elements.Data | |
| parent | 17785848eb53f8d0420f6b6b3be00a0d10be4e1e (diff) | |
Basic API auth and Google login
Diffstat (limited to 'backend/Elements.Data')
8 files changed, 270 insertions, 2 deletions
diff --git a/backend/Elements.Data/ApplicationContextFactory.cs b/backend/Elements.Data/ApplicationContextFactory.cs new file mode 100644 index 0000000..b8e4ebd --- /dev/null +++ b/backend/Elements.Data/ApplicationContextFactory.cs @@ -0,0 +1,15 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Design; + +namespace Elements.Data; + +public class ApplicationContextFactory : IDesignTimeDbContextFactory<ApplicationDbContext> +{ + public ApplicationDbContext CreateDbContext(string[] args) + { + DbContextOptionsBuilder<ApplicationDbContext> optionsBuilder = new(); + optionsBuilder.UseSqlite("Data Source=elements.db"); + + return new ApplicationDbContext(optionsBuilder.Options); + } +}
\ No newline at end of file diff --git a/backend/Elements.Data/ApplicationDbContext.cs b/backend/Elements.Data/ApplicationDbContext.cs index 619719a..a912cc4 100644 --- a/backend/Elements.Data/ApplicationDbContext.cs +++ b/backend/Elements.Data/ApplicationDbContext.cs @@ -1,7 +1,14 @@ -using Microsoft.EntityFrameworkCore;
+using Elements.Data.Models;
+using Microsoft.EntityFrameworkCore;
namespace Elements.Data;
public class ApplicationDbContext : DbContext
{
+ public DbSet<Element> Elements { get; set; }
+ public DbSet<User> Users { get; set; }
+ public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
+ {
+
+ }
}
diff --git a/backend/Elements.Data/Elements.Data.csproj b/backend/Elements.Data/Elements.Data.csproj index 408fea2..2fb1f63 100644 --- a/backend/Elements.Data/Elements.Data.csproj +++ b/backend/Elements.Data/Elements.Data.csproj @@ -7,6 +7,11 @@ </PropertyGroup>
<ItemGroup> + <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.12"> + <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> + <PrivateAssets>all</PrivateAssets> + </PackageReference> + <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.12" /> <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.12" /> </ItemGroup>
diff --git a/backend/Elements.Data/Migrations/20231020142907_Initial.Designer.cs b/backend/Elements.Data/Migrations/20231020142907_Initial.Designer.cs new file mode 100644 index 0000000..1272d62 --- /dev/null +++ b/backend/Elements.Data/Migrations/20231020142907_Initial.Designer.cs @@ -0,0 +1,80 @@ +// <auto-generated /> +using Elements.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace Elements.Data.Migrations +{ + [DbContext(typeof(ApplicationDbContext))] + [Migration("20231020142907_Initial")] + partial class Initial + { + /// <inheritdoc /> + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "7.0.12"); + + modelBuilder.Entity("Elements.Data.Models.Element", b => + { + b.Property<int>("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property<string>("Name") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property<int>("State") + .HasColumnType("INTEGER"); + + b.Property<int>("UserId") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("Elements"); + }); + + modelBuilder.Entity("Elements.Data.Models.User", b => + { + b.Property<int>("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property<string>("GoogleId") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property<string>("Name") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("Users"); + }); + + modelBuilder.Entity("Elements.Data.Models.Element", b => + { + b.HasOne("Elements.Data.Models.User", null) + .WithMany("Elements") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Elements.Data.Models.User", b => + { + b.Navigation("Elements"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/backend/Elements.Data/Migrations/20231020142907_Initial.cs b/backend/Elements.Data/Migrations/20231020142907_Initial.cs new file mode 100644 index 0000000..8257e5b --- /dev/null +++ b/backend/Elements.Data/Migrations/20231020142907_Initial.cs @@ -0,0 +1,64 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace Elements.Data.Migrations +{ + /// <inheritdoc /> + public partial class Initial : Migration + { + /// <inheritdoc /> + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Users", + columns: table => new + { + Id = table.Column<int>(type: "INTEGER", nullable: false) + .Annotation("Sqlite:Autoincrement", true), + GoogleId = table.Column<string>(type: "TEXT", nullable: false), + Name = table.Column<string>(type: "TEXT", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Users", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Elements", + columns: table => new + { + Id = table.Column<int>(type: "INTEGER", nullable: false) + .Annotation("Sqlite:Autoincrement", true), + UserId = table.Column<int>(type: "INTEGER", nullable: false), + Name = table.Column<string>(type: "TEXT", nullable: false), + State = table.Column<int>(type: "INTEGER", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Elements", x => x.Id); + table.ForeignKey( + name: "FK_Elements_Users_UserId", + column: x => x.UserId, + principalTable: "Users", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_Elements_UserId", + table: "Elements", + column: "UserId"); + } + + /// <inheritdoc /> + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Elements"); + + migrationBuilder.DropTable( + name: "Users"); + } + } +} diff --git a/backend/Elements.Data/Migrations/ApplicationDbContextModelSnapshot.cs b/backend/Elements.Data/Migrations/ApplicationDbContextModelSnapshot.cs new file mode 100644 index 0000000..b713511 --- /dev/null +++ b/backend/Elements.Data/Migrations/ApplicationDbContextModelSnapshot.cs @@ -0,0 +1,77 @@ +// <auto-generated /> +using Elements.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace Elements.Data.Migrations +{ + [DbContext(typeof(ApplicationDbContext))] + partial class ApplicationDbContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "7.0.12"); + + modelBuilder.Entity("Elements.Data.Models.Element", b => + { + b.Property<int>("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property<string>("Name") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property<int>("State") + .HasColumnType("INTEGER"); + + b.Property<int>("UserId") + .HasColumnType("INTEGER"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("Elements"); + }); + + modelBuilder.Entity("Elements.Data.Models.User", b => + { + b.Property<int>("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property<string>("GoogleId") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property<string>("Name") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("Users"); + }); + + modelBuilder.Entity("Elements.Data.Models.Element", b => + { + b.HasOne("Elements.Data.Models.User", null) + .WithMany("Elements") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Elements.Data.Models.User", b => + { + b.Navigation("Elements"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/backend/Elements.Data/Models/Element.cs b/backend/Elements.Data/Models/Element.cs index 2f602e5..08f25a8 100644 --- a/backend/Elements.Data/Models/Element.cs +++ b/backend/Elements.Data/Models/Element.cs @@ -1,5 +1,16 @@ +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + namespace Elements.Data.Models; +public enum ElementState { + HasColor, + HasIcon +} + public class Element { - + public int Id {get; init;} + public required int UserId { get; init; } + public required string Name {get; init;} + public required ElementState State {get; init;} }
\ No newline at end of file diff --git a/backend/Elements.Data/Models/User.cs b/backend/Elements.Data/Models/User.cs new file mode 100644 index 0000000..b44a1e2 --- /dev/null +++ b/backend/Elements.Data/Models/User.cs @@ -0,0 +1,9 @@ +namespace Elements.Data.Models; + +public class User +{ + public int Id { get; init; } + public required string GoogleId { get; init; } + public required string Name { get; set; } + public required ICollection<Element> Elements { get; set; } +}
\ No newline at end of file |
