Пример #1
0
        public HttpResponseMessage Update(ContactSaveDto updatedContactDto)
        {
            if (!ModelState.IsValid)
            {
                return(Request.CreateResponse());
            }

            var responseModel = _contactService.Update(updatedContactDto);

            return(Request.CreateResponse(responseModel.Errors == null ? HttpStatusCode.OK : HttpStatusCode.BadRequest,
                                          responseModel));
        }
Пример #2
0
        public HttpResponseMessage Create(ContactSaveDto contactDto)
        {
            //validate the incoming Dto
            if (!ModelState.IsValid)
            {
                return(Request.CreateResponse());
            }

            var payload = _contactService.Create(contactDto);

            return(Request.CreateResponse(HttpStatusCode.OK, payload));
        }
        public ResponseModel Update(ContactSaveDto contact)
        {
            var updatedContactEntity = _unitOfWork.ContactRepository.GetById(contact.ContactId);

            //can't tell from modelstate validation if a contactid was provided in the first place.  uses the same model as create which expects a null
            //contact id.
            if (updatedContactEntity == null)
            {
                return new ResponseModel {
                           Errors = new List <string> {
                               "ContactId provided was invalid"
                           }
                }
            }
            ;

            if (_unitOfWork.CustomerRepository.GetById(contact.CustomerId) == null)
            {
                return new ResponseModel {
                           Errors = new List <string> {
                               "CustomerId provided was invalid"
                           }
                }
            }
            ;

            //update the values of our retrieved entity.
            updatedContactEntity.AlternatePhone = contact.AlternatePhone ?? "";
            updatedContactEntity.EmailAddress   = contact.EmailAddress;
            updatedContactEntity.FirstName      = contact.FirstName;
            updatedContactEntity.LastName       = contact.LastName;
            updatedContactEntity.PrimaryPhone   = contact.PrimaryPhone;

            //we need to reset other primary contacts (potentially) if the assigned customer changes or the contact wasn't previously primary
            //but only if we are currently being set to primary.
            if ((!updatedContactEntity.IsPrimary || updatedContactEntity.CustomerId != contact.CustomerId) && contact.IsPrimary)
            {
                UnsetPrimaryContact(contact.ContactId, contact.CustomerId);
            }

            updatedContactEntity.IsPrimary  = contact.IsPrimary;
            updatedContactEntity.CustomerId = contact.CustomerId;

            //perform the db update
            _unitOfWork.ContactRepository.Update(updatedContactEntity);
            var result = _unitOfWork.ContactRepository.Save();

            //create and return the successful response payload.
            return(new ResponseModel {
                Message = result > 0 ? "success" : "no records updated."
            });
        }
        public ResponseModel Create(ContactSaveDto contact)
        {
            var responseModel = new ResponseModel {
                Errors = new List <string>()
            };

            //check for a provided contact id, which is invalid.  new contacts cannot have an id specified.
            if (contact.ContactId != null)
            {
                responseModel.Errors.Add("ContactId must be null when creating a new contact");
            }

            //validate the customer id provided.  a contact must be assigned to a valid customer.
            if (_unitOfWork.CustomerRepository.GetById(contact.CustomerId) == null)
            {
                responseModel.Errors.Add("customer id does not exist");
            }

            if (responseModel.Errors.Count == 0)
            {
                //convert the incoming contact dto to a usable entity and create a skeleton response model
                var contactEntity = Mapper.Map <Contact>(contact);

                //attempt to create the contact and add the appropriate message to the response model
                _unitOfWork.ContactRepository.Create(contactEntity);

                //reset, if necessary, other contacts' IsPrimary flags
                if (contactEntity.IsPrimary)
                {
                    UnsetPrimaryContact(contactEntity.ContactId, contactEntity.CustomerId);
                }

                if (_unitOfWork.ContactRepository.Save() > 0)
                {
                    responseModel.Message = "success";
                }
                else
                {
                    responseModel.Errors.Add("failed to create contact");
                }
            }

            return(responseModel);
        }