Ejemplo n.º 1
0
        public async Task <IResult> Insert(CategoryViewModel categoryView, IFormFile img)
        {
            var result = new Result
            {
                Operation = Operation.Create,
                Status    = Status.Success
            };

            try
            {
                var categoryCheck = _categoryRepository.CheckCategory(categoryView.CategoryName, categoryView.CategoryId);
                if (!categoryCheck)
                {
                    result.Status     = Status.Fail;
                    result.StatusCode = HttpStatusCode.BadRequest;
                    result.Message    = "This category already exists.";
                    return(result);
                }
                CategoryModel category = new CategoryModel();
                category.MapFromViewModel(categoryView);
                category.CreatedBy   = _specificClaim.GetSpecificClaim("Id");
                category.CreatedDate = DateTime.Now;
                if (!category.ParentCategory)
                {
                    category.ChildCategory = categoryView.ChildCategory;
                }
                if (img != null)
                {
                    Images         image          = new Images();
                    ImageExtention imageExtention = new ImageExtention();
                    image.ImageName       = img.FileName;
                    image.ImageContent    = imageExtention.Image(img);
                    image.ImageExtenstion = Path.GetExtension(img.FileName);
                    category.Images       = image;
                }
                var insertCategory = await _categoryRepository.InsertCategory(category);

                return(insertCategory);
            }
            catch (Exception e)
            {
                result.Status     = Status.Error;
                result.Message    = e.Message;
                result.StatusCode = HttpStatusCode.InternalServerError;
                return(result);
            }
        }
Ejemplo n.º 2
0
        public async Task <IResult> Update(UserViewModel viewModel, IFormFile img)
        {
            var result = new Result()
            {
                Operation = Operation.Update,
                Status    = Status.Success
            };

            try
            {
                UserModel userModel = new UserModel();
                userModel.MapFromViewModel(viewModel);
                if (img != null)
                {
                    ImageExtention imageExtention = new ImageExtention();
                    userModel.ImageContent = imageExtention.Image(img);
                }
                var updateUser = await _userRepository.Update(userModel);

                if (updateUser.Status != Status.Success)
                {
                    return(updateUser);
                }
                userModel = updateUser.Body;
                UserViewModel userView = new UserViewModel();
                userView.MapFromModel(userModel);
                result.Body       = userView;
                result.Status     = Status.Success;
                result.StatusCode = HttpStatusCode.OK;
                return(result);
            }
            catch (Exception e)
            {
                result.Status     = Status.Error;
                result.Message    = e.Message;
                result.StatusCode = HttpStatusCode.InternalServerError;
                return(result);
            }
        }
Ejemplo n.º 3
0
        public async Task <IResult> AddProductImages(int id, List <IFormFile> img)
        {
            var result = new Result
            {
                Operation = Operation.Create,
                Status    = Status.Success
            };

            try
            {
                var          productObj   = _productRepository.GetProductDetail(id);
                ProductModel productModel = new ProductModel();
                productModel.MapFromViewModel(productObj);
                productModel.ModifiedBy   = _specificClaim.GetSpecificClaim("Id");
                productModel.ModifiedDate = DateTime.Now;
                ImageExtention imageExtention = new ImageExtention();
                var            images         = new List <ProductImage>();
                foreach (IFormFile i in img)
                {
                    ProductImage productimage = new ProductImage();
                    productimage.ImageName       = i.FileName;
                    productimage.ImageContent    = imageExtention.Image(i);
                    productimage.ImageExtenstion = Path.GetExtension(i.FileName);
                    productimage.ProductId       = productModel.ProductId;
                    images.Add(productimage);
                }
                var addImages = await _productRepository.AddProductImages(productModel, images);

                return(addImages);
            }
            catch (Exception e)
            {
                result.Status     = Status.Error;
                result.Message    = e.Message;
                result.StatusCode = HttpStatusCode.InternalServerError;
                return(result);
            }
        }
