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")); }
public async Task <IActionResult> Get(int id) { var categoryFromRepo = await _repository.GetCategory(id); if (categoryFromRepo == null) { return(NotFound()); } return(Ok(categoryFromRepo)); }
public async Task <IActionResult> CreateBrand([FromBody] BrandToCreateDto brandToCreateDto) { var categoryFromRepo = await _repository.GetCategory(brandToCreateDto.CategoryId); var brandToCreate = _mapper.Map <Brand>(brandToCreateDto); if (await _repository.EntityExists(brandToCreate)) { return(BadRequest("Brand Name Exists")); } _repository.Add(brandToCreate); if (await _repository.SaveAllChangesAsync()) { var brandToReturn = _mapper.Map <BrandToCreateDto>(brandToCreateDto); return(CreatedAtRoute("GetBrand", new { id = brandToCreate.Id }, brandToReturn)); } return(BadRequest("An Error occurred while creating the brand")); }