From 427cfb49eac0fc24593ed4c3a08d2e4d38c75de1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Burak=20Halefo=C4=9Flu?= Date: Wed, 26 Jun 2024 11:16:55 +0300 Subject: [PATCH 1/2] cassandra added --- Core/Core.csproj | 2 + .../Cassandra/CassandraRepositoryBase.cs | 155 ++++++++++++++++++ .../CassandraConnectionSettings.cs | 10 ++ Core/DataAccess/Cassandra/ICassRepository.cs | 37 +++++ Core/Entities/CassDbEntity.cs | 6 + 5 files changed, 210 insertions(+) create mode 100644 Core/DataAccess/Cassandra/CassandraRepositoryBase.cs create mode 100644 Core/DataAccess/Cassandra/Configurations/CassandraConnectionSettings.cs create mode 100644 Core/DataAccess/Cassandra/ICassRepository.cs create mode 100644 Core/Entities/CassDbEntity.cs diff --git a/Core/Core.csproj b/Core/Core.csproj index 313c4d98..2f04ec57 100644 --- a/Core/Core.csproj +++ b/Core/Core.csproj @@ -10,6 +10,8 @@ + + diff --git a/Core/DataAccess/Cassandra/CassandraRepositoryBase.cs b/Core/DataAccess/Cassandra/CassandraRepositoryBase.cs new file mode 100644 index 00000000..bb578c75 --- /dev/null +++ b/Core/DataAccess/Cassandra/CassandraRepositoryBase.cs @@ -0,0 +1,155 @@ +using System; +using System.Linq; +using System.Linq.Expressions; +using System.Threading.Tasks; +using Cassandra; +using Cassandra.Data.Linq; +using Cassandra.Mapping; +using Core.DataAccess.Cassandra.Configurations; +using Core.Entities; +using Core.Utilities.IoC; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; + +namespace Core.DataAccess.Cassandra; + +// https://github.com/datastax/csharp-driver +public class CassandraRepositoryBase + : ICassRepository + where T : CassDbEntity +{ + private readonly IMapper _mapper; + private readonly MappingConfiguration _mappingConfiguration; + private readonly Table _table; + + protected CassandraRepositoryBase(MappingConfiguration mappingConfiguration) + { + _mappingConfiguration = mappingConfiguration; + var configuration = ServiceTool.ServiceProvider.GetService(); + var cassandraConnectionSettings = + configuration.GetSection("CassandraConnectionSettings").Get(); + var cluster = Cluster.Builder() + .AddContactPoints(cassandraConnectionSettings.Host) + .WithCredentials(cassandraConnectionSettings.UserName, cassandraConnectionSettings.Password) + .WithApplicationName("CustomerProjectServer") + .WithCompression(CompressionType.Snappy) + .Build(); + var session = cluster.Connect(); + session.CreateKeyspaceIfNotExists(cassandraConnectionSettings.Keyspace); + _table = new Table(session, mappingConfiguration); + _table.CreateIfNotExists(); + _mapper = new Mapper(session, mappingConfiguration); + } + + public IQueryable GetList(Expression> predicate = null) + { + return predicate == null + ? _table.Execute().AsQueryable() + : _table.Where(predicate) + .Execute().AsQueryable(); + } + + public T GetById(long id) + { + return _table.FirstOrDefault(u => u.Id == id).Execute(); + } + + public long GetCount(Expression> predicate = null) + { + return predicate == null + ? _table.Count().Execute() + : _table.Where(predicate) + .Count().Execute(); + } + + public async Task GetCountAsync(Expression> predicate = null) + { + return await Task.Run(() => predicate == null + ? _table.Count().Execute() + : _table.Where(predicate) + .Count().Execute()); + } + + public async Task> GetListAsync(Expression> predicate = null) + { + return await Task.Run(() => predicate == null + ? _table.Execute().AsQueryable() + : _table.Where(predicate) + .Execute().AsQueryable()); + } + + public async Task GetByIdAsync(long id) + { + return await Task.Run(() => + { + return _table.FirstOrDefault(u => u.Id == id).Execute(); + }); + } + + public async Task GetAsync(Expression> predicate) + { + return (await Task.Run(() => _table.Where(predicate).FirstOrDefault().Execute()))!; + } + + public bool Any(Expression> predicate = null) + { + var data = predicate == null + ? _table.FirstOrDefault().Execute() + : _table.Where(predicate).FirstOrDefault().Execute(); + + return data != null; + } + + public async Task AnyAsync(Expression> predicate = null) + { + return await Task.Run(() => + { + var data = predicate == null + ? _table.FirstOrDefault().Execute() + : _table.Where(predicate).FirstOrDefault().Execute(); + + return data != null; + }); + } + + public void Add(T entity) + { + var filter = _table.Execute().MaxBy(e => e.Id); + var id = filter?.Id ?? 0; + entity.Id = id + 1; + _table.Insert(entity).Execute(); + } + + public async Task AddAsync(T entity) + { + await Task.Run(() => + { + var filter = _table.Execute().MaxBy(e => e.Id); + var id = filter?.Id ?? 0; + entity.Id = id + 1; + _table.Insert(entity).Execute(); + }); + } + + public async Task UpdateAsync(T entity) + { + await _mapper.DeleteAsync(entity); + await _mapper.InsertAsync(entity); + } + + public void Update(T entity) + { + _mapper.Delete(entity); + _mapper.Insert(entity); + } + + public void Delete(T entity) + { + _mapper.Delete(entity); + } + + public async Task DeleteAsync(T entity) + { + await _mapper.DeleteAsync(entity); + } +} \ No newline at end of file diff --git a/Core/DataAccess/Cassandra/Configurations/CassandraConnectionSettings.cs b/Core/DataAccess/Cassandra/Configurations/CassandraConnectionSettings.cs new file mode 100644 index 00000000..366160fa --- /dev/null +++ b/Core/DataAccess/Cassandra/Configurations/CassandraConnectionSettings.cs @@ -0,0 +1,10 @@ +namespace Core.DataAccess.Cassandra.Configurations; + +public class CassandraConnectionSettings +{ + public string Host { get; set; } + public string UserName { get; set; } + public string Password { get; set; } + + public string Keyspace { get; set; } +} \ No newline at end of file diff --git a/Core/DataAccess/Cassandra/ICassRepository.cs b/Core/DataAccess/Cassandra/ICassRepository.cs new file mode 100644 index 00000000..fee9c806 --- /dev/null +++ b/Core/DataAccess/Cassandra/ICassRepository.cs @@ -0,0 +1,37 @@ +using System; +using System.Linq; +using System.Linq.Expressions; +using System.Threading.Tasks; +using Core.Entities; + +namespace Core.DataAccess.Cassandra; + +public interface ICassRepository where T : class, IEntity +{ + void Add(T entity); + + IQueryable GetList(Expression> predicate = null); + + Task UpdateAsync(T record); + + void Update(T record); + Task DeleteAsync(T record); + + void Delete(T record); + + T GetById(long id); + + Task AddAsync(T entity); + + Task> GetListAsync(Expression> predicate = null); + + Task GetByIdAsync(long id); + + Task GetAsync(Expression> predicate); + + bool Any(Expression> predicate = null); + Task AnyAsync(Expression> predicate = null); + + long GetCount(Expression> predicate = null); + Task GetCountAsync(Expression> predicate = null); +} \ No newline at end of file diff --git a/Core/Entities/CassDbEntity.cs b/Core/Entities/CassDbEntity.cs new file mode 100644 index 00000000..a22178d0 --- /dev/null +++ b/Core/Entities/CassDbEntity.cs @@ -0,0 +1,6 @@ +namespace Core.Entities; + +public abstract class CassDbEntity: IEntity +{ + public int Id { get; set; } +} \ No newline at end of file From ed64f643f9eeb04e101267d4e617b038d51155c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Burak=20Halefo=C4=9Flu?= Date: Sat, 29 Jun 2024 09:02:08 +0300 Subject: [PATCH 2/2] refact --- .../Configurations/CassandraConnectionSettings.cs | 1 - WebAPI/appsettings.Production.json | 6 ++++++ WebAPI/appsettings.Staging.json | 6 ++++++ WebAPI/appsettings.json | 9 ++++++++- 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/Core/DataAccess/Cassandra/Configurations/CassandraConnectionSettings.cs b/Core/DataAccess/Cassandra/Configurations/CassandraConnectionSettings.cs index 366160fa..f7024992 100644 --- a/Core/DataAccess/Cassandra/Configurations/CassandraConnectionSettings.cs +++ b/Core/DataAccess/Cassandra/Configurations/CassandraConnectionSettings.cs @@ -5,6 +5,5 @@ public class CassandraConnectionSettings public string Host { get; set; } public string UserName { get; set; } public string Password { get; set; } - public string Keyspace { get; set; } } \ No newline at end of file diff --git a/WebAPI/appsettings.Production.json b/WebAPI/appsettings.Production.json index 58f76de0..9102329e 100644 --- a/WebAPI/appsettings.Production.json +++ b/WebAPI/appsettings.Production.json @@ -1,6 +1,12 @@ { "Services": { + }, + "CassandraConnectionSettings": { + "Host": "localhost", + "UserName": "test", + "Password": "test", + "Keyspace": "test" }, "ConnectionStrings": { "DArchPgContext": "Host=localhost;Port=5432;Database=TestDb;Username=postgres;Password=test", diff --git a/WebAPI/appsettings.Staging.json b/WebAPI/appsettings.Staging.json index 38db1995..fbeb04e6 100644 --- a/WebAPI/appsettings.Staging.json +++ b/WebAPI/appsettings.Staging.json @@ -2,6 +2,12 @@ "Services": { }, + "CassandraConnectionSettings": { + "Host": "localhost", + "UserName": "test", + "Password": "test", + "Keyspace": "test" + }, "ConnectionStrings": { "DArchPgContext": "Host=localhost;Port=5432;Database=TestDb;Username=postgres;Password=test", "DArchMsContext": "data source=(localdb)\\MSSQLLocalDB;initial catalog=TestDb;persist security info=False;user id=sa;password=test;", diff --git a/WebAPI/appsettings.json b/WebAPI/appsettings.json index 6f1112cc..ffa311a4 100644 --- a/WebAPI/appsettings.json +++ b/WebAPI/appsettings.json @@ -33,7 +33,8 @@ "MessageBrokerOptions": { "HostName": "localhost", "UserName": "guest", - "Password": "guest" + "Password": "guest", + "Port": 0 }, "CacheOptions": { "Host": "localhost", @@ -60,6 +61,12 @@ "ConnectionString": "mongodb://localhost:27017/?readPreference=primary&appname=MongoDB%20Compass&ssl=false", "DatabaseName": "customerdb" }, + "CassandraConnectionSettings": { + "Host": "localhost", + "UserName": "test", + "Password": "test", + "Keyspace": "test" + }, "SeriLogConfigurations": { "PostgreConfiguration": { "ConnectionString": "Host=localhost;Port=5432;Database=TestDb;Username=postgres;Password=test;"