public async Task <ServiceResult> NewProduct(string token, ProductAddForDto model)
        {
            var serRes = new ServiceResult();

            var json       = JsonConvert.SerializeObject(model);
            var httpClinet = _httpClientFactory.CreateClient("ingamedemo");

            httpClinet.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
            var response = await httpClinet.PostAsync("product/newproduct", new StringContent(json, Encoding.UTF8, "application/json"));

            if (!response.IsSuccessStatusCode)
            {
                var message = await response.Content.ReadAsStringAsync();

                if (string.IsNullOrEmpty(message))
                {
                    message = "Beklenmedik bir hata meydana geldi. Lütfen daha sonra tekrar deneyiniz.";
                }

                serRes.ResultStatus.Explanation = message;
                return(serRes);
            }

            serRes.ResultStatus.Status = Enums.ResultStatus.Success;
            return(serRes);
        }
Exemple #2
0
        public async Task <IActionResult> NewProduct([FromBody] ProductAddForDto model)
        {
            try
            {
                var dataModel = _mapper.Map <Products>(model);
                dataModel.Category   = null;
                dataModel.CreateDate = DateTime.Now;
                dataModel.IsActive   = true;

                await _unitOfWork.ProductRepository.Add(dataModel);

                var result = await _unitOfWork.Save();

                if (!result)
                {
                    return(BadRequest("Ürün eklenemedi. Lütfen daha sonra tekrar deneyiniz"));
                }

                return(Ok());
            }
            catch (Exception ex)
            {
                return(StatusCode(500, ex.Message));
            }
        }
Exemple #3
0
        public async Task <IActionResult> ProductUpdate(int id, [FromBody] ProductAddForDto model)
        {
            try
            {
                var getProduct = await _unitOfWork.ProductRepository.GetById(id);

                if (getProduct == null)
                {
                    return(NotFound("Ürün bulunamadı."));
                }

                if (!getProduct.IsActive)
                {
                    return(NotFound("Ürün güncellenemez. Ürün silinmiş"));
                }

                getProduct.Name        = model.Name;
                getProduct.ImageUrl    = model.ImageUrl;
                getProduct.CategoryId  = model.CategoryId;
                getProduct.Description = model.Description;
                getProduct.Price       = model.Price;

                await _unitOfWork.ProductRepository.Update(getProduct);

                var result = await _unitOfWork.Save();

                if (!result)
                {
                    return(BadRequest("Ürün güncellenemedi. Lütfen daha sonra tekrar deneyiniz"));
                }

                return(Ok());
            }
            catch (Exception ex)
            {
                return(StatusCode(500, ex.Message));
            }
        }
        public async Task <IActionResult> NewProduct(ProductFormForDto model)
        {
            #region Kategorileri Çekiyoruz

            var serRes = await _categoryService.GetCategoryManagement(GetToken());

            if (serRes.ResultStatus.Status != Enums.ResultStatus.Success)
            {
                AddSweetAlert("Beklenmedik bir durum oluştu", serRes.ResultStatus.Explanation, Enums.NotificationType.warning);

                return(View(new ProductFormForDto()));
            }

            if (serRes.Result != null && serRes.Result.Categories != null && serRes.Result.Categories.Any())
            {
                model.Categories = serRes.Result.Categories;
            }

            #endregion

            if (!ModelState.IsValid)
            {
                if (string.IsNullOrEmpty(model.Name) || !model.CategoryId.HasValue || model.File == null || !model.Price.HasValue)
                {
                    ModelState.AddModelError("", "Lütfen tüm alanları doldurunuz.");
                }

                return(View(model));
            }

            var uniqueFileName = Tools.GetUniqueFileName(model.File.FileName);
            var uploadsPath    = Path.Combine(_hostingEnvironment.WebRootPath, "uploads");

            // Ürün ekleniyor.
            var dataModel = new ProductAddForDto
            {
                Name        = model.Name,
                ImageUrl    = "/uploads/" + uniqueFileName,
                Price       = model.Price.Value,
                CategoryId  = model.CategoryId.Value,
                Description = model.Description,
                UserName    = GetUserName()
            };

            var newProductSerRes = await _productService.NewProduct(GetToken(), dataModel);

            if (newProductSerRes.ResultStatus.Status != Enums.ResultStatus.Success)
            {
                AddSweetAlert("Hata", serRes.ResultStatus.Explanation, Enums.NotificationType.error);
                return(View(model));
            }

            var filePath = Path.Combine(uploadsPath, uniqueFileName);
            using (var stream = new FileStream(filePath, FileMode.Create))
            {
                await model.File.CopyToAsync(stream);

                stream.Close();
            }

            AddSweetAlert("Tebrikler", "Ürün başarıyla eklendi", Enums.NotificationType.success);

            return(RedirectToAction("Index"));
        }