-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from InternAcademy/5-setup-mongodb-database
Added MongoDb configuration
- Loading branch information
Showing
28 changed files
with
1,715 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/server/CookingApp/Infrastructure/Attributes/CollectionNameAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace CookingApp.Infrastructure.Attributes | ||
{ | ||
/// <summary> | ||
/// Sets the Collection Name for a Mongo Entity | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Class)] | ||
[ExcludeFromCodeCoverage] | ||
public class CollectionNameAttribute : Attribute | ||
{ | ||
public string Name { get; set; } | ||
|
||
public CollectionNameAttribute(string name) | ||
{ | ||
this.Name = name; | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/server/CookingApp/Infrastructure/Common/MongoEntity.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
namespace CookingApp.Infrastructure.Common | ||
{ | ||
using CookingApp.Infrastructure.Interfaces; | ||
using MongoDB.Bson.Serialization.Attributes; | ||
using MongoDB.Bson; | ||
|
||
/// <summary> | ||
/// Base Class for a Mongo Entity | ||
/// </summary> | ||
public abstract class MongoEntity : IMongoEntity | ||
{ | ||
/// <inheritdoc/> | ||
[BsonId] | ||
[BsonRepresentation(BsonType.ObjectId)] | ||
[BsonElement("_id", Order = 1)] | ||
public string Id { get; set; } = ObjectId.GenerateNewId().ToString(); | ||
|
||
/// <inheritdoc/> | ||
[BsonElement("_version", Order = 3)] | ||
public int RowVersion { get; set; } = 1; | ||
|
||
/// <inheritdoc/> | ||
[BsonElement("_createdDateTime", Order = 4)] | ||
[BsonRepresentation(BsonType.String)] | ||
public DateTime CreatedDateTime { get; set; } | ||
|
||
/// <inheritdoc/> | ||
[BsonElement("_updatedDateTime", Order = 5)] | ||
[BsonRepresentation(BsonType.String)] | ||
public DateTime UpdatedDateTime { get; set; } | ||
|
||
/// <inheritdoc/> | ||
[BsonElement("_deletedDateTime", Order = 6)] | ||
public DateTime? DeletedDateTime { get; set; } | ||
|
||
/// <inheritdoc/> | ||
[BsonIgnore] | ||
public bool IsDeleted => this.DeletedDateTime != null; | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
src/server/CookingApp/Infrastructure/Configurations/Database/MongoConfiguration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
namespace CookingApp.Infrastructure.Configurations.Database | ||
{ | ||
using MongoDB.Bson; | ||
|
||
public class MongoConfiguration | ||
{ | ||
public SoftDeleteConfiguration SoftDeleteConfiguration { get; private set; } = new(); | ||
|
||
public string? ConnectionString { get; private set; } | ||
public string? Database { get; private set; } | ||
|
||
public BsonType EnumConvention { get; private set; } = BsonType.Int32; | ||
public bool IgnoreIfDefaultConvention { get; private set; } = true; | ||
|
||
public bool IgnoreIfNullConvention { get; private set; } = true; | ||
|
||
/// <summary> | ||
/// Sets the Mongo Connection String | ||
/// </summary> | ||
/// <param name="value">Connection String</param> | ||
/// <returns><see cref="MongoConfiguration"/></returns> | ||
public MongoConfiguration WithConnectionString(string? value) | ||
{ | ||
this.ConnectionString = value; | ||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// Sets the Database Name | ||
/// </summary> | ||
/// <param name="value">Database Name</param> | ||
/// <returns><see cref="MongoConfiguration"/></returns> | ||
public MongoConfiguration WithDatabaseName(string? value) | ||
{ | ||
this.Database = value; | ||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// Configures Soft Deletes | ||
/// </summary> | ||
/// <param name="value">Soft Delete Configuration</param> | ||
/// <returns><see cref="MongoConfiguration"/></returns> | ||
public MongoConfiguration WithSoftDeletes(Action<SoftDeleteConfiguration> value) | ||
{ | ||
var softDeleteConfig = new SoftDeleteConfiguration(); | ||
value(softDeleteConfig); | ||
this.SoftDeleteConfiguration = softDeleteConfig; | ||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// Sets how enums should be represented in the database | ||
/// </summary> | ||
/// <param name="value">Enum representation</param> | ||
/// <returns><see cref="MongoConfiguration"/></returns> | ||
public MongoConfiguration RepresentEnumValuesAs(BsonType value = BsonType.Int32) | ||
{ | ||
this.EnumConvention = value; | ||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// Sets whether to ignore default values during serialization. | ||
/// </summary> | ||
/// <param name="ignoreIfDefault"></param> | ||
/// <returns><see cref="MongoConfiguration"/></returns> | ||
public MongoConfiguration WithIgnoreIfDefaultConvention(bool ignoreIfDefault = true) | ||
{ | ||
IgnoreIfDefaultConvention = ignoreIfDefault; | ||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// Sets whether to ignore null values during serialization. | ||
/// </summary> | ||
/// <param name="ignoreIfNull"></param> | ||
/// <returns><see cref="MongoConfiguration"/></returns> | ||
public MongoConfiguration WithIgnoreIfNullConvention(bool ignoreIfNull = true) | ||
{ | ||
IgnoreIfNullConvention = ignoreIfNull; | ||
return this; | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/server/CookingApp/Infrastructure/Configurations/Database/MongoSettings.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace CookingApp.Infrastructure.Configurations.Database | ||
{ | ||
public record MongoSettings | ||
{ | ||
public string Database { get; init; } = null!; | ||
public string Url { get; init; } = null!; | ||
|
||
public bool SoftDeleteEnabled { get; init; } = true; | ||
public int SoftDeletePollInMinutes { get; init; } = 60; | ||
public int SoftDeleteRetentionInDays { get; init; } = 28; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/server/CookingApp/Infrastructure/Configurations/Database/SoftDeleteConfiguration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
namespace CookingApp.Infrastructure.Configurations.Database | ||
{ | ||
/// <summary> | ||
/// Soft Delete Configuration | ||
/// </summary> | ||
public class SoftDeleteConfiguration | ||
{ | ||
public bool IsEnabled { get; private set; } | ||
public TimeSpan DeleteAfter { get; private set; } = TimeSpan.FromDays(31); | ||
|
||
/// <summary> | ||
/// Enables Soft Deletions | ||
/// </summary> | ||
/// <param name="value">True to enable</param> | ||
/// <returns><see cref="MongoConfiguration"/></returns> | ||
public SoftDeleteConfiguration Enabled(bool value = true) | ||
{ | ||
this.IsEnabled = value; | ||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// Sets the time period that Mongo will determine a record as permanently deleted | ||
/// </summary> | ||
/// <param name="value">Time Delay</param> | ||
/// <returns><see cref="MongoConfiguration"/></returns> | ||
public SoftDeleteConfiguration HardDeleteAfter(TimeSpan value) | ||
{ | ||
this.DeleteAfter = value; | ||
return this; | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/server/CookingApp/Infrastructure/Enums/SortDirection.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
namespace CookingApp.Infrastructure.Enums | ||
{ | ||
/// <summary> | ||
/// Sort Direction for Ordering | ||
/// </summary> | ||
public enum SortDirection | ||
{ | ||
/// <summary> | ||
/// Ascending Order | ||
/// </summary> | ||
Ascending, | ||
|
||
/// <summary> | ||
/// Descending Order | ||
/// </summary> | ||
Descending | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/server/CookingApp/Infrastructure/Extensions/MongoEntityExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
namespace CookingApp.Infrastructure.Extensions | ||
{ | ||
using CookingApp.Infrastructure.Attributes; | ||
using CookingApp.Infrastructure.Interfaces; | ||
|
||
public static class MongoEntityExtensions | ||
{ | ||
/// <summary> | ||
/// Gets the Collection Name for an Entity | ||
/// </summary> | ||
/// <typeparam name="T">Mongo Entity</typeparam> | ||
/// <returns>Collection Name</returns> | ||
public static string GetCollectionName<T>() where T : class, IMongoEntity | ||
{ | ||
var collectionNameAttribute = (CollectionNameAttribute)typeof(T).GetCustomAttributes(typeof(CollectionNameAttribute), true).FirstOrDefault(); | ||
Check warning on line 15 in src/server/CookingApp/Infrastructure/Extensions/MongoEntityExtensions.cs GitHub Actions / build
|
||
return collectionNameAttribute != null ? collectionNameAttribute.Name.ToLower() : typeof(T).Name.ToLower(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/server/CookingApp/Infrastructure/Interfaces/IMongoEntity.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
namespace CookingApp.Infrastructure.Interfaces | ||
{ | ||
using MongoDB.Bson.Serialization.Attributes; | ||
|
||
/// <summary> | ||
/// Interface representing a MongoDB entity with essential metadata properties. | ||
/// </summary> | ||
public interface IMongoEntity | ||
{ | ||
/// <summary> | ||
/// Gets or sets the unique identifier for the entity. | ||
/// </summary> | ||
string Id { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the date and time when the entity was created. | ||
/// </summary> | ||
DateTime CreatedDateTime { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the date and time when the entity was last updated. | ||
/// </summary> | ||
DateTime UpdatedDateTime { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the row version for concurrency control. | ||
/// </summary> | ||
int RowVersion { get; set; } | ||
|
||
/// <summary> | ||
/// Gets a value indicating whether the entity is marked as deleted. | ||
/// </summary> | ||
bool IsDeleted { get; } | ||
|
||
/// <summary> | ||
/// Gets or sets the date and time when the entity was deleted, if applicable. | ||
/// </summary> | ||
DateTime? DeletedDateTime { get; set; } | ||
} | ||
} |
Oops, something went wrong.