コード例 #1
0
        /// <summary>
        /// 【只有这个是异步的】
        /// </summary>
        /// <returns></returns>
        public static Task <ExReaderPlus.Manage.PassageManager.Passage> GetPassage(UserDictionary.Passage passageInfo)
        {
            ExReaderPlus.PassageIO.PassageIO passageIO = new ExReaderPlus.PassageIO.PassageIO();
            var passage = passageIO.ReadPassage(passageInfo);

            return(passage);
        }
コード例 #2
0
 public static ActionPassage FromDBPassage(UserDictionary.Passage psg)
 {
     return(new ActionPassage()
     {
         Id = psg.Id, Name = psg.Name, Abstract = psg.Abstract, LastReadTime = psg.LastReadTime
     });
 }
コード例 #3
0
        public async Task OpenFileTest()
        {
            var passageIO   = new PassageIO();
            var passageInfo = new UserDictionary.Passage();
            var passage     = new ExReaderPlus.Manage.PassageManager.Passage();

            passageInfo.Id  = 2;
            passage.Content = "Hello world";

            Assert.AreEqual(true, await passageIO.SavaPassage(passage, passageInfo));
        }
コード例 #4
0
        /// <summary>
        /// 删除一篇文章
        /// </summary>
        /// <param name="str"></param>
        public async Task <bool> DeletePassage(UserDictionary.Passage passage)
        {
            try
            {
                StorageFolder folder = ApplicationData.Current.LocalFolder;
                StorageFile   file   = await folder.TryGetItemAsync(passage.Id.ToString()) as StorageFile;

                await file.DeleteAsync();

                return(true);
            }
            catch (Exception e)
            {
                return(false);
            }
        }
コード例 #5
0
        /// <summary>
        /// 读一篇文章内容
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public async Task <Manage.PassageManager.Passage> ReadPassage(UserDictionary.Passage passage)
        {
            Manage.PassageManager.Passage p = new Manage.PassageManager.Passage();
            StorageFolder folder            = ApplicationData.Current.LocalFolder;
            StorageFile   file = await folder.TryGetItemAsync(passage.Id.ToString()) as StorageFile;

            if (file != null)
            {
                p.Content = await FileIO.ReadTextAsync(file);

                p.HeadName = passage.Name;
                return(p);
            }
            else
            {
                return(null);
            }
        }
コード例 #6
0
        /// <summary>
        /// 存一篇文章
        /// 传入参数,文章类和文章序列
        /// </summary>
        /// <param name="passage"></param>
        /// <param name="str1"></param>
        /// <returns></returns>
        public async Task <bool> SavaPassage(Manage.PassageManager.Passage passage, UserDictionary.Passage passageInfo)
        {
            StorageFolder applicationFolder = ApplicationData.Current.LocalFolder;

            try
            {
                StorageFile saveFile =
                    await applicationFolder.CreateFileAsync(passageInfo.Id.ToString());

                await FileIO.WriteTextAsync(saveFile, passage.Content);

                return(true);
            }
            catch
            {
                return(false);
            }
        }
コード例 #7
0
        public async Task SaveFileTask()
        {
            var passageIO   = new PassageIO();
            var passageInfo = new UserDictionary.Passage();

            passageInfo.Id = 1;


            var passage = new ExReaderPlus.Manage.PassageManager.Passage();

            passage.Content = "Hello world";
            await passageIO.SavaPassage(passage, passageInfo);

            var passage1 = new ExReaderPlus.Manage.PassageManager.Passage();

            passage1 = await passageIO.ReadPassage(passageInfo);

            Assert.AreEqual(passage1.Content, "Hello world");
        }
コード例 #8
0
 /// <summary>
 /// 传入参数:文章信息
 /// 把一篇文章从书架删除,并且删除缓存文件
 /// 成功返回 1
 /// 失败返回 0
 /// 异常返回 -1
 /// </summary>
 /// <returns></returns>
 public static int DeletePassageInfoAndPassage(UserDictionary.Passage passageInfo)
 {
     using (var db = new DataContext())
     {
         db.Passages.Remove(
             db.Passages.FirstOrDefault(p => p.Id.Equals(passageInfo.Id))
             );
         ExReaderPlus.PassageIO.PassageIO passageIO = new ExReaderPlus.PassageIO.PassageIO();
         try {
             db.SaveChanges();
             passageIO.DeletePassage(passageInfo);//删除缓存文件
             return(1);
         }
         catch
         {
             return(-1);
         }
         finally
         {
             db.Database.CloseConnection();
         }
     }
 }
コード例 #9
0
 /// <summary>
 /// 存储文章到书架,并且把文章缓存到路径
 /// 传入参数
 /// 【1】文章(包含文章内容和文章标题)
 /// 传出参数
 /// 异常 -1
 /// 成功 1
 /// 有文件、数据库操作,建议异步
 ///  FIXME TODO
 /// </summary>
 /// <param name="passage"></param>
 /// <returns></returns>
 public static int SavaPassageInfoAndPassage(ExReaderPlus.Manage.PassageManager.Passage passage)
 {
     using (var db = new DataContext())
     {
         var returnPassageInfo = ifExsist(passage);
         if (returnPassageInfo == null)//如果文章不存在,就新建文章条目
         {
             db.Database.Migrate();
             var passageInfo = new UserDictionary.Passage();
             passageInfo.Name         = passage.HeadName;
             passageInfo.LastReadTime = System.DateTime.Now;//写入时间
             passageInfo.Abstract     = getAbstract(passage.Content);
             passageInfo.RemainWords  = passage.Content.Length.ToString();
             //C#中int最大为2^32-1不考虑超过的情况
             db.Passages.Add(passageInfo);//存入文件信息
             try
             {
                 db.SaveChanges();
             }
             catch
             {
                 return(-1);
             }
             finally
             {
                 db.Database.CloseConnection();
             }
             db.Database.Migrate();
             var savePassageInfo = db.Passages                                                            //用于传入保存函数的文章信息,主要用来获取数据库自动生成的id
                                   .Where(p => p.Name.Equals(passageInfo.Name))
                                   .Where(p => p.Abstract.Equals(passageInfo.Abstract)).FirstOrDefault(); //通过文章名字和文章摘要查找文章来获取新建的ID
             ExReaderPlus.PassageIO.PassageIO passageIO = new ExReaderPlus.PassageIO.PassageIO();
             try
             {
                 passageIO.SavaPassage(passage, savePassageInfo);//根据获取的唯一ID保存文件
                 return(1);
             }
             catch
             {
                 return(-1);
             }
         }
         else
         {
             db.Passages.Find(returnPassageInfo.Id).LastReadTime = System.DateTime.Now;
             try
             {
                 db.SaveChanges();
                 return(1);
             }
             catch
             {
                 return(-1);
             }
             finally
             {
                 db.Database.CloseConnection();
             }
         }
     }
 }