public IActionResult AddPrice([FromBody] PriceForCreationDto price) { if (price == null) { return(BadRequest()); } var priceToReturn = _mapper.Map <Price>(price); _repository.Add(priceToReturn); if (!_repository.Save()) { return(StatusCode(500, "Unexpected error on creating price")); } return(CreatedAtRoute("Price", new { id = priceToReturn.Id }, priceToReturn)); }
public async Task <PriceDto> Create(Guid userId, PriceForCreationDto priceForCreationDto) { var product = await _productRepository.GetById(priceForCreationDto.ProductId); if (product == null) { throw new KeyNotFoundException($"Product with id: {priceForCreationDto.ProductId} not found."); } var price = new Price() { Value = priceForCreationDto.Value, DateTime = priceForCreationDto.DateTime, ProductId = priceForCreationDto.ProductId, Product = product, CreatedBy = userId }; price = await _priceRepository.Add(price); await _priceRepository.CommitAsync(); return(_mapper.Map <Price, PriceDto>(price)); }