public async Task <IActionResult> PostAdvertiserAsync([FromBody] CustomerForCreationUiModel customerForCreationUiModel) { var userAudit = await _inquiryUserProcessor.GetUserByLoginAsync(GetEmailFromClaims()); if (userAudit == null) { return(BadRequest("AUDIT_USER_NOT_EXIST")); } if (await _inquiryCustomerProcessor.SearchIfAnyPersonByEmailOrLoginExistsAsync(customerForCreationUiModel.CustomerUserLogin)) { Log.Error( $"--Method:PostAdvertiserAsync -- Message:ADVERTISER_LOGIN_OR_EMAIL_ALREADY_EXIST-- Datetime:{DateTime.Now} " + $"-- UserInfo:{customerForCreationUiModel.CustomerUserLogin}, "); return(BadRequest(new { errorMessage = "ADVERTISER_LOGIN_OR_EMAIL_ALREADY_EXIST" })); } var newCreatedAdvertiser = await _createCustomerProcessor.CreateCustomerAsync(userAudit.Id, customerForCreationUiModel, true); switch (newCreatedAdvertiser.Message) { case ("SUCCESS_CREATION"): { Log.Information( $"--Method:PostAdvertiserAsync -- Message:ADVERTISER_CREATION_SUCCESSFULLY -- " + $"Datetime:{DateTime.Now} -- AdvertiserInfo:{customerForCreationUiModel.CustomerVat + customerForCreationUiModel.CustomerEmail}"); return(Created(nameof(PostAdvertiserAsync), newCreatedAdvertiser)); } case ("ERROR_ALREADY_EXISTS"): { Log.Error( $"--Method:PostAdvertiserAsync -- Message:ERROR_ADVERTISER_ALREADY_EXISTS -- " + $"Datetime:{DateTime.Now} -- AdvertiserInfo:{customerForCreationUiModel.CustomerVat + customerForCreationUiModel.CustomerEmail}"); return(BadRequest(new { errorMessage = "ADVERTISER_ALREADY_EXISTS" })); } case ("ERROR_NOT_MADE_PERSISTENT"): { Log.Error( $"--Method:PostAdvertiserAsync -- Message:ADVERTISER_NOT_MADE_PERSISTENT -- " + $"Datetime:{DateTime.Now} -- AdvertiserInfo:{customerForCreationUiModel.CustomerVat + customerForCreationUiModel.CustomerEmail}"); return(BadRequest(new { errorMessage = "ERROR_CREATION_NEW_ADVERTISER" })); } case ("UNKNOWN_ERROR"): { Log.Error( $"--Method:PostAdvertiserAsync -- Message:ERROR_CREATION_NEW_ADVERTISER -- " + $"Datetime:{DateTime.Now} -- AdvertiserInfo:{customerForCreationUiModel.CustomerVat + customerForCreationUiModel.CustomerEmail}"); return(BadRequest(new { errorMessage = "ERROR_CREATION_NEW_ADVERTISER" })); } } return(NotFound()); }
public async Task <IActionResult> PostAdvertisedAsync([FromBody] CustomerForCreationUiModel customerForCreationUiModel) { var userAudit = await _inquiryUserProcessor.GetUserByLoginAsync(GetEmailFromClaims()); if (userAudit == null) { return(BadRequest("AUDIT_USER_NOT_EXIST")); } var newCreatedAdvertised = await _createCustomerProcessor.CreateCustomerAsync(userAudit.Id, customerForCreationUiModel, false); switch (newCreatedAdvertised.Message) { case ("SUCCESS_CREATION"): { Log.Information( $"--Method:PostAdvertisedAsync -- Message:ADVERTISED_CREATION_SUCCESSFULLY -- " + $"Datetime:{DateTime.Now} -- AdvertisedInfo:{customerForCreationUiModel.CustomerVat + customerForCreationUiModel.CustomerEmail}"); return(Created(nameof(PostAdvertisedAsync), newCreatedAdvertised)); } case ("ERROR_ALREADY_EXISTS"): { Log.Error( $"--Method:PostAdvertisedAsync -- Message:ERROR_ADVERTISED_ALREADY_EXISTS -- " + $"Datetime:{DateTime.Now} -- AdvertisedInfo:{customerForCreationUiModel.CustomerVat + customerForCreationUiModel.CustomerEmail}"); return(BadRequest(new { errorMessage = "CATEGORY_ALREADY_EXISTS" })); } case ("ERROR_NOT_MADE_PERSISTENT"): { Log.Error( $"--Method:PostAdvertisedAsync -- Message:ERROR_ADVERTISED_NOT_MADE_PERSISTENT -- " + $"Datetime:{DateTime.Now} -- AdvertisedInfo:{customerForCreationUiModel.CustomerVat + customerForCreationUiModel.CustomerEmail}"); return(BadRequest(new { errorMessage = "ERROR_CREATION_NEW_CATEGORY" })); } case ("UNKNOWN_ERROR"): { Log.Error( $"--Method:PostAdvertisedAsync -- Message:ERROR_CREATION_NEW_ADVERTISED -- " + $"Datetime:{DateTime.Now} -- AdvertisedInfo:{customerForCreationUiModel.CustomerVat + customerForCreationUiModel.CustomerEmail}"); return(BadRequest(new { errorMessage = "ERROR_CREATION_NEW_ADVERTISED" })); } } return(NotFound()); }
public Task <CustomerUiModel> CreateCustomerAsync(Guid accountIdToCreateThisCustomer, CustomerForCreationUiModel newCustomerUiModel, bool isAdvertiser) { var response = new CustomerUiModel() { Message = "START_CREATION" }; if (newCustomerUiModel == null) { response.Message = "ERROR_INVALID_CUSTOMER_MODEL"; return(Task.Run(() => response)); } try { var userToBeCreated = new User(); userToBeCreated.InjectWithInitialAttributes(newCustomerUiModel.CustomerUserLogin, newCustomerUiModel.CustomerUserPassword); userToBeCreated.InjectWithAuditCreation(accountIdToCreateThisCustomer); var roleAdminToBeInjected = _roleRepository.FindRoleByName("ADMIN"); if (roleAdminToBeInjected == null) { throw new RoleDoesNotExistException("ADMIN"); } var roleUserToBeInjected = _roleRepository.FindRoleByName("USER"); if (roleUserToBeInjected == null) { throw new RoleDoesNotExistException("USER"); } var userRoleAdminToBeInjected = new UserRole(); userRoleAdminToBeInjected.InjectWithRole(roleAdminToBeInjected); userRoleAdminToBeInjected.InjectWithAuditCreation(accountIdToCreateThisCustomer); userToBeCreated.InjectWithUserRole(userRoleAdminToBeInjected); var userRoleUserToBeInjected = new UserRole(); userRoleUserToBeInjected.InjectWithRole(roleUserToBeInjected); userRoleUserToBeInjected.InjectWithAuditCreation(accountIdToCreateThisCustomer); userToBeCreated.InjectWithUserRole(userRoleUserToBeInjected); var categoryToBeInjected = _categoryRepository.FindBy(newCustomerUiModel.CustomerCategoryId); if (categoryToBeInjected == null) { throw new CategoryDoesNotExistException(newCustomerUiModel.CustomerCategoryId); } Customer customerToBeInjected = isAdvertiser ? (Customer) new Advertiser() : new Advertised(); customerToBeInjected = isAdvertiser ? (Customer)_autoMapper.Map <Advertiser>(newCustomerUiModel) : _autoMapper.Map <Advertised>(newCustomerUiModel); customerToBeInjected.InjectWithAuditCreation(accountIdToCreateThisCustomer); customerToBeInjected.InjectWithUser(userToBeCreated); customerToBeInjected.InjectWithCategory(categoryToBeInjected); ThrowExcIfCustomerCannotBeCreated(customerToBeInjected); ThrowExcIfThisCustomerAlreadyExist(customerToBeInjected); Log.Debug( $"Create Customer: {newCustomerUiModel.CustomerVat}" + "--CreateCustomer-- @NotComplete@ [CreateCustomerProcessor]. " + "Message: Just Before MakeItPersistence"); MakeCustomerPersistent(customerToBeInjected); Log.Debug( $"Create Customer: {newCustomerUiModel.CustomerVat}" + "--CreateCustomer-- @Complete@ [CreateCustomerProcessor]. " + "Message: Just After MakeItPersistence"); //Todo: Azure Action Send Email for Verification response = ThrowExcIfCustomerWasNotBeMadePersistent(customerToBeInjected); response.Message = "SUCCESS_CREATION"; } catch (InvalidCustomerException e) { response.Message = "ERROR_INVALID_CUSTOMER_MODEL"; Log.Error( $"Create Customer: {newCustomerUiModel.CustomerVat}" + "--CreateCustomer-- @NotComplete@ [CreateCustomerProcessor]. " + $"Broken rules: {e.BrokenRules}"); } catch (CustomerAlreadyExistsException ex) { response.Message = "ERROR_CUSTOMER_ALREADY_EXISTS"; Log.Error( $"Create Customer: {newCustomerUiModel.CustomerVat}" + "--CreateCustomer-- @fail@ [CreateCustomerProcessor]. " + $"@innerfault:{ex?.Message} and {ex?.InnerException}"); } catch (CustomerDoesNotExistAfterMadePersistentException exx) { response.Message = "ERROR_CUSTOMER_NOT_MADE_PERSISTENT"; Log.Error( $"Create Customer: {newCustomerUiModel.CustomerVat}" + "--CreateCustomer-- @fail@ [CreateCustomerProcessor]." + $" @innerfault:{exx?.Message} and {exx?.InnerException}"); } catch (Exception exxx) { response.Message = "UNKNOWN_ERROR"; Log.Error( $"Create Customer: {newCustomerUiModel.CustomerVat}" + $"--CreateCustomer-- @fail@ [CreateCustomerProcessor]. " + $"@innerfault:{exxx.Message} and {exxx.InnerException}"); } return(Task.Run(() => response)); }