69 lines
2.1 KiB
C#
69 lines
2.1 KiB
C#
|
|
using Cassandra;
|
||
|
|
using Cassandra.Mapping;
|
||
|
|
using JiShe.CollectBus.Cassandra.Extensions;
|
||
|
|
using Volo.Abp.Domain.Entities;
|
||
|
|
using Volo.Abp.Domain.Repositories;
|
||
|
|
|
||
|
|
namespace JiShe.CollectBus.Cassandra
|
||
|
|
{
|
||
|
|
public class CassandraRepository<TEntity, TKey>
|
||
|
|
: ICassandraRepository<TEntity, TKey>
|
||
|
|
where TEntity : class
|
||
|
|
{
|
||
|
|
|
||
|
|
public CassandraRepository(ICassandraProvider cassandraProvider, MappingConfiguration mappingConfig)
|
||
|
|
{
|
||
|
|
Mapper = new Mapper(cassandraProvider.Session, mappingConfig);
|
||
|
|
cassandraProvider.Session.CreateTable<TEntity>(cassandraProvider.CassandraConfig.Keyspace);
|
||
|
|
}
|
||
|
|
|
||
|
|
public readonly IMapper Mapper;
|
||
|
|
|
||
|
|
public virtual async Task<TEntity> GetAsync(TKey id)
|
||
|
|
{
|
||
|
|
return await Mapper.SingleOrDefaultAsync<TEntity>("WHERE id = ?", id);
|
||
|
|
}
|
||
|
|
|
||
|
|
public virtual async Task<List<TEntity>> GetListAsync()
|
||
|
|
{
|
||
|
|
return (await Mapper.FetchAsync<TEntity>()).ToList();
|
||
|
|
}
|
||
|
|
|
||
|
|
public virtual async Task<TEntity> InsertAsync(TEntity entity)
|
||
|
|
{
|
||
|
|
await Mapper.InsertAsync(entity);
|
||
|
|
return entity;
|
||
|
|
}
|
||
|
|
|
||
|
|
public virtual async Task<TEntity> UpdateAsync(TEntity entity)
|
||
|
|
{
|
||
|
|
await Mapper.UpdateAsync(entity);
|
||
|
|
return entity;
|
||
|
|
}
|
||
|
|
|
||
|
|
public virtual async Task DeleteAsync(TEntity entity)
|
||
|
|
{
|
||
|
|
await Mapper.DeleteAsync(entity);
|
||
|
|
}
|
||
|
|
|
||
|
|
public virtual async Task DeleteAsync(TKey id)
|
||
|
|
{
|
||
|
|
await Mapper.DeleteAsync<TEntity>("WHERE id = ?", id);
|
||
|
|
}
|
||
|
|
|
||
|
|
public virtual async Task<List<TEntity>> GetPagedListAsync(
|
||
|
|
int skipCount,
|
||
|
|
int maxResultCount,
|
||
|
|
string sorting)
|
||
|
|
{
|
||
|
|
var cql = $"SELECT * FROM {typeof(TEntity).Name.ToLower()}";
|
||
|
|
if (!string.IsNullOrWhiteSpace(sorting))
|
||
|
|
{
|
||
|
|
cql += $" ORDER BY {sorting}";
|
||
|
|
}
|
||
|
|
cql += $" LIMIT {maxResultCount} OFFSET {skipCount}";
|
||
|
|
|
||
|
|
return (await Mapper.FetchAsync<TEntity>(cql)).ToList();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|