Beispiel #1
0
        /// <summary>
        /// StrToRomanNumberWithError
        /// </summary>
        public static RopResult <RomanNumber, string> StrToRomanNumberWithError(string input)
        {
            var digitsWithErrs =
                input
                .ToCharArray()
                .Select(CharToRomanDigitWithError);
            var errors =
                digitsWithErrs
                .Where(r => !r.IsSuccess)
                .Select(r => r.FailureValues.First());

            if (errors.Any())
            {
                return(Rop.Fail <RomanNumber, string>(errors));
            }
            else
            {
                var digits =
                    digitsWithErrs
                    .Where(r => r.IsSuccess)
                    .Select(r => r.SuccessValue)
                    .ToList();
                var romanNumber = new RomanNumber(digits);
                return(Rop.Succeed <RomanNumber, string>(romanNumber));
            }
        }
Beispiel #2
0
 private static RopResult <SimpleRequest, string> EmailNotBlank(SimpleRequest input)
 {
     if (input.Email == "")
     {
         return(Rop.Fail <SimpleRequest, string>("Email must not be blank"));
     }
     else
     {
         return(Rop.Succeed <SimpleRequest, string>(input));
     }
 }
Beispiel #3
0
 private static RopResult <SimpleRequest, string> Name50(SimpleRequest input)
 {
     if (input.Name.Length > 50)
     {
         return(Rop.Fail <SimpleRequest, string>("Name must not be longer than 50 chars"));
     }
     else
     {
         return(Rop.Succeed <SimpleRequest, string>(input));
     }
 }
Beispiel #4
0
        /// <summary>
        /// Return all customers
        /// </summary>
        public RopResult <IEnumerable <Customer>, DomainMessage> GetAll()
        {
            var db         = new DbContext();
            var ropResults = db.Customers().Select(FromDbCustomer);

            if (ropResults.Any(r => !r.IsSuccess))
            {
                return(Rop.Fail <IEnumerable <Customer>, DomainMessage>(DomainMessage.SqlCustomerIsInvalid()));
            }
            else
            {
                var goodResults = ropResults.Select(r => r.SuccessValue);
                return(Rop.Succeed <IEnumerable <Customer>, DomainMessage>(goodResults));
            }
        }
Beispiel #5
0
        /// <summary>
        /// CharToRomanDigitWithError
        /// </summary>
        public static RopResult <RomanDigit, string> CharToRomanDigitWithError(char ch)
        {
            switch (ch)
            {
            case 'I': return(Rop.Succeed <RomanDigit, string>(RomanDigit.I));

            case 'V': return(Rop.Succeed <RomanDigit, string>(RomanDigit.V));

            case 'X': return(Rop.Succeed <RomanDigit, string>(RomanDigit.X));

            default:
                var msg = String.Format("{0} is not a valid char", ch);
                return(Rop.Fail <RomanDigit, string>(msg));
            }
        }
Beispiel #6
0
        public IHttpActionResult Post(int customerId, [FromBody] CustomerDto dto)
        {
            var logSuccessR     = LogSuccessR <CustomerDto>("Post {0}");
            var dtoToCustomerR  = Rop.Bind <CustomerDto, Customer, DomainMessage>(DtoConverter.DtoToCustomer);
            var upsertCustomerR = Rop.Bind <Customer, Unit, DomainMessage>(_dao.Upsert);
            var logFailureR     = LogFailureR <Unit>();
            var okR             = Rop.Lift <Unit, IHttpActionResult, DomainMessage>(this.Ok);

            dto.Id = customerId;                                      // set the DTO's CustomerId

            var dtoR = Rop.Succeed <CustomerDto, DomainMessage>(dto); // start with a success

            return(dtoR                                               // start with a success
                   .Pipe(logSuccessR)                                 // log the success branch
                   .Pipe(dtoToCustomerR)                              // convert the DTO to a Customer
                   .Pipe(upsertCustomerR)                             // upsert the Customer
                   .Pipe(logFailureR)                                 // log any errors
                   .Pipe(okR)                                         // return OK on the happy path
                   .Pipe(ToHttpResult));                              // other errors returned as BadRequest, etc
        }
Beispiel #7
0
        public IHttpActionResult Get(int customerId)
        {
            var logSuccessR       = LogSuccessR <int>("Get {0}");
            var createCustomerIdR = Rop.Bind <int, CustomerId, DomainMessage>(DomainUtilities.CreateCustomerId);
            var getByIdR          = Rop.Bind <CustomerId, Customer, DomainMessage>(_dao.GetById);
            var customerToDtoR    = Rop.Lift <Customer, CustomerDto, DomainMessage>(DtoConverter.CustomerToDto);
            var logFailureR       = LogFailureR <CustomerDto>();
            var okR = Rop.Lift <CustomerDto, IHttpActionResult, DomainMessage>(this.Ok);

            var idR = Rop.Succeed <int, DomainMessage>(customerId);    // start with a success

            return(idR
                   .Pipe(logSuccessR)       // log the success branch
                   .Pipe(createCustomerIdR) // convert the int into a CustomerId
                   .Pipe(getByIdR)          // get the Customer for that CustomerId
                   .Pipe(customerToDtoR)    // convert the Customer into a DTO
                   .Pipe(logFailureR)       // log any errors
                   .Pipe(okR)               // return OK on the happy path
                   .Pipe(ToHttpResult));    // other errors returned as BadRequest, etc
        }
Beispiel #8
0
        /// <summary>
        /// Insert/update the customer
        /// If it already exists, update it, otherwise insert it.
        /// If the email address has changed, raise a EmailAddressChanged event on DomainEvents
        /// </summary>
        public RopResult <Unit, DomainMessage> Upsert(Customer customer)
        {
            if (customer == null)
            {
                throw new ArgumentNullException("customer");
            }

            var db             = new DbContext();
            var existingDbCust = GetById(customer.Id);
            var newDbCust      = ToDbCustomer(customer);

            return(Rop.Match(existingDbCust,
                             success =>
            {
                // update
                db.Update(newDbCust);

                // check for changed email
                if (!customer.EmailAddress.Equals(success.EmailAddress))
                {
                    // return a Success with the extra event
                    var msg = DomainMessage.EmailAddressChanged(success.EmailAddress, customer.EmailAddress);
                    return Rop.SucceedWithMsg <Unit, DomainMessage>(Unit.Instance, msg);
                }
                else
                {
                    // return a Success with no extra event
                    return Rop.Succeed <Unit, DomainMessage>(Unit.Instance);
                }
            },
                             failure =>
            {
                // insert
                db.Insert(newDbCust);

                // return a Success
                return Rop.Succeed <Unit, DomainMessage>(Unit.Instance);
            }));
        }