tesses-cms/Tesses.CMS.Providers.Dapper/Class1.cs

299 lines
10 KiB
C#

using System;
using System.Collections.Generic;
using Tesses.CMS;
using Dapper;
using System.Data.Common;
using System.Linq;
using Newtonsoft.Json;
namespace Tesses.CMS.Providers
{
public class MysqlDapperContentProvider : IContentProvider
{
DbConnection con;
public MysqlDapperContentProvider(DbConnection connection)
{
this.con = connection;
con.Execute("CREATE TABLE IF NOT EXISTS Users (Id bigint NOT NULL PRIMARY KEY AUTOINCREMENT,AboutMe text,AccountsToMail text,Email text,IsAdmin bool,IsInvited bool,IsVerified bool,PasswordHash text,ProperName text,Salt text,Username text,Webhooks text);");
con.Execute("CREATE TABLE IF NOT EXISTS Sessions (Id bigint NOT NULL PRIMARY KEY AUTOINCREMENT,Session text,Account bigint);");
con.Execute("CREATE TABLE IF NOT EXISTS VerificationCodes (Id bigint NOT NULL PRIMARY KEY AUTOINCREMENT,Session text,Account bigint,Expires bigint);");
con.Execute("CREATE TABLE IF NOT EXISTS Movies (Id bigint NOT NULL PRIMARY KEY AUTOINCREMENT,UserId bigint,CreationTime bigint,LastUpdated bigint,Description text,Name text,ProperName text);");
}
public void ChangeSession(string session, long account)
{
con.Execute("UPDATE Sessions set Account = @account WHERE Session = @session;",new{session,account});
}
public bool ContainsSession(string cookie)
{
return con.QueryFirstOrDefault("SELECT * FROM Sessions WHERE Session = @cookie;",new{cookie})!=null;
}
public bool ContainsVerificationCode(string code)
{
var res=con.QueryFirstOrDefault("SELECT * FROM VerificationCodes WHERE Session = @code;",new{code});
if(res != null)
{
long expires = res.Expires;
if(DateTimeOffset.Now>= DateTimeOffset.FromUnixTimeSeconds(expires))
{
con.Execute("DELETE FROM VerificationCodes WHERE Session = @code;",new{code});
return false;
}
return true;
}
return false;
}
public Movie CreateMovie(string user, string movie, string properName, string description)
{
var userId=GetUserAccount(user).Id;
Movie movie1=new Movie();
movie1.UserId = userId;
movie1.Name = movie;
movie1.ProperName=properName;
movie1.Description=description;
con.Execute("INSERT INTO Movies(movie) values (@movie);",new{movie=new DapperMovie(movie1)});
return GetMovie(user,movie);
}
public void CreateSession(string session, long account)
{
con.Execute("INSERT INTO Sessions(Session,Account) values (@session,@account);",new{session,account});
}
public void CreateUser(CMSConfiguration configuration, string user, string properName, string email, string password)
{
bool first=con.QueryFirstOrDefault<DapperUserAccount>("SELECT * FROM Users LIMIT 0, 1;") == null;
UserAccount account=new UserAccount();
account.IsAdmin = first;
account.IsVerified = first;
account.Email =email;
account.ProperName = properName;
account.Username = user;
account.NewSalt();
account.PasswordHash = account.GetPasswordHash(password);
DapperUserAccount account1=new DapperUserAccount(account);
con.Execute("INSERT INTO Users(user) values (@user);",new{user=account1});
}
public void CreateVerificationCode(string code, long account)
{
long expires=DateTimeOffset.Now.AddDays(1).ToUnixTimeSeconds();
con.Execute("INSERT INTO VerificationCodes(Session,Account,Expires) values (@code,@account,@expires);",new{code,account,expires});
}
public void DeleteSession(string session)
{
con.Execute("DELETE FROM Sessions WHERE Session = @session;",new{session});
}
public void DeleteVerificationCode(string code)
{
con.Execute("DELETE FROM VerificationCodes WHERE Session = @code;",new{code});
}
public UserAccount GetFirstUser()
{
return con.QueryFirstOrDefault<DapperUserAccount>("SELECT * FROM Users LIMIT 0, 1;")?.Account;
}
public Movie GetMovie(string user, string movie)
{
long? id =GetUserAccount(user)?.Id;
if(!id.HasValue) return null;
return con.QueryFirstOrDefault<DapperMovie>("SELECT * FROM Users WHERE UserId=@id; AND Name=@movie",new{movie,id})?.MovieObject;
}
public IEnumerable<Movie> GetMovies(string user)
{
long? id =GetUserAccount(user)?.Id;
if(!id.HasValue) yield break;
foreach(var item in GetMovies(id.Value))
{
yield return item;
}
}
public IEnumerable<Movie> GetMovies(long user)
{
foreach(var item in con.Query<DapperMovie>("SELECT * FROM Movies;"))
{
yield return item.MovieObject;
}
}
public long? GetSession(string session)
{
return con.QueryFirstOrDefault("SELECT * FROM Sessions WHERE Session = @session;",new{session})?.Id;
}
public UserAccount GetUserAccount(string user)
{
return con.QueryFirstOrDefault<DapperUserAccount>("SELECT * FROM Users WHERE Username=@user;",new{user})?.Account;
}
public UserAccount GetUserById(long account)
{
return con.QueryFirstOrDefault<DapperUserAccount>("SELECT * FROM Users WHERE Id=@id;",new{id=account})?.Account;
}
public IEnumerable<UserAccount> GetUsers()
{
foreach(var user in con.Query<DapperUserAccount>("SELECT * FROM Users;"))
{
yield return user.Account;
}
}
public long? GetVerificationAccount(string code)
{
var res=con.QueryFirstOrDefault("SELECT * FROM VerificationCodes WHERE Session = @code;",new{code});
if(res != null)
{
long expires = res.Expires;
if(DateTimeOffset.Now>= DateTimeOffset.FromUnixTimeSeconds(expires))
{
con.Execute("DELETE FROM VerificationCodes WHERE Session = @code;",new{code});
return null;
}
return res.Account;
}
return null;
}
public void UpdateMovie(Movie movie)
{
DapperMovie dapperMovie=new DapperMovie(movie);
con.Execute("UPDATE Users set movie = @movie WHERE Id = @id;",new{movie=dapperMovie,id=movie.Id});
}
public void UpdateUser(UserAccount account)
{
DapperUserAccount account1=new DapperUserAccount(account);
con.Execute("UPDATE Users set user = @user WHERE Id = @id;",new{user=account1,id=account1.Id});
}
}
internal class DapperMovie
{
public long Id {get;set;}
public long UserId {get;set;}
public long CreationTime {get;set;}
public long LastUpdated {get;set;}
public string Description {get;set;}
public string Name {get;set;}
public string ProperName {get;set;}
public Movie MovieObject {
get{
return new Movie(){
UserId = UserId,
CreationTime=DateTimeOffset.FromUnixTimeSeconds(CreationTime).DateTime,
LastUpdated=DateTimeOffset.FromUnixTimeSeconds(LastUpdated).DateTime,
Description=Description,
Id=Id,
Name=Name,
ProperName=ProperName
};
}
set{
UserId=value.UserId;
CreationTime=new DateTimeOffset(value.CreationTime).ToUnixTimeSeconds();
LastUpdated=new DateTimeOffset(value.LastUpdated).ToUnixTimeSeconds();
Description=value.Description;
Id=value.Id;
Name=value.Name;
ProperName=value.ProperName;
}
}
public DapperMovie(Movie movie1)
{
MovieObject = movie1;
}
public DapperMovie()
{
}
}
internal class DapperUserAccount
{
public DapperUserAccount()
{
}
public DapperUserAccount(UserAccount account)
{
Account =account;
}
public long Id {get;set;}
public string AboutMe {get;set;}
public string AccountsToMail { get; set; }
public string Email { get; set; }
public bool IsAdmin {get;set;}
public bool IsInvited {get;set;}
public bool IsVerified {get;set;}
public string PasswordHash {get;set;}
public string ProperName {get;set;}
public string Salt {get;set;}
public string Username {get;set;}
public string Webhooks {get;set;}
public UserAccount Account {
get {
return new UserAccount(){
Id = Id,
AboutMe = AboutMe,
AccountsToMail=JsonConvert.DeserializeObject<List<MailEntry>>(AccountsToMail),
Email=Email,
IsAdmin=IsAdmin,
IsInvited=IsInvited,
IsVerified=IsVerified,
PasswordHash=PasswordHash,
ProperName=ProperName,
Salt=Salt,
Username=Username,
Webhooks = JsonConvert.DeserializeObject<List<WebHook>>(Webhooks)
};
}
set{
Id = value.Id;
AboutMe = value.AboutMe;
AccountsToMail = JsonConvert.SerializeObject(value.AccountsToMail);
Email=value.Email;
IsAdmin = value.IsAdmin;
IsInvited = value.IsInvited;
IsVerified = value.IsVerified;
PasswordHash = value.PasswordHash;
ProperName = value.ProperName;
Salt = value.Salt;
Username = value.Username;
Webhooks = JsonConvert.SerializeObject(value.Webhooks);
}
}
}
}