Skip to content

Commit

Permalink
add migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
tomyork817 committed Mar 29, 2024
1 parent 1a0fd74 commit a1a712e
Show file tree
Hide file tree
Showing 27 changed files with 873 additions and 29 deletions.
17 changes: 17 additions & 0 deletions Application.Abstractions/Application.Abstractions.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.17" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Domain\Domain.csproj" />
</ItemGroup>

</Project>
13 changes: 13 additions & 0 deletions Application.Abstractions/DataAccess/IDatabaseContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Domain.Applications;
using Microsoft.EntityFrameworkCore;

namespace Application.Abstractions.DataAccess;

public interface IDatabaseContext
{
DbSet<ApplicationActivity> ApplicationActivities { get; set; }
DbSet<UnsubmittedApplication> UnsubmittedApplications { get; set; }
DbSet<SubmittedApplication> SubmittedApplications { get; set; }

Task<int> SaveChangesAsync(CancellationToken cancellationToken = default);
}
2 changes: 2 additions & 0 deletions Domain/Applications/ApplicationActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ namespace Domain.Applications;

public class ApplicationActivity : IEquatable<ApplicationActivity>
{
public ApplicationActivity() { }

public ApplicationActivity(Guid id, string name, string description)
{
Id = id;
Expand Down
7 changes: 6 additions & 1 deletion Domain/Applications/SubmittedApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@ namespace Domain.Applications;

public class SubmittedApplication : IEquatable<SubmittedApplication>
{
public SubmittedApplication() { }

public SubmittedApplication(
Guid id,
Guid userId,
string name,
string? description,
string plan)
string plan,
DateTime createdDateTime)
{
Id = id;
UserId = userId;
Name = name;
Plan = plan;
CreatedDateTime = createdDateTime;
Description = description;
}

Expand All @@ -22,6 +26,7 @@ public SubmittedApplication(
public string Name { get; set; }
public string? Description { get; set; }
public string Plan { get; set; }
public DateTime CreatedDateTime { get; }

public bool Equals(SubmittedApplication? other) => other?.Id.Equals(Id) ?? false;

Expand Down
7 changes: 6 additions & 1 deletion Domain/Applications/UnsubmittedApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,24 @@ namespace Domain.Applications;

public class UnsubmittedApplication : IEquatable<UnsubmittedApplication>
{
public UnsubmittedApplication() { }

public UnsubmittedApplication(
Guid id,
Guid userId,
ApplicationActivity? activity,
string? name,
string? description,
string? plan)
string? plan,
DateTime createdDateTime)
{
Id = id;
UserId = userId;
Activity = activity;
Name = name;
Description = description;
Plan = plan;
CreatedDateTime = createdDateTime;
}

public Guid Id { get; }
Expand All @@ -24,6 +28,7 @@ public UnsubmittedApplication(
public string? Name { get; set; }
public string? Description { get; set; }
public string? Plan { get; set; }
public DateTime CreatedDateTime { get; }

public bool Equals(UnsubmittedApplication? other) => other?.Id.Equals(Id) ?? false;

Expand Down
4 changes: 4 additions & 0 deletions Domain/Domain.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentValidation" Version="11.9.0" />
</ItemGroup>

</Project>
24 changes: 24 additions & 0 deletions Domain/Validators/ApplicationActivityValidator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Domain.Applications;
using FluentValidation;

namespace Domain.Validators;

public class ApplicationActivityValidator : AbstractValidator<ApplicationActivity>
{
private const int MaxNameLength = 100;
private const int MaxDescriptionLength = 300;

public ApplicationActivityValidator()
{
RuleFor(x => x.Id)
.NotEmpty();

RuleFor(x => x.Name)
.NotEmpty()
.MaximumLength(MaxNameLength);

RuleFor(x => x.Description)
.NotEmpty()
.MaximumLength(MaxDescriptionLength);
}
}
25 changes: 0 additions & 25 deletions Domain/Validators/ApplicationValidator.cs

This file was deleted.

34 changes: 34 additions & 0 deletions Domain/Validators/SubmittedApplicationValidator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Domain.Applications;
using FluentValidation;

namespace Domain.Validators;

public class SubmittedApplicationValidator : AbstractValidator<SubmittedApplication>
{
private const int MaxNameLength = 100;
private const int MaxDescriptionLength = 300;
private const int MaxPlanLength = 1000;

public SubmittedApplicationValidator()
{
RuleFor(x => x.Id)
.NotEmpty();

RuleFor(x => x.UserId)
.NotEmpty();

RuleFor(x => x.Name)
.NotEmpty()
.MaximumLength(MaxNameLength);

RuleFor(x => x.Description)
.MaximumLength(MaxDescriptionLength);

RuleFor(x => x.Plan)
.NotEmpty()
.MaximumLength(MaxPlanLength);

RuleFor(x => x.CreatedDateTime)
.NotEmpty();
}
}
36 changes: 36 additions & 0 deletions Domain/Validators/UnsubmittedApplicationValidator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Domain.Applications;
using FluentValidation;

namespace Domain.Validators;

public class UnsubmittedApplicationValidator : AbstractValidator<UnsubmittedApplication>
{
private const int MaxNameLength = 100;
private const int MaxDescriptionLength = 300;
private const int MaxPlanLength = 1000;

public UnsubmittedApplicationValidator()
{
RuleFor(x => x.Id)
.NotEmpty();

RuleFor(x => x.UserId)
.NotEmpty();

RuleFor(x => x.Name)
.MaximumLength(MaxNameLength);

RuleFor(x => x.Description)
.MaximumLength(MaxDescriptionLength);

RuleFor(x => x.Plan)
.MaximumLength(MaxPlanLength);

RuleFor(x => x)
.Must(x => x.Name is not null || x.Description is not null || x.Plan is not null)
.WithMessage("Not all required fields are filled in application to submit");

RuleFor(x => x.CreatedDateTime)
.NotEmpty();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Domain.Applications;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Infrastructure.DataAccess.Configurations;

public class ApplicationActivityConfiguration : IEntityTypeConfiguration<ApplicationActivity>
{
public void Configure(EntityTypeBuilder<ApplicationActivity> builder)
{
builder.HasKey(x => x.Id);

builder.HasIndex(x => x.Name)
.IsUnique();

builder.Property(x => x.Description);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Domain.Applications;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Infrastructure.DataAccess.Configurations;

public class SubmittedApplicationConfiguration : IEntityTypeConfiguration<SubmittedApplication>
{
public void Configure(EntityTypeBuilder<SubmittedApplication> builder)
{
builder.HasKey(x => x.Id);

builder.HasIndex(x => x.UserId);

builder.Property(x => x.Name);
builder.Property(x => x.Description);
builder.Property(x => x.Plan);
builder.Property(x => x.CreatedDateTime);

builder.HasOne(x => x.Activity)
.WithMany();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Domain.Applications;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Infrastructure.DataAccess.Configurations;

public class UnsumbittedApplicationConfiguration : IEntityTypeConfiguration<UnsubmittedApplication>
{
public void Configure(EntityTypeBuilder<UnsubmittedApplication> builder)
{
builder.HasKey(x => x.Id);

builder.HasIndex(x => x.UserId)
.IsUnique();

builder.Property(x => x.Name);
builder.Property(x => x.Description);
builder.Property(x => x.Plan);
builder.Property(x => x.CreatedDateTime);

builder.HasOne(x => x.Activity)
.WithMany();
}
}
22 changes: 22 additions & 0 deletions Infrastructure.DataAccess/DatabaseContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Application.Abstractions.DataAccess;
using Domain.Applications;
using Microsoft.EntityFrameworkCore;

namespace Infrastructure.DataAccess;

public class DatabaseContext : DbContext, IDatabaseContext
{
public DatabaseContext(DbContextOptions<DatabaseContext> options) : base(options)
{
Database.EnsureCreated();
}

public DbSet<ApplicationActivity> ApplicationActivities { get; set; }
public DbSet<UnsubmittedApplication> UnsubmittedApplications { get; set; }
public DbSet<SubmittedApplication> SubmittedApplications { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfigurationsFromAssembly(typeof(IAssemblyMarker).Assembly);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Application.Abstractions.DataAccess;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;

namespace Infrastructure.DataAccess.Extensions;

public static class ServiceCollectionExtensions
{
public static IServiceCollection AddDataAccess(
this IServiceCollection collection,
Action<DbContextOptionsBuilder> configuration)
{
collection.AddDbContext<DatabaseContext>(configuration);
collection.AddScoped<IDatabaseContext>(x => x.GetRequiredService<DatabaseContext>());

return collection;
}
}
3 changes: 3 additions & 0 deletions Infrastructure.DataAccess/IAssemblyMarker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace Infrastructure.DataAccess;

public interface IAssemblyMarker { }
17 changes: 17 additions & 0 deletions Infrastructure.DataAccess/Infrastructure.DataAccess.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,21 @@
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.17" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.17">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.17">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.11" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Application.Abstractions\Application.Abstractions.csproj" />
</ItemGroup>

</Project>
Loading

0 comments on commit a1a712e

Please sign in to comment.