Beispiel #1
0
 public IActionResult CatalogBrands()
 {
     using (var db = new CatalogDb())
     {
         return(Ok(db.CatalogBrands));
     }
 }
Beispiel #2
0
        public async Task <IActionResult> UploadImage(int catalogItemId)
        {
            using (var db = new CatalogDb())
            {
                var item = db.CatalogItems.SingleOrDefault(i => i.Id == catalogItemId);
                if (item == null)
                {
                    return(NotFound(new { Message = $"Item with id {catalogItemId} not found." }));
                }

                string rootPath  = _hostingEnvironment.WebRootPath ?? "wwwroot";
                string imagePath = Path.Combine(rootPath, "Images");

                // Create if no exists
                Directory.CreateDirectory(imagePath);

                string extension = GetExtensionFromContentType(Request.ContentType);
                string fileName  = Path.Combine(imagePath, $"{catalogItemId}{extension}");

                using (var stream = new FileStream(fileName, FileMode.Create))
                {
                    await Request.Body.CopyToAsync(stream);
                }

                item.PictureFileName = Path.GetFileName(fileName);
                item.PictureUri      = $"{ImageBaseUri}/{item.PictureFileName}";
                db.SaveChanges();
            }

            return(Ok());
        }
Beispiel #3
0
        public IActionResult Items(string name, [FromQuery] int pageSize = 10, [FromQuery] int pageIndex = 0)
        {
            using (var db = new CatalogDb())
            {
                long totalItems = db.CatalogItems.Where(c => c.Name.StartsWith(name)).LongCount();

                var itemsOnPage = db.CatalogItems.Where(c => c.Name.StartsWith(name)).Skip(pageSize * pageIndex).Take(pageSize).OrderBy(c => c.Name).ToList();
                itemsOnPage = ChangeUriPlaceholder(itemsOnPage);

                var model = new PaginatedItems <CatalogItem>(pageIndex, pageSize, totalItems, itemsOnPage);
                return(Ok(model));
            }
        }
Beispiel #4
0
        public IActionResult DeleteProduct(int id)
        {
            using (var db = new CatalogDb())
            {
                var product = db.CatalogItems.SingleOrDefault(x => x.Id == id);

                if (product == null)
                {
                    return(NotFound());
                }

                db.CatalogItems.Remove(product);
                db.SaveChanges();

                return(NoContent());
            }
        }
Beispiel #5
0
        public IActionResult UpdateProduct([FromBody] CatalogItem productToUpdate)
        {
            using (var db = new CatalogDb())
            {
                var item = db.CatalogItems.SingleOrDefault(i => i.Id == productToUpdate.Id);
                if (item == null)
                {
                    return(NotFound(new { Message = $"Item with id {productToUpdate.Id} not found." }));
                }
                db.CatalogItems.Remove(item);

                ChangeUriPlaceholder(productToUpdate);
                db.CatalogItems.Add(productToUpdate);
                db.SaveChanges();

                return(CreatedAtAction(nameof(GetItemById), new { id = productToUpdate.Id }, null));
            }
        }
Beispiel #6
0
        public IActionResult GetItemById(int id)
        {
            if (id <= 0)
            {
                return(BadRequest());
            }

            using (var db = new CatalogDb())
            {
                var item = db.CatalogItems.SingleOrDefault(ci => ci.Id == id);
                if (item != null)
                {
                    ChangeUriPlaceholder(item);
                    return(Ok(item));
                }

                return(NotFound());
            }
        }
Beispiel #7
0
        public IActionResult CreateProduct([FromBody] CatalogItem product)
        {
            using (var db = new CatalogDb())
            {
                var item = new CatalogItem
                {
                    CatalogBrandId  = product.CatalogBrandId,
                    CatalogTypeId   = product.CatalogTypeId,
                    Description     = product.Description,
                    Name            = product.Name,
                    PictureFileName = product.PictureFileName,
                    Price           = product.Price
                };
                item.Id = db.CatalogItems.Max(r => r.Id) + 1;
                ChangeUriPlaceholder(item);

                db.CatalogItems.Add(item);
                db.SaveChanges();

                return(CreatedAtAction(nameof(GetItemById), new { id = item.Id }, item));
            }
        }
Beispiel #8
0
        public IActionResult Items(int?catalogTypeId, int?catalogBrandId, [FromQuery] int pageSize = 10, [FromQuery] int pageIndex = 0)
        {
            using (var db = new CatalogDb())
            {
                IEnumerable <CatalogItem> items = db.CatalogItems;

                if (catalogTypeId != null && catalogTypeId > -1)
                {
                    items = items.Where(r => r.CatalogTypeId == catalogTypeId);
                }

                if (catalogBrandId != null && catalogBrandId > -1)
                {
                    items = items.Where(r => r.CatalogBrandId == catalogBrandId);
                }

                long totalItems  = items.LongCount();
                var  itemsOnPage = items.Skip(pageSize * pageIndex).Take(pageSize).OrderBy(c => c.Name).ToList();
                itemsOnPage = ChangeUriPlaceholder(itemsOnPage);

                var model = new PaginatedItems <CatalogItem>(pageIndex, pageSize, totalItems, itemsOnPage);
                return(Ok(model));
            }
        }