summaryrefslogtreecommitdiff
path: root/slips/src/app/Marina/DataAccess/DataMappers/LeaseDataMapper.cs
blob: 8214b05de8687f3d157062272c876f16dae29715 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
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 LeaseDataMapper : ILeaseDataMapper {
		public LeaseDataMapper()
			: this( Resolve.DependencyFor< IDatabaseGateway >( ), Resolve.DependencyFor< ISlipDataMapper >( ) ) {}

		public LeaseDataMapper( IDatabaseGateway gateway, ISlipDataMapper slipMapper ) {
			_gateway = gateway;
			_slipMapper = slipMapper;
		}

		public IEnumerable< ISlipLease > AllLeasesFor( long customerId ) {
			foreach ( IDatabaseRow row in _gateway.FindAllRowsMatching( Queries.SelectLeasesFor( customerId ) ) ) {
				yield return
					new SlipLease(
						_slipMapper.FindBy( row.From< long >( LeaseTable.SlipID ) ),
						LeaseDurations.FindFor( row.From< DateTime >( LeaseTable.StartDate ), row.From< DateTime >( LeaseTable.EndDate ) ),
						row.From< DateTime >( LeaseTable.StartDate ),
						row.From< DateTime >( LeaseTable.EndDate )
						);
			}
		}

		public void Insert( IEnumerable< ISlipLease > leases, long forCustomerId ) {
			using ( IDatabaseTransaction transaction = new DatabaseTransaction( ) ) {
				IList< IQuery > queries = new List< IQuery >( );
				foreach ( ISlipLease lease in leases ) {
					queries.Add(
						DatabaseInsert.Into( LeaseTable.TableName )
							.AddValue( LeaseTable.StartDate, lease.StartDate( ) )
							.AddValue( LeaseTable.EndDate, lease.ExpiryDate( ) )
							.AddValue( LeaseTable.SlipID, lease.Slip( ).ID( ) )
							.AddValue( LeaseTable.CustomerID, forCustomerId )
							.AddValue( LeaseTable.LeaseTypeID, lease.Duration( ).ID( ) ).Build( )
						);
				}
				_gateway.Execute( queries );
				transaction.Commit( );
			}
		}

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

		public bool IsLeased( long slipId ) {
			IQuery query =
				DatabaseSelect.From( LeaseTable.TableName ).AddColumn( LeaseTable.SlipID ).Where( LeaseTable.SlipID, slipId ).Build( );

			return !_gateway.LoadRowUsing( query ).Equals( DatabaseRow.Blank );
		}

		private readonly IDatabaseGateway _gateway;
		private readonly ISlipDataMapper _slipMapper;

		private static class Queries {
			public static IQuery SelectLeasesFor( long customerId ) {
				return DatabaseSelect
					.From( LeaseTable.TableName )
					.AddColumn( LeaseTable.EndDate )
					.AddColumn( LeaseTable.ID )
					.AddColumn( LeaseTable.LeaseTypeID )
					.AddColumn( LeaseTable.SlipID )
					.AddColumn( LeaseTable.StartDate )
					.Where( LeaseTable.CustomerID, customerId ).Build( );
			}

			public static IQuery SelectLeaseFor( long slipId ) {
				return DatabaseSelect.From( LeaseTable.TableName )
					.AddColumn( LeaseTable.StartDate )
					.AddColumn( LeaseTable.EndDate )
					.Where( LeaseTable.SlipID, slipId ).Build( );
			}
		}
	}
}