Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ public interface IPersonnelLocationsDocRepository
Task<PersonnelLocation> GetByIdAsync(string id);
Task<PersonnelLocation> GetByOldIdAsync(string id);
Task<PersonnelLocation> InsertAsync(PersonnelLocation location);
Task<PersonnelLocation> UpdateAsync(PersonnelLocation location);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ public interface IUnitLocationsDocRepository
Task<UnitsLocation> GetByIdAsync(string id);
Task<UnitsLocation> GetByOldIdAsync(string id);
Task<UnitsLocation> InsertAsync(UnitsLocation location);
Task<UnitsLocation> UpdateAsync(UnitsLocation location);
}
}
12 changes: 6 additions & 6 deletions Core/Resgrid.Services/MappingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ public class MappingService : IMappingService
{
private readonly IPoiTypesRepository _poiTypesRepository;
private readonly IPoisRepository _poisRepository;
private readonly IMongoRepository<MapLayer> _mapLayersRepository;
private readonly Lazy<IMongoRepository<MapLayer>> _mapLayersRepository;
private readonly IMapLayersDocRepository _mapLayersDocRepository;

public MappingService(IPoiTypesRepository poiTypesRepository, IPoisRepository poisRepository, IMongoRepository<MapLayer> mapLayersRepository,
public MappingService(IPoiTypesRepository poiTypesRepository, IPoisRepository poisRepository, Lazy<IMongoRepository<MapLayer>> mapLayersRepository,
IMapLayersDocRepository mapLayersDocRepository)
{
_poiTypesRepository = poiTypesRepository;
Expand Down Expand Up @@ -75,9 +75,9 @@ public async Task<MapLayer> SaveMapLayerAsync(MapLayer mapLayer)
else
{
if (mapLayer.Id.Timestamp == 0)
await _mapLayersRepository.InsertOneAsync(mapLayer);
await _mapLayersRepository.Value.InsertOneAsync(mapLayer);
else
await _mapLayersRepository.ReplaceOneAsync(mapLayer);
await _mapLayersRepository.Value.ReplaceOneAsync(mapLayer);

return mapLayer;
}
Expand All @@ -93,7 +93,7 @@ public async Task<List<MapLayer>> GetMapLayersForTypeDepartmentAsync(int departm
}
else
{
var layers = await _mapLayersRepository.FilterByAsync(filter => filter.DepartmentId == departmentId && filter.Type == (int)type && filter.IsDeleted == false);
var layers = await _mapLayersRepository.Value.FilterByAsync(filter => filter.DepartmentId == departmentId && filter.Type == (int)type && filter.IsDeleted == false);

return layers.ToList();
}
Expand All @@ -109,7 +109,7 @@ public async Task<MapLayer> GetMapLayersByIdAsync(string id)
}
else
{
var layers = await _mapLayersRepository.FindByIdAsync(id);
var layers = await _mapLayersRepository.Value.FindByIdAsync(id);

return layers;
}
Expand Down
36 changes: 27 additions & 9 deletions Core/Resgrid.Services/UnitsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public class UnitsService : IUnitsService
private readonly IUnitTypesRepository _unitTypesRepository;
private readonly IUnitRolesRepository _unitRolesRepository;
private readonly IUnitStateRoleRepository _unitStateRoleRepository;
private readonly IMongoRepository<UnitsLocation> _unitLocationRepository;
private readonly Lazy<IMongoRepository<UnitsLocation>> _unitLocationRepository;
private readonly IUnitLocationsDocRepository _unitLocationsDocRepository;
private readonly ISubscriptionsService _subscriptionsService;
private readonly IUserStateService _userStateService;
private readonly IEventAggregator _eventAggregator;
Expand All @@ -32,8 +33,9 @@ public class UnitsService : IUnitsService
public UnitsService(IUnitsRepository unitsRepository, IUnitStatesRepository unitStatesRepository,
IUnitLogsRepository unitLogsRepository, IUnitTypesRepository unitTypesRepository, ISubscriptionsService subscriptionsService,
IUnitRolesRepository unitRolesRepository, IUnitStateRoleRepository unitStateRoleRepository, IUserStateService userStateService,
IEventAggregator eventAggregator, ICustomStateService customStateService, IMongoRepository<UnitsLocation> unitLocationRepository,
IUnitActiveRolesRepository unitActiveRolesRepository, IDepartmentGroupsService departmentGroupsService, ILimitsService limitsService)
IEventAggregator eventAggregator, ICustomStateService customStateService, Lazy<IMongoRepository<UnitsLocation>> unitLocationRepository,
IUnitLocationsDocRepository unitLocationsDocRepository, IUnitActiveRolesRepository unitActiveRolesRepository,
IDepartmentGroupsService departmentGroupsService, ILimitsService limitsService)
{
_unitsRepository = unitsRepository;
_unitStatesRepository = unitStatesRepository;
Expand All @@ -46,6 +48,7 @@ public UnitsService(IUnitsRepository unitsRepository, IUnitStatesRepository unit
_eventAggregator = eventAggregator;
_customStateService = customStateService;
_unitLocationRepository = unitLocationRepository;
_unitLocationsDocRepository = unitLocationsDocRepository;
_unitActiveRolesRepository = unitActiveRolesRepository;
_departmentGroupsService = departmentGroupsService;
_limitsService = limitsService;
Expand Down Expand Up @@ -528,18 +531,28 @@ where callEnabledStates.Contains(us.State)
{
try
{
if (location.Id.Timestamp == 0)
await _unitLocationRepository.InsertOneAsync(location);
if (Config.DataConfig.DocDatabaseType == Config.DatabaseTypes.Postgres)
{
if (String.IsNullOrWhiteSpace(location.PgId))
location = await _unitLocationsDocRepository.InsertAsync(location);
else
location = await _unitLocationsDocRepository.UpdateAsync(location);
}
else
await _unitLocationRepository.ReplaceOneAsync(location);
{
if (location.Id.Timestamp == 0)
await _unitLocationRepository.Value.InsertOneAsync(location);
else
await _unitLocationRepository.Value.ReplaceOneAsync(location);
}

_eventAggregator.SendMessage<UnitLocationUpdatedEvent>(new UnitLocationUpdatedEvent()
{
DepartmentId = departmentId,
UnitId = location.UnitId.ToString(),
Latitude = double.Parse(location.Latitude.ToString()),
Longitude = double.Parse(location.Longitude.ToString()),
RecordId = location.Id.ToString(),
RecordId = location.GetId(),
});
}
catch (Exception ex)
Expand All @@ -554,7 +567,10 @@ public async Task<UnitsLocation> GetLatestUnitLocationAsync(int unitId, DateTime
{
try
{
var location = _unitLocationRepository.AsQueryable().Where(x => x.UnitId == unitId).OrderByDescending(y => y.Timestamp).FirstOrDefault();
if (Config.DataConfig.DocDatabaseType == Config.DatabaseTypes.Postgres)
return await _unitLocationsDocRepository.GetLatestLocationsByUnitIdAsync(unitId);

var location = _unitLocationRepository.Value.AsQueryable().Where(x => x.UnitId == unitId).OrderByDescending(y => y.Timestamp).FirstOrDefault();

//var layers = await _personnelLocationRepository.FilterByAsync(filter => filter.DepartmentId == departmentId && filter.Type == (int)type && filter.IsDeleted == false);

Expand All @@ -572,10 +588,12 @@ public async Task<List<UnitsLocation>> GetLatestUnitLocationsAsync(int departmen
{
try
{
if (Config.DataConfig.DocDatabaseType == Config.DatabaseTypes.Postgres)
return await _unitLocationsDocRepository.GetLatestLocationsByDepartmentIdAsync(departmentId);

//var locations = _unitLocationRepository.AsQueryable().Where(x => x.DepartmentId == departmentId).OrderByDescending(y => y.Timestamp).GroupBy(x => x.UnitId);//.FirstOrDefault();

var locations = _unitLocationRepository
var locations = _unitLocationRepository.Value
.AsQueryable()
.Where(x => x.DepartmentId == departmentId).OrderByDescending(y => y.Timestamp)
.GroupBy(pv => pv.UnitId, (key, group) => new
Expand Down
35 changes: 27 additions & 8 deletions Core/Resgrid.Services/UsersService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,23 @@ public string AffiliateRoleId
private readonly ICacheProvider _cacheProvider;
private readonly IIdentityRepository _identityRepository;
private readonly IDepartmentSettingsService _departmentSettingsService;
private readonly IMongoRepository<PersonnelLocation> _personnelLocationRepository;
private readonly Lazy<IMongoRepository<PersonnelLocation>> _personnelLocationRepository;
private readonly IPersonnelLocationsDocRepository _personnelLocationsDocRepository;
private readonly IEventAggregator _eventAggregator;
private readonly ILimitsService _limitsService;

public UsersService(IDepartmentMembersRepository departmentMembersRepository, ICacheProvider cacheProvider,
IIdentityRepository identityRepository, IDepartmentSettingsService departmentSettingsService,
IMongoRepository<PersonnelLocation> personnelLocationRepository, IEventAggregator eventAggregator,
Lazy<IMongoRepository<PersonnelLocation>> personnelLocationRepository, IPersonnelLocationsDocRepository personnelLocationsDocRepository,
IEventAggregator eventAggregator,
ILimitsService limitsService)
{
_departmentMembersRepository = departmentMembersRepository;
_cacheProvider = cacheProvider;
_identityRepository = identityRepository;
_departmentSettingsService = departmentSettingsService;
_personnelLocationRepository = personnelLocationRepository;
_personnelLocationsDocRepository = personnelLocationsDocRepository;
_eventAggregator = eventAggregator;
_limitsService = limitsService;
}
Expand Down Expand Up @@ -257,17 +260,27 @@ public async Task<PersonnelLocation> SavePersonnelLocationAsync(PersonnelLocatio
{
try
{
if (personnelLocation.Id.Timestamp == 0)
await _personnelLocationRepository.InsertOneAsync(personnelLocation);
if (Config.DataConfig.DocDatabaseType == Config.DatabaseTypes.Postgres)
{
if (String.IsNullOrWhiteSpace(personnelLocation.PgId))
personnelLocation = await _personnelLocationsDocRepository.InsertAsync(personnelLocation);
else
personnelLocation = await _personnelLocationsDocRepository.UpdateAsync(personnelLocation);
}
else
await _personnelLocationRepository.ReplaceOneAsync(personnelLocation);
{
if (personnelLocation.Id.Timestamp == 0)
await _personnelLocationRepository.Value.InsertOneAsync(personnelLocation);
else
await _personnelLocationRepository.Value.ReplaceOneAsync(personnelLocation);
}

_eventAggregator.SendMessage<PersonnelLocationUpdatedEvent>(new PersonnelLocationUpdatedEvent() {
DepartmentId = personnelLocation.DepartmentId,
UserId = personnelLocation.UserId,
Latitude = personnelLocation.Latitude,
Longitude = personnelLocation.Longitude,
RecordId = personnelLocation.Id.ToString(),
RecordId = personnelLocation.GetId(),
});
}
catch (Exception ex)
Expand All @@ -282,7 +295,10 @@ public async Task<List<PersonnelLocation>> GetLatestLocationsForDepartmentPerson
{
try
{
var locations = _personnelLocationRepository
if (Config.DataConfig.DocDatabaseType == Config.DatabaseTypes.Postgres)
return await _personnelLocationsDocRepository.GetLatestLocationsByDepartmentIdAsync(departmentId);

var locations = _personnelLocationRepository.Value
.AsQueryable()
.Where(x => x.DepartmentId == departmentId).OrderByDescending(y => y.Timestamp)
.GroupBy(pv => pv.UserId, (key, group) => new
Expand All @@ -308,7 +324,10 @@ public async Task<PersonnelLocation> GetPersonnelLocationByIdAsync(string id)
{
try
{
var layers = await _personnelLocationRepository.FindByIdAsync(id);
if (Config.DataConfig.DocDatabaseType == Config.DatabaseTypes.Postgres)
return await _personnelLocationsDocRepository.GetByIdAsync(id);

var layers = await _personnelLocationRepository.Value.FindByIdAsync(id);

return layers;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ public async Task<List<PersonnelLocation>> GetAllLocationsByUnitIdAsync(string u
using (var connection = new NpgsqlConnection(Config.DataConfig.DocumentConnectionString))
{
await connection.OpenAsync();
var personLocationsData = await connection.QueryAsync<PersonnelLocation>($"SELECT data FROM public.personnellocations ul WHERE ul.userid = '{userId}' ORDER BY timestamp DESC;");
var personLocationsData = await connection.QueryAsync<PersonnelLocation>(
"SELECT data FROM public.personnellocations ul WHERE ul.userid = @userId ORDER BY timestamp DESC;",
new { userId });

if (personLocationsData != null)
return personLocationsData.ToList();
Expand All @@ -32,7 +34,9 @@ public async Task<PersonnelLocation> GetLatestLocationsByUnitIdAsync(string user
using (var connection = new NpgsqlConnection(Config.DataConfig.DocumentConnectionString))
{
await connection.OpenAsync();
var unitLocationsData = await connection.QueryAsync<PersonnelLocation>($"SELECT data FROM public.personnellocations ul WHERE ul.userid = '{userId}' ORDER BY timestamp DESC LIMIT 1;");
var unitLocationsData = await connection.QueryAsync<PersonnelLocation>(
"SELECT data FROM public.personnellocations ul WHERE ul.userid = @userId ORDER BY timestamp DESC LIMIT 1;",
new { userId });

if (unitLocationsData != null)
return unitLocationsData.FirstOrDefault();
Expand All @@ -46,7 +50,9 @@ public async Task<List<PersonnelLocation>> GetLatestLocationsByDepartmentIdAsync
using (var connection = new NpgsqlConnection(Config.DataConfig.DocumentConnectionString))
{
await connection.OpenAsync();
var unitLocationsData = await connection.QueryAsync<PersonnelLocation>($"SELECT DISTINCT ON (userid) data FROM public.personnellocations ul WHERE ul.departmentid = {departmentId} ORDER BY timestamp DESC;");
var unitLocationsData = await connection.QueryAsync<PersonnelLocation>(
"SELECT DISTINCT ON (userid) data FROM public.personnellocations ul WHERE ul.departmentid = @departmentId ORDER BY ul.userid, ul.timestamp DESC;",
new { departmentId });

if (unitLocationsData != null)
return unitLocationsData.ToList();
Expand All @@ -60,19 +66,24 @@ public async Task<PersonnelLocation> GetByIdAsync(string id)
using (var connection = new NpgsqlConnection(Config.DataConfig.DocumentConnectionString))
{
await connection.OpenAsync();
var unitLocationsData = await connection.QueryAsync<PersonnelLocation>($"SELECT data FROM public.personnellocations ul WHERE ul.oid = '{id}';");
var unitLocationsData = await connection.QueryAsync<PersonnelLocation>(
"SELECT data FROM public.personnellocations ul WHERE ul.oid = @id;",
new { id });

if (unitLocationsData != null)
if (unitLocationsData != null && unitLocationsData.Any())
return unitLocationsData.FirstOrDefault();

if (!int.TryParse(id, out var numericId))
return null;

var unitLocationsData2 = await connection.QueryAsync<PersonnelLocation>(
"SELECT data FROM public.personnellocations ul WHERE ul.id = @id;",
new { id = numericId });

if (unitLocationsData2 != null && unitLocationsData2.Any())
return unitLocationsData2.FirstOrDefault();
else
{
var unitLocationsData2 = await connection.QueryAsync<PersonnelLocation>($"SELECT data FROM public.personnellocations ul WHERE ul.id = {id};");

if (unitLocationsData2 != null)
return unitLocationsData2.FirstOrDefault();
else
return null;
}
return null;
}
}

Expand All @@ -81,7 +92,9 @@ public async Task<PersonnelLocation> GetByOldIdAsync(string id)
using (var connection = new NpgsqlConnection(Config.DataConfig.DocumentConnectionString))
{
await connection.OpenAsync();
var personnelLocationsData = await connection.QueryAsync<PersonnelLocation>($"SELECT data FROM public.personnellocations ul WHERE ul.oid = '{id}';");
var personnelLocationsData = await connection.QueryAsync<PersonnelLocation>(
"SELECT data FROM public.personnellocations ul WHERE ul.oid = @id;",
new { id });

if (personnelLocationsData != null)
return personnelLocationsData.FirstOrDefault();
Expand All @@ -92,14 +105,54 @@ public async Task<PersonnelLocation> GetByOldIdAsync(string id)

public async Task<PersonnelLocation> InsertAsync(PersonnelLocation location)
{
var dataJson = JsonConvert.SerializeObject(location);

using (var connection = new NpgsqlConnection(Config.DataConfig.DocumentConnectionString))
{
await connection.OpenAsync();
var result = await connection.ExecuteScalarAsync<string>($"INSERT INTO public.personnellocations (departmentid, userid, data) VALUES ({location.DepartmentId}, '{location.UserId}', '{JsonConvert.SerializeObject(location)}') RETURNING id;");
var result = await connection.ExecuteScalarAsync<string>(
"INSERT INTO public.personnellocations (departmentid, userid, data) VALUES (@departmentId, @userId, CAST(@dataJson AS jsonb)) RETURNING id::text;",
new
{
departmentId = location.DepartmentId,
userId = location.UserId,
dataJson
});
location.PgId = result;

return location;
}
}

public async Task<PersonnelLocation> UpdateAsync(PersonnelLocation location)
{
if (location == null)
throw new ArgumentNullException(nameof(location));

if (string.IsNullOrWhiteSpace(location.PgId))
throw new InvalidOperationException("Personnel location PgId is required for updates.");

if (!int.TryParse(location.PgId, out var pgId))
throw new ArgumentException("Personnel location PgId must be a valid integer.", nameof(location));

var dataJson = JsonConvert.SerializeObject(location);

using (var connection = new NpgsqlConnection(Config.DataConfig.DocumentConnectionString))
{
await connection.OpenAsync();

await connection.ExecuteAsync(
"UPDATE public.personnellocations SET departmentid = @departmentId, userid = @userId, data = CAST(@dataJson AS jsonb) WHERE id = @id;",
new
{
departmentId = location.DepartmentId,
userId = location.UserId,
dataJson,
id = pgId
});

return location;
}
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}
}
Loading
Loading