コード例 #1
0
        public async Task <IActionResult> CreateProduct([FromBody] ProductToCreateDto productToCreateDto)
        {
            var productToCreate = _mapper.Map <Product>(productToCreateDto);

            if (await _repository.EntityExists(productToCreate))
            {
                return(BadRequest("Product With Same Name and Vendor Exists"));
            }
            var category = await _repository.GetCategory(productToCreate.CategoryId);

            if (category == null)
            {
                return(BadRequest("Invalid Category Id"));
            }
            var productCode = await _repository.GenerateProductCode(productToCreateDto.CategoryId);

            if (productToCreateDto.Price == 0)
            {
                return(BadRequest("Invalid Price"));
            }
            var merchant = await _repository.GetMerchant(productToCreateDto.MerchantId);

            if (merchant == null)
            {
                return(BadRequest("The Merchant for whom this product is to be created does not exist"));
            }
            var brand = await _repository.GetBrand(productToCreateDto.BrandId);

            if (brand == null)
            {
                return(BadRequest("The Brand for whom this product is to be created does not exist"));
            }
            var store = await _repository.GetStore(productToCreateDto.StoreId);

            if (store == null)
            {
                return(BadRequest("The Store for which this product is to be stored does not exist"));
            }
            productToCreate.Code = productCode;
            store.Products.Add(productToCreate);
            if (await _repository.SaveAllChangesAsync())
            {
                var productToReturn = _mapper.Map <ProductToReturnDto>(productToCreate);
                return(CreatedAtRoute("GetProduct", new { code = productToReturn.Code }, productToReturn));
            }
            return(BadRequest("An Error occurred while creating product"));
        }
コード例 #2
0
        public async Task <IActionResult> Get(int id)
        {
            var brandFromRepo = await _repository.GetBrand(id);

            if (brandFromRepo == null)
            {
                return(NotFound());
            }
            return(Ok(brandFromRepo));
        }