예제 #1
0
 private static Result <Maybe <Email> > GetImporterEmail(ProductDefinition definition)
 {
     if (definition.ImporterEmail == null)
     {
         return(Result.Ok(new Maybe <Email>()));
     }
     return(Email.Create(definition.ImporterEmail)
            .Map(x => (Maybe <Email>)x));
 }
예제 #2
0
        public HttpResponse CreateProduct(ProductDefinition definition)
        {
            Result <ProductName>      productName    = ProductName.Create(definition.Name);
            Result <ManufacturerName> manufacturer   = ManufacturerName.Create(definition.Manufacturer);
            Result <Maybe <Email> >   emailOrNothing = GetImporterEmail(definition);

            return(Result.Combine(productName, manufacturer, emailOrNothing)
                   .OnSuccess(() => new Product
            {
                ProductId = definition.ProductId,
                Category = definition.Category,
                Name = productName.Value,
                Manufacturer = manufacturer.Value,
                ImporterEmail = emailOrNothing.Value,
                Quantity = definition.Quantity
            })
                   .OnSuccess(p => _repository.Add(p))
                   .OnBoth(r => r.IsSuccess ? Commit() : Response.BadRequest(r.Error)));
        }