dotnet-core_mail-server/MailServer/DatabaseHelper.cs

53 lines
1.8 KiB
C#
Raw Permalink Normal View History

2016-11-27 17:38:47 +00:00
using MailServer.Exceptions;
using SMTPServer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MailServer
{
class DatabaseHelper
{
MysqlDB DBContext;
public DatabaseHelper()
{
DBContext = new MysqlDB();
DBContext.Database.EnsureCreatedAsync();
}
public Folders GetInboxFromAccount(int accountid) => GetStandardFolderFromAccount(accountid, StandardFolders.INBOX);
public Folders GetStandardFolderFromAccount(int accountid, StandardFolders folder)
{
var res = DBContext.Folders.Where(x => x.AccountId.Equals(accountid) && x.StandardFolder.Equals(folder));
if (res.Count() < 1) throw new NotFoundException("Folder for specified account not found", Exceptions.Type.FOLDER);
return res.FirstOrDefault();
}
public Accounts GetAccount(string name, string domain) => GetAccount(name, GetDomain(domain).Id);
public Accounts GetAccount(string name, int domain)
{
name = name.ToLower();
var res = DBContext.Accounts.Where(x => x.Domain.Equals(domain) && x.Name.Equals(name));
if (res.Count() < 1) throw new NotFoundException("no account found", Exceptions.Type.ACCOUNT);
return res.FirstOrDefault();
}
public Domains GetDomain(string domain)
{
domain = domain.ToLower();
var res = DBContext.Domains.Where(x => x.Domain.Equals(domain));
if (res.Count() < 1) throw new NotFoundException("no domain found", Exceptions.Type.DOMAIN);
return res.FirstOrDefault();
}
public void AddMail(Mails mail)
{
DBContext.Add(mail);
DBContext.SaveChangesAsync();
}
}
}