public async Task <ActionResult <ApiContactResultModel> > Add([FromBody] ContactsModel model)
        {
            var result    = new ApiContactResultModel();
            var contactId = new IdValueObject().Value;

            try
            {
                model.Id = contactId;
                await _mediator.Send(new AddContactCommand(model));

                result.Success   = true;
                result.ContactId = contactId;
                return(Ok(result));
            }
            catch (InvalidEntityException ex)
            {
                result.Errors = new List <string> {
                    ex.Message
                };
                return(BadRequest(result));
            }
            catch (EntityValidationException ex)
            {
                result.Errors = ex.ValidationResults.Select(x => x.ErrorMessage);
                return(BadRequest(result));
            }
            catch (Exception ex)
            {
                result.Errors = new List <string> {
                    ex.Message
                };
                _logger.Error(ex, "Error adding contact");
                return(BadRequest(result));
            }
        }
        public void ValidIdGeneratesValidObject()
        {
            var id = new IdValueObject();

            var vo = new IdValueObject(id.Value);

            Assert.NotNull(vo);
            Assert.AreEqual(id.Value, vo.Value);
        }
        public void ObjectsWithSameValueAreEquals()
        {
            var id = new IdValueObject();

            var vo1 = new IdValueObject(id.Value);
            var vo2 = new IdValueObject(id.Value);

            Assert.NotNull(vo1);
            Assert.NotNull(vo2);
            Assert.AreEqual(vo1, vo2);
        }
Example #4
0
        public bool ExistsContactWithName(ContactNameValueObject name, IdValueObject ignoredId = null)
        {
            if (name == null)
            {
                throw new InvalidParametersException("Invalid name");
            }

            return(Context
                   .Contacts
                   .Any(x => x.FirstName == name.FirstName && x.LastName == name.LastName && (ignoredId == null || x.Id != ignoredId.Value)));
        }
        public void DeleteWithInvalidContactThrowException()
        {
            var id = new IdValueObject();

            repo.Setup(x => x.GetById(It.Is <IdValueObject>(p => p == id))).Returns <ContactEntity>(null);
            var cmd     = new DeleteContactCommand(id.Value);
            var handler = new DeleteContactCommandHandler(uow.Object, eventBus.Object, repo.Object);

            Assert.Throws <EntityNotFound>(() => handler.Handle(cmd, new System.Threading.CancellationToken()));
            repo.VerifyAll();
        }
        public ContactEntity(IdValueObject id, ContactNameValueObject name)
        {
            if (id == null || name == null)
            {
                throw new DomainException("Invalid paramaters");
            }

            Id              = id;
            Name            = name;
            _emailAddresses = new List <EmailValueObject>();
            _phoneNumbers   = new List <PhoneValueObject>();
        }
Example #7
0
        public ContactEntity GetById(IdValueObject id)
        {
            if (id == null)
            {
                throw new InvalidParametersException("Invalid contact id");
            }

            var contact = Context
                          .Contacts
                          .Include(x => x.Emails)
                          .Include(x => x.Phones)
                          .SingleOrDefault(x => x.Id == id.Value);

            return(contact?.ToEntity());
        }
Example #8
0
        public void Delete(IdValueObject id)
        {
            if (id == null)
            {
                throw new InvalidParametersException("Invalid contact id");
            }

            var contact = Context.Contacts.SingleOrDefault(x => x.Id == id.Value);

            if (contact == null)
            {
                throw new InvalidEntityException("Invalid entity");
            }

            Context.Contacts.Remove(contact);
        }
Example #9
0
        /// <summary>
        /// 通过校验定义Schema, 执行校验
        /// </summary>
        /// <param name="validationSchema"></param>
        /// <param name="value"></param>
        /// <param name="errHint"></param>
        /// <returns></returns>
        public static bool InvokeValid(ValidationSchema validationSchema, string value, CheckVarHandle checkHandl, out string errHint)
        {
            List <ValidationItemSchema> items = validationSchema.Validators;



            for (int i = 0; i < items.Count; i++)
            {
                ValidationItemSchema          item    = items[i];
                IdsObjectList <IdValueObject> options = new IdsObjectList <IdValueObject>();
                for (int j = 0; j < item.Options.Count; j++)
                {
                    IdValueObject op    = item.Options[j];
                    IdValueObject newOp = new IdValueObject();
                    newOp.Id    = op.Id;
                    newOp.Value = op.Value;
                    if (checkHandl != null)
                    {
                        newOp.Value = checkHandl(newOp.Value).ToString();
                    }
                    options.Add(newOp);
                }



                Validator validator = GetValidator(item.ValidatorName);

                ActionUtils.SetObjProperties(validator, options);
                errHint = validator.ErrHint;
                if (!validator.Check(value))
                {
                    return(false);
                }
            }

            errHint = "";
            return(true);
        }