summaryrefslogtreecommitdiff
path: root/slips/src/app/Marina/DataAccess/DataMappers/BoatDataMapper.cs
blob: fbd33cde34eddb8f25b4f373c9db48c65090cd82 (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using System;
using System.Collections.Generic;
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 BoatDataMapper : IBoatDataMapper {
		public BoatDataMapper() : this( Resolve.DependencyFor< IDatabaseGateway >( ) ) {}

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

		public IEnumerable< IBoat > AllBoatsFor( long customerId ) {
			return Map.From( _gateway.FindAllRowsMatching( Queries.SelectBoatsFor( customerId ) ) );
		}

		public void Insert( IEnumerable< IBoat > boats, long forCustomerId ) {
			_gateway.Execute( Queries.InsertQueriesFor( boats, forCustomerId ) );
		}

		public void Update( IEnumerable< IBoat > boats, long forCustomerId ) {
			using ( IDatabaseTransaction transaction = new DatabaseTransaction( ) ) {
				_gateway.Execute( DatabaseDelete.Where( BoatTable.CustomerID, forCustomerId ) );
				Insert( boats, forCustomerId );
				transaction.Commit( );
			}
		}

		private readonly IDatabaseGateway _gateway;

		private class Queries {
			public static IQuery SelectBoatsFor( long customerId ) {
				return DatabaseSelect.From( BoatTable.TableName )
					.AddColumn( BoatTable.Length )
					.AddColumn( BoatTable.Manufacturer )
					.AddColumn( BoatTable.ModelYear )
					.AddColumn( BoatTable.RegistrationNumber )
					.AddColumn( BoatTable.BoatID )
					.Where( BoatTable.CustomerID, customerId.ToString( ) ).Build( );
			}

			public static IEnumerable< IQuery > InsertQueriesFor( IEnumerable< IBoat > boats, long customerId ) {
				foreach ( IBoat boat in boats ) {
					yield return DatabaseInsert.Into( BoatTable.TableName )
						.AddValue( BoatTable.CustomerID, customerId )
						.AddValue( BoatTable.Length, boat.LengthInFeet( ) )
						.AddValue( BoatTable.Manufacturer, boat.Manufacturer( ) )
						.AddValue( BoatTable.ModelYear, boat.YearOfModel( ).Year )
						.AddValue( BoatTable.RegistrationNumber, boat.RegistrationNumber( ) ).Build( );
				}
			}
		}

		private class Map {
			public static IEnumerable< IBoat > From( IEnumerable< IDatabaseRow > rows ) {
				foreach ( IDatabaseRow dataRow in rows ) {
					yield return new Boat(
						dataRow.From< long >( BoatTable.BoatID ),
						dataRow.From< string >( BoatTable.RegistrationNumber ),
						dataRow.From< string >( BoatTable.Manufacturer ),
						dataRow.From< int >( BoatTable.ModelYear ) != 0
							? new DateTime( dataRow.From< int >( BoatTable.ModelYear ), 1, 01 )
							: DateTime.MinValue,
						dataRow.From< long >( BoatTable.Length )
						);
				}
			}
		}
	}
}