Skip to content

Commit bab0566

Browse files
committed
Fix
1 parent 5481d17 commit bab0566

File tree

15 files changed

+1845
-327
lines changed

15 files changed

+1845
-327
lines changed

backend/NXTBackend.API.Core/IDomainService.cs

Lines changed: 46 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// See README in the root project for more information.
44
// ============================================================================
55

6+
using System.Linq.Expressions;
67
using NXTBackend.API.Domain.Common;
78
using NXTBackend.API.Models;
89

@@ -19,46 +20,54 @@ namespace NXTBackend.API.Core;
1920
/// <typeparam name="T">The model type.</typeparam>
2021
public interface IDomainService<T> where T : BaseEntity
2122
{
22-
/// <summary>
23-
/// Find the entity by its ID.
24-
/// </summary>
25-
/// <param name="id">The ID.</param>
26-
/// <returns>The entity found by that ID or null if not found.</returns>
27-
public Task<T?> FindByIdAsync(Guid id);
23+
/// <summary>
24+
/// Configures the query to include related entities.
25+
/// </summary>
26+
/// <param name="includeExpression">Expression that defines the related entity to include.</param>
27+
/// <returns>The service instance for method chaining.</returns>
28+
IDomainService<T> Include(Expression<Func<T, object>> includeExpression);
2829

29-
/// <summary>
30-
/// Update the entity.
31-
/// </summary>
32-
/// <param name="entity">The updated entity.</param>
33-
/// <returns>The updated entity.</returns>
34-
public Task<T> UpdateAsync(T entity);
30+
/// <summary>
31+
/// Find the entity by its ID.
32+
/// </summary>
33+
/// <param name="id">The ID.</param>
34+
/// <returns>The entity found by that ID or null if not found.</returns>
35+
Task<T?> FindByIdAsync(Guid id);
3536

36-
/// <summary>
37-
///
38-
/// </summary>
39-
/// <param name="ids"></param>
40-
/// <returns></returns>
41-
public Task<bool> AreValid(IEnumerable<Guid> ids);
37+
/// <summary>
38+
/// Update the entity.
39+
/// </summary>
40+
/// <param name="entity">The updated entity.</param>
41+
/// <returns>The updated entity.</returns>
42+
Task<T> UpdateAsync(T entity);
4243

44+
/// <summary>
45+
/// Validates if all provided IDs exist in the database.
46+
/// </summary>
47+
/// <param name="ids">Collection of IDs to validate.</param>
48+
/// <returns>True if all IDs are valid, false otherwise.</returns>
49+
Task<bool> AreValid(IEnumerable<Guid> ids);
4350

44-
/// <summary>
45-
/// Delete the entity.
46-
/// </summary>
47-
/// <param name="entity">The entity to delete</param>
48-
/// <returns>The deleted entity.</returns>
49-
public Task<T> DeleteAsync(T entity);
51+
/// <summary>
52+
/// Delete the entity.
53+
/// </summary>
54+
/// <param name="entity">The entity to delete</param>
55+
/// <returns>The deleted entity.</returns>
56+
Task<T> DeleteAsync(T entity);
5057

51-
/// <summary>
52-
/// Create a new entity.
53-
/// </summary>
54-
/// <param name="entity">The newly created entity.</param>
55-
/// <returns>The newly created entity.</returns>
56-
public Task<T> CreateAsync(T entity);
58+
/// <summary>
59+
/// Create a new entity.
60+
/// </summary>
61+
/// <param name="entity">The newly created entity.</param>
62+
/// <returns>The newly created entity.</returns>
63+
Task<T> CreateAsync(T entity);
5764

58-
/// <summary>
59-
/// Get all entities.
60-
/// </summary>
61-
/// <param name="pagination">Specific pagination parameters (as to avoid large queries)</param>
62-
/// <returns>A paginated list of entities.</returns>
63-
public Task<PaginatedList<T>> GetAllAsync(PaginationParams pagination, SortingParams sorting, FilterDictionary? filter = null);
64-
}
65+
/// <summary>
66+
/// Get all entities.
67+
/// </summary>
68+
/// <param name="pagination">Specific pagination parameters (as to avoid large queries)</param>
69+
/// <param name="sorting">Parameters for sorting the results</param>
70+
/// <param name="filter">Optional filters to apply to the query</param>
71+
/// <returns>A paginated list of entities.</returns>
72+
Task<PaginatedList<T>> GetAllAsync(PaginationParams pagination, SortingParams sorting, FilterDictionary? filter = null);
73+
}

