Skip to content

Commit

Permalink
Added base model, client model, entity configuration files.
Browse files Browse the repository at this point in the history
  • Loading branch information
rmaffitsancsoft committed Apr 10, 2024
1 parent 213bd90 commit 38fd8f1
Show file tree
Hide file tree
Showing 8 changed files with 212 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/dotnet/HQ.Server/Data/Configuration/BaseConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore;
using HQ.Server.Data.Models;

namespace HQ.Server.Data.Configuration;

public abstract class BaseConfiguration<T> : IEntityTypeConfiguration<T> where T : Base
{
public virtual void Configure(EntityTypeBuilder<T> builder)
{
builder.HasKey(t => t.Id);
}
}
18 changes: 18 additions & 0 deletions src/dotnet/HQ.Server/Data/Configuration/ClientConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore;
using HQ.Server.Data.Models;

namespace HQ.Server.Data.Configuration;

public class ClientConfiguration : BaseConfiguration<Client>
{
public override void Configure(EntityTypeBuilder<Client> builder)
{
base.Configure(builder);

builder.ToTable("clients");

builder.HasIndex(t => t.Name)
.IsUnique();
}
}
5 changes: 4 additions & 1 deletion src/dotnet/HQ.Server/Data/HQDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using Microsoft.EntityFrameworkCore;
using HQ.Server.Data.Models;
using Microsoft.EntityFrameworkCore;
using System.Reflection;

namespace HQ.Server.Data
{
public class HQDbContext : DbContext
{
public DbSet<Client> Clients { get; set; } = null!;

public HQDbContext(DbContextOptions<HQDbContext> options)
: base(options)
{
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace HQ.Server.Data.Migrations
{
/// <inheritdoc />
public partial class AddedClients : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "clients",
columns: table => new
{
id = table.Column<Guid>(type: "uuid", nullable: false),
name = table.Column<string>(type: "text", nullable: false),
official_name = table.Column<string>(type: "text", nullable: true),
billing_email = table.Column<string>(type: "text", nullable: true),
hourly_rate = table.Column<decimal>(type: "numeric", nullable: true),
created_at = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
updated_at = table.Column<DateTime>(type: "timestamp with time zone", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("pk_clients", x => x.id);
});

migrationBuilder.CreateIndex(
name: "ix_clients_name",
table: "clients",
column: "name",
unique: true);
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "clients");
}
}
}
43 changes: 43 additions & 0 deletions src/dotnet/HQ.Server/Data/Migrations/HQDbContextModelSnapshot.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// <auto-generated />
using System;
using HQ.Server.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
Expand All @@ -20,6 +21,48 @@ protected override void BuildModel(ModelBuilder modelBuilder)
.HasAnnotation("Relational:MaxIdentifierLength", 63);

NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);

modelBuilder.Entity("HQ.Server.Data.Models.Client", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid")
.HasColumnName("id");

b.Property<string>("BillingEmail")
.HasColumnType("text")
.HasColumnName("billing_email");

b.Property<DateTime>("CreatedAt")
.HasColumnType("timestamp with time zone")
.HasColumnName("created_at");

b.Property<decimal?>("HourlyRate")
.HasColumnType("numeric")
.HasColumnName("hourly_rate");

b.Property<string>("Name")
.IsRequired()
.HasColumnType("text")
.HasColumnName("name");

b.Property<string>("OfficialName")
.HasColumnType("text")
.HasColumnName("official_name");

b.Property<DateTime?>("UpdatedAt")
.HasColumnType("timestamp with time zone")
.HasColumnName("updated_at");

b.HasKey("Id")
.HasName("pk_clients");

b.HasIndex("Name")
.IsUnique()
.HasDatabaseName("ix_clients_name");

b.ToTable("clients", (string)null);
});
#pragma warning restore 612, 618
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/dotnet/HQ.Server/Data/Models/Base.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace HQ.Server.Data.Models;

public abstract class Base
{
public Guid Id { get; set; } = Guid.NewGuid();
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
public DateTime? UpdatedAt { get; set; }
}
9 changes: 9 additions & 0 deletions src/dotnet/HQ.Server/Data/Models/Client.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace HQ.Server.Data.Models;

public class Client : Base
{
public string Name { get; set; } = null!;
public string? OfficialName { get; set; }
public string? BillingEmail { get; set; }
public decimal? HourlyRate { get; set; }
}

0 comments on commit 38fd8f1

Please sign in to comment.