1- using Microsoft . AspNetCore . Mvc ;
1+ using Microsoft . AspNetCore . Mvc ;
22using Microsoft . EntityFrameworkCore ;
33using OV_DB . Helpers ;
44using OV_DB . Models ;
1414using CsvHelper . Configuration . Attributes ;
1515using System . Globalization ;
1616using NetTopologySuite . Geometries ;
17+ using System . Security . Claims ;
1718
1819namespace OV_DB . Controllers
1920{
@@ -31,12 +32,15 @@ public ExportController(OVDBDatabaseContext dbContext)
3132 [ HttpPost ( "Trainlog" ) ]
3233 public async Task < IActionResult > ExportToTrainlog ( [ FromBody ] ExportRequest request )
3334 {
34- var adminClaim = ( User . Claims . SingleOrDefault ( c => c . Type == "admin" ) . Value ?? "false" ) ;
35+ var adminClaim = ( User . Claims . SingleOrDefault ( c => c . Type == "admin" ) ? . Value ?? "false" ) ;
3536 if ( string . Equals ( adminClaim , "false" , StringComparison . OrdinalIgnoreCase ) )
3637 {
3738 return Forbid ( ) ;
3839 }
3940
41+ var userIdClaim = int . Parse ( User . Claims . SingleOrDefault ( c => c . Type == ClaimTypes . NameIdentifier ) ? . Value ?? "-1" ) ;
42+ var user = await _dbContext . Users . FindAsync ( userIdClaim ) ;
43+
4044 if ( ( request . RouteInstanceIds == null || ! request . RouteInstanceIds . Any ( ) ) &&
4145 ( request . RouteIds == null || ! request . RouteIds . Any ( ) ) )
4246 {
@@ -48,8 +52,6 @@ public async Task<IActionResult> ExportToTrainlog([FromBody] ExportRequest reque
4852 . ThenInclude ( r => r . RouteType )
4953 . Include ( ri => ri . Route )
5054 . ThenInclude ( r => r . Regions )
51- . ThenInclude ( rg => rg . ParentRegion )
52- . ThenInclude ( pr => pr . ParentRegion )
5355 . Include ( ri => ri . RouteInstanceProperties ) ;
5456
5557 if ( request . RouteIds != null && request . RouteIds . Any ( ) )
@@ -140,7 +142,7 @@ public async Task<IActionResult> ExportToTrainlog([FromBody] ExportRequest reque
140142 }
141143 else
142144 {
143- end = start . AddHours ( 1 ) ; // Default duration?
145+ end = start ; // Default duration?
144146 }
145147
146148 // 3. Flags/Stations
@@ -296,9 +298,18 @@ public async Task<IActionResult> ExportToTrainlog([FromBody] ExportRequest reque
296298 }
297299
298300 // Tags
299- string materialType = properties . ContainsKey ( "Voertuig type" ) ? properties [ "Voertuig type" ] :
300- ( properties . ContainsKey ( "train_type" ) ? properties [ "train_type" ] : "" ) ;
301- string reg = properties . ContainsKey ( "Voertuig nummer" ) ? properties [ "Voertuig nummer" ] : "" ;
301+ string materialKey = ! string . IsNullOrEmpty ( user ? . TrainlogMaterialKey ) ? user . TrainlogMaterialKey : "Voertuig type" ;
302+ string regKey = ! string . IsNullOrEmpty ( user ? . TrainlogRegistrationKey ) ? user . TrainlogRegistrationKey : "Voertuig nummer" ;
303+ string seatKey = ! string . IsNullOrEmpty ( user ? . TrainlogSeatKey ) ? user . TrainlogSeatKey : "Stoel" ;
304+
305+ string materialType = properties . ContainsKey ( materialKey ) ? properties [ materialKey ] : "" ;
306+ if ( string . IsNullOrEmpty ( materialType ) && string . IsNullOrEmpty ( user ? . TrainlogMaterialKey ) && properties . ContainsKey ( "train_type" ) )
307+ {
308+ materialType = properties [ "train_type" ] ;
309+ }
310+
311+ string reg = properties . ContainsKey ( regKey ) ? properties [ regKey ] : "" ;
312+ string seat = properties . ContainsKey ( seatKey ) ? properties [ seatKey ] : "" ;
302313 string note = "" ;
303314
304315 // Create Row Object
@@ -308,15 +319,15 @@ public async Task<IActionResult> ExportToTrainlog([FromBody] ExportRequest reque
308319 Username = "ovdb_export" ,
309320 OriginStation = origin ,
310321 DestinationStation = destination ,
311- StartDatetime = start . ToString ( "yyyy-MM-dd HH:mm:ss" ) ,
312- EndDatetime = end . ToString ( "yyyy-MM-dd HH:mm:ss" ) ,
322+ StartDatetime = start == end ? start . ToString ( "yyyy-MM-dd" ) : start . ToString ( "yyyy-MM-dd HH:mm:ss" ) ,
323+ EndDatetime = start == end ? start . ToString ( "yyyy-MM-dd" ) : end . ToString ( "yyyy-MM-dd HH:mm:ss" ) ,
313324 EstimatedTripDuration = duration . ToString ( "F0" ) ,
314325 ManualTripDuration = "" ,
315326 TripLength = lengthMeters . ToString ( "F0" ) ,
316327 Operator = route . OperatingCompany ?? "" ,
317328 Countries = countriesJson ,
318- UtcStartDatetime = start . ToUniversalTime ( ) . ToString ( "yyyy-MM-dd HH:mm:ss" ) ,
319- UtcEndDatetime = end . ToUniversalTime ( ) . ToString ( "yyyy-MM-dd HH:mm:ss" ) ,
329+ UtcStartDatetime = start == end ? start . ToString ( "yyyy-MM-dd" ) : start . ToUniversalTime ( ) . ToString ( "yyyy-MM-dd HH:mm:ss" ) ,
330+ UtcEndDatetime = start == end ? start . ToString ( "yyyy-MM-dd" ) : end . ToUniversalTime ( ) . ToString ( "yyyy-MM-dd HH:mm:ss" ) ,
320331 LineName = route . LineNumber ?? "" ,
321332 Created = DateTime . Now . ToString ( "yyyy-MM-dd HH:mm:ss" ) ,
322333 LastModified = DateTime . Now . ToString ( "yyyy-MM-dd HH:mm:ss" ) ,
@@ -329,7 +340,6 @@ public async Task<IActionResult> ExportToTrainlog([FromBody] ExportRequest reque
329340 Price = "" ,
330341 Currency = "" ,
331342 PurchasingDate = "" ,
332- Visibility = "public" ,
333343 Path = encodedPath
334344 } ) ;
335345 }
@@ -347,7 +357,7 @@ private string GetFlagFromRegions(Point point, IEnumerable<Region> regions)
347357 {
348358 // Find region containing point with a flag
349359 var match = regions . FirstOrDefault ( r => ! string . IsNullOrEmpty ( r . FlagEmoji ) && r . Geometry != null && r . Geometry . Contains ( point ) ) ;
350- return match ? . FlagEmoji ?? "" ;
360+ return match ? . FlagEmoji ?? "🇺🇳 " ;
351361 }
352362
353363 private string AppendFlag ( string flag , string name )
@@ -359,68 +369,5 @@ private string AppendFlag(string flag, string name)
359369 return name ;
360370 }
361371
362- public class TrainlogExportRow
363- {
364- [ Name ( "uid" ) ]
365- public string Uid { get ; set ; }
366- [ Name ( "username" ) ]
367- public string Username { get ; set ; }
368- [ Name ( "origin_station" ) ]
369- public string OriginStation { get ; set ; }
370- [ Name ( "destination_station" ) ]
371- public string DestinationStation { get ; set ; }
372- [ Name ( "start_datetime" ) ]
373- public string StartDatetime { get ; set ; }
374- [ Name ( "end_datetime" ) ]
375- public string EndDatetime { get ; set ; }
376- [ Name ( "estimated_trip_duration" ) ]
377- public string EstimatedTripDuration { get ; set ; }
378- [ Name ( "manual_trip_duration" ) ]
379- public string ManualTripDuration { get ; set ; }
380- [ Name ( "trip_length" ) ]
381- public string TripLength { get ; set ; }
382- [ Name ( "operator" ) ]
383- public string Operator { get ; set ; }
384- [ Name ( "countries" ) ]
385- public string Countries { get ; set ; }
386- [ Name ( "utc_start_datetime" ) ]
387- public string UtcStartDatetime { get ; set ; }
388- [ Name ( "utc_end_datetime" ) ]
389- public string UtcEndDatetime { get ; set ; }
390- [ Name ( "line_name" ) ]
391- public string LineName { get ; set ; }
392- [ Name ( "created" ) ]
393- public string Created { get ; set ; }
394- [ Name ( "last_modified" ) ]
395- public string LastModified { get ; set ; }
396- [ Name ( "type" ) ]
397- public string Type { get ; set ; }
398- [ Name ( "material_type" ) ]
399- public string MaterialType { get ; set ; }
400- [ Name ( "seat" ) ]
401- public string Seat { get ; set ; }
402- [ Name ( "reg" ) ]
403- public string Reg { get ; set ; }
404- [ Name ( "waypoints" ) ]
405- public string Waypoints { get ; set ; }
406- [ Name ( "notes" ) ]
407- public string Notes { get ; set ; }
408- [ Name ( "price" ) ]
409- public string Price { get ; set ; }
410- [ Name ( "currency" ) ]
411- public string Currency { get ; set ; }
412- [ Name ( "purchasing_date" ) ]
413- public string PurchasingDate { get ; set ; }
414- [ Name ( "visibility" ) ]
415- public string Visibility { get ; set ; }
416- [ Name ( "path" ) ]
417- public string Path { get ; set ; }
418- }
419-
420- public class ExportRequest
421- {
422- public List < int > RouteInstanceIds { get ; set ; }
423- public List < int > RouteIds { get ; set ; }
424- }
425372 }
426373}
0 commit comments