Ejemplo n.º 1
0
        private static Result <Wish> Validate(int userId, int productId)
        {
            if (userId < 1)
            {
                return(OperationResult.BadRequest <Wish>("UserId must be greater than zero"));
            }

            if (productId < 1)
            {
                return(OperationResult.BadRequest <Wish>("ProductId must be greater than zero"));
            }

            return(OperationResult.OK <Wish>());
        }
Ejemplo n.º 2
0
        private static Result <User> ValidateName(string value)
        {
            if (string.IsNullOrEmpty(value?.Trim()))
            {
                return(OperationResult.BadRequest <User>("Name is required"));
            }

            if (value.Length < 3 && value.Length > 150)
            {
                return(OperationResult.BadRequest <User>("Name must be 3-150 characters"));
            }

            return(OperationResult.OK <User>());
        }
Ejemplo n.º 3
0
        private static Result <User> ValidateEmail(string value)
        {
            if (string.IsNullOrEmpty(value?.Trim()))
            {
                return(OperationResult.BadRequest <User>("Email Is required"));
            }

            const string pattern = @"\A(?:[a-zA-Z0-9!#$%&'*+/=?^_`{|}-]+(?:\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?)\Z";

            if (!Regex.IsMatch(value, pattern))
            {
                return(OperationResult.BadRequest <User>("Invalid Email"));
            }

            return(OperationResult.OK <User>());
        }
Ejemplo n.º 4
0
        public static Result <Product> Create(int id, string name)
        {
            if (id < 0)
            {
                return(OperationResult.BadRequest <Product>("Id is required"));
            }

            if (string.IsNullOrEmpty(name?.Trim()))
            {
                return(OperationResult.BadRequest <Product>("Name is required"));
            }

            if (name.Length < 3 && name.Length > 150)
            {
                return(OperationResult.BadRequest <Product>("Name must be 3-150 characters"));
            }

            return(OperationResult.OK(new Product(id, name)));
        }
Ejemplo n.º 5
0
        public async Task <Result <ProductModel> > Create(ProductCreationModel productModel)
        {
            try
            {
                var productResult = Product.Create(productModel.Name);

                if (productResult.Failure)
                {
                    return(OperationResult.BadRequest <ProductModel>(productResult));
                }

                if (await this.productQueryRepository.ProductExists(productModel.Name))
                {
                    return(OperationResult.NotFound <ProductModel>("Product already exists"));
                }

                this.productRepository.Add(productResult.Value);

                unitOfWork.Save();

                var model = Mapper.Map <ProductModel>(productResult.Value);

                var indexResult = await indexService.IndexDocumentAsync(model);

                if (indexResult.Failure)
                {
                    await Rollback(productResult.Value);

                    return(OperationResult.InternalServerError <ProductModel>(indexResult));
                }

                return(OperationResult.Created(model));
            }
            catch (Exception ex)
            {
                return(OperationResult.InternalServerError <ProductModel>(ex));
            }
        }
Ejemplo n.º 6
0
        public async Task <Result <UserModel> > Create(UserCreationModel userModel)
        {
            try
            {
                var userResult = User.Create(userModel.Name, userModel.Email);

                if (userResult.Failure)
                {
                    return(OperationResult.BadRequest <UserModel>(userResult));
                }

                if (await userQueryRepository.UserExists(userModel.Email))
                {
                    return(OperationResult.Conflict <UserModel>($"User already exists with email {userModel.Email}"));
                }

                this.userRepository.Add(userResult.Value);

                unitOfWork.Save();

                var model = Mapper.Map <UserModel>(userResult.Value);

                var indexResult = await indexService.IndexDocumentAsync(model);

                if (indexResult.Failure)
                {
                    await Rollback(userResult.Value);

                    return(OperationResult.InternalServerError <UserModel>(indexResult));
                }

                return(OperationResult.Created <UserModel>(model));
            }
            catch (Exception ex)
            {
                return(OperationResult.InternalServerError <UserModel>(ex.GetExceptionMessages()));
            }
        }