Exemple #1
0
        public async Task <IActionResult> EditProductAsync(ProductUpsertDto dto)
        {
            var claimsIdentity = this.User.Identity as ClaimsIdentity;
            var userId         = claimsIdentity.Claims.ToList().FirstOrDefault(x => x.Type == "id").Value;

            dto.CompanyId = Convert.ToInt32(claimsIdentity.Claims.ToList().FirstOrDefault(x => x.Type == "companyId").Value);

            return(await _productService.EditProductAsync(dto, userId));
        }
Exemple #2
0
        public async Task <IActionResult> AddProductAsync(ProductUpsertDto product, string userId)
        {
            try
            {
                if (!(await PermissionMonitor.CheckPermissionsAsync(_userRepository, userId, "Modyfikacja produktów")))
                {
                    return(new JsonResult(new ApiResponse <object>
                    {
                        Data = null,
                        Code = 403,
                        ErrorMessage = "Brak uprawnień"
                    }));
                }
                var validator = new ProductValidator();
                var result    = validator.Validate(product);

                if (result.IsValid)
                {
                    var productToDB = _mapper.Map <ApiDomain.Entity.Product>(product);
                    return(new JsonResult(new ApiResponse <ProductUpsertDto>()
                    {
                        Code = 201,
                        ErrorMessage = "",
                        Data = _mapper.Map <ProductUpsertDto>(await _productRepository.AddProductAsync(productToDB))
                    }));
                }
                else
                {
                    var errorMsg = "";

                    foreach (var err in result.Errors)
                    {
                        errorMsg += err.ErrorMessage + "\r\n";
                    }

                    return(new JsonResult(new ApiResponse <ProductUpsertDto>()
                    {
                        Code = 406,
                        ErrorMessage = errorMsg,
                        Data = null
                    }));
                }
            }
            catch
            {
                return(new JsonResult(new ApiResponse <object>
                {
                    Code = 500,
                    Data = null,
                    ErrorMessage = "Nastąpił problem z serwerem, skontaktuj się z działem IT."
                }));
            }
        }
Exemple #3
0
        public async Task <IActionResult> EditProductAsync(ProductUpsertDto product, string userId)
        {
            try
            {
                if (!(await PermissionMonitor.CheckPermissionsAsync(_userRepository, userId, "Modyfikacja produktów")))
                {
                    return(new JsonResult(new ApiResponse <object>
                    {
                        Data = null,
                        Code = 403,
                        ErrorMessage = "Brak uprawnień"
                    }));
                }

                ApiDomain.Entity.Product DBProduct = await _productRepository.GetProductByIdAsync(product.Id);

                if (DBProduct != null)
                {
                    DBProduct.MarkupRate = product.MarkupRate;
                    DBProduct.Name       = product.Name;
                    DBProduct.UnitValue  = product.UnitValue;
                    DBProduct.VatRate    = product.VatRate;
                    await _productRepository.EditProductAsync(DBProduct);

                    return(new JsonResult(new ApiResponse <object>
                    {
                        Code = 200,
                        Data = null,
                        ErrorMessage = ""
                    }));
                }
                else
                {
                    return(new JsonResult(new ApiResponse <object>
                    {
                        Code = 404,
                        Data = null,
                        ErrorMessage = "Produkt nie odnaleziony."
                    }));
                }
            }
            catch
            {
                return(new JsonResult(new ApiResponse <object>
                {
                    Code = 500,
                    Data = null,
                    ErrorMessage = "Nastąpił problem z serwerem, skontaktuj się z działem IT."
                }));
            }
        }