public async Task <IActionResult> PutCountry(int id, Country country)
        {
            var _countriesService = new CountriesService(_context);

            if (id != country.Id)
            {
                return(BadRequest());
            }
            var CountryValidater = new CountryValidator();
            var resultValidation = CountryValidater.Validate(country);

            if (!resultValidation.IsValid)
            {
                return(BadRequest(resultValidation.Errors));
            }

            var result = await _countriesService.PutCountry(id, country);

            if (result)
            {
                return(Ok());
            }

            return(NoContent());
        }
示例#2
0
        protected override System.ComponentModel.DataAnnotations.ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (value == null)
            {
                return(System.ComponentModel.DataAnnotations.ValidationResult.Success);
            }

            if (!(value is string ssn))
            {
                return(base.IsValid(value, validationContext));
            }

            CountryValidator taxValidator = new CountryValidator();
            ValidationResult result       = taxValidator.ValidateNationalIdentityCode(ssn, CountryCode);

            if (result.IsValid)
            {
                return(System.ComponentModel.DataAnnotations.ValidationResult.Success);
            }

            validationContext.Items.Add("Error", result.ErrorMessage);

            IEnumerable <string> memberNames = null;

            if (validationContext.MemberName != null)
            {
                memberNames = new[] { validationContext.MemberName };
            }

            return(new System.ComponentModel.DataAnnotations.ValidationResult(FormatErrorMessage(validationContext.DisplayName), memberNames));
        }
示例#3
0
        public async Task <Result <CountryDto> > AddCountryAsync(string name)
        {
            var country = new Country {
                Name = name
            };

            var result = new CountryValidator().Validate(country);

            if (!result.IsValid)
            {
                return(new Result <CountryDto>(ResultType.Invalid, string.Join("; ", result.Errors)));
            }

            var duplicates = await _unitOfWork.CountryRepository
                             .GetAsync(c => c.Name == name)
                             .ConfigureAwait(false);

            if (duplicates.Any())
            {
                return(new Result <CountryDto>(ResultType.Invalid,
                                               "The specified country is already in the list!"));
            }

            _unitOfWork.CountryRepository.Add(country);
            await _unitOfWork.SaveAsync().ConfigureAwait(false);

            return(new Result <CountryDto>(Auto.Mapper.Map <CountryDto>(country)));
        }
示例#4
0
        public async Task <Result <CountryDto> > UpdateCountryAsync(int countryId, string name)
        {
            var country = new Country {
                Name = name
            };

            var result = new CountryValidator().Validate(country);

            if (!result.IsValid)
            {
                return(new Result <CountryDto>(ResultType.Invalid, string.Join("; ", result.Errors)));
            }

            var countries = await _unitOfWork.CountryRepository
                            .GetAsync(c => c.Id == countryId)
                            .ConfigureAwait(false);

            country = countries.FirstOrDefault();

            if (country == null)
            {
                return(new Result <CountryDto>(ResultType.Invalid, "Could not find country!"));
            }

            country.Name = name;
            _unitOfWork.CountryRepository.Edit(country);
            await _unitOfWork.SaveAsync().ConfigureAwait(false);

            return(new Result <CountryDto>(Auto.Mapper.Map <CountryDto>(country)));
        }
示例#5
0
        public OperationResult <bool> AddCustomer(Customers customer)
        {
            var result = new OperationResult <bool>();

            try
            {
                if (!CountryValidator.Validate(customer.Country))
                {
                    result.Data             = false;
                    result.Status           = eOperationStatus.GeneralError;
                    result.ExceptionMessage = "Country code does not exists";
                }
                else if (_customerRepo.Get(customer.CustomerId) != null)
                {
                    result.Data             = false;
                    result.Status           = eOperationStatus.GeneralError;
                    result.ExceptionMessage = $"Customer: {customer.CustomerId} already exists";
                }
                else
                {
                    _customerRepo.Add(customer);
                    var saveResult = _context.SaveChanges();
                    result.Data   = true;
                    result.Status = eOperationStatus.Success;
                }
            }
            catch (Exception ex)
            {
                result.Status           = eOperationStatus.GeneralError;
                result.ExceptionMessage = ex.Message;
                result.Data             = false;
            }

            return(result);
        }
示例#6
0
        public ActionResult Edit([Bind(Include = "Id,Name")] Country country)
        {
            CountryValidator validar = new CountryValidator();
            ValidationResult Result  = validar.Validate(country);

            if (!Result.IsValid)
            {
                foreach (ValidationFailure item in Result.Errors)
                {
                    ModelState.AddModelError(item.PropertyName, item.ErrorMessage);
                }
                return(View(country));
            }
            else
            {
                var objCountry = countryService.Actualizar(country);
                return(View(country));
            }
            //if (ModelState.IsValid)
            //{
            //    db.Entry(country).State = EntityState.Modified;
            //    db.SaveChanges();
            //    return RedirectToAction("Index");
            //}
            //return View(country);
        }
