Matomat/Matomat/Model/User.cs
2013-07-12 01:09:35 +00:00

111 lines
3.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Matomat.Database.Tables;
using Matomat.Database.Query;
using Matomat.Helper;
namespace Matomat.Model
{
class User
{
private long userid;
private TableUser table;
public User(long userid)
{
this.userid = userid;
if (this.init() == false)
{
throw new ArgumentException("User wurde nich gefunden\nUserId: " + userid + " unbekannt");
}
}
private bool init()
{
this.table = new TableUser();
Dictionary<string, string> load = new Dictionary<string, string>();
load.Add("userid", userid.ToString());
this.table.load(load);
return (this.table.userid == this.userid);
}
public string GetShortname()
{
return this.table.shortname;
}
public double GetCredits()
{
return ((double)this.table.credits) / 100;
}
public long GetUserId()
{
return this.table.userid;
}
public double GetUserAll()
{
DBQuery query = Factory.getDBO().getQuery();
query.select("SUM(" + Factory.getDBO().quoteName("p.cost") + ") AS " + Factory.getDBO().quoteName("num"));
query.from(Factory.getDBO().quoteName("history", "h"));
query.leftJoin(Factory.getDBO().quoteName("product", "p") + " ON " + Factory.getDBO().quoteName("p.id") + " = " + Factory.getDBO().quoteName("h.prod"));
query.where(Factory.getDBO().quoteName("p.iscommand") + " = " + Factory.getDBO().quote(0.ToString()));
query.where(Factory.getDBO().quoteName("h.user") + " = " + Factory.getDBO().quote(this.table.id.ToString()));
Factory.getDBO().setQuery(query);
object[] row = Factory.getDBO().getResult();
if (row == null)
{
Console.WriteLine(Factory.getDBO().getError());
return 0.0;
}
return (double.Parse(row[0].ToString()))/100;
}
public bool GetAdmin()
{
return this.table.admin;
}
public void SetUserKonto(double cost)
{
int p = (int)(cost * 100);
Dictionary<string, object> k = new Dictionary<string, object>();
k.Add("credits", this.table.credits + p);
this.table.bind(k);
this.table.store();
}
public static void Add(long userid, string username, bool isadmin, string kurzel, string email)
{
TableUser u = new TableUser();
Dictionary<string, object> k = new Dictionary<string, object>();
k.Add("userid", userid);
k.Add("username", username);
k.Add("admin", isadmin);
k.Add("shortname", kurzel);
k.Add("email", email);
u.bind(k);
u.store();
}
public int GetId()
{
return this.table.id;
}
public static void Remove(int id)
{
TableUser u = new TableUser();
Dictionary<string, string> load = new Dictionary<string, string>();
load.Add("id", id.ToString());
u.load(load);
u.delete();
}
}
}