summaryrefslogtreecommitdiff
path: root/slips/src/app/Marina/Domain/CustomerRegistration.cs
blob: 9e7b13b25ae42e67e5c75bcada87366ddb749914 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System;
using System.Collections.Generic;
using Marina.Domain.Interfaces;

namespace Marina.Domain {
	public class CustomerRegistration : IRegistration, IEquatable< CustomerRegistration > {
		private CustomerRegistration()
			: this( string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty ) {}

		public CustomerRegistration( string userName, string password, string firstName, string lastName, string phoneNumber,
		                             string city ) {
			_userName = userName;
			_password = password;
			_firstName = firstName;
			_lastName = lastName;
			_phoneNumber = phoneNumber;
			_city = city;
		}

		public string Username() {
			return _userName;
		}

		public string Password() {
			return _password;
		}

		public string FirstName() {
			return _firstName;
		}

		public string LastName() {
			return _lastName;
		}

		public string PhoneNumber() {
			return _phoneNumber;
		}

		public string City() {
			return _city;
		}

		public bool IsValid() {
			foreach ( IBusinessRule businessRule in AllRules( ) ) {
				if ( !businessRule.IsBroken( ) ) {
					return false;
				}
			}
			return true;
		}

		public IEnumerable< IBrokenRule > BrokenRules() {
			foreach ( IBusinessRule businessRule in AllRules( ) ) {
				if ( !businessRule.IsBroken( ) ) {
					yield return businessRule.Description( );
				}
			}
		}

		private IEnumerable< IBusinessRule > AllRules() {
			yield return new PasswordRule( _password );
			yield return new UserNameRule( _userName );
		}

		public bool Equals( CustomerRegistration customerRegistration ) {
			if ( customerRegistration == null ) {
				return false;
			}
			if ( !Equals( _userName, customerRegistration._userName ) ) {
				return false;
			}
			if ( !Equals( _password, customerRegistration._password ) ) {
				return false;
			}
			if ( !Equals( _firstName, customerRegistration._firstName ) ) {
				return false;
			}
			if ( !Equals( _lastName, customerRegistration._lastName ) ) {
				return false;
			}
			if ( !Equals( _phoneNumber, customerRegistration._phoneNumber ) ) {
				return false;
			}
			if ( !Equals( _city, customerRegistration._city ) ) {
				return false;
			}
			return true;
		}

		public override bool Equals( object obj ) {
			if ( ReferenceEquals( this, obj ) ) {
				return true;
			}
			return Equals( obj as CustomerRegistration );
		}

		public override int GetHashCode() {
			int result = _userName != null ? _userName.GetHashCode( ) : 0;
			result = 29*result + ( _password != null ? _password.GetHashCode( ) : 0 );
			result = 29*result + ( _firstName != null ? _firstName.GetHashCode( ) : 0 );
			result = 29*result + ( _lastName != null ? _lastName.GetHashCode( ) : 0 );
			result = 29*result + ( _phoneNumber != null ? _phoneNumber.GetHashCode( ) : 0 );
			result = 29*result + ( _city != null ? _city.GetHashCode( ) : 0 );
			return result;
		}

		private readonly string _userName;
		private readonly string _password;
		private readonly string _firstName;
		private readonly string _lastName;
		private readonly string _phoneNumber;
		private readonly string _city;

		private class PasswordRule : IBusinessRule {
			private readonly string _password;

			public PasswordRule( string password ) {
				_password = password;
			}

			public bool IsBroken() {
				return !string.IsNullOrEmpty( _password );
			}

			public IBrokenRule Description() {
				return new BrokenRule( "Password cannot be blank" );
			}
		}

		private class UserNameRule : IBusinessRule {
			private readonly string _userName;

			public UserNameRule( string userName ) {
				_userName = userName;
			}

			public bool IsBroken() {
				return !string.IsNullOrEmpty( _userName );
			}

			public IBrokenRule Description() {
				return new BrokenRule( "Username cannot be blank" );
			}
		}

		public static IRegistration BlankRegistration() {
			return new CustomerRegistration( );
		}
	}
}