backend/NXTBackend.API.Core/Notifications/Notification.cs

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@ namespace NXTBackend.API.Core.Notifications;
55
/// <summary>
66
/// Represents an abstract base class for notifications that can be sent through various channels.
77
/// </summary>
8-
public abstract class Notification()
8+
public abstract class Notification
99
{
10+
/// <summary>
11+
/// The ID of the user who should receive this notification
12+
/// </summary>
13+
public Guid NotifiableId { get; set; }
14+
1015
/// <summary>
1116
/// Gets the view name for this notification.
1217
/// Can be overridden by implementing classes.
@@ -24,7 +29,7 @@ public abstract class Notification()
2429
public virtual string ToText() => string.Empty;
2530

2631
/// <summary>
27-
/// Stores this notification in the database using the provided service.
32+
/// Stores this notification in the database.
2833
/// Must be implemented by derived classes.
2934
/// </summary>
3035
public abstract Domain.Entities.Notification ToDatabase();
@@ -33,28 +38,18 @@ public abstract class Notification()
3338
/// Determines whether this notification should be sent.
3439
/// </summary>
3540
public virtual bool ShouldSend() => true;
41+
42+
/// <summary>
43+
/// Determines whether this notification should be ignored / discarded.
44+
/// </summary>
45+
public virtual bool ShouldBeDiscarded() => false;
3646

3747
/// <summary>
38-
/// Retrieves the View (HTML) of this notification
39-
/// </summary>
40-
/// <param name="name"></param>
41-
/// <returns></returns>
42-
public string GetTemplate()
43-
{
44-
// string cacheKey = $"template_{View}";
45-
// var cachedTemplate = _cache.GetString(cacheKey);
46-
// if (!string.IsNullOrEmpty(cachedTemplate))
47-
// return cachedTemplate;
48-
49-
// Cache miss - read from file
50-
string templatePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "templates", $"{View}.html");
51-
string template = File.ReadAllText(templatePath);
52-
// var cacheOptions = new DistributedCacheEntryOptions
53-
// {
54-
// AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(24)
55-
// };
56-
57-
// _cache.SetString(cacheKey, template, cacheOptions);
58-
return template;
59-
}
60-
}
48+
/// Retrieves the HTML template for this notification
49+
/// </summary>
50+
public string GetTemplate()
51+
{
52+
string templatePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "templates", $"{View}.html");
53+
return File.ReadAllText(templatePath);
54+
}
55+
}

backend/NXTBackend.API.Core/Notifications/Welcome/Welcome.cs

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,43 @@
1010

1111
namespace NXTBackend.API.Core.Notifications.Welcome;
1212

13-
public class Welcome(User user) : Notification
13+
public class Welcome : Notification
1414
{
1515
public override string View => nameof(Welcome);
16+
17+
private readonly User user;
1618

17-
public override MailMessage ToMail()
18-
{
19-
var mail = new MailMessage();
19+
public Welcome(User to)
20+
{
21+
user = to;
22+
}
2023

21-
mail.To.Add(user.Details?.Email ?? throw new ServiceException("No Email!"));
22-
mail.Subject = "Welcome to our platform!";
24+
public override MailMessage ToMail()
25+
{
26+
var mail = new MailMessage();
2327

24-
// Render the view to HTML
25-
// TODO: Instead I may want to use Razor, but I don't wanna spend 6 years implementing that now
26-
mail.Body = GetTemplate()
27-
.Replace("{{login}}", user.Login);
28-
mail.IsBodyHtml = true;
28+
mail.To.Add(user.Details?.Email ?? throw new ServiceException("No Email!"));
29+
mail.Subject = "Welcome to our platform!";
2930

30-
return mail;
31-
}
31+
// Render the view to HTML
32+
// TODO: Instead I may want to use Razor, but I don't wanna spend 6 years implementing that now
33+
mail.Body = GetTemplate()
34+
.Replace("{{login}}", user.Login);
35+
mail.IsBodyHtml = true;
36+
37+
return mail;
38+
}
3239

3340
public override bool ShouldSend()
3441
{
35-
return user.Details?.Email is not null;
42+
return true;
3643
}
3744

3845
public override Domain.Entities.Notification ToDatabase()
3946
{
40-
throw new NotImplementedException();
47+
return new Domain.Entities.Notification
48+
{
49+
Type = nameof(Welcome),
50+
};
4151
}
4252
}

0 commit comments

Comments
 (0)