66 lines
2.3 KiB
C#
66 lines
2.3 KiB
C#
using MessengerApi.Factories;
|
|
using MessengerBroker.Configuration.Model;
|
|
using MessengerBroker.Factories;
|
|
using MessengerBroker.Model.Http;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace MessengerBroker.Handlers.Endpoint
|
|
{
|
|
public class UsersEndpointHandler
|
|
{
|
|
private readonly BrokerConfiguration configuration;
|
|
private readonly BrokerDbContextFactory brokerDbContextFactory;
|
|
private readonly DbContextFactory messengerDbContextFactory;
|
|
|
|
public UsersEndpointHandler(
|
|
BrokerConfiguration configuration,
|
|
BrokerDbContextFactory brokerDbContextFactory,
|
|
DbContextFactory messengerDbContextFactory)
|
|
{
|
|
this.configuration = configuration;
|
|
this.brokerDbContextFactory = brokerDbContextFactory;
|
|
this.messengerDbContextFactory = messengerDbContextFactory;
|
|
}
|
|
|
|
public Task<Users.UsersResponse> GetUsers()
|
|
{
|
|
var foreignUserIds = (Guid[])null;
|
|
|
|
using(var broCtx = this.brokerDbContextFactory.CreateDbContext())
|
|
{
|
|
foreignUserIds = broCtx.Users.Select(x => x.Id).ToArray();
|
|
}
|
|
|
|
using (var apiCtx = this.messengerDbContextFactory.CreateDbContext())
|
|
{
|
|
var localUsers = apiCtx.Users
|
|
.Where(x => !foreignUserIds.Any(f => f == x.Id))
|
|
.ToArray();
|
|
|
|
var localRoutes = apiCtx.UserRoutes
|
|
.Include(x => x.From)
|
|
.Include(x => x.To)
|
|
.Where(x => localUsers.Any(l => l.Id == x.From.Id) && localUsers.Any(l => l.Id == x.To.Id))
|
|
.ToArray();
|
|
|
|
return Task.FromResult(new Users.UsersResponse
|
|
{
|
|
Users = localUsers.Select(x => new Users.UsersResponse.User
|
|
{
|
|
Id = x.Id,
|
|
ApiKey = x.ApiKey,
|
|
IsEnabled = x.IsEnabled,
|
|
Name = x.Name
|
|
}).ToArray(),
|
|
UserRoutes = localRoutes.Select(x => new Users.UsersResponse.UserRoute
|
|
{
|
|
Id = x.Id,
|
|
FromId = x.From.Id,
|
|
ToId = x.To.Id
|
|
}).ToArray()
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|