public async Task SaveShouldUpdateGivenExistingEntry()
        {
            using var scope = ServiceProvider.CreateScope();
            var sut        = scope.ServiceProvider.GetRequiredService <ILocalPhoneService>();
            var context    = scope.ServiceProvider.GetRequiredService <ApplicationContext>();
            var unitOfWork = scope.ServiceProvider.GetRequiredService <IUnitOfWork>();

            var(address, phone) = await SeedDatabaseFixture.AddDummyPhoneAndAddressAsync(context);

            var newAddress = await SeedDatabaseFixture.AddDummyAddressAsync(context);

            const string newNumber   = "4578-8795";
            const string newAreaCode = "84";
            var          phoneToEdit = new LocalPhone
            {
                Id       = phone.Id,
                Uuid     = phone.Uuid,
                Number   = newNumber,
                AreaCode = newAreaCode
            };

            var result = await sut.SaveAsync(phoneToEdit, newAddress.Uuid);

            await unitOfWork.SaveChangesAsync();

            var newPhone = await context.LocalPhones.Include(x => x.PhoneAddress).FirstAsync(x => x.Uuid == phoneToEdit.Uuid);

            Assert.True(result.IsSuccessful);
            Assert.Equal(phone.Uuid, newPhone.Uuid);
            Assert.Equal(phone.Id, newPhone.Id);
            Assert.Equal(newAddress.Uuid, newPhone.PhoneAddress.Uuid);
            Assert.NotEqual(address.Uuid, newPhone.PhoneAddress.Uuid);
            Assert.Equal(newNumber, newPhone.Number);
            Assert.Equal(newAreaCode, newPhone.AreaCode);
        }
        public async Task <IServiceResult> SaveAsync(LocalPhone phone, Guid addressUuid)
        {
            try
            {
                if (!await _addressRepository.CheckExistenceAsync(addressUuid))
                {
                    return(new NotFoundResult());
                }
                var address = await _addressRepository.GetAsync(addressUuid);

                phone.PhoneAddressId = address.Id;
                var phoneUuid = await _localPhoneRepository.SaveAsync(phone);

                if (phoneUuid == default)
                {
                    return(new NotFoundResult());
                }
                return(new SuccessResult <Guid>(phoneUuid));
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Error while saving local phone");
                return(new FailResult());
            }
        }
Ejemplo n.º 3
0
        public GeneralResponse AddLocalPhone(IEnumerable <AddLocalPhoneRequest> requests, Guid CreateEmployeeID)
        {
            GeneralResponse response = new GeneralResponse();

            try
            {
                foreach (var request in requests)
                {
                    Query     query = new Query();
                    Criterion crt   = new Criterion("LocalPhoneNumber", request.LocalPhoneNumber, CriteriaOperator.Equal);
                    query.Add(crt);
                    Response <LocalPhone> localPhones = _localPhoneRepository.FindByQuery(query);
                    if (localPhones.data.Count() > 0)
                    {
                        response.ErrorMessages.Add("این شمارهداخلی قبلا ثبت شده است");
                        return(response);
                    }

                    LocalPhone localPhone = new LocalPhone();
                    localPhone.ID               = Guid.NewGuid();
                    localPhone.CreateDate       = PersianDateTime.Now;
                    localPhone.CreateEmployee   = _employeeRepository.FindBy(CreateEmployeeID);
                    localPhone.LocalPhoneNumber = request.LocalPhoneNumber;
                    localPhone.OwnerEmployee    = _employeeRepository.FindBy(request.OwnerEmployeeID);
                    localPhone.RowVersion       = 1;

                    #region Validation

                    if (localPhone.GetBrokenRules().Count() > 0)
                    {
                        foreach (BusinessRule businessRule in localPhone.GetBrokenRules())
                        {
                            response.ErrorMessages.Add(businessRule.Rule);
                        }

                        return(response);
                    }

                    #endregion

                    _localPhoneRepository.Add(localPhone);
                }


                _uow.Commit();
            }
            catch (Exception ex)
            {
                response.ErrorMessages.Add(ex.Message);
            }

            return(response);
        }
