Skip to content

Commit

Permalink
Merge pull request #190 from fluentcms/177-review-litedbrepositories
Browse files Browse the repository at this point in the history
177 review litedbrepositories
  • Loading branch information
pournasserian authored Nov 16, 2023
2 parents 9f8eb5c + 3d1f4b6 commit a955f21
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/FluentCMS.Repositories/Abstractions/IPageRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ namespace FluentCMS.Repositories;
public interface IPageRepository : IGenericRepository<Page>
{
Task<IEnumerable<Page>> GetBySiteId(Guid siteId, CancellationToken cancellationToken = default);
Task<Page> GetByPath(string path);
Task<Page> GetByPath(string path, CancellationToken cancellationToken = default);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@

namespace FluentCMS.Repositories.LiteDb;

public class LiteDbGenericRepository<TEntity> : IGenericRepository<TEntity>
public class GenericRepository<TEntity> : IGenericRepository<TEntity>
where TEntity : IEntity
{
protected ILiteCollectionAsync<TEntity> Collection { get; }
protected ILiteCollectionAsync<BsonDocument> BsonCollection { get; }
protected LiteDbContext DbContext { get; private set; }

public LiteDbGenericRepository(LiteDbContext dbContext)
public GenericRepository(LiteDbContext dbContext)
{
DbContext = dbContext;
Collection = dbContext.Database.GetCollection<TEntity>(GetCollectionName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

namespace FluentCMS.Repositories.LiteDb;

public class LiteDbHostRepository : IHostRepository
public class HostRepository : IHostRepository
{
private readonly LiteDbContext _dbContext;
public LiteDbHostRepository(LiteDbContext dbContext)
public HostRepository(LiteDbContext dbContext)
{
_dbContext = dbContext;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace FluentCMS.Repositories.LiteDb;

public class LiteDbPageRepository : LiteDbGenericRepository<Page>, IPageRepository
public class PageRepository : GenericRepository<Page>, IPageRepository
{
public LiteDbPageRepository(LiteDbContext dbContext) : base(dbContext)
public PageRepository(LiteDbContext dbContext) : base(dbContext)
{
}

Expand All @@ -15,8 +15,10 @@ public async Task<IEnumerable<Page>> GetBySiteId(Guid siteId, CancellationToken
return await Collection.FindAsync(x => x.SiteId == siteId);
}

public async Task<Page> GetByPath(string path)
public async Task<Page> GetByPath(string path, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();

return await Collection.FindOneAsync(x => x.Path == path);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

namespace FluentCMS.Repositories.LiteDb;

internal class LiteDbRoleRepository : LiteDbGenericRepository<Role>, IRoleRepository
internal class RoleRepository : GenericRepository<Role>, IRoleRepository
{
public LiteDbRoleRepository(LiteDbContext dbContext) : base(dbContext)
public RoleRepository(LiteDbContext dbContext) : base(dbContext)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ public static IServiceCollection AddInMemoryLiteDbRepositories(this IServiceColl

private static void ConfigureServices(IServiceCollection services)
{
services.AddScoped(typeof(IGenericRepository<>), typeof(LiteDbGenericRepository<>));
services.AddScoped(typeof(IGenericRepository<>), typeof(GenericRepository<>));

services.AddScoped<ISiteRepository, LiteDbSiteRepository>();
services.AddScoped<IHostRepository, LiteDbHostRepository>();
services.AddScoped<IPageRepository, LiteDbPageRepository>();
services.AddScoped<IUserRepository, LiteDbUserRepository>();
services.AddScoped<IRoleRepository, LiteDbRoleRepository>();
services.AddScoped<ISiteRepository, SiteRepository>();
services.AddScoped<IHostRepository, HostRepository>();
services.AddScoped<IPageRepository, PageRepository>();
services.AddScoped<IUserRepository, UserRepository>();
services.AddScoped<IRoleRepository, RoleRepository>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace FluentCMS.Repositories.LiteDb;

public class LiteDbSiteRepository : LiteDbGenericRepository<Site>, ISiteRepository
public class SiteRepository : GenericRepository<Site>, ISiteRepository
{
public LiteDbSiteRepository(LiteDbContext dbContext) : base(dbContext)
public SiteRepository(LiteDbContext dbContext) : base(dbContext)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace FluentCMS.Repositories.LiteDb;

public class LiteDbUserRepository : LiteDbGenericRepository<User>, IUserRepository
public class UserRepository : GenericRepository<User>, IUserRepository
{
public LiteDbUserRepository(LiteDbContext dbContext) : base(dbContext)
public UserRepository(LiteDbContext dbContext) : base(dbContext)
{
}

Expand Down
17 changes: 9 additions & 8 deletions src/FluentCMS.Repositories/MongoDB/PageRepository.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using FluentCMS.Entities;
using MongoDB.Driver;
using System.Threading;

namespace FluentCMS.Repositories.MongoDB;

Expand All @@ -9,10 +10,15 @@ public PageRepository(IMongoDBContext mongoDbContext) : base(mongoDbContext)
{
}

public Task<Page> GetByPath(string path)
public async Task<Page> GetByPath(string path, CancellationToken cancellationToken = default)
{
// TODO: implement here
return Task.FromResult<Page>(null);
cancellationToken.ThrowIfCancellationRequested();

var filter = Builders<Page>.Filter.Eq(x => x.Path, path);

var findResult = await Collection.FindAsync(filter, null, cancellationToken);

return findResult.SingleOrDefault(cancellationToken);
}

public async Task<IEnumerable<Page>> GetBySiteId(Guid siteId, CancellationToken cancellationToken = default)
Expand All @@ -25,9 +31,4 @@ public async Task<IEnumerable<Page>> GetBySiteId(Guid siteId, CancellationToken

return findResult.ToEnumerable(cancellationToken);
}

public Task<IEnumerable<Page>> GetBySiteIdAndParentId(Guid siteId, Guid? parentId = null)
{
throw new NotImplementedException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public async Task Should_Update()
dbEntity.LastUpdatedAt.ShouldBeGreaterThan(utcNow);
}

private static LiteDbGenericRepository<AuditedDummyEntity> GetInstance()
private static GenericRepository<AuditedDummyEntity> GetInstance()
{
var liteDbContext = new LiteDbContext(Options.Create(new LiteDbOptions()));
return new LiteDbGenericRepository<AuditedDummyEntity>(liteDbContext);
return new GenericRepository<AuditedDummyEntity>(liteDbContext);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ public async Task Should_GetAll_With_Filter()
dbEntities.All(x => x.Id == firstGuid).ShouldBeTrue();
}

private static LiteDbGenericRepository<DummyEntity> GetInstance()
private static GenericRepository<DummyEntity> GetInstance()
{
var liteDbContext = new LiteDbContext(Options.Create(new LiteDbOptions()));
return new LiteDbGenericRepository<DummyEntity>(liteDbContext);
return new GenericRepository<DummyEntity>(liteDbContext);
}
}

0 comments on commit a955f21

Please sign in to comment.