Example #1
0
        public async Task <IEnumerable <AdminBrandDto> > GetAdminBrands()
        {
            var brands = await _context.Brands.ToListAsync();

            var output = new List <AdminBrandDto>();

            foreach (var brand in brands)
            {
                var numberOfProducts = await _context.Products.CountAsync(p => p.BrandId == brand.Id);

                output.Add(AdminBrandDto.Create(brand, numberOfProducts));
            }

            return(output);
        }
        public IActionResult UpdateBrand(AdminBrandDto dto)
        {
            if (!AuthCheck())
            {
                return(RedirectToAction("Login", "Account", new { area = "Admin" }));
            }

            string messages = string.Join(Environment.NewLine, ModelState.Values.SelectMany(v => v.Errors).Select(v => v.ErrorMessage + " " + v.Exception));

            try
            {
                if (ModelState.IsValid)
                {
                    var allowedExtensions = new[] { ".jpg", ".jpeg", ".png", ".bmp" };

                    if (dto.BrandPhoto != null)
                    {
                        FileInfo fs  = new FileInfo(dto.BrandPhoto.FileName);
                        string   ext = Path.GetExtension(fs.Extension);

                        if (!allowedExtensions.Contains(ext.ToLower()))
                        {
                            HttpContext.Session.SetString("warning", "Lütfen sadece .jpg, .jpeg, .bmp veya .png uzantılı resim yükleyiniz.");
                            return(RedirectToAction("BrandList"));
                        }
                    }
                    //dto.IsActive = true;
                    dto.BandNameUrl = dto.BrandName.ConvertToFriendlyUrl();
                    var  brandDb = _mapper.Map <Brand>(dto);
                    bool a       = _brandRepo.UpdateEntity(brandDb);

                    bool b = true;

                    if (dto.BrandPhoto != null)
                    {
                        b = ProcessBrandPhoto(dto.BrandPhoto, brandDb.Id);
                    }

                    if (a && b)
                    {
                        HttpContext.Session.SetString("success", " Marka Güncelleme işlemi başarılı.");
                        return(RedirectToAction("BrandList"));
                    }
                    else
                    {
                        HttpContext.Session.SetString("warning", " Marka Güncellenirken hata oluştu. Hata kayıt altına alındı.");

                        _errorRepo.CreateEntity(new ErrorLog
                        {
                            Culture       = "tr",
                            ErrorLocation = "Admin Brand UpdateBrand satır 180",
                            ErrorDetail   = $"a:{a}, b:{b}",
                            ErrorUrl      = HttpContext.Request.Path,
                        });
                        return(RedirectToAction("BrandList"));
                    }
                }

                HttpContext.Session.SetString("warning", $"{messages}, Lütfen formu eksiksiz doldurunuz.");
                return(RedirectToAction("BrandList"));
            }
            catch (Exception e)
            {
                _errorRepo.CreateEntity(new ErrorLog
                {
                    ErrorDetail   = e.Message + " " + e.InnerException + " " + messages,
                    ErrorLocation = "BrandController UpdateBrandPost",
                    ErrorUrl      = HttpContext.Request.Path
                });
                return(RedirectToAction("BrandList"));
            }
        }