Ejemplo n.º 4
0
        public async Task <IResult> CreateUser(UserViewModel viewModel, IFormFile img, bool fromAdmin)
        {
            var result = new Result
            {
                Operation = Operation.Create,
                Status    = Status.Success
            };

            try
            {
                if (viewModel == null)
                {
                    result.Message    = "noUser";
                    result.Status     = Status.Fail;
                    result.StatusCode = HttpStatusCode.BadRequest;
                    return(result);
                }
                if (img == null)
                {
                    result.Message    = "noUserImage";
                    result.Status     = Status.Fail;
                    result.StatusCode = HttpStatusCode.BadRequest;
                    return(result);
                }
                var            user           = new UserModel();
                ImageExtention imageExtention = new ImageExtention();
                user.MapFromViewModel(viewModel);
                user.ImageContent = imageExtention.Image(img);
                if (user.Password == null)
                {
                    user.Password = Guid.NewGuid().ToString().Replace("-", "");
                }
                var userRoles = new List <UserRolesModel>();
                foreach (var role in viewModel.Roles)
                {
                    UserRolesModel rolesModel = new UserRolesModel();
                    rolesModel.RoleId = role;
                    userRoles.Add(rolesModel);
                }
                user.Roles = userRoles;
                var newUser = await _userRepository.CreateUser(user);

                if (newUser.Status == Status.Success)
                {
                    if (fromAdmin)
                    {
                        var mailLink = await _loginService.MailPasswordLink(newUser.Body);

                        return(mailLink);
                    }
                    result.Status     = Status.Success;
                    result.StatusCode = HttpStatusCode.OK;
                    result.Body       = "success";
                    return(result);
                }
                return(newUser);
            }
            catch (Exception e)
            {
                result.Status     = Status.Error;
                result.Message    = e.Message;
                result.StatusCode = HttpStatusCode.InternalServerError;
                return(result);
            }
        }
Ejemplo n.º 5
0
        public async Task <IResult> Update(CategoryViewModel categoryView, IFormFile img)
        {
            var result = new Result
            {
                Operation = Operation.Update,
                Status    = Status.Success
            };

            try
            {
                var categoryCheck = Detail(categoryView.CategoryId);
                if (categoryCheck.Status != Status.Success)
                {
                    result.Status     = Status.Fail;
                    result.StatusCode = HttpStatusCode.BadRequest;
                    result.Message    = "This category already exists.";
                    return(result);
                }
                CategoryViewModel viewModel = new CategoryViewModel();
                viewModel = categoryCheck.Body;
                var categoryDetail = _categoryRepository.GetCategory(categoryView.CategoryId);
                categoryDetail.MapFromViewModel(categoryView);
                categoryDetail.CreatedBy    = viewModel.CreatedBy;
                categoryDetail.CreatedDate  = viewModel.CreatedDate;
                categoryDetail.ModifiedBy   = _specificClaim.GetSpecificClaim("Id");
                categoryDetail.ModifiedDate = DateTime.Now;
                if (categoryView.ImageId == null)
                {
                    if (categoryDetail.ImageId != null)
                    {
                        var deleteImage = await _categoryRepository.EditCategoryImage(categoryDetail.CategoryId, categoryDetail.ImageId, Operation.Delete);

                        categoryDetail.Images  = null;
                        categoryDetail.ImageId = null;
                    }
                }
                if (!categoryView.ParentCategory)
                {
                    categoryDetail.ChildCategory = categoryView.ChildCategory;
                }
                else
                {
                    categoryDetail.ChildCategory = null;
                }
                if (img != null)
                {
                    ImageExtention imageExtention = new ImageExtention();
                    if (categoryDetail == null)
                    {
                        result.Status     = Status.Fail;
                        result.StatusCode = HttpStatusCode.BadRequest;
                        result.Message    = "Category does not exist.";
                        return(result);
                    }
                    if (categoryDetail.Images == null)
                    {
                        Images image = new Images();
                        image.ImageName       = img.FileName;
                        image.ImageContent    = imageExtention.Image(img);
                        image.ImageExtenstion = Path.GetExtension(img.FileName);
                        categoryDetail.Images = image;
                    }
                }
                var updateCategory = await _categoryRepository.UpdateCategory(categoryDetail);

                return(updateCategory);
            }
            catch (Exception e)
            {
                result.Status     = Status.Error;
                result.Message    = e.Message;
                result.StatusCode = HttpStatusCode.InternalServerError;
                return(result);
            }
        }