Beispiel #1
0
        public async Task <IActionResult> EditProduct([FromRoute(Name = "product_id")] Int32 productId, [FromBody] EditProductRequest request)
        {
            // validate input
            if (!ModelState.IsValid)
            {
                return(HttpResult.BadRequest(ModelState));
            }

            // locate product
            var foundProduct = await _bagsContext.Products
                               .WithUnsafeIncludes()
                               .Where(product => product.Id == productId)
                               .SingleOrDefaultAsync();

            if (foundProduct == null)
            {
                return(HttpResult.NotFound($"{productId}"));
            }

            // update the product
            foundProduct.Name = request.Name;
            if (request.Images != null)
            {
                foundProduct.ImagesJson = JsonConvert.SerializeObject(request.Images);
            }
            await _bagsContext.SaveChangesAsync();

            return(HttpResult.Ok(foundProduct.ToUnsafeExpandedWireFormat(_amazon)));
        }
Beispiel #2
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()));
        }
Beispiel #3
0
        public async Task <IActionResult> AddTag([FromRoute(Name = "product_id")] Int32 productId, [FromBody] AddTagRequest request)
        {
            // validate input
            if (!ModelState.IsValid)
            {
                return(HttpResult.BadRequest(ModelState));
            }

            // validate product
            var foundProduct = await _bagsContext.Products
                               .WithUnsafeIncludes()
                               .Where(product => product.Id == productId)
                               .SingleOrDefaultAsync();

            if (foundProduct == null)
            {
                return(HttpResult.NotFound($"{productId}"));
            }

            // validate tags
            var foundTags = await _bagsContext.Tags
                            .WithSafeIncludes()
                            .Where(tag => request.TagIds.Contains(tag.Id))
                            .ToListAsync();

            foreach (var expectedTagId in request.TagIds)
            {
                if (!foundTags.Any(foundTag => foundTag.Id == expectedTagId))
                {
                    return(HttpResult.NotFound($"{expectedTagId}"));
                }
            }

            // link tag and product
            var preexistingTags = foundProduct.Tags.Select(productTag => productTag.Tag).ToList();
            var productTags     = foundTags
                                  .Where(foundTag => !preexistingTags.Contains(foundTag))
                                  .Select(foundTag => new Models.ProductTag {
                Product = foundProduct, Tag = foundTag
            })
                                  .ToList();

            _bagsContext.ProductTags.AddRange(productTags);
            await _bagsContext.SaveChangesAsync();

            return(HttpResult.Ok(foundProduct.ToUnsafeExpandedWireFormat(_amazon)));
        }
Beispiel #4
0
        public async Task <IActionResult> RemoveTag([FromRoute(Name = "product_id")] Int32 productId, [FromRoute(Name = "tag_id")] Int32 tagId)
        {
            // validate input
            if (!ModelState.IsValid)
            {
                return(HttpResult.BadRequest(ModelState));
            }

            // validate product
            var foundProduct = await _bagsContext.Products
                               .Where(product => product.Id == productId)
                               .SingleOrDefaultAsync();

            if (foundProduct == null)
            {
                return(HttpResult.NotFound($"{productId}"));
            }

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

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

            // locate product tag
            var foundProductTag = await _bagsContext.ProductTags
                                  .Where(productTag => productTag.ProductId == foundProduct.Id && productTag.TagId == foundTag.Id)
                                  .SingleOrDefaultAsync();

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

            // delete
            _bagsContext.ProductTags.Remove(foundProductTag);
            await _bagsContext.SaveChangesAsync();

            return(HttpResult.NoContent());
        }
        public async Task <IActionResult> GetCategory([FromRoute(Name = "category_id")] Guid categoryId)
        {
            // validate input
            if (!ModelState.IsValid)
            {
                return(HttpResult.BadRequest(ModelState));
            }

            // get category
            var foundTagCategory = await _bagsContext.TagCategories
                                   .WithUnsafeIncludes()
                                   .Where(category => category.Id == categoryId)
                                   .SingleOrDefaultAsync();

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

            return(HttpResult.Ok(foundTagCategory.ToUnsafeExpandedWireFormat()));
        }
Beispiel #6
0
        public async Task <IActionResult> GetProduct([FromRoute(Name = "product_id")] Int32 productId)
        {
            // validate input
            if (!ModelState.IsValid)
            {
                return(HttpResult.BadRequest(ModelState));
            }

            // locate product
            var foundProduct = await _bagsContext.Products
                               .WithUnsafeIncludes()
                               .Where(product => product.Id == productId)
                               .SingleOrDefaultAsync();

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

            return(HttpResult.Ok(foundProduct.ToUnsafeExpandedWireFormat(_amazon)));
        }
Beispiel #7
0
        public async Task <IActionResult> GetTag([FromRoute(Name = "tag_id")] Int32 tagId)
        {
            // validate input
            if (!ModelState.IsValid)
            {
                return(HttpResult.BadRequest(ModelState));
            }

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

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

            return(HttpResult.Ok(foundTag.ToUnsafeExpandedWireFormat()));
        }
        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()));
        }