-
Notifications
You must be signed in to change notification settings - Fork 0
/
CreateWithDocument_Service.cs
128 lines (104 loc) · 5.03 KB
/
CreateWithDocument_Service.cs
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
namespace PLC.Services
{
public class ClientService : BaseComponentService<IClientBLL, Client, int, ClientDto>, IClientService
{
private const string DOCUMENT_TYPE_MISCELLANEOUS = "Miscellaneous";
private const string DOCUMENT_CATEGORY_CLIENTS = "Clients";
private const string ROLE_CLIENT_CONSULTANT = "Client Consultant";
private const string ROLE_CLIENT_USER = "Client User";
private const string ROLE_CLIENT_MANAGER = "Client Manager";
#region Constructor
private readonly IClientBLL _clientBLL;
private readonly INotificationBLL _notificationBLL;
private readonly ISignalRConnectionService _signalRService;
private readonly IRoleBLL _roleBLL;
private readonly IDocumentTypeBLL _documentTypeBLL;
private readonly IAgreementBLL _agreementBLL;
private readonly IClientAgreementBLL _clientAgreementBLL;
public ClientService(IPLCUnitOfWork unitOfWork, IUserResolverService userResolverService,
IClientBLL clientBLL, INotificationBLL notificationBLL, ISignalRConnectionService signalRService, IRoleBLL roleBLL,
IActionItemBLL actionItemBLL, IEntityTypeBLL entityTypeBLL, IDocumentBLL documentBLL, ILocationBLL locationBLL,
ICommentBLL commentBLL, IContactBLL contactBLL, IUserBLL userBLL, IDocumentTypeBLL documentTypeBLL, IAgreementBLL agreementBLL, IClientAgreementBLL clientAgreementBLL)
: base(unitOfWork, userResolverService, clientBLL, actionItemBLL, entityTypeBLL, documentBLL, locationBLL, commentBLL, contactBLL, userBLL)
{
_clientBLL = clientBLL;
_notificationBLL = notificationBLL;
_signalRService = signalRService;
_roleBLL = roleBLL;
_documentTypeBLL = documentTypeBLL;
_agreementBLL = agreementBLL;
_clientAgreementBLL = clientAgreementBLL;
}
#endregion
#region Custom Code
public override async Task<int> CreateAsync(ClientDto dto)
{
if (dto == null)
{
throw new ArgumentNullException(nameof(dto), "ClientService>CreateAsync>dto");
}
Client model = BLL.MapToModel(dto);
Document logo = null;
if (dto.Logo != null)
{
logo = await CreateLogoDocument(dto);
}
Location primaryLocation = _locationBLL.Create(dto.PrimaryLocation);
Location billingLocation = _locationBLL.Create(dto.BillingLocation);
BLL.Create(model);
int result = await SaveChangesAsync(); //Saving changes to set location & document ids before creating the relationships
if (result <= 0)
{
return 0;
}
bool addedDocument = false;
if (logo != null)
{
dto.LogoId = model.LogoId = logo.Id; //needed to create the file
addedDocument = await AddDocumentRelationshipForLogo(logo, model.Id);
}
bool addedPrimary = await AddLocationToEntity(primaryLocation, true, billingLocation == null, model.Id);
bool addedBilling = await AddLocationToEntity(billingLocation, primaryLocation == null, true, model.Id);
if (addedDocument || addedPrimary || addedBilling)
{
result = await SaveChangesAsync();
}
return result > 0 ? model.Id : 0;
}
private async Task<bool> AddDocumentRelationshipForLogo(Document logo, int clientId)
{
if (logo == null)
{
return false;
}
logo.Relationships.Add(new DocumentRelationship
{
Document = logo,
EntityId = clientId.ToString(),
EntityTypeId = await _entityTypeBLL.GetIdByName(nameof(Client))
});
return true;
}
private async Task<Document> CreateLogoDocument(ClientDto clientDto)
{
DocumentType documentType = await GetUnitOfWork().DocumentTypes.GetQueryable().FirstAsync(x => x.Name == DOCUMENT_TYPE_MISCELLANEOUS && x.Category == DOCUMENT_CATEGORY_CLIENTS);
if (documentType == null)
{
throw new NullReferenceException("Document Type was not found");
}
clientDto.Logo.DocumentTypeId = documentType.Id;
return _documentBLL.Create(clientDto.Logo);
}
public async Task<bool> AddLocationToEntity(Location location, bool isPrimary, bool isBilling, UId entityId)
{
if (location == null)
{
return false;
}
int entityTypeId = await _entityTypeBLL.GetIdByName(type.Name);
await _locationBLL.AddLocationToEntity(location.Id, isPrimary, isBilling, entityId, entityTypeId);
return true;
}
#endregion
}
}