The EmployeeModel class
module empmodel;
import mysql;
import std.conv;
import std.array;
struct Employee
{
int id; //row id or record id
string empid; //employee number
string deprt; //department
string paygd; //salary grade
string email; //email address
string pword; //password
string fname; //first name
string lname; //last name
string phone; //phone number
string photo; //ID photo
string street; //street address
string city; //city name
string province; //province name
string postcode; //postal code
}
struct Admin
{
string email; //email address
string pword; //passsword
}
string[] departments =
[
"Management and Admin",
"Accounting and Finance",
"Production",
"Maintenance",
"Shipping and Receiving",
"Purchasing and Supplies",
"IT Services",
"Human Resources",
"Marketing"
];
string[][] provinces =
[
["AB", "Alberta"],
["BC", "British Columbia"],
["MB", "Manitoba"],
["NB", "New Brunswick"],
["NL", "Newfoundland and Labrador"],
["NS", "Nova Scotia"],
["NT", "Northwest Territories"],
["NU", "Nunavut"],
["ON", "Ontario"],
["PE", "Prince Edward Island"],
["QC", "Quebec"],
["SK", "Saskatchewan"],
["YT", "Yukon Territory"]
];
string[] paygrades =
[
"A100", "A200", "A300", "A400",
"B100", "B200", "B300", "B400",
"C100", "C200", "C300", "C400",
"D100", "D200", "D300", "D400",
"E100", "E200", "E300", "E400",
"F100", "F200", "F300", "F400"
];
class EmployeeModel
{
Connection conn;
this()
{
string url = "host=localhost;port=3306;user=owner;pwd=qwerty;db=empdb";
conn = new Connection(url);
scope(exit) conn.close;
insertIntoAdmins(); // be sure to remove this after the first run!
}
void insertIntoAdmins()
{
import vibe.http.auth.digest_auth;
string email1 = "admin1@lorem.com";
string email2 = "admin2@lorem.com";
string email3 = "admin3@lorem.com";
string realm = "The Lorem Ipsum Company";
string pass1 = createDigestPassword(realm, email1, "secret");
string pass2 = createDigestPassword(realm, email2, "secret");
string pass3 = createDigestPassword(realm, email3, "secret");
string sql = "insert into admins(email, pword)
values ('" ~ email1 ~ "','" ~ pass1 ~ "')";
conn.exec(sql);
sql = "insert into admins(email, pword)
values ('" ~ email2 ~ "','" ~ pass2 ~ "')";
conn.exec(sql);
sql = "insert into admins(email, pword)
values ('" ~ email3 ~ "','" ~ pass3 ~ "')";
conn.exec(sql);
}
ulong addEmployee(Employee e)
{
string sql =
"insert into employees
(
empid,
deprt, paygd, email, pword,
fname, lname, phone, photo,
street, city, province, postcode
)
values(?,?,?,?,?,?,?,?,?,?,?,?,?)";
Prepared pstmt = conn.prepare(sql);
pstmt.setArgs
(
to!string(e.empid),
to!string(e.deprt),
to!string(e.paygd),
to!string(e.email),
to!string(e.pword),
to!string(e.fname),
to!string(e.lname),
to!string(e.phone),
to!string(e.photo),
to!string(e.street),
to!string(e.city),
to!string(e.province),
to!string(e.postcode)
);
return conn.exec(pstmt);
}
}Last updated