예제 #1
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
                           ));
            }
        }
예제 #2
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());
            }
        }
        public void Handle(AlterProduct message)
        {
            var product = repository.GetById <Products.Domain.Product>(message.Id);

            int committedVersion = message.OriginalVersion;

            if (!String.Equals(product.Code, message.NewCode, StringComparison.OrdinalIgnoreCase))
            {
                product.ChangeCode(message.NewCode, committedVersion++);
            }

            if (!String.Equals(product.Name, message.NewTitle, StringComparison.OrdinalIgnoreCase))
            {
                product.ChangeTitle(message.NewTitle, committedVersion++);
            }

            if (!String.Equals(product.Description, message.NewDescription, StringComparison.OrdinalIgnoreCase))
            {
                product.ChangeDescription(message.NewDescription, committedVersion++);
            }

            if (message.NewProductType != product.Type)
            {
                product.ChangeType(message.NewProductType, committedVersion);
            }

            repository.Save(product);
        }