Exemple #1
0
        public IHttpActionResult Put(Guid id, AlterProductCommand cmd)
        {
            // TODO: is this to be handled here or in the Aggregate?
            if (string.IsNullOrWhiteSpace(cmd.Name))
            {
                var response = new HttpResponseMessage(HttpStatusCode.Forbidden)
                {
                    Content      = new StringContent("code must be supplied in the body"),
                    ReasonPhrase = "Missing product code"
                };
                throw new HttpResponseException(response);
            }

            try
            {
                var command = new AlterProduct(id, cmd.Version, cmd.Name, cmd.Description, cmd.Price);
                handler.Handle(command);

                return(Ok(command));
            }
            catch (AggregateNotFoundException)
            {
                return(NotFound());
            }
            catch (AggregateDeletedException)
            {
                return(Conflict());
            }
        }
Exemple #2
0
        public ActionResult Put(Guid id, AlterProductCommand cmd)
        {
            if (string.IsNullOrWhiteSpace(cmd.Name))
            {
                return(Problem(
                           title: "Missing product code",
                           detail: "code must be supplied in the body",
                           statusCode: StatusCodes.Status400BadRequest
                           ));
            }

            try
            {
                var command = new AlterProduct(id, cmd.Version, cmd.Name, cmd.Description, cmd.Price);
                handler.Handle(command);

                return(Ok(command));
            }
            catch (AggregateNotFoundException)
            {
                return(NotFound());
            }
            catch (AggregateDeletedException)
            {
                return(Conflict());
            }
            catch (Exception ex)
            {
                return(Problem(
                           title: "Error product",
                           detail: ex.Message,
                           statusCode: StatusCodes.Status400BadRequest
                           ));
            }
        }
        public async Task <AlterProductResponse> AlterProductAsync(Guid id, AlterProductCommand cmd)
        {
            using (var client = new HttpClient())
            {
                var url      = new Uri(string.Format("{0}/products/{1}", BaseServiceUrl, id));
                var response = await client.PutAsync <AlterProductCommand>(url, cmd, new JsonMediaTypeFormatter());

                if (response.StatusCode != HttpStatusCode.OK)
                {
                    throw new ApplicationException(string.Format("There was an error altering the Product with id {0}", id));
                }

                var result = await response.Content.ReadAsStringAsync();

                var dto = JsonConvert.DeserializeObject <AlterProductResponse>(result);
                return(dto);
            }
        }
        public async Task<IHttpActionResult> Put(Guid id, AlterProductCommand cmd)
        {
            var result = await productsService.AlterProductAsync(id, cmd);

            return Ok(result);
        }
Exemple #5
0
        public async Task <IHttpActionResult> Put(Guid id, AlterProductCommand cmd)
        {
            var result = await productsService.AlterProductAsync(id, cmd);

            return(Ok(result));
        }
        public async Task<AlterProductResponse> AlterProductAsync(Guid id, AlterProductCommand cmd)
        {
            using (var client = new HttpClient())
            {
                var url = new Uri(string.Format("{0}/products/{1}", BaseServiceUrl, id));
                var response = await client.PutAsync<AlterProductCommand>(url, cmd, new JsonMediaTypeFormatter());
                if (response.StatusCode != HttpStatusCode.OK)
                {
                    throw new ApplicationException(string.Format("There was an error altering the Product with id {0}", id));
                }

                var result = await response.Content.ReadAsStringAsync();
                var dto = JsonConvert.DeserializeObject<AlterProductResponse>(result);
                return dto;
            }
        }