示例#7
0
        public CountryValidatorTests()
        {
            context   = new TestingContext();
            validator = new CountryValidator(new UnitOfWork(context));

            context.DropData();
            SetUpData();
        }
        public void IsValidCountryName()
        {
            ICountryValidator validator = new CountryValidator();
            string            name      = "US";
            bool result = validator.IsCountryValid(name);

            Assert.True(result);
        }
        public void IsValidCountryNameInvalidInput()
        {
            ICountryValidator validator = new CountryValidator();
            string            name      = "USD";
            bool result = validator.IsCountryValid(name);

            Assert.False(result);
        }
示例#10
0
        private static void ValidateCountry(string country)
        {
            var validator = new CountryValidator();
            var result    = validator.Validate(country);

            if (!result.IsValid)
            {
                throw new IdentityDomainException(nameof(country));
            }
        }
示例#11
0
        public OperationResult <bool> AddAddress(string id, Addresses address)
        {
            var result = new OperationResult <bool>();

            try
            {
                var customer = _customerRepo.Get(id);
                if (customer != null)
                {
                    if (!CountryValidator.Validate(address.Country))
                    {
                        result.Data             = false;
                        result.Status           = eOperationStatus.GeneralError;
                        result.ExceptionMessage = "Country code does not exists";
                    }
                    else if (_addrRepo.Get(address.AddressId) != null)
                    {
                        result.Data             = false;
                        result.Status           = eOperationStatus.GeneralError;
                        result.ExceptionMessage = $"Address:{address.AddressId} already exists";
                    }
                    else
                    {
                        address.CustomerId = id;
                        _addrRepo.Add(address);

                        var saveResult = _context.SaveChanges();
                        if (saveResult > 0)
                        {
                            result.Data   = true;
                            result.Status = eOperationStatus.Success;
                        }
                        else
                        {
                            result.Data   = false;
                            result.Status = eOperationStatus.GeneralError;
                        }
                    }
                }
                else
                {
                    result.Data             = false;
                    result.Status           = eOperationStatus.NotFound;
                    result.ExceptionMessage = "Customer not found";
                }
            }
            catch (Exception ex)
            {
                result.Data             = false;
                result.ExceptionMessage = ex.Message;
                result.Status           = eOperationStatus.GeneralError;
            }
            return(result);
        }
示例#12
0
        public async Task <ActionResult <Country> > PostCountry(Country country)
        {
            var validator   = new CountryValidator();
            var resultValid = validator.Validate(country);

            if (!resultValid.IsValid)
            {
                return(BadRequest(resultValid.Errors));
            }
            var result = _countriesService.Add(country);

            return(CreatedAtAction("GetCountry", new { id = result.Id }, result));
        }
        public CompanyTINAttribute(Country countryCode)
        {
            if (!Enum.IsDefined(typeof(Country), countryCode))
            {
                throw new ArgumentNullException(nameof(countryCode));
            }
            else if (!CountryValidator.IsCountrySupported(CountryCode))
            {
                throw new NotSupportedException("This country is not supported");
            }

            CountryCode = countryCode;
        }
示例#14
0
        public void CountryValidator()
        {
            var v = new CountryValidator(PropertyName, Description);

            AssertValidatorProperties(v);

            var o = new SimpleObject();

            Assert.IsTrue(v.Validate(o));
            o.SimpleProperty = string.Empty;
            Assert.IsTrue(v.Validate(o));
            o.SimpleProperty = "IT";
            Assert.IsTrue(v.Validate(o));

            o.SimpleProperty = "XX";
            Assert.IsFalse(v.Validate(o));
        }
示例#15
0
        public OperationResult <bool> UpdateCustomer(Customers customer)
        {
            var result = new OperationResult <bool>();

            try
            {
                if (!CountryValidator.Validate(customer.Country))
                {
                    result.Data             = false;
                    result.Status           = eOperationStatus.GeneralError;
                    result.ExceptionMessage = "Country code does not exists";
                }
                else
                {
                    var toUpdate = _customerRepo.Get(customer.CustomerId);
                    toUpdate.Addresses = customer.Addresses;
                    toUpdate.City      = customer.City;
                    toUpdate.Country   = customer.Country;
                    toUpdate.Name      = customer.Name;
                    toUpdate.Street    = customer.Street;
                    toUpdate.Zip       = customer.Zip;
                    var saveResult = _context.SaveChanges();
                    if (saveResult > 0)
                    {
                        result.Data   = true;
                        result.Status = eOperationStatus.Success;
                    }
                    else
                    {
                        result.Data             = false;
                        result.Status           = eOperationStatus.GeneralError;
                        result.ExceptionMessage = "Could not update Customer";
                    }
                }
            }
            catch (Exception ex)
            {
                result.Status           = eOperationStatus.GeneralError;
                result.ExceptionMessage = ex.Message;
                result.Data             = false;
            }

            return(result);
        }
