public HttpResponseMessage SaveUserProfile(HttpRequestMessage request, UserProfileViewModel userprofile)
 {
     return(CreateHttpResponse(request, () =>
     {
         HttpResponseMessage response = null;
         if (!ModelState.IsValid)
         {
             response = request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
         }
         else
         {
             tbl_users_profiles newUserProfile = new tbl_users_profiles();
             newUserProfile.tenant_id = userprofile.tenant_id;
             newUserProfile.userid = userprofile.userid;
             newUserProfile.user_name = userprofile.user_name;
             newUserProfile.email = userprofile.email;
             newUserProfile.logo = userprofile.logo;
             newUserProfile.logo_image_type = userprofile.logo_image_type;
             newUserProfile.domain = userprofile.domain;
             _userProfileRepository.Add(newUserProfile);
             _unitOfWork.Commit();
             response = request.CreateResponse <UserProfileViewModel>(HttpStatusCode.Created, userprofile);
         }
         return response;
     }));
 }
Example #2
0
        public tbl_user CreateUser(int tenantid, string userid, string user_name, string email, string password, int[] roles, bool istenant)
        {
            var existingUser = _userRepository.GetSingleByUsername(userid);

            if (existingUser != null)
            {
                if (existingUser.user_name == user_name)
                {
                    throw new Exception("Username is already in use");
                }
                else if (existingUser.email == email)
                {
                    throw new Exception("Email is already in use");
                }
            }


            var passwordSalt = _encryptionService.CreateSalt();

            var user = new tbl_user()
            {
                tenant_id     = tenantid,
                userid        = userid,
                user_name     = user_name,
                salt          = passwordSalt,
                email         = email.ToLower(),
                is_locked     = false,
                is_tenant     = istenant,
                password      = _encryptionService.EncryptPassword(password, passwordSalt),
                date_created  = DateTime.Now,
                date_modified = DateTime.Now
            };

            var saveUserProfile = new tbl_users_profiles
            {
                tenant_id = tenantid,
                userid    = userid,
                user_name = user_name,
                email     = email.ToLower()
            };

            _userRepository.Add(user);
            _userProfileRepository.Add(saveUserProfile);
            _unitOfWork.Commit();

            if (roles != null || roles.Length > 0)
            {
                foreach (var role in roles)
                {
                    addUserToRole(user, role);
                }
            }
            _unitOfWork.Commit();

            return(user);
        }
        public HttpResponseMessage updateUserProfile(HttpRequestMessage request, UserProfileViewModel userprofile)
        {
            return(CreateHttpResponse(request, () =>
            {
                HttpResponseMessage response = null;

                if (!ModelState.IsValid)
                {
                    response = request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
                }
                else
                {
                    var existingUserProfile = _userProfileRepository.GetSingle(userprofile.id);
                    if (existingUserProfile != null)
                    {
                        existingUserProfile.userid = userprofile.userid;
                        existingUserProfile.user_name = userprofile.user_name;
                        existingUserProfile.logo = userprofile.logo;
                        existingUserProfile.logo_image_type = userprofile.logo_image_type;
                        existingUserProfile.contact_no = userprofile.contact_no;
                        existingUserProfile.alt_contact_no = userprofile.alt_contact_no;
                        existingUserProfile.domain = userprofile.domain;

                        _userProfileRepository.Edit(existingUserProfile);
                    }
                    else
                    {
                        tbl_users_profiles newUserProfile = new tbl_users_profiles();
                        newUserProfile.tenant_id = userprofile.tenant_id;
                        newUserProfile.userid = userprofile.userid;
                        newUserProfile.user_name = userprofile.user_name;
                        newUserProfile.email = userprofile.email;
                        newUserProfile.logo = userprofile.logo;
                        newUserProfile.logo_image_type = userprofile.logo_image_type;
                        newUserProfile.domain = userprofile.domain;

                        _userProfileRepository.Add(newUserProfile);
                    }
                    _unitOfWork.Commit();

                    response = request.CreateResponse(HttpStatusCode.OK);
                }

                return response;
            }));
        }