PageRenderTime 44ms CodeModel.GetById 40ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/source/app/NOS.Registration/AutoRegistrationPresenter.cs

http://github.com/agross/netopenspace
C# | 76 lines | 67 code | 9 blank | 0 comment | 5 complexity | abfe02b345d5f3ac30a357dc84a26cac MD5 | raw file
 1using System;
 2using System.Web.UI.WebControls;
 3
 4namespace NOS.Registration
 5{
 6	public class AutoRegistrationPresenter
 7	{
 8		readonly ILogger _logger;
 9		readonly IRegistrationRepository _repository;
10		readonly IAutoRegistrationView _view;
11
12		public AutoRegistrationPresenter(IAutoRegistrationView view)
13			: this(view, new RegistrationRepository(), new DefaultLogger())
14		{
15		}
16
17		public AutoRegistrationPresenter(IAutoRegistrationView view, IRegistrationRepository repository, ILogger logger)
18		{
19			_view = view;
20			_repository = repository;
21			_logger = logger;
22
23			view.UserCreated += View_UserCreated;
24			view.ValidateInvoiceAddress += ValidateInvoiceAddress;
25		}
26
27		void View_UserCreated(object sender, EventArgs e)
28		{
29			if (!_view.AutoRegisterUser)
30			{
31				_logger.Info("User opted-out of auto registration", _view.UserName);
32				return;
33			}
34
35			try
36			{
37				var user = new User(_view.UserName)
38				           {
39				           	Data =
40				           		{
41				           			Xing = _view.Xing,
42				           			Twitter = _view.Twitter,
43				           			Name = _view.Name,
44				           			Blog = _view.Blog,
45				           			Email = _view.Email,
46				           			Picture = _view.Picture,
47				           			Sponsoring = _view.Sponsoring,
48				           			InvoiceAddress = _view.InvoiceAddress,
49				           			RegisteredAt = DateTime.Now
50				           		}
51				           };
52
53				_repository.Save(user);
54				_logger.Info("Saved registration data", _view.UserName);
55			}
56			catch (Exception ex)
57			{
58				_logger.Error(String.Format("Saving registration data failed: {0}", ex), _view.UserName);
59			}
60		}
61
62		void ValidateInvoiceAddress(object sender, ServerValidateEventArgs e)
63		{
64			if (!_view.AutoRegisterUser)
65			{
66				e.IsValid = true;
67				return;
68			}
69
70			if (_view.Sponsoring > 0)
71			{
72				e.IsValid = _view.InvoiceAddress != null && _view.InvoiceAddress.Length > 0;
73			}
74		}
75	}
76}