Esempio n. 1
0
        public async Task <IActionResult> EditBrand([FromBody] EditBrandDto brand)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                if (!await _brandRepository.BrandExistsAsync(brand.Id))
                {
                    ModelState.AddModelError("brand", "Brand not found");
                    return(BadRequest(ModelState));
                }

                if (await _brandRepository.IsDuplicateBrandAsync(brand))
                {
                    ModelState.AddModelError("brand", "Brand already exists");
                    return(BadRequest(ModelState));
                }

                var wasBrandEdited = await _brandRepository.EditBrandAsync(brand);

                if (wasBrandEdited)
                {
                    return(Ok());
                }

                return(NoContent());
            }
            catch (Exception e)
            {
                _logger.LogCritical($"PUT {Route} - {e.GetType().Name} - {e.Message} - {e.StackTrace}");
                return(StatusCode(500, "An error ocurred in server"));
            }
        }