示例#16
0
        public ActionResult Create([Bind(Include = "Id,Name")] Country country)
        {
            CountryValidator validar = new CountryValidator();
            ValidationResult Result  = validar.Validate(country);

            if (!Result.IsValid)
            {
                foreach (ValidationFailure item in Result.Errors)
                {
                    ModelState.AddModelError(item.PropertyName, item.ErrorMessage);
                }
                return(View(country));
            }
            else
            {
                var objCountry = countryService.Insertar(country);
                return(RedirectToAction("Index"));
            }
        }
示例#17
0
        public void Setup()
        {
            _mockCountryDal = new Mock <ICountryDal>();
            _mockCountries  = new List <Country>
            {
                new Country {
                    Id = 1, Name = "Country1", CreateDate = DateTime.Now, Active = true
                },
                new Country {
                    Id = 2, Name = "Country2", CreateDate = DateTime.Now, Active = true
                },
                new Country {
                    Id = 3, Name = "Country3", CreateDate = DateTime.Now, Active = true
                }
            };
            _validator = new CountryValidator();

            _mockCountryDal.Setup(m => m.GetAll(null)).Returns(_mockCountries);
        }
        public ResponseBase <TBL_SLI_COUNTRY> Insert(TBL_SLI_COUNTRY country)
        {
            CountryValidator validator;

            try
            {
                validator = new CountryValidator();
                _results  = validator.Validate(country);
                if (_results.IsValid)
                {
                    country.BIT_STATE = true;
                    country.VCH_NAME  = country.VCH_NAME.ToUpper();
                    var countryFound = _context.TBL_SLI_COUNTRY.Where(x => x.VCH_NAME.Contains(country.VCH_NAME)).FirstOrDefault();
                    _response = new UtilityResponse <TBL_SLI_COUNTRY>().SetResponseBaseForUniqueValidation();
                    if (countryFound == null)
                    {
                        _context.TBL_SLI_COUNTRY.Add(country);
                        _context.SaveChanges();
                        _response = new UtilityResponse <TBL_SLI_COUNTRY>().SetResponseBaseForObj(country);
                    }
                }
                else
                {
                    _response = new UtilityResponse <TBL_SLI_COUNTRY>().SetResponseBaseFunctionalErrors(_results);
                }
                return(_response);
            }
            catch (Exception ex)
            {
                _response = new UtilityResponse <TBL_SLI_COUNTRY>().SetResponseBaseForException(ex);
                return(_response);
            }
            finally
            {
                _response = null;
                _results  = null;
                validator = null;
                _context.Database.Connection.Close();
            }
        }
示例#19
0
        public OperationResult <bool> UpdateAddress(string id, Addresses address)
        {
            var result = new OperationResult <bool>();

            try
            {
                var customer = _customerRepo.Get(id);
                if (customer != null)
                {
                    if (!CountryValidator.Validate(address.Country))
                    {
                        result.Data             = false;
                        result.Status           = eOperationStatus.GeneralError;
                        result.ExceptionMessage = "Country code does not exists";
                    }
                    else
                    {
                        var addressToUpdate = _addrRepo.Get(address.AddressId);
                        if (addressToUpdate != null && addressToUpdate.CustomerId == id)
                        {
                            addressToUpdate.AddressType = address.AddressType;
                            addressToUpdate.City        = address.City;
                            addressToUpdate.Country     = address.Country;
                            addressToUpdate.Name        = address.Name;
                            addressToUpdate.Street      = address.Street;
                            addressToUpdate.Zip         = address.Zip;
                            var saveResult = _context.SaveChanges();
                            if (saveResult > 0)
                            {
                                result.Data   = true;
                                result.Status = eOperationStatus.Success;
                            }
                            else
                            {
                                result.Data   = false;
                                result.Status = eOperationStatus.GeneralError;
                            }
                        }
                        else
                        {
                            result.Data             = false;
                            result.Status           = eOperationStatus.NotFound;
                            result.ExceptionMessage = "Address not found";
                        }
                    }
                }
                else
                {
                    result.Data             = false;
                    result.Status           = eOperationStatus.NotFound;
                    result.ExceptionMessage = "Customer not found";
                }
            }
            catch (Exception ex)
            {
                result.Data             = false;
                result.ExceptionMessage = ex.Message;
                result.Status           = eOperationStatus.GeneralError;
            }
            return(result);
        }