/// <summary>Add a new stock of a beer for a specific wholesaler.</summary> /// <param name="newStock">The new stock to add.</param> /// <returns> /// A success result with the new stock as value.<br /> /// A failure result with Validation as error code if newStock has params validation errors.<br /> /// A failure result with NotFound as error code if the wholesaler or the beer does not exist. /// </returns> public async Task <Result <Stock> > AddStockAsync(Stock newStock) { if (!newStock.Is(StockSpecifications.HasValidQuantity)) { return(Result.Failure(newStock, ResultErrorCode.Validation, "The quantity should be greater or equal than zero.")); } if (!newStock.Is(StockSpecifications.HasValidWholesalerId)) { return(Result.Failure(newStock, ResultErrorCode.Validation, "The wholesaler id should not be empty.")); } if (!newStock.Is(StockSpecifications.HasValidBeerId)) { return(Result.Failure(newStock, ResultErrorCode.Validation, "The beer id should not be empty.")); } var wholesaler = await _context.Wholesalers.FindAsync(newStock.WholesalerId); if (wholesaler == null) { return(Result.Failure(newStock, ResultErrorCode.NotFound, $"The wholesaler with {newStock.WholesalerId} id was not found.")); } var beer = await _context.Beers.FindAsync(newStock.BeerId); if (beer == null) { return(Result.Failure(newStock, ResultErrorCode.NotFound, $"The beer with {newStock.BeerId}id was not found.")); } await _context.Stocks.AddAsync(newStock); await _context.SaveChangesAsync(); return(Result.Success(newStock)); }
/// <summary>Add a new beer for a specific brewery.</summary> /// <param name="newBeer">The new beer to add.</param> /// <returns> /// A success result with the new beer as value.<br /> /// A failure result with Validation as error code if newBeer has params validation errors.<br /> /// A failure result with NotFound as error code if the brewery does not exist. /// </returns> public async Task <Result <Beer> > AddBeerAsync(Beer newBeer) { if (!newBeer.Is(BeerSpecifications.HasValidName)) { return(Result.Failure(newBeer, ResultErrorCode.Validation, "The name should not be empty.")); } if (!newBeer.Is(BeerSpecifications.HasValidStrength)) { return(Result.Failure(newBeer, ResultErrorCode.Validation, "The strength should be greater or equal than zero.")); } if (!newBeer.Is(BeerSpecifications.HasValidPrice)) { return(Result.Failure(newBeer, ResultErrorCode.Validation, "The price should be greater than zero.")); } if (!newBeer.Is(BeerSpecifications.HasValidBreweryId)) { return(Result.Failure(newBeer, ResultErrorCode.Validation, "The brewery id should not be empty.")); } var brewery = await _context.Breweries.FindAsync(newBeer.BreweryId); if (brewery == null) { return(Result.Failure(newBeer, ResultErrorCode.NotFound, $"The brewery with {newBeer.BreweryId} was not found.")); } await _context.Beers.AddAsync(newBeer); await _context.SaveChangesAsync(); return(Result.Success(newBeer)); }