Skip to content

Commit

Permalink
Add more DB handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
CorruptComputer committed Jul 26, 2024
1 parent e4757a1 commit 953e66f
Show file tree
Hide file tree
Showing 51 changed files with 881 additions and 100 deletions.
2 changes: 1 addition & 1 deletion Bones.Api/Features/Identity/ChangeUserEmail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public async Task<CommandResponse> Handle(Command request, CancellationToken can
};
}

CommandResponse emailChanged = await sender.Send(new ChangeUserEmailDb.Command(request.UserId, request.Email));
CommandResponse emailChanged = await sender.Send(new ChangeUserEmailDb.Command(request.UserId, request.Email), cancellationToken);

if (!emailChanged.Success)
{
Expand Down
4 changes: 2 additions & 2 deletions Bones.Api/Models/ApiQueryResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public sealed record ApiQueryResponse<TResult>
/// <returns>The newly translated TResult?</returns>
public static implicit operator TResult?(ApiQueryResponse<TResult> response)
{
if (response.Success == false || response.Result == null)
if (!response.Success || Equals(response.Result, default(TResult)))
{
return default;
}
Expand All @@ -47,7 +47,7 @@ public sealed record ApiQueryResponse<TResult>
/// <returns>The newly translated QueryResponse&lt;TResult&gt;</returns>
public static implicit operator ApiQueryResponse<TResult>(TResult? result)
{
if (result == null)
if (Equals(result, default(TResult)))
{
return new()
{
Expand Down
26 changes: 13 additions & 13 deletions Bones.Database/BonesDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)

if (string.IsNullOrWhiteSpace(connectionString))
{
throw new UnrecoverableException("Missing appsettings configuration for connection string: BonesDb");
throw new BonesException("Missing appsettings configuration for connection string: BonesDb");
}

optionsBuilder.UseNpgsql(connectionString);
Expand All @@ -87,18 +87,18 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
/// <summary>
/// Create models
/// </summary>
/// <param name="modelBuilder"></param>
protected override void OnModelCreating(ModelBuilder modelBuilder)
/// <param name="builder"></param>
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(modelBuilder);

// Identity
modelBuilder.Entity<BonesUser>().ToTable("BonesUsers", "Identity");
modelBuilder.Entity<BonesUserRole>().ToTable("BonesUserRoles", "Identity");
modelBuilder.Entity<BonesUserLogin>().ToTable("BonesUserLogins", "Identity");
modelBuilder.Entity<BonesUserClaim>().ToTable("BonesUserClaims", "Identity");
modelBuilder.Entity<BonesUserToken>().ToTable("BonesUserTokens", "Identity");
modelBuilder.Entity<BonesRole>().ToTable("BonesRoles", "Identity");
modelBuilder.Entity<BonesRoleClaim>().ToTable("BonesRoleClaims", "Identity");
base.OnModelCreating(builder);

const string identitySchema = "Identity";
builder.Entity<BonesUser>().ToTable("BonesUsers", identitySchema);
builder.Entity<BonesUserRole>().ToTable("BonesUserRoles", identitySchema);
builder.Entity<BonesUserLogin>().ToTable("BonesUserLogins", identitySchema);
builder.Entity<BonesUserClaim>().ToTable("BonesUserClaims", identitySchema);
builder.Entity<BonesUserToken>().ToTable("BonesUserTokens", identitySchema);
builder.Entity<BonesRole>().ToTable("BonesRoles", identitySchema);
builder.Entity<BonesRoleClaim>().ToTable("BonesRoleClaims", identitySchema);
}
}
4 changes: 1 addition & 3 deletions Bones.Database/DbSets/Identity/BonesRole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@ namespace Bones.Database.DbSets.Identity;
/// Model for the Identity.BonesRoles table.
/// </summary>
[Table("BonesRoles", Schema = "Identity")]
public class BonesRole : IdentityRole<Guid>
{
}
public class BonesRole : IdentityRole<Guid>;
4 changes: 1 addition & 3 deletions Bones.Database/DbSets/Identity/BonesRoleClaim.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@ namespace Bones.Database.DbSets.Identity;
/// Model for the Identity.BonesRoleClaims table.
/// </summary>
[Table("BonesRoleClaims", Schema = "Identity")]
public class BonesRoleClaim : IdentityRoleClaim<Guid>
{
}
public class BonesRoleClaim : IdentityRoleClaim<Guid>;
4 changes: 1 addition & 3 deletions Bones.Database/DbSets/Identity/BonesUserClaim.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@ namespace Bones.Database.DbSets.Identity;
/// Model for the Identity.BonesUserClaims table.
/// </summary>
[Table("BonesUserClaims", Schema = "Identity")]
public class BonesUserClaim : IdentityUserClaim<Guid>
{
}
public class BonesUserClaim : IdentityUserClaim<Guid>;
5 changes: 1 addition & 4 deletions Bones.Database/DbSets/Identity/BonesUserLogin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,4 @@ namespace Bones.Database.DbSets.Identity;
/// Model for the Identity.BonesUserLogins table.
/// </summary>
[Table("BonesUserLogins", Schema = "Identity")]
public class BonesUserLogin : IdentityUserLogin<Guid>
{

}
public class BonesUserLogin : IdentityUserLogin<Guid>;
4 changes: 1 addition & 3 deletions Bones.Database/DbSets/Identity/BonesUserRole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@ namespace Bones.Database.DbSets.Identity;
/// Model for the Identity.BonesUserRoles table.
/// </summary>
[Table("BonesUserRoles", Schema = "Identity")]
public class BonesUserRole : IdentityUserRole<Guid>
{
}
public class BonesUserRole : IdentityUserRole<Guid>;
4 changes: 1 addition & 3 deletions Bones.Database/DbSets/Identity/BonesUserToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@ namespace Bones.Database.DbSets.Identity;
/// Model for the Identity.BonesUserTokens table.
/// </summary>
[Table("BonesUserTokens", Schema = "Identity")]
public class BonesUserToken : IdentityUserToken<Guid>
{
}
public class BonesUserToken : IdentityUserToken<Guid>;
5 changes: 5 additions & 0 deletions Bones.Database/DbSets/ProjectManagement/Items/Item.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,9 @@ public class Item
public List<Tag> Tags { get; set; } = [];

public List<ItemVersion> Versions { get; set; } = [];

/// <summary>
/// Disables viewing this item, and when safe to do so it will be removed.
/// </summary>
public bool DeleteFlag { get; set; } = false;
}
5 changes: 5 additions & 0 deletions Bones.Database/DbSets/ProjectManagement/Items/ItemValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ public class ItemValue

public string? Value { get; private set; }

/// <summary>
/// Disables viewing this item value, and when safe to do so it will be removed.
/// </summary>
public bool DeleteFlag { get; set; } = false;

public bool TrySetValue<T>(T value)
{
string? valueStr = value?.ToString();
Expand Down
5 changes: 5 additions & 0 deletions Bones.Database/DbSets/ProjectManagement/Items/ItemVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@ public class ItemVersion
public required Item Item { get; set; }

public required List<ItemValue> Values { get; set; }

/// <summary>
/// Disables viewing this item version, and when safe to do so it will be removed.
/// </summary>
public bool DeleteFlag { get; set; } = false;
}
8 changes: 7 additions & 1 deletion Bones.Database/DbSets/ProjectManagement/Layouts/Layout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,16 @@ public class Layout
public Guid Id { get; init; }

[MaxLength(512)]
public required string Name { get; init; }
public required string Name { get; set; }

/// <summary>
/// The versions for this layout
/// </summary>
public List<LayoutVersion> Versions { get; set; } = [];

/// <summary>
/// Disables creating of new items using this layout,
/// and when all items using it are deleted it will be removed.
/// </summary>
public bool DeleteFlag { get; set; } = false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,10 @@ public class LayoutVersion
public required long Version { get; init; }

public List<ItemField> Fields { get; init; } = [];

/// <summary>
/// Disables creating of new items using this layout version,
/// and when all items using it are deleted it will be removed.
/// </summary>
public bool DeleteFlag { get; set; } = false;
}
6 changes: 6 additions & 0 deletions Bones.Database/DbSets/ProjectManagement/Projects/Project.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,10 @@ public class Project
public required string Name { get; set; }

