示例#1
0
        public void SetFav(string workbookId, bool remove)
        {
            if (_favLists == null)
            {
                _favLists = GetFavList(TsUserName, TsUserId);
            }

            if (_favLists.SiteFavsList == null)
            {
                _favLists.SiteFavsList = new List <SiteFavs>();
            }
            var curSiteExists = _favLists.SiteFavsList.Any(f => f.SiteId == TsCurrentSiteId);

            LogMessage($"{(remove? "Saving" : "Removing")} favourite for {Environment.NewLine}Site {TsCurrentSiteName} and workbook id {workbookId}");
            if (!remove)
            {
                var fwrk = new FavWorkbook {
                    WorkbookId = workbookId
                };
                if (!curSiteExists)
                {
                    var curSiteFavs = new SiteFavs {
                        SiteId = TsCurrentSiteId
                    };
                    curSiteFavs.WorkbookList = new List <FavWorkbook>();
                    curSiteFavs.WorkbookList.Add(fwrk);
                    _favLists.SiteFavsList.Add(curSiteFavs);
                }
                else
                {
                    var curSiteFavs = _favLists.SiteFavsList.First(f => f.SiteId == TsCurrentSiteId);
                    if (curSiteFavs.WorkbookList == null)
                    {
                        curSiteFavs.WorkbookList = new List <FavWorkbook>();
                    }
                    var curWrkBookExists = curSiteFavs.WorkbookList.Any(w => w.WorkbookId == workbookId);
                    if (!curWrkBookExists)
                    {
                        curSiteFavs.WorkbookList.Add(fwrk);
                    }
                }
            }
            else
            {
                if (curSiteExists)
                {
                    var curSiteFavs = _favLists.SiteFavsList.First(f => f.SiteId == TsCurrentSiteId);
                    if (curSiteFavs.WorkbookList == null)
                    {
                        curSiteFavs.WorkbookList = new List <FavWorkbook>();
                    }
                    if (curSiteFavs.WorkbookList.Any(w => w.WorkbookId == workbookId))
                    {
                        curSiteFavs.WorkbookList.RemoveAll(w => w.WorkbookId == workbookId);
                    }
                }
            }

            SaveFavLists(_favLists, TsUserName, TsUserId);
        }
示例#2
0
        private void SaveFavLists(UserFavLists favlists, string userName, string userId)
        {
            if (userName.Contains("\\"))
            {
                userName = userName.Split('\\').Last();
            }

            var xmlFilePath = Server.MapPath($"~/Resources/UserFavLists/FavList-{userName}-{userId}.xml");

            var writer = System.IO.File.Exists(xmlFilePath)
                ? System.IO.File.Open(xmlFilePath, FileMode.Truncate, FileAccess.Write)
                : System.IO.File.Open(xmlFilePath, FileMode.OpenOrCreate, FileAccess.Write);

            using (writer)
            {
                var srlzr = new XmlSerializer(typeof(UserFavLists));
                srlzr.Serialize(writer, favlists);
            }
        }