public IActionResult Create(BrandCreate model)
 {
     if (ModelState.IsValid)
     {
         var brand = new Brand()
         {
             BrandName  = model.BrandName,
             IsDelete   = false,
             CategoryId = model.CategoryId
         };
         var fileName = string.Empty;
         if (model.BrandImg != null)
         {
             string uploadFolder = Path.Combine(webHostEnvironment.WebRootPath, "images/Brand");
             fileName = $"{Guid.NewGuid()}_{model.BrandImg.FileName}";
             var filePath = Path.Combine(uploadFolder, fileName);
             using (var fs = new FileStream(filePath, FileMode.Create))
             {
                 model.BrandImg.CopyTo(fs);
             }
         }
         brand.ImagePath = fileName;
         brandRepository.Create(brand);
         return(RedirectToAction("Index", "Product"));
     }
     return(View());
 }
示例#2
0
        /// <summary>
        /// Cria uma marca nova
        /// </summary>
        /// <param name="token">token</param>
        /// <param name="brand">Objeto do tipo marca</param>
        /// <returns></returns>
        public BrandDetail CreateBrand(string token, BrandCreate brand)
        {
            try
            {
                _logger.LogInformation("Criar uma marcar nova: Enviando requisição para a API");
                var response = _brandApi.CreateBrand(token, brand).Result;
                if (!response.IsSuccessStatusCode)
                {
                    var contentResult = response.Content.ReadAsStringAsync().Result;
                    _logger.LogError($"Criar uma marcar nova: API retornou erro :( - {response.StatusCode}-{response.ReasonPhrase} -> {contentResult}");
                    if (((int)response.StatusCode) >= 400 && ((int)response.StatusCode) < 500)
                    {
                        return(null);
                    }
                }
                _logger.LogInformation("Criar uma marcar nova: API retornou sucesso :)");

                var json = response.Content.ReadAsStringAsync().Result;
                return(Task.Factory.StartNew(() => JsonConvert.DeserializeObject <BrandDetail>(json)).Result);
            }
            catch (Exception)
            {
                _logger.LogError($"Criar uma marcar nova: API retornou erro :( ");
                return(null);
            }
        }
示例#3
0
        public async Task <ActionResult <Brand> > PostBrand(BrandCreate brandCreateDTO)
        {
            brandCreateDTO.UserId = User.UserGuidId();
            var brand = _brandMapper.BrandCreate(brandCreateDTO);

            _bll.BrandService.Add(brand);
            await _bll.SaveChangesAsync();

            return(CreatedAtAction("GetBrand", new { id = brand.Id }, brand));
        }
        /// <summary>
        /// Cria uma marca nova
        /// </summary>
        /// <param name="token">token</param>
        /// <param name="brand">Objeto do tipo marca</param>
        /// <returns></returns>
        public async Task <HttpResponseMessage> CreateBrand(string token, BrandCreate brand)
        {
            _client             = new HttpClient();
            _client.BaseAddress = AppSettings.Apis.BrandApi;
            _client.DefaultRequestHeaders.Accept.Clear();
            _client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            return(await new HttpClientHelper(_client)
                   .SetEndpoint("brands")
                   .AddHeader("Authorization", $"Bearer {token}")
                   .WithContentSerialized(brand)
                   .PostAsync());
        }