public List<Initiative> Initiatives { get; set; } = [];

/// <summary>
/// Disables access to this Project and schedules deletes for everything within,
/// when all items using it are deleted it will be removed.
/// </summary>
public bool DeleteFlag { get; set; } = false;
}
5 changes: 5 additions & 0 deletions Bones.Database/DbSets/ProjectManagement/Queues/Queue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@ public class Queue
public required string Name { get; set; }

public List<Item> Items { get; set; } = [];

/// <summary>
/// Disables viewing this queue, and when safe to do so it will be removed.
/// </summary>
public bool DeleteFlag { get; set; } = false;
}
2 changes: 1 addition & 1 deletion Bones.Database/Operations/Identity/ChangeUserEmailDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Bones.Database.Operations.Identity;

public class ChangeUserEmailDb(BonesDbContext dbContext, ISender sender) : IRequestHandler<ChangeUserEmailDb.Command, CommandResponse>
public sealed class ChangeUserEmailDb(BonesDbContext dbContext, ISender sender) : IRequestHandler<ChangeUserEmailDb.Command, CommandResponse>
{
/// <summary>
/// DB Command for updating the email address on a user.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Bones.Database.Operations.Identity;

public class ClearEmailVerificationsForUserDb(BonesDbContext dbContext) : IRequestHandler<ClearEmailVerificationsForUserDb.Command, CommandResponse>
public sealed class ClearEmailVerificationsForUserDb(BonesDbContext dbContext) : IRequestHandler<ClearEmailVerificationsForUserDb.Command, CommandResponse>
{
/// <summary>
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Bones.Database.Operations.Identity;

public class ClearExpiredEmailVerificationsDb(BonesDbContext dbContext) : IRequestHandler<ClearExpiredEmailVerificationsDb.Query, QueryResponse<int>>
public sealed class ClearExpiredEmailVerificationsDb(BonesDbContext dbContext) : IRequestHandler<ClearExpiredEmailVerificationsDb.Query, QueryResponse<int>>
{
/// <summary>
/// DB Query for clearing the expired email verifications, returns the number removed.
Expand Down
10 changes: 7 additions & 3 deletions Bones.Database/Operations/Identity/CreateEmailVerificationDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Bones.Database.Operations.Identity;

public class CreateEmailVerificationDb(BonesDbContext dbContext) : IRequestHandler<CreateEmailVerificationDb.Command, CommandResponse>
public sealed class CreateEmailVerificationDb(BonesDbContext dbContext) : IRequestHandler<CreateEmailVerificationDb.Command, CommandResponse>
{
/// <summary>
/// DB Command for generating an email verification token and queuing an email to be sent.
Expand All @@ -26,11 +26,15 @@ public bool IsRequestValid()

public async Task<CommandResponse> Handle(Command request, CancellationToken cancellationToken)
{
BonesUser? user = dbContext.Users.FirstOrDefault(u => u.Id == request.UserId);
BonesUser? user = await dbContext.Users.FirstOrDefaultAsync(u => u.Id == request.UserId, cancellationToken);

if (user == null)
{
throw new UnrecoverableException($"Unable to find user by ID: {request.UserId}");
return new()
{
Success = false,
FailureReason = $"Unable to find user by ID: {request.UserId}"
};
}

EntityEntry<UserEmailVerification> created = await dbContext.UserEmailVerifications.AddAsync(new()
Expand Down
2 changes: 1 addition & 1 deletion Bones.Database/Operations/Identity/CreateUserDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Bones.Database.Operations.Identity;

public class CreateUserDb(BonesDbContext dbContext, ISender sender) : IRequestHandler<CreateUserDb.Command, CommandResponse>
public sealed class CreateUserDb(BonesDbContext dbContext, ISender sender) : IRequestHandler<CreateUserDb.Command, CommandResponse>
{
/// <summary>
/// DB Command for creating a user.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Bones.Database.Operations.Identity;

public class EmailAvailableForUseDb(BonesDbContext dbContext) : IRequestHandler<EmailAvailableForUseDb.Command, CommandResponse>
public sealed class EmailAvailableForUseDb(BonesDbContext dbContext) : IRequestHandler<EmailAvailableForUseDb.Command, CommandResponse>
{
/// <summary>
/// DB Command for checking if an email is available to use.
Expand Down
2 changes: 1 addition & 1 deletion Bones.Database/Operations/Identity/GetUserByIdDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Bones.Database.Operations.Identity;

public class GetUserByIdDb(BonesDbContext dbContext) : IRequestHandler<GetUserByIdDb.Query, QueryResponse<BonesUser>>
public sealed class GetUserByIdDb(BonesDbContext dbContext) : IRequestHandler<GetUserByIdDb.Query, QueryResponse<BonesUser>>
{
/// <summary>
/// DB Query to get a user by UserId
Expand Down
10 changes: 4 additions & 6 deletions Bones.Database/Operations/Identity/VerifyUserEmailDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Bones.Database.Operations.Identity;

public class VerifyUserEmailDb(BonesDbContext dbContext) : IRequestHandler<VerifyUserEmailDb.Command, CommandResponse>
public sealed class VerifyUserEmailDb(BonesDbContext dbContext) : IRequestHandler<VerifyUserEmailDb.Command, CommandResponse>
{
/// <summary>
/// DB Command for verifying an email address for a user.
Expand Down Expand Up @@ -35,20 +35,18 @@ public async Task<CommandResponse> Handle(Command request, CancellationToken can
&& verification.Token == request.Token
&& verification.ValidUntilDateTime > DateTimeOffset.UtcNow);

if (!validMatches.Any())
if (!await validMatches.AnyAsync(cancellationToken))
{
return new()
{
Success = false,
FailureReason = "Supplied information is invalid or the verification time has expired."
FailureReason = "Supplied information is invalid or the token has expired."
};
}

await validMatches.ExecuteDeleteAsync(cancellationToken);

BonesUser? acct = await dbContext.Users.FirstOrDefaultAsync(user => user.Id == request.UserId,
cancellationToken);

BonesUser? acct = await dbContext.Users.FirstOrDefaultAsync(user => user.Id == request.UserId, cancellationToken);
if (acct == null)
{
return new()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Bones.Database.Operations.ProjectManagement.Initiatives;

public class CreateInitiativeDb(BonesDbContext dbContext) : IRequestHandler<CreateInitiativeDb.Command, CommandResponse>
public sealed class CreateInitiativeDb(BonesDbContext dbContext) : IRequestHandler<CreateInitiativeDb.Command, CommandResponse>
{
/// <summary>
/// DB Command for creating an Initiative.
Expand All @@ -30,8 +30,6 @@ public bool IsRequestValid()
}
}



/// <inheritdoc />
public async Task<CommandResponse> Handle(Command request, CancellationToken cancellationToken)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,47 @@
using Bones.Database.DbSets.ProjectManagement.Initiatives;

namespace Bones.Database.Operations.ProjectManagement.Initiatives;

public class DeleteInitiativeDb
public sealed class DeleteInitiativeDb(BonesDbContext dbContext) : IRequestHandler<DeleteInitiativeDb.Command, CommandResponse>
{
/// <summary>
/// DB Command for deleting an Initiative.
/// </summary>
/// <param name="InitiativeId">Internal ID of the initiative</param>
public record Command(Guid InitiativeId) : IValidatableRequest<CommandResponse>
{
/// <inheritdoc />
public bool IsRequestValid()
{
if (InitiativeId == Guid.Empty)
{
return false;
}

return true;
}
}

/// <inheritdoc />
public async Task<CommandResponse> Handle(Command request, CancellationToken cancellationToken)
{
// TODO: fix this

Check warning on line 28 in Bones.Database/Operations/ProjectManagement/Initiatives/DeleteInitiativeDb.cs

View workflow job for this annotation

GitHub Actions / Test

Complete the task associated to this 'TODO' comment. (https://rules.sonarsource.com/csharp/RSPEC-1135)
IQueryable<Initiative> initiative = dbContext.Initiatives.Where(i => i.Id == request.InitiativeId);
if (await initiative.AnyAsync(cancellationToken))
{
return new()
{
Success = false,
FailureReason = "Invalid InitiativeId."
};
}

await initiative.ExecuteDeleteAsync(cancellationToken);
await dbContext.SaveChangesAsync(cancellationToken);

return new()
{
Success = true
};
}
}
Loading

0 comments on commit 953e66f

Please sign in to comment.