public void Present(UpdateBeerResponse response) { ViewModel = new ApiUpdateBeerViewModel { HttpCode = response.Errors != null && response.Errors.Any() ? 400 : 201, Success = response.Errors == null || !response.Errors.Any(), Data = response.Errors != null && response.Errors.Any() ? null : new { Id = response.Data.Id.Value, Label = response.Data.Label.Value, Description = response.Data.Description.Value, Stock = response.Data.Stock.Value, Available = response.Data.Stock.Value > 0, LimitedStock = response.Data.Stock.Value <= 50 }, Errors = response.Errors }; }
public UpdateBeerResponse UpdateBeer(UpdateBeerRequest updateBeerRequest) { var response = new UpdateBeerResponse() { BaseResponse = CreateBaseResponse(updateBeerRequest.BaseRequest) }; response.BaseResponse = VerifyBaseRequest(updateBeerRequest.BaseRequest, response.BaseResponse); if (response.BaseResponse.ResponseStatus == ResponseStatus.Failure) { response.Beer = null; return(response); } var beer = _beerRepository.UpdateBeer(updateBeerRequest.Beer); response.Beer = beer; return(response); }
public void Execute(UpdateBeerRequest request, IUpdateBeerPresenter presenter) { var response = new UpdateBeerResponse(); var validator = new UpdateBeerValidator(_catalog); var validationResult = validator.Validate(request); if (!validationResult.IsValid) { response.Data = null; response.Errors = validationResult.Errors .Select(e => new KeyValuePair <string, string>(e.PropertyName, e.ErrorMessage)) .ToDictionary(x => x.Key, x => x.Value); } else { var beer = _catalog.GetOneBeer(request.Id); var toUpdate = new BeerModel( beer.Id.Value, beer.CreatedAt.Value, DateTime.Now, request.Label, request.Description, request.Stock ); var updated = _catalog.UpdateBeer(toUpdate); response.Data = updated; response.Errors = null; } presenter.Present(response); }