public IActionResult Create([FromBody] EditBidResource resource)
        {
            AppLogger.LogResourceRequest(nameof(Create), base.GetUsernameForRequest());

            try
            {
                var result = _bidService.ValidateResource(resource);

                if (!result.IsValid)
                {
                    GetErrorsForModelState(result.ErrorMessages);
                }

                if (ModelState.IsValid)
                {
                    var bid = _bidService.Add(resource);

                    return(CreatedAtAction(nameof(Create), bid));
                }

                return(ValidationProblem());
            }
            catch (Exception ex)
            {
                return(BadRequestExceptionHandler(ex, nameof(Create)));
            }
        }
        public void Put_WhenResourceInvalid_ReturnsWithValidationErrors()
        {
            // Get a list of bids so we can grab the correct id that is stored in
            // the (in)memory db
            var getBids = _client.AuthorizeRequest().GetAsync(APIROUTE).Result;

            getBids.EnsureSuccessStatusCode();

            var bidsRaw = getBids.Content.ReadAsStringAsync().Result;
            var bids    = JsonConvert.DeserializeObject <IEnumerable <BidResource> >(bidsRaw);

            // Verify we have something to look up
            Assert.NotEmpty(bids);

            // The Test
            // Arrange - Grab first one
            var bid = bids.FirstOrDefault();

            var editedBid = new EditBidResource
            {
                Account     = "tester2",
                BidQuantity = "thisIsInvalid", // Validation should fail to convert to double
                Type        = "Sell"
            };

            // Act
            var response = _client.AuthorizeRequest().PutAsync(APIROUTE + bid.BidListId, ClientHelper.EncodeContent(editedBid)).Result;
            var json     = response.Content.ReadAsStringAsync().Result;

            Assert.Equal(System.Net.HttpStatusCode.BadRequest, response.StatusCode);
            Assert.Contains("validation errors", json);
        }
        public IActionResult Update(int id, [FromBody] EditBidResource resource)
        {
            AppLogger.LogResourceRequest(nameof(Update), base.GetUsernameForRequest());

            try
            {
                var result = _bidService.ValidateResource(resource);

                if (!result.IsValid)
                {
                    GetErrorsForModelState(result.ErrorMessages);
                }

                if (ModelState.IsValid)
                {
                    if (_bidService.FindById(id) == null)
                    {
                        return(NotFound(AppConfig.ResourceNotFoundById + id));
                    }

                    _bidService.Update(id, resource);

                    return(Ok());
                }

                return(ValidationProblem());
            }
            catch (Exception ex)
            {
                return(BadRequestExceptionHandler(ex, nameof(Update)));
            }
        }
        public ValidationResult ValidateResource(EditBidResource resource)
        {
            var result = new ValidationResult();

            if (resource != null)
            {
                var validator = new BidValidator();
                var vr        = validator.Validate(resource);

                if (vr.IsValid)
                {
                    result.IsValid = true;
                    return(result);
                }


                if (vr.Errors.Any())
                {
                    foreach (var error in vr.Errors)
                    {
                        result.ErrorMessages.Add(error.PropertyName, error.ErrorMessage);
                    }
                }
            }

            return(result);
        }
        public void Put_ReturnsOk()
        {
            // Get a list of bids so we can grab the correct id that is stored in
            // the (in)memory db
            var getBids = _client.AuthorizeRequest().GetAsync(APIROUTE).Result;

            getBids.EnsureSuccessStatusCode();

            var bidsRaw = getBids.Content.ReadAsStringAsync().Result;
            var bids    = JsonConvert.DeserializeObject <IEnumerable <BidResource> >(bidsRaw);

            // Verify we have something to look up
            Assert.NotEmpty(bids);

            // The Test
            // Arrange - Grab first one
            var bid = bids.FirstOrDefault();

            var editedBid = new EditBidResource
            {
                Account     = "tester2",
                BidQuantity = "3",
                Type        = "Sell"
            };

            // Act
            var response = _client.AuthorizeRequest().PutAsync(APIROUTE + bid.BidListId, ClientHelper.EncodeContent(editedBid)).Result;

            // Assert Ok
            response.EnsureSuccessStatusCode();
        }
        public void Update(int id, EditBidResource resource)
        {
            var bidToUpdate = _bidRepo.FindById(id);

            if (bidToUpdate != null && resource != null)
            {
                _bidRepo.Update(id, _mapper.Map(resource, bidToUpdate));
                _bidRepo.SaveChanges();
            }
        }
        public BidResource Add(EditBidResource resource)
        {
            if (resource == null)
            {
                throw new ArgumentNullException();
            }

            var bid = _mapper.Map <BidList>(resource);

            _bidRepo.Add(bid);
            _bidRepo.SaveChanges();

            return(_mapper.Map <BidResource>(bid));
        }
        public void Post_WhenResourceIsInvalied_ReturnsBadRequestWithValidationErrors()
        {
            var bid = new EditBidResource
            {
                Account     = "tester",
                BidQuantity = "notvalid", // This will fail validation since cannot convert to double
                Type        = "Buy"
            };

            var response = _client.AuthorizeRequest().PostAsync(APIROUTE, ClientHelper.EncodeContent(bid)).Result;
            var json     = response.Content.ReadAsStringAsync().Result;

            Assert.Equal(System.Net.HttpStatusCode.BadRequest, response.StatusCode);
            Assert.Contains("validation errors", json);
        }
        public void Put_WhenBidIsNull_ReturnsNotFound()
        {
            // Arrange
            var editedBid = new EditBidResource
            {
                Account     = "tester2",
                BidQuantity = "3",
                Type        = "Sell"
            };

            // Act
            var response = _client.AuthorizeRequest().PutAsync(APIROUTE + 99999, ClientHelper.EncodeContent(editedBid)).Result;

            // Assert
            Assert.Equal(System.Net.HttpStatusCode.NotFound, response.StatusCode);
        }
        public void Post_ReturnsOk_WithNewBidResource()
        {
            var bid = new EditBidResource
            {
                Account     = "tester",
                BidQuantity = "5",
                Type        = "Buy"
            };

            var response = _client.AuthorizeRequest().PostAsync(APIROUTE, ClientHelper.EncodeContent(bid)).Result;

            response.EnsureSuccessStatusCode();

            var json   = response.Content.ReadAsStringAsync().Result;
            var result = JsonConvert.DeserializeObject <BidResource>(json);

            Assert.NotNull(result);
            Assert.IsType <BidResource>(result);
        }