-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replaced EF MessageManager with Dapper MessageProvider.
- Loading branch information
1 parent
1db22ea
commit 4978093
Showing
20 changed files
with
281 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 23 additions & 6 deletions
29
Geesemon.DataAccess.Dapper/Extensions/ServiceCollectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,41 @@ | ||
using Geesemon.DataAccess.Dapper.Providers; | ||
using Dapper; | ||
|
||
using Geesemon.DataAccess.Dapper.Providers; | ||
using Geesemon.DataAccess.Dapper.TypeHandlers; | ||
using Geesemon.Model.Common; | ||
|
||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Geesemon.DataAccess.Dapper.Extensions; | ||
public static class ServiceCollectionExtensions | ||
{ | ||
public static IServiceCollection AddDapperServices(this IServiceCollection services) | ||
public static IServiceCollection InitializeDapper(this IServiceCollection services) | ||
{ | ||
services.AddSingleton<DapperConnection>(); | ||
AddDapperServices(services); | ||
AddProviders(services); | ||
AddSqlMappers(); | ||
|
||
services.AddProviders(); | ||
return services; | ||
} | ||
|
||
private static IServiceCollection AddDapperServices(IServiceCollection services) | ||
{ | ||
services.AddSingleton<DbConnection>(); | ||
|
||
return services; | ||
} | ||
|
||
private static IServiceCollection AddProviders(this IServiceCollection services) | ||
private static IServiceCollection AddProviders(IServiceCollection services) | ||
{ | ||
services.AddSingleton<UserProvider>(); | ||
|
||
services.AddSingleton<MessageProvider>(); | ||
|
||
return services; | ||
} | ||
|
||
private static void AddSqlMappers() | ||
{ | ||
SqlMapper.AddTypeHandler(typeof(string[]), new JsonTypeHandler<string[]>()); | ||
SqlMapper.AddTypeHandler(typeof(ForwardedMessage), new JsonTypeHandler<ForwardedMessage>()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using Dapper; | ||
|
||
using Geesemon.Model.Models; | ||
|
||
namespace Geesemon.DataAccess.Dapper.Providers; | ||
|
||
public class MessageProvider : BaseProvider<Message> | ||
{ | ||
public MessageProvider(DbConnection dbConnection) | ||
: base(dbConnection) | ||
{ | ||
} | ||
|
||
public async Task<int> GetNotReadMessagesCount(Guid chatId, Guid currentUserId) | ||
{ | ||
using var connection = dbConnection.Open(); | ||
|
||
var tableName = GetTableName(); | ||
var query = | ||
$@" | ||
SELECT count(*) FROM {tableName} | ||
WHERE ChatId = @chatId AND | ||
FromId != @currentUserId AND | ||
Messages.Id != ALL (SELECT MessageId FROM ReadMessages WHERE ReadById = @currentUserId) | ||
"; | ||
var result = await connection.ExecuteScalarAsync<int>(query, new { chatId, currentUserId }); | ||
|
||
return result; | ||
} | ||
|
||
public async Task<List<Message>> GetByChatIdAsync(Guid chatId, int skip, int take = 30) | ||
{ | ||
using var connection = dbConnection.Open(); | ||
|
||
var tableName = GetTableName(); | ||
var columns = GetColumns(); | ||
var query = | ||
$@" | ||
SELECT {columns} FROM {tableName} | ||
WHERE(ChatId = @chatId) | ||
ORDER BY CreatedAt desc | ||
OFFSET @skip ROWS | ||
FETCH NEXT @take ROWS ONLY; | ||
"; | ||
|
||
var response = await connection.QueryAsync<Message>(query, new { take, skip, chatId }); | ||
|
||
return response.ToList(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
Geesemon.DataAccess.Dapper/TypeHandlers/JsonTypeHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System.Data; | ||
using System.Text.Json; | ||
|
||
using static Dapper.SqlMapper; | ||
|
||
namespace Geesemon.DataAccess.Dapper.TypeHandlers; | ||
internal class JsonTypeHandler<T> : TypeHandler<T> | ||
{ | ||
public override T? Parse(object value) | ||
{ | ||
var json = value.ToString(); | ||
|
||
return JsonSerializer.Deserialize<T>(json); | ||
} | ||
|
||
public override void SetValue(IDbDataParameter parameter, T? value) | ||
{ | ||
parameter.Value = JsonSerializer.Serialize(value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
using Geesemon.DataAccess.Providers.MessageProvider; | ||
using Geesemon.Model.Models; | ||
//using Geesemon.DataAccess.Providers.MessageProvider; | ||
//using Geesemon.Model.Models; | ||
|
||
namespace Geesemon.DataAccess.Managers | ||
{ | ||
public class MessageManager : MessageProvider, IManager<Message> | ||
{ | ||
public MessageManager(AppDbContext appContext) | ||
: base(appContext) | ||
{ } | ||
} | ||
} | ||
//namespace Geesemon.DataAccess.Managers | ||
//{ | ||
// public class MessageManager : MessageProvider, IManager<Message> | ||
// { | ||
// public MessageManager(AppDbContext appContext) | ||
// : base(appContext) | ||
// { } | ||
// } | ||
//} |
Oops, something went wrong.