AutoPackage/AutoPekage/LiteDbHelper.cs

181 lines
5.2 KiB
C#
Raw Normal View History

2017-02-05 15:30:23 +08:00
using LiteDB;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace AutoPekage
{
class LiteDbHelper
{
private static string liteDbPath = Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "AutoPackage", "sittings.db");
public static void init()
{
if (!File.Exists(liteDbPath))
{
Directory.CreateDirectory(Path.GetDirectoryName(liteDbPath));
AddStmpServer("qq", "smtp.qq.com");
CreateDefaultSettings();
}
}
public static SettingsDao CreateDefaultSettings()
{
SettingsDao dao = new SettingsDao();
dao.AskAutoDeleteFile = true;
dao.AskAutoLoadFile = true;
dao.AutoDeleteFile = 0;
dao.AutoLoadFile = 0;
dao.DefaultFileName = "我的打包文件.zip";
dao.LastPath = null;
dao.SendMail = false;
dao.TitleModel = 0;
Settings = dao;
return dao;
}
public static void AddStmpServer(string serverName, string stmp)
{
var dao = new StmpServerDao();
dao.ServerName = serverName;
dao.Stmp = stmp;
AddStmpServer(dao);
}
public static void AddStmpServer(StmpServerDao dao)
{
using (var db = new LiteDatabase(liteDbPath))
{
var col = db.GetCollection<StmpServerDao>("stmpservers");
col.Insert(dao);
}
}
public static List<StmpServerDao> GetAllStmpServer()
{
using (var db = new LiteDatabase(liteDbPath))
{
var col = db.GetCollection<StmpServerDao>("stmpservers");
return col.FindAll().ToList();
}
}
public static void AddSender(string name)
{
var dao = new SenderDao();
dao.Name = name;
AddSender(dao);
}
public static void AddSender(string name, string username, string password, string nikename, string stmpServer)
{
var dao = new SenderDao();
dao.Name = name;
dao.NickName = nikename;
dao.Password = password;
dao.StmpServer = stmpServer;
dao.UserName = username;
AddSender(dao);
}
public static void AddSender(SenderDao dao)
{
using (var db = new LiteDatabase(liteDbPath))
{
var col = db.GetCollection<SenderDao>("senders");
if (dao.Id == 0)
{
col.Insert(dao);
}
else
{
col.Update(dao);
}
}
}
public static void DelSender(SenderDao dao)
{
using (var db = new LiteDatabase(liteDbPath))
{
var col = db.GetCollection<SenderDao>("senders");
col.Delete(dao.Id);
}
}
public static List<SenderDao> GetAllSender()
{
using (var db = new LiteDatabase(liteDbPath))
{
var col = db.GetCollection<SenderDao>("senders");
return col.FindAll().ToList();
}
}
public static SenderDao GetSenderById(int id)
{
using (var db = new LiteDatabase(liteDbPath))
{
var col = db.GetCollection<SenderDao>("senders");
return col.FindById(id);
}
}
public static void AddMailTo(string mailto)
{
var mailToDao = new MailToDao();
mailToDao.MailTo = mailto;
using (var db = new LiteDatabase(liteDbPath))
{
var col = db.GetCollection<MailToDao>("mailto");
col.Insert(mailToDao);
}
}
public static List<MailToDao> GetAllMailTo()
{
using (var db = new LiteDatabase(liteDbPath))
{
var col = db.GetCollection<MailToDao>("mailto");
return col.FindAll().ToList();
}
}
public static SettingsDao Settings
{
get
{
using (var db = new LiteDatabase(liteDbPath))
{
var col = db.GetCollection<SettingsDao>("settings");
var dao = col.FindOne(Query.All());
if (dao == null)
{
dao = CreateDefaultSettings();
}
return dao;
}
}
set
{
using (var db = new LiteDatabase(liteDbPath))
{
var col = db.GetCollection<SettingsDao>("settings");
if (col.Count() > 0)
{
col.Update(value);
}
else
{
col.Insert(value);
}
}
}
}
}
}