summaryrefslogtreecommitdiff
path: root/slips/src/app/Marina/DataAccess/DataMappers/DockDataMapper.cs
blob: bf2ae1323f0c78481289a31624a6bdc6ca224680 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using Marina.DataAccess.Builders;
using Marina.DataAccess.Schemas;
using Marina.Domain;
using Marina.Domain.Interfaces;
using Marina.Infrastructure.Container;

namespace Marina.DataAccess.DataMappers {
	public class DockDataMapper : IDockDataMapper {
		public DockDataMapper() : this( Resolve.DependencyFor< IDatabaseGateway >( ) ) {}

		public DockDataMapper( IDatabaseGateway gateway ) {
			_gateway = gateway;
		}

		public IDock FindBy( long dockId ) {
			return Map.From( _gateway.LoadRowUsing( Queries.SelectDockBy( dockId ) ) );
		}

		private readonly IDatabaseGateway _gateway;

		private static class Queries {
			public static IQuery SelectDockBy( long dockId ) {
				return DatabaseSelect
					.From( DockTable.TableName )
					.AddColumn( DockTable.DockID )
					.AddColumn( DockTable.DockName )
					.AddColumn( DockTable.LocationId )
					.AddColumn( DockTable.WaterService )
					.AddColumn( DockTable.ElectricalService )
					.AddColumn( LocationTable.Name )
					.InnerJoinOn( LocationTable.ID, DockTable.LocationId )
					.Where( DockTable.DockID, dockId ).Build( );
			}
		}

		private class Map {
			public static IDock From( IDatabaseRow row ) {
				return new Dock(
					row.From< long >( DockTable.DockID ),
					row.From< string >( DockTable.DockName ),
					new Location( row.From< string >( LocationTable.Name ) ),
					GetEnabledUtilities( row )
					);
			}

			private static IUtility GetEnabledUtilities( IDatabaseRow row ) {
				return Utilities.For(
					row.From< bool >( DockTable.WaterService ) ? Utilities.Water : null,
					row.From< bool >( DockTable.ElectricalService ) ? Utilities.Electrical : null
					);
			}
		}
	}
}