main
 1using System.Linq;
 2using gorilla.commons.utility;
 3using MoMoney.Domain.Accounting;
 4using MoMoney.Domain.repositories;
 5using MoMoney.DTO;
 6using MoMoney.Service.Contracts.Application;
 7
 8namespace MoMoney.Service.Application
 9{
10    public class RegisterNewCompanyCommand : IRegisterNewCompanyCommand
11    {
12        readonly ICompanyFactory factory;
13        readonly Notification notification;
14        readonly ICompanyRepository companies;
15
16        public RegisterNewCompanyCommand(ICompanyFactory factory, Notification notification,
17                                         ICompanyRepository companies)
18        {
19            this.factory = factory;
20            this.notification = notification;
21            this.companies = companies;
22        }
23
24        public void run(RegisterNewCompany item)
25        {
26            if (company_has_already_been_registered(item))
27                notification.notify(create_error_message_from(item));
28            else
29                factory.create().change_name_to(item.company_name);
30        }
31
32        bool company_has_already_been_registered(RegisterNewCompany dto)
33        {
34            return companies.all().Count(x => x.name.is_equal_to_ignoring_case(dto.company_name)) > 0;
35        }
36
37        string create_error_message_from(RegisterNewCompany dto)
38        {
39            return "A Company named {0}, has already been submitted!".formatted_using(dto.company_name);
40        }
41    }
42}