Ejemplo n.º 1
0
        public static void ValidateData(this IHDSContext context, CreateStoreDto dto)
        {
            var errors = new StringBuilder();

            // StoreName
            errors.AddIfExists(dto.StoreName.ValidateRequired(ValidationMessages.StoreNameRequired));
            errors.AddIfExists(dto.StoreName.ValidateLength(100, ValidationMessages.StoreNameLength));
            if (dto.Addresses != null && dto.Addresses.Any())
            {
                // Addresses
                for (int i = 0; i < dto.Addresses.Count; i++)
                {
                    if (dto.Addresses.Select(o => o.Primary).Count() > 1)
                    {
                        errors.AddIfExists(ValidationMessages.PrimaryAddressSingle);
                    }
                    // AddressTypeID
                    errors.AddIfExists(dto.Addresses[i].AddressTypeID.ValidateRequired($"Address [{i}]:{ValidationMessages.AddressTypeIDRequired}"));
                    errors.AddIfExists(context.KeyExists <AddressType>(dto.Addresses[i].AddressTypeID, $"Address [{i}]:{ValidationMessages.AddressTypeIDNotFound}"));
                    // Address
                    if (dto.Addresses[i].Address != null)
                    {
                        // Street Address
                        errors.AddIfExists(dto.Addresses[i].Address?.StreetAddress.ValidateRequired($"Address [{i}]:{ValidationMessages.StreetAddressRequired}"));
                        errors.AddIfExists(dto.Addresses[i].Address?.StreetAddress.ValidateLength(100, $"Address [{i}]:{ValidationMessages.StreetAddressRequired}"));
                        // City
                        errors.AddIfExists(dto.Addresses[i].Address?.City.ValidateRequired($"Address [{i}]:{ValidationMessages.CityRequired}"));
                        errors.AddIfExists(dto.Addresses[i].Address?.City.ValidateLength(50, $"Address [{i}]:{ValidationMessages.CityLength}"));
                        // State
                        errors.AddIfExists(dto.Addresses[i].Address?.State.ValidateRequired($"Address [{i}]:{ValidationMessages.StateRequired}"));
                        errors.AddIfExists(dto.Addresses[i].Address?.State.ValidateLength(50, $"Address [{i}]:{ValidationMessages.StateLength}"));
                        // PostCode
                        errors.AddIfExists(dto.Addresses[i].Address?.PostalCode.ValidateRequired($"Address [{i}]:{ValidationMessages.PostalCodeRequired}"));
                        errors.AddIfExists(dto.Addresses[i].Address?.PostalCode.ValidateLength(10, $"Address [{i}]:{ValidationMessages.PostalCodeLength}"));
                    }
                    else
                    {
                        errors.AddIfExists($"Address [{i}]:{ValidationMessages.AddressRequired}");
                    }
                }
            }
            if (dto.ContactMethods != null && dto.ContactMethods.Any())
            {
                // ContactMethods
                for (int i = 0; i < dto.ContactMethods.Count; i++)
                {
                    // ContactMethodTypeID
                    errors.AddIfExists(dto.ContactMethods[i].ContactMethodTypeID.ValidateRequired($"ContactMethod [{i}]:{ValidationMessages.ContactMethodTypeIDRequired}"));
                    errors.AddIfExists(context.KeyExists <ContactMethodType>(dto.ContactMethods[i].ContactMethodTypeID, $"ContactMethod [{i}]:{ValidationMessages.ContactMethodTypeIDNotFound}"));
                    // ContactMethod Value
                    errors.AddIfExists(dto.ContactMethods[i].ContactMethodValue.ValidateRequired($"ContactMethod [{i}]:{ValidationMessages.ContactMethodValueRequired}"));
                    errors.AddIfExists(dto.ContactMethods[i].ContactMethodValue.ValidateLength(100, $"ContactMethod [{i}]:{ValidationMessages.ContactMethodValueLength}"));
                }
            }

            if (errors.Length > 0)
            {
                throw new ValidationException(errors.ToString());
            }
        }
Ejemplo n.º 2
0
        public StoreVerboseDto Create(CreateStoreDto dto)
        {
            _dbContext.ValidateData(dto);
            var store = dto.ToEntity();

            _dbContext.Store.Add(store);

            _dbContext.SaveChanges();

            return(store.ToVerboseDto());
        }
Ejemplo n.º 3
0
        public IActionResult Create([Required][FromBody] CreateStoreDto store)
        {
            try
            {
                var result = _repo.Create(store);

                return(StatusCode((int)HttpStatusCode.Created, result));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                return(BadRequest(ex.Message));
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Creates the store.
        /// </summary>
        /// <param name="createModel"></param>
        /// <returns></returns>
        public async Task <StoreDto> CreateStore(CreateStoreDto createModel)
        {
            if (!validationContext.IsValid)
            {
                return(null);
            }

            var storeModel = mapper.Map <CreateStoreDto, Store>(createModel);

            storeModel.IsDisabled = false;

            unitOfWork.GetRepository <Store>().Insert(storeModel);

            await unitOfWork.SaveAsync();

            return(mapper.Map <Store, StoreDto>(storeModel));
        }
Ejemplo n.º 5
0
 public static Store ToEntity(this CreateStoreDto dto)
 {
     Init();
     return(Mapper.Map <Store>(dto));
 }