Exemplo n.º 1
0
        public async Task <IActionResult> DeleteCategory([FromRoute(Name = "category_id")] Guid categoryId)
        {
            // validate input
            if (!ModelState.IsValid)
            {
                return(HttpResult.BadRequest(ModelState));
            }

            // locate category
            var foundCategory = await _bagsContext.TagCategories
                                .Where(category => category.Id == categoryId)
                                .SingleOrDefaultAsync();

            if (foundCategory == null)
            {
                return(HttpResult.NoContent());
            }

            // validate it doesn't have any tags
            if (foundCategory.Tags.Any())
            {
                return(HttpResult.Conflict("Category must have no associated tags before it can be deleted."));
            }

            // delete
            _bagsContext.TagCategories.Remove(foundCategory);
            await _bagsContext.SaveChangesAsync();

            return(HttpResult.NoContent());
        }
Exemplo n.º 2
0
        public async Task <IActionResult> DeleteTag([FromRoute(Name = "tag_id")] Int32 tagId)
        {
            // validate input
            if (!ModelState.IsValid)
            {
                return(HttpResult.BadRequest(ModelState));
            }

            // locate tag
            var foundTag = await _bagsContext.Tags
                           .Include(tag => tag.Products)
                           .Where(tag => tag.Id == tagId)
                           .SingleOrDefaultAsync();

            if (foundTag == null)
            {
                return(HttpResult.NoContent());
            }

            // validate it doesn't have any products
            if (foundTag.Products.Any())
            {
                return(HttpResult.Conflict("Tag must have no associated products before it can be deleted."));
            }

            // delete
            _bagsContext.Tags.Remove(foundTag);
            await _bagsContext.SaveChangesAsync();

            return(HttpResult.NoContent());
        }
Exemplo n.º 3
0
        public async Task <IActionResult> EditTag([FromRoute(Name = "tag_id")] Int32 tagId, [FromBody] EditTagRequest request)
        {
            // validate input
            if (!ModelState.IsValid)
            {
                return(HttpResult.BadRequest(ModelState));
            }

            // locate tag
            var foundTag = await _bagsContext.Tags
                           .WithSafeIncludes()
                           .Where(tag => tag.Id == tagId)
                           .SingleOrDefaultAsync();

            if (foundTag == null)
            {
                return(HttpResult.NotFound());
            }

            // verify there are changes
            if (foundTag.TagCategoryId == request.CategoryId && foundTag.Name == request.Name)
            {
                return(HttpResult.Ok(foundTag.ToSafeExpandedWireFormat()));
            }

            // locate the new category
            var newCategory = await _bagsContext.TagCategories
                              .Where(category => category.Id == request.CategoryId)
                              .SingleOrDefaultAsync();

            if (newCategory == null)
            {
                return(HttpResult.BadRequest($"Category {request.CategoryId} does not exist."));
            }

            // verify no conflict on unique constraint
            var duplicateTag = await _bagsContext.Tags
                               .WithSafeIncludes()
                               .Where(tag => tag.TagCategoryId == request.CategoryId && tag.Name == request.Name)
                               .SingleOrDefaultAsync();

            if (duplicateTag != null)
            {
                return(HttpResult.Conflict(duplicateTag.ToSafeExpandedWireFormat()));
            }

            // change the category/name
            foundTag.TagCategory = newCategory;
            foundTag.Name        = request.Name;
            await _bagsContext.SaveChangesAsync();

            return(HttpResult.Ok(foundTag.ToSafeExpandedWireFormat()));
        }
Exemplo n.º 4
0
        public async Task <IActionResult> EditCategory([FromRoute(Name = "category_id")] Guid categoryId, [FromBody] EditCategoryRequest request)
        {
            // validate input
            if (!ModelState.IsValid)
            {
                return(HttpResult.BadRequest(ModelState));
            }

            // locate category
            var foundCategory = await _bagsContext.TagCategories
                                .Where(category => category.Id == categoryId)
                                .SingleOrDefaultAsync();

            if (foundCategory == null)
            {
                return(HttpResult.NotFound());
            }

            // verify there are actual changes
            if (foundCategory.Name == request.Name)
            {
                return(HttpResult.Ok(foundCategory.ToBaseWireFormat()));
            }

            // verify no conflicts with unique constraint
            var duplicateCategory = await _bagsContext.TagCategories
                                    .Where(category => category.Name == request.Name)
                                    .SingleOrDefaultAsync();

            if (duplicateCategory != null)
            {
                return(HttpResult.Conflict(duplicateCategory.ToBaseWireFormat()));
            }

            // change the name
            foundCategory.Name = request.Name;
            await _bagsContext.SaveChangesAsync();

            return(HttpResult.Ok(foundCategory.ToBaseWireFormat()));
        }