/// <summary>
 /// Initializes a new instance of the <see cref="LibraryModel"/> class.
 /// </summary>
 /// <param name="library">The library.</param>
 public LibraryModel(Library library)
 {
     LibraryId = library.LibraryId.ToString();
     Name = library.Name;
     Description = library.Description;
     HostWebUrl = library.HostWebUrl;
     TenantId = library.TenantId.ToString();
 }
        public LibraryConnectionResult Connect(string tenantWebUrl, Library library, string userId, string accessToken = "")
        {
            accessToken = GetAccessToken(accessToken);
            var userEmail = GetCurrentUserEmail();

            //check with provisioning service that they are authoriesed to connect
            var libraryId = library.LibraryId;

            var libraryIdWithReadPermission = _provisioningService.GetWebWhereUserHasPermissions(
                library.HostWebUrl, accessToken, SPBasePermissions.ViewListItems);

            if (libraryIdWithReadPermission == null || libraryIdWithReadPermission.Id == null || new Guid(libraryIdWithReadPermission.Id) != libraryId)
            {
                return null;
            }

            // update db connected User if they have connection
            var user = _loginSettingsService.GetUserById(new Guid(userId));
            if (user != null)
            {
                user.DefaultLibraryId = library.LibraryId;
                _loginSettingsService.Save();

                var accessInfo = new SharePointAccessInfo(library.HostWebUrl)
                {
                    AccessToken = accessToken,
                    UserEmail = userEmail
                };
                accessInfo.Update();

                var libraryConnectionResult = new LibraryConnectionResult
                {
                    Library = library,
                    AccessInfo = accessInfo
                };

                return libraryConnectionResult;
            }
            return null;
        }
        public HttpResponseMessage Create(string webUrl, Library newLibrary, string accessToken = "")
        {
            accessToken = GetAccessToken(accessToken);

            try
            {
                Common.Models.Web libraryWebModel = _provisioningService.CreateClauseLibraryWeb(webUrl, newLibrary.Name, accessToken);

                Library library = new Library
                {
                    LibraryId = new Guid(libraryWebModel.Id),
                    TenantId = newLibrary.TenantId,
                    Name = newLibrary.Name,
                    Description = newLibrary.Description,
                    HostWebUrl = webUrl
                };

                //Create Library in Database
                _loginSettingsService.Add(library);
                _loginSettingsService.Save();

                return new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(library.LibraryId.ToString()) };
            }
            catch (Exception e)
            {
                return new HttpResponseMessage(HttpStatusCode.BadRequest) {ReasonPhrase = e.Message};
            }
        }