summaryrefslogtreecommitdiff
path: root/slips/src/app/Marina/Task/RegistrationTasks.cs
blob: 44c60ee8de447e17a2e4b9349bf99b436c2bfbb5 (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
88
89
90
91
92
using System;
using System.Collections.Generic;
using Marina.Domain.Interfaces;
using Marina.Domain.Repositories;
using Marina.Infrastructure.Container;
using Marina.Presentation.DTO;
using Marina.Task.Mappers;

namespace Marina.Task {
	public class RegistrationTasks : IRegistrationTasks {
		public RegistrationTasks()
			: this(
				Resolve.DependencyFor< IBrokenRulesToDisplayItemMapper >( ),
				Resolve.DependencyFor< ICustomerRepository >( )
				) {}

		public RegistrationTasks( IBrokenRulesToDisplayItemMapper mapper, ICustomerRepository customers ) {
			_mapper = mapper;
			_customers = customers;
		}

		public IEnumerable< DisplayResponseLineDTO > RegisterNew( RegisterCustomerDTO customer ) {
			if ( null == _customers.FindBy( customer.UserName ) ) {
				ICustomer newCustomer = _customers.NewCustomer( );
				newCustomer.RegisterAccount( customer.UserName,
				                             customer.Password,
				                             customer.FirstName,
				                             customer.LastName,
				                             customer.Phone,
				                             customer.City );
				if ( !newCustomer.Registration( ).IsValid( ) ) {
					return _mapper.MapFrom( newCustomer.Registration( ).BrokenRules( ) );
				}
				else {
					_customers.Save( newCustomer );
					return new DisplayResponseLines( "Success!" );
				}
			}
			else {
				return
					new DisplayResponseLines(
						string.Format( "The username {0} is already taken. Please try another!", customer.UserName ) );
			}
		}

		public IEnumerable< DisplayResponseLineDTO > AddNewBoatUsing( BoatRegistrationDTO boat ) {
			ICustomer customer = _customers.FindBy( boat.CustomerId );
			customer.RegisterBoat( boat.RegistrationNumber,
			                       boat.Manufacturer,
			                       new DateTime( Convert.ToInt32( boat.ModelYear ), 1, 1 ),
			                       Convert.ToInt64( boat.Length ) );
			_customers.Save( customer );
			return new DisplayResponseLines( "Success!" );
		}

		public CustomerRegistrationDisplayDTO LoadRegistrationFor( long customerId ) {
			IRegistration registration = _customers.FindBy( customerId ).Registration( );
			return
				new CustomerRegistrationDisplayDTO( customerId.ToString( ),
				                                    registration.Username( ),
				                                    registration.FirstName( ),
				                                    registration.LastName( ),
				                                    registration.PhoneNumber( ),
				                                    registration.City( )
					);
		}

		public IEnumerable< DisplayResponseLineDTO > UpdateRegistrationFor( UpdateCustomerRegistrationDTO registration ) {
			ICustomer customer = _customers.FindBy( registration.CustomerId );

			customer.UpdateRegistrationTo( registration.Username, registration.Password, registration.FirstName,
			                               registration.LastName, registration.PhoneNumber, registration.City );
			if ( customer.Registration( ).IsValid( ) ) {
				_customers.Save( customer );
			}
			return _mapper.MapFrom( customer.Registration( ).BrokenRules( ) );
		}

		public IEnumerable< BoatRegistrationDTO > AllBoatsFor( long customerId ) {
			ICustomer customer = _customers.FindBy( customerId );

			foreach ( IBoat boat in customer.RegisteredBoats( ) ) {
				yield return
					new BoatRegistrationDTO( boat.RegistrationNumber( ), boat.Manufacturer( ), boat.YearOfModel( ).ToString( ),
					                         boat.LengthInFeet( ).ToString( ), customerId );
			}
		}

		private readonly IBrokenRulesToDisplayItemMapper _mapper;
		private readonly ICustomerRepository _customers;
	}
}