Ejemplo n.º 4
0
        public GeneralResponse EditLocalPhone(IEnumerable <EditLocalPhoneRequest> requests, Guid ModifiedEmployeeID)
        {
            GeneralResponse response = new GeneralResponse();



            try
            {
                foreach (var request in requests)
                {
                    LocalPhone localPhone = _localPhoneRepository.FindBy(request.ID);
                    localPhone.ModifiedDate     = PersianDateTime.Now;
                    localPhone.ModifiedEmployee = _employeeRepository.FindBy(ModifiedEmployeeID);
                    if (request.LocalPhoneNumber != null)
                    {
                        localPhone.LocalPhoneNumber = request.LocalPhoneNumber;
                    }

                    if (localPhone.RowVersion != request.RowVersion)
                    {
                        response.ErrorMessages.Add("EditConcurrencyKey");
                        return(response);
                    }
                    else
                    {
                        localPhone.RowVersion += 1;
                    }

                    if (localPhone.GetBrokenRules().Count() > 0)
                    {
                        foreach (BusinessRule businessRule in localPhone.GetBrokenRules())
                        {
                            response.ErrorMessages.Add(businessRule.Rule);
                        }

                        return(response);
                    }

                    _localPhoneRepository.Save(localPhone);
                }
                _uow.Commit();

                ////response.success = true;
            }
            catch (Exception ex)
            {
                response.ErrorMessages.Add(ex.Message);
            }
            return(response);
        }
        public async Task SaveShouldReturnNotFoundResultGivenNonExistingPhoneUuid()
        {
            using var scope = ServiceProvider.CreateScope();
            var sut     = scope.ServiceProvider.GetRequiredService <ILocalPhoneService>();
            var context = scope.ServiceProvider.GetRequiredService <ApplicationContext>();

            var address = await SeedDatabaseFixture.AddDummyAddressAsync(context);

            var dummyPhone = new LocalPhone
            {
                Uuid = Guid.NewGuid()
            };

            var result = await sut.SaveAsync(dummyPhone, address.Uuid);

            Assert.IsType <NotFoundResult>(result);
        }
        public async Task SaveShouldReturnNotFoundResultGivenNonExistingAddressUuid()
        {
            using var scope = ServiceProvider.CreateScope();
            var sut     = scope.ServiceProvider.GetRequiredService <ILocalPhoneService>();
            var context = scope.ServiceProvider.GetRequiredService <ApplicationContext>();

            var(_, phone) = await SeedDatabaseFixture.AddDummyPhoneAndAddressAsync(context);

            var phoneToEdit = new LocalPhone
            {
                Id       = phone.Id,
                Uuid     = phone.Uuid,
                Number   = "4578-8795",
                AreaCode = "84"
            };

            var result = await sut.SaveAsync(phoneToEdit, Guid.NewGuid());

            Assert.IsType <NotFoundResult>(result);
        }
Ejemplo n.º 7
0
        public GetGeneralResponse <LocalPhoneView> GetLocalPhone(GetRequest request)
        {
            GetGeneralResponse <LocalPhoneView> response = new GetGeneralResponse <LocalPhoneView>();

            try
            {
                LocalPhone     localPhone     = new LocalPhone();
                LocalPhoneView localPhoneView = localPhone.ConvertToLocalPhoneView();

                localPhone = _localPhoneRepository.FindBy(request.ID);
                if (localPhone != null)
                {
                    localPhoneView = localPhone.ConvertToLocalPhoneView();
                }

                response.data = localPhoneView;
            }
            catch (Exception ex)
            {
            }

            return(response);
        }
Ejemplo n.º 8
0
 public static LocalPhoneView ConvertToLocalPhoneView(this LocalPhone localPhone)
 {
     return(Mapper.Map <LocalPhone, LocalPhoneView>(localPhone));
 }