Esempio n. 1
0
        public async Task <IActionResult> PutCategory(long id, CategoryPut categoryPut)
        {
            if (id != categoryPut.Id)
            {
                return(BadRequest());
            }

            Category category = new Category();

            category.Id    = categoryPut.Id;
            category.Title = categoryPut.Title;

            _context.Entry(category).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CategoryExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Esempio n. 2
0
        public async Task UpdateCategoryAsync(string name, CategoryPut category)
        {
            var userId = GetUserId();

            ValidationHelper.ValidateCategoryName(name);

            if (category == null)
            {
                throw new ValidationErrorException("Category must be specified.");
            }
            Validator.CheckIsValid(category);

            var utcNow = Time.UtcNow;

            await RethrowUniqueKeyExceptionAsync(
                "UK_Category",
                () => new ValidationErrorException("Category with '{0}' name already exists.", category.Name),
                async() =>
            {
                using (var persistence = Container.Get <IPersistenceService>())
                {
                    var dbCategory = await GetCategoryAsync(persistence, userId, category.Name);

                    dbCategory.Name       = category.Name;
                    dbCategory.Type       = category.Type;
                    dbCategory.ChangeDate = utcNow;

                    await persistence.SaveChangesAsync();
                }
            });
        }
        public async Task <HttpResponseMessage> UpdateCategoryAsync([FromUri] string name, [FromBody] CategoryPut category)
        {
            return(await ExecuteAsync(async() =>
            {
                var categoryService = Container.Get <ICategoryService>();

                await categoryService.UpdateCategoryAsync(name, category);

                return new Information {
                    Message = "Category updated."
                };
            }));
        }