public HttpResponseMessage ListAddOrEdit([FromBody] EmailListDTO uDto)
        {
            string key;
            var    ur          = new AppUserRepository();
            var    EmailListId = 0;
            var    userId      = ur.ValidateUser(uDto.Key, out key, ref EmailListId);

            AppUserRoleRepository aur = new AppUserRoleRepository();


            if (userId > 0 && aur.IsInRole(userId, "Admin"))
            {
                var EmailList = new EmailList();
                var errors    = ValidateDtoData(uDto, EmailList);
                if (errors.Any())
                {
                    return(ProcessValidationErrors(Request, errors, key));
                }
                var NEUserId = 0;
                if (int.TryParse(uDto.EmailListId, out NEUserId))
                {
                    if (NEUserId == -1)
                    {
                        //  creating new User record
                        return(ProcessNewEmailListRecord(Request, uDto, key, EmailListId, userId));
                    }
                    else
                    {
                        //  editing existing User record
                        return(ProcessExistingEmailListRecord(Request, uDto, NEUserId, key, EmailListId, userId));
                    }
                }
                //  no idea what this is
                var msg = "invalid data structure submitted";
                return(Request.CreateResponse(HttpStatusCode.BadRequest, msg));
            }
            var message = "validation failed";

            return(Request.CreateResponse(HttpStatusCode.NotFound, message));
        }
        private HttpResponseMessage ProcessExistingEmailListRecord(HttpRequestMessage request, EmailListDTO cqDto, int contactId, string key, int EmailListId, int userId)
        {
            var ur   = new EmailListRepository();
            var user = new EmailList();

            user = ur.GetById(contactId);


            var validationErrors = GetListValidationErrors(ur, user, cqDto, EmailListId, userId);

            if (validationErrors.Any())
            {
                return(ProcessValidationErrors(request, validationErrors, key));
            }

            ur.Save(user);


            cqDto.Key = key;
            return(request.CreateResponse(HttpStatusCode.Accepted, cqDto));
        }
 private List <DbValidationError> GetListValidationErrors(EmailListRepository pr, EmailList contact, EmailListDTO cqDto, int YieldID, int userId)
 {
     contact.ProcessRecord(cqDto);
     return(pr.Validate(contact));
 }
        private HttpResponseMessage ProcessNewEmailListRecord(HttpRequestMessage request, EmailListDTO uDto, string key, int EmailListId, int userId)
        {
            var ur   = new EmailListRepository();
            var user = new EmailList();


            var validationErrors = GetListValidationErrors(ur, user, uDto, EmailListId, userId);

            if (validationErrors.Any())
            {
                return(ProcessValidationErrors(request, validationErrors, key));
            }

            user             = ur.Save(user);
            uDto.Key         = key;
            uDto.EmailListId = user.EmailListId.ToString();
            var response = request.CreateResponse(HttpStatusCode.Created, uDto);

            response.Headers.Location = new Uri(Url.Link("Default", new
            {
                id = user.EmailListId
            }));
            return(response);
        }