Skip to content

Commit 2946744

Browse files
committed
refactor: introduce IFaqRepository and update FAQ command handlers to use it
1 parent a4a22c8 commit 2946744

5 files changed

Lines changed: 46 additions & 9 deletions

File tree

backend/src/CCE.Application/PlatformSettings/Commands/CreateFaq/CreateFaqCommandHandler.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,20 @@ namespace CCE.Application.PlatformSettings.Commands.CreateFaq;
1111
public sealed class CreateFaqCommandHandler
1212
: IRequestHandler<CreateFaqCommand, Response<System.Guid>>
1313
{
14+
private readonly IFaqRepository _repo;
1415
private readonly ICceDbContext _db;
1516
private readonly MessageFactory _msg;
1617
private readonly ICurrentUserAccessor _currentUser;
1718
private readonly ISystemClock _clock;
1819

1920
public CreateFaqCommandHandler(
21+
IFaqRepository repo,
2022
ICceDbContext db,
2123
MessageFactory msg,
2224
ICurrentUserAccessor currentUser,
2325
ISystemClock clock)
2426
{
27+
_repo = repo;
2528
_db = db;
2629
_msg = msg;
2730
_currentUser = currentUser;
@@ -37,7 +40,7 @@ public CreateFaqCommandHandler(
3740
var answer = LocalizedText.Create(request.AnswerAr, request.AnswerEn);
3841

3942
var faq = Faq.Create(question, answer, request.Order, userId, _clock);
40-
_db.Add(faq);
43+
_repo.Add(faq);
4144
await _db.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
4245

4346
return _msg.Ok(faq.Id, "CONTENT_CREATED");

backend/src/CCE.Application/PlatformSettings/Commands/DeleteFaq/DeleteFaqCommandHandler.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,33 @@
22
using CCE.Application.Common.Interfaces;
33
using CCE.Application.Messages;
44
using MediatR;
5-
using Microsoft.EntityFrameworkCore;
65

76
namespace CCE.Application.PlatformSettings.Commands.DeleteFaq;
87

98
public sealed class DeleteFaqCommandHandler
109
: IRequestHandler<DeleteFaqCommand, Response<VoidData>>
1110
{
11+
private readonly IFaqRepository _repo;
1212
private readonly ICceDbContext _db;
1313
private readonly MessageFactory _msg;
1414

15-
public DeleteFaqCommandHandler(ICceDbContext db, MessageFactory msg)
15+
public DeleteFaqCommandHandler(IFaqRepository repo, ICceDbContext db, MessageFactory msg)
1616
{
17+
_repo = repo;
1718
_db = db;
1819
_msg = msg;
1920
}
2021

2122
public async Task<Response<VoidData>> Handle(
2223
DeleteFaqCommand request, CancellationToken cancellationToken)
2324
{
24-
var faq = await _db.Faqs
25-
.FirstOrDefaultAsync(f => f.Id == request.Id, cancellationToken)
25+
var faq = await _repo.GetByIdAsync(request.Id, cancellationToken)
2626
.ConfigureAwait(false);
2727

2828
if (faq is null)
2929
return _msg.FaqNotFound<VoidData>();
3030

31-
_db.Delete(faq);
31+
_repo.Delete(faq);
3232
await _db.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
3333

3434
return _msg.Ok("CONTENT_DELETED");

backend/src/CCE.Application/PlatformSettings/Commands/UpdateFaq/UpdateFaqCommandHandler.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,26 @@
55
using CCE.Domain.PlatformSettings;
66
using CCE.Domain.PlatformSettings.ValueObjects;
77
using MediatR;
8-
using Microsoft.EntityFrameworkCore;
98

109
namespace CCE.Application.PlatformSettings.Commands.UpdateFaq;
1110

1211
public sealed class UpdateFaqCommandHandler
1312
: IRequestHandler<UpdateFaqCommand, Response<System.Guid>>
1413
{
14+
private readonly IFaqRepository _repo;
1515
private readonly ICceDbContext _db;
1616
private readonly MessageFactory _msg;
1717
private readonly ICurrentUserAccessor _currentUser;
1818
private readonly ISystemClock _clock;
1919

2020
public UpdateFaqCommandHandler(
21+
IFaqRepository repo,
2122
ICceDbContext db,
2223
MessageFactory msg,
2324
ICurrentUserAccessor currentUser,
2425
ISystemClock clock)
2526
{
27+
_repo = repo;
2628
_db = db;
2729
_msg = msg;
2830
_currentUser = currentUser;
@@ -32,8 +34,7 @@ public UpdateFaqCommandHandler(
3234
public async Task<Response<System.Guid>> Handle(
3335
UpdateFaqCommand request, CancellationToken cancellationToken)
3436
{
35-
var faq = await _db.Faqs
36-
.FirstOrDefaultAsync(f => f.Id == request.Id, cancellationToken)
37+
var faq = await _repo.GetByIdAsync(request.Id, cancellationToken)
3738
.ConfigureAwait(false);
3839

3940
if (faq is null)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using CCE.Domain.PlatformSettings;
2+
3+
namespace CCE.Application.PlatformSettings;
4+
5+
/// <summary>Repository for the standalone FAQ entity.</summary>
6+
public interface IFaqRepository
7+
{
8+
Task<Faq?> GetByIdAsync(System.Guid id, CancellationToken ct);
9+
void Add(Faq faq);
10+
void Delete(Faq faq);
11+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using CCE.Application.PlatformSettings;
2+
using CCE.Domain.PlatformSettings;
3+
using CCE.Infrastructure.Persistence;
4+
using Microsoft.EntityFrameworkCore;
5+
6+
namespace CCE.Infrastructure.PlatformSettings;
7+
8+
public sealed class FaqRepository : IFaqRepository
9+
{
10+
private readonly CceDbContext _db;
11+
12+
public FaqRepository(CceDbContext db) => _db = db;
13+
14+
public async Task<Faq?> GetByIdAsync(System.Guid id, CancellationToken ct)
15+
=> await _db.Faqs
16+
.FirstOrDefaultAsync(f => f.Id == id, ct)
17+
.ConfigureAwait(false);
18+
19+
public void Add(Faq faq) => _db.Faqs.Add(faq);
20+
21+
public void Delete(Faq faq) => _db.Faqs.Remove(faq);
22+
}

0 commit comments

Comments
 (0)