Browse Source

Add station aliases to database

master
Kenneth Bruen 2 years ago
parent
commit
2465a90305
Signed by: kbruen
GPG Key ID: C1980A470C3EE5B1
  1. 17
      server/Models/Database/StationAlias.cs
  2. 23
      server/Services/Implementations/Database.cs

17
server/Models/Database/StationAlias.cs

@ -0,0 +1,17 @@
using System.Text.Json.Serialization;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
namespace Server.Models.Database;
public record StationAlias(
[property: BsonId]
[property: BsonRepresentation(BsonType.ObjectId)]
[property: JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
string? Id,
string Name,
[property: BsonRepresentation(BsonType.ObjectId)]
string? ListingId
) {
public StationAlias() : this(null, "", null) { }
}

23
server/Services/Implementations/Database.cs

@ -36,6 +36,7 @@ public class Database : Server.Services.Interfaces.IDatabase {
)) ))
.ToList(); .ToList();
public IReadOnlyList<TrainListing> Trains => trainListingsCollection.FindSync(_ => true).ToList(); public IReadOnlyList<TrainListing> Trains => trainListingsCollection.FindSync(_ => true).ToList();
public IReadOnlyList<StationAlias> StationAliases => stationAliasCollection.FindSync(_ => true).ToList();
private static readonly string DbDir = Environment.GetEnvironmentVariable("DB_DIR") ?? Path.Join(Environment.CurrentDirectory, "db"); private static readonly string DbDir = Environment.GetEnvironmentVariable("DB_DIR") ?? Path.Join(Environment.CurrentDirectory, "db");
private static readonly string DbFile = Path.Join(DbDir, "db.json"); private static readonly string DbFile = Path.Join(DbDir, "db.json");
@ -46,6 +47,7 @@ public class Database : Server.Services.Interfaces.IDatabase {
private readonly IMongoCollection<DbRecord> dbRecordCollection; private readonly IMongoCollection<DbRecord> dbRecordCollection;
private readonly IMongoCollection<TrainListing> trainListingsCollection; private readonly IMongoCollection<TrainListing> trainListingsCollection;
private readonly IMongoCollection<StationListing> stationListingsCollection; private readonly IMongoCollection<StationListing> stationListingsCollection;
private readonly IMongoCollection<StationAlias> stationAliasCollection;
private readonly AsyncThrottle throttle; private readonly AsyncThrottle throttle;
private readonly Dictionary<string, string> trainObjectIds = new(); private readonly Dictionary<string, string> trainObjectIds = new();
@ -62,8 +64,11 @@ public class Database : Server.Services.Interfaces.IDatabase {
dbRecordCollection = db.GetCollection<DbRecord>("db"); dbRecordCollection = db.GetCollection<DbRecord>("db");
trainListingsCollection = db.GetCollection<TrainListing>("trainListings"); trainListingsCollection = db.GetCollection<TrainListing>("trainListings");
stationListingsCollection = db.GetCollection<StationListing>("stationListings"); stationListingsCollection = db.GetCollection<StationListing>("stationListings");
stationAliasCollection = db.GetCollection<StationAlias>("stationAliases");
Migration(); Migration();
Task.Run(async () => await Initialize());
} }
private void Migration() { private void Migration() {
@ -170,6 +175,13 @@ public class Database : Server.Services.Interfaces.IDatabase {
} }
} }
private async Task Initialize() {
await foreach (var entry in await stationAliasCollection.FindAsync(_ => true)) {
if (entry?.ListingId is null) continue;
stationObjectIds.Add(entry.Name, entry.ListingId);
}
}
private readonly SemaphoreSlim insertTrainLock = new (1, 1); private readonly SemaphoreSlim insertTrainLock = new (1, 1);
public async Task<string> FoundTrain(string rank, string number, string company) { public async Task<string> FoundTrain(string rank, string number, string company) {
number = string.Join("", number.TakeWhile(c => c is >= '0' and <= '9')); number = string.Join("", number.TakeWhile(c => c is >= '0' and <= '9'));
@ -226,7 +238,16 @@ public class Database : Server.Services.Interfaces.IDatabase {
} }
); );
if (update.IsAcknowledged && update.ModifiedCount > 0) { if (update.IsAcknowledged && update.ModifiedCount > 0) {
stationObjectIds[name] = update.UpsertedId.AsObjectId.ToString(); var listingId = update.UpsertedId.AsObjectId.ToString();
stationObjectIds[name] = listingId;
await stationAliasCollection.UpdateOneAsync(
Builders<StationAlias>.Filter.Eq("name", name),
Builders<StationAlias>.Update.Combine(
Builders<StationAlias>.Update.SetOnInsert("name", name),
Builders<StationAlias>.Update.SetOnInsert("listingId", listingId)
),
new UpdateOptions { IsUpsert = true }
);
} }
} }
finally { finally {

Loading…
Cancel
Save