diff --git a/client/MAVN.Service.CustomerProfile.Client/Api/IPartnerContactApi.cs b/client/MAVN.Service.CustomerProfile.Client/Api/IPartnerContactApi.cs index 620b504..c9c49c6 100644 --- a/client/MAVN.Service.CustomerProfile.Client/Api/IPartnerContactApi.cs +++ b/client/MAVN.Service.CustomerProfile.Client/Api/IPartnerContactApi.cs @@ -1,4 +1,4 @@ -using System.Threading.Tasks; +using System.Threading.Tasks; using JetBrains.Annotations; using MAVN.Service.CustomerProfile.Client.Models; using MAVN.Service.CustomerProfile.Client.Models.Enums; @@ -41,20 +41,14 @@ public interface IPartnerContactApi /// The data with which the Partner contact needs to be created /// [Post("/api/partnerContacts")] - Task CreateIfNotExistAsync([Body] PartnerContactRequestModel partnerContactRequest); + Task CreateOrUpdateAsync([Body] PartnerContactRequestModel partnerContactRequest); - /// - /// Updates Partner contact. - /// - /// The Partner contact data. - [Put("/api/partnerContacts")] - Task UpdateAsync([Body] PartnerContactUpdateRequestModel partnerContactUpdate); /// /// Deletes/Archives a specific Partner contact /// /// The Location Id [Delete("/api/partnerContacts/{locationId}")] - Task DeleteAsync(string locationId); + Task DeleteIfExistAsync(string locationId); } } diff --git a/src/MAVN.Service.CustomerProfile.Domain/Repositories/IPartnerContactRepository.cs b/src/MAVN.Service.CustomerProfile.Domain/Repositories/IPartnerContactRepository.cs index aa1c6d1..b4febfd 100644 --- a/src/MAVN.Service.CustomerProfile.Domain/Repositories/IPartnerContactRepository.cs +++ b/src/MAVN.Service.CustomerProfile.Domain/Repositories/IPartnerContactRepository.cs @@ -1,4 +1,4 @@ -using System.Collections.Generic; +using System.Collections.Generic; using System.Threading.Tasks; using MAVN.Service.CustomerProfile.Domain.Enums; using MAVN.Service.CustomerProfile.Domain.Models; @@ -10,10 +10,9 @@ public interface IPartnerContactRepository Task GetByLocationIdAsync(string locationId); Task GetByEmailAsync(string email); Task GetByPhoneAsync(string phone); - Task DeleteAsync(string locationId); + Task DeleteIfExistsAsync(string locationId); Task> GetPaginatedAsync(int skip, int take); Task GetTotalAsync(); - Task CreateIfNotExistAsync(PartnerContactModel partnerContact); - Task UpdateAsync(string locationId, string firstName, string lastName, string phoneNumber, string email); + Task CreateOrUpdateAsync(PartnerContactModel partnerContact); } } diff --git a/src/MAVN.Service.CustomerProfile.Domain/Services/IPartnerContactService.cs b/src/MAVN.Service.CustomerProfile.Domain/Services/IPartnerContactService.cs index 7fc3ce3..adf59c6 100644 --- a/src/MAVN.Service.CustomerProfile.Domain/Services/IPartnerContactService.cs +++ b/src/MAVN.Service.CustomerProfile.Domain/Services/IPartnerContactService.cs @@ -1,4 +1,4 @@ -using System.Threading.Tasks; +using System.Threading.Tasks; using MAVN.Service.CustomerProfile.Domain.Enums; using MAVN.Service.CustomerProfile.Domain.Models; @@ -8,8 +8,7 @@ public interface IPartnerContactService { Task GetByLocationIdAsync(string locationId); Task GetPaginatedAsync(int currentPage, int pageSize); - Task CreateIfNotExistsAsync(PartnerContactModel partnerContact); - Task UpdateAsync(string modelLocationId, string modelFirstName, string modelLastName, string modelPhoneNumber, string modelEmail); - Task RemoveAsync(string locationId); + Task CreateOrUpdateAsync(PartnerContactModel partnerContact); + Task RemoveIfExistsAsync(string locationId); } } diff --git a/src/MAVN.Service.CustomerProfile.DomainServices/PartnerContactService.cs b/src/MAVN.Service.CustomerProfile.DomainServices/PartnerContactService.cs index 30a4f78..8d584ef 100644 --- a/src/MAVN.Service.CustomerProfile.DomainServices/PartnerContactService.cs +++ b/src/MAVN.Service.CustomerProfile.DomainServices/PartnerContactService.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Threading.Tasks; using Common.Log; using Lykke.Common.Log; @@ -63,46 +63,16 @@ public async Task GetPaginatedAsync(int currentPa }; } - public async Task CreateIfNotExistsAsync(PartnerContactModel partnerContact) + public async Task CreateOrUpdateAsync(PartnerContactModel partnerContact) { - var creationResult = await _partnerContactRepository.CreateIfNotExistAsync(partnerContact); + await _partnerContactRepository.CreateOrUpdateAsync(partnerContact); - if (creationResult == PartnerContactErrorCodes.PartnerContactAlreadyExists) - { - _log.Warning("Partner Contact already exists", context: partnerContact.LocationId); - return creationResult; - } - - _log.Info("Partner Contact is created", context: partnerContact.LocationId); - - return creationResult; + _log.Info("Partner Contact is created or updated", context: partnerContact.LocationId); } - public async Task UpdateAsync(string locationId, string firstName, string lastName, string phoneNumber, - string email) + public Task RemoveIfExistsAsync(string locationId) { - var result = await _partnerContactRepository.UpdateAsync(locationId, firstName, lastName, phoneNumber, email); - - if (result == PartnerContactErrorCodes.PartnerContactDoesNotExist) - { - _log.Warning("Partner Contact was not updated", context: locationId); - } - - return result; - } - - public async Task RemoveAsync(string locationId) - { - var deleted = await _partnerContactRepository.DeleteAsync(locationId); - - if (deleted) - { - _log.Info("Partner Contact was removed", context: locationId); - } - else - { - _log.Warning("Partner Contact was not removed", context: locationId); - } + return _partnerContactRepository.DeleteIfExistsAsync(locationId); } } } diff --git a/src/MAVN.Service.CustomerProfile.MsSqlRepositories/Repositories/PartnerContactRepository.cs b/src/MAVN.Service.CustomerProfile.MsSqlRepositories/Repositories/PartnerContactRepository.cs index 8ccdad5..e4f283d 100644 --- a/src/MAVN.Service.CustomerProfile.MsSqlRepositories/Repositories/PartnerContactRepository.cs +++ b/src/MAVN.Service.CustomerProfile.MsSqlRepositories/Repositories/PartnerContactRepository.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; @@ -104,7 +104,7 @@ public async Task GetByPhoneAsync(string phone) } } - public async Task DeleteAsync(string locationId) + public async Task DeleteIfExistsAsync(string locationId) { using (var context = _contextFactory.CreateDataContext()) { @@ -116,7 +116,7 @@ public async Task DeleteAsync(string locationId) .FirstOrDefaultAsync(c => c.LocationId == locationId); if (entity == null) - return false; + return; var archiveEntity = PartnerContactArchiveEntity.Create(entity); @@ -131,12 +131,9 @@ public async Task DeleteAsync(string locationId) catch (Exception e) { _log.Error(e, "Error occured while deleting partner contact ", $"locationId = {locationId}"); - return false; } } } - - return true; } public async Task> GetPaginatedAsync(int skip, int take) @@ -162,7 +159,7 @@ public async Task GetTotalAsync() } } - public async Task CreateIfNotExistAsync(PartnerContactModel partnerContact) + public async Task CreateOrUpdateAsync(PartnerContactModel partnerContact) { using (var context = _contextFactory.CreateDataContext()) { @@ -171,44 +168,27 @@ public async Task CreateIfNotExistAsync(PartnerContact if (existentPartnerContact != null) { - return PartnerContactErrorCodes.PartnerContactAlreadyExists; - } - - var entity = PartnerContactEntity.Create(partnerContact); - - entity = _encryptionService.Encrypt(entity); - - context.PartnerContacts.Add(entity); - - await context.SaveChangesAsync(); - - return PartnerContactErrorCodes.None; - } - } - - public async Task UpdateAsync(string locationId, string firstName, string lastName, string phoneNumber, string email) - { - using (var context = _contextFactory.CreateDataContext()) - { - var entity = await context.PartnerContacts.FirstOrDefaultAsync(o => o.LocationId == locationId); + existentPartnerContact = _encryptionService.Decrypt(existentPartnerContact); - if (entity == null) - return PartnerContactErrorCodes.PartnerContactDoesNotExist; + existentPartnerContact.FirstName = partnerContact.FirstName; + existentPartnerContact.LastName = partnerContact.LastName; + existentPartnerContact.PhoneNumber = partnerContact.PhoneNumber; + existentPartnerContact.Email = partnerContact.Email; - entity = _encryptionService.Decrypt(entity); + existentPartnerContact = _encryptionService.Encrypt(existentPartnerContact); - entity.FirstName = firstName; - entity.LastName = lastName; - entity.PhoneNumber = phoneNumber; - entity.Email = email; + context.PartnerContacts.Update(existentPartnerContact); + } + else + { + var entity = PartnerContactEntity.Create(partnerContact); - entity = _encryptionService.Encrypt(entity); + entity = _encryptionService.Encrypt(entity); - context.PartnerContacts.Update(entity); + context.PartnerContacts.Add(entity); + } await context.SaveChangesAsync(); - - return PartnerContactErrorCodes.None; } } diff --git a/src/MAVN.Service.CustomerProfile/Controllers/PartnerContactsController.cs b/src/MAVN.Service.CustomerProfile/Controllers/PartnerContactsController.cs index 24cd049..3dc2853 100644 --- a/src/MAVN.Service.CustomerProfile/Controllers/PartnerContactsController.cs +++ b/src/MAVN.Service.CustomerProfile/Controllers/PartnerContactsController.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Net; @@ -87,33 +87,10 @@ public async Task GetPartnerContactsPaginatedA /// /// [HttpPost] - [ProducesResponseType(typeof(PartnerContactErrorCodes), (int)HttpStatusCode.OK)] - public async Task CreateIfNotExistAsync([FromBody]PartnerContactRequestModel partnerContactRequest) + [ProducesResponseType((int)HttpStatusCode.NoContent)] + public async Task CreateOrUpdateAsync([FromBody]PartnerContactRequestModel partnerContactRequest) { - var result = await _partnerContactService.CreateIfNotExistsAsync(_mapper.Map(partnerContactRequest)); - - return _mapper.Map(result); - } - - /// - /// Updates Partner contact profile. - /// - /// - /// - /// Error codes: - /// - **PartnerContactDoesNotExist** - /// - /// - /// 200 - Partner contact profile successfully updated. - /// 400 - if an invalid input data was provided - /// - [HttpPut] - [ProducesResponseType(typeof(PartnerContactErrorCodes), (int)HttpStatusCode.OK)] - public async Task UpdateAsync([FromBody] PartnerContactUpdateRequestModel model) - { - var result = await _partnerContactService.UpdateAsync(model.LocationId, model.FirstName, model.LastName, - model.PhoneNumber, model.Email); - return _mapper.Map(result); + await _partnerContactService.CreateOrUpdateAsync(_mapper.Map(partnerContactRequest)); } /// @@ -122,12 +99,12 @@ public async Task UpdateAsync([FromBody] PartnerContac [HttpDelete("{locationId}")] [ProducesResponseType(typeof(void), (int)HttpStatusCode.OK)] [ProducesResponseType(typeof(ErrorResponse), (int)HttpStatusCode.BadRequest)] - public Task DeleteAsync([Required][FromRoute] string locationId) + public Task DeleteIfExistAsync([Required][FromRoute] string locationId) { if (string.IsNullOrWhiteSpace(locationId)) throw new BadRequestException($"{nameof(locationId)} can't be empty"); - return _partnerContactService.RemoveAsync(locationId); + return _partnerContactService.RemoveIfExistsAsync(locationId); } private string GetApiKeyName() diff --git a/tests/MAVN.Service.CustomerProfile.Tests/ProfileContactServiceTests.cs b/tests/MAVN.Service.CustomerProfile.Tests/ProfileContactServiceTests.cs index 14a58d4..a059ba9 100644 --- a/tests/MAVN.Service.CustomerProfile.Tests/ProfileContactServiceTests.cs +++ b/tests/MAVN.Service.CustomerProfile.Tests/ProfileContactServiceTests.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; @@ -15,59 +15,6 @@ namespace MAVN.Service.CustomerProfile.Tests { public class ProfileContactServiceTests { - [Fact] - public async Task TryingToCreateProfileContact_EverythingValid_SuccessfullyCreated() - { - var partnerContactRepository = new Mock(); - var partnerContactResult = PartnerContactErrorCodes.None; - partnerContactRepository - .Setup(x => x.CreateIfNotExistAsync(It.IsAny())) - .ReturnsAsync(partnerContactResult) - .Verifiable(); - - PartnerContactService partnerContactService; - - using (var logFactory = LogFactory.Create().AddUnbufferedConsole()) - { - partnerContactService = new PartnerContactService( - partnerContactRepository.Object, - logFactory); - } - - var actual = await partnerContactService.CreateIfNotExistsAsync(new PartnerContactModel - { - - }); - - Assert.Equal(partnerContactResult, actual); - } - - [Fact] - public async Task TryingToCreateProfileContact_ProfileContactAlreadyExists_ErrorCodeIsReturned() - { - var partnerContactRepository = new Mock(); - var partnerContactResult = PartnerContactErrorCodes.PartnerContactAlreadyExists; - - partnerContactRepository - .Setup(x => x.CreateIfNotExistAsync(It.IsAny())) - .ReturnsAsync(partnerContactResult); - - PartnerContactService partnerContactService; - - using (var logFactory = LogFactory.Create().AddUnbufferedConsole()) - { - partnerContactService = new PartnerContactService( - partnerContactRepository.Object, - logFactory); - } - - var actual = await partnerContactService.CreateIfNotExistsAsync(new PartnerContactModel - { - }); - - Assert.Equal(partnerContactResult, actual); - } - [Fact] public async Task TryingToGetProfileContactByLocationId_ValidLocationId_SuccessfullyReturned() { @@ -121,8 +68,7 @@ public async Task TryingToDeleteProfileContactByLocationId_ValidLocationId_Succe { var partnerContactRepository = new Mock(); partnerContactRepository - .Setup(x => x.DeleteAsync(It.IsAny())) - .ReturnsAsync(true) + .Setup(x => x.DeleteIfExistsAsync(It.IsAny())) .Verifiable(); PartnerContactService partnerContactService; @@ -134,7 +80,7 @@ public async Task TryingToDeleteProfileContactByLocationId_ValidLocationId_Succe logFactory); } - await partnerContactService.RemoveAsync("testContactId"); + await partnerContactService.RemoveIfExistsAsync("testContactId"); partnerContactRepository.Verify(); }