示例#1
0
        public async Task <CategoryDTO> UpdateAsync(CategoryPutRequest model, ApiDbContext apiDbContext)
        {
            try
            {
                var category = await apiDbContext.Categories.FindAsync(model.Id);

                if (category == null)
                {
                    throw new Exception($"No existe la categoría {model.Name} con id {model.Id}");
                }

                var categoryFound = apiDbContext.Categories.FirstOrDefault(p => p.Id != model.Id && p.Code.ToUpper().Trim().Equals(model.Code.ToUpper().Trim()));
                if (categoryFound != null)
                {
                    throw new Exception($"Ya existe una categoría con el código {model.Code}");
                }

                category.Code = model.Code;
                category.Name = model.Name;

                await apiDbContext.SaveChangesAsync();

                return(ModelToDTO(category));
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
示例#2
0
        public async Task <IActionResult> Update(CategoryPutRequest model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    throw new Exception("Petición de actualización inválida");
                }

                return(Ok(await _categoryService.UpdateAsync(model, _apiDbContext)));
            }
            catch (Exception e)
            {
                return(StatusCode(500, e.Message));
            }
        }