add initial appointmentsmanager
This commit is contained in:
94
AppointmentsManager/AppointmentsLib/Models/Appointment.cs
Normal file
94
AppointmentsManager/AppointmentsLib/Models/Appointment.cs
Normal file
@@ -0,0 +1,94 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AppointmentsLib.Models
|
||||
{
|
||||
public class Appointment
|
||||
{
|
||||
public readonly int AppointmentId;
|
||||
public Contact Contact;
|
||||
public DateTime Time;
|
||||
public string Description;
|
||||
|
||||
internal Appointment(int appointmentId, int contactId, string time, string description)
|
||||
{
|
||||
AppointmentId = appointmentId;
|
||||
Contact = Contact.GetContactById(contactId);
|
||||
Time = DateTime.Parse(time);
|
||||
Description = description;
|
||||
}
|
||||
|
||||
public static IEnumerable<Appointment> GetAppointments()
|
||||
{
|
||||
var reader = Database.Execute("SELECT appointmentId, contactId, time, description FROM Appointment;").ExecuteReader();
|
||||
|
||||
List<Appointment> appointments = new();
|
||||
|
||||
while (reader.Read())
|
||||
{
|
||||
appointments.Add(new Appointment(reader.GetInt32(0), reader.GetInt32(1), reader.GetString(2), reader.GetString(3)));
|
||||
}
|
||||
|
||||
reader.Close();
|
||||
return appointments;
|
||||
}
|
||||
|
||||
public static Appointment GetAppointmentById(int appointmentId)
|
||||
{
|
||||
var cmd = Database.Execute($"SELECT appointmentId, contactId, time, description FROM Appointment WHERE appointmentId = @id;");
|
||||
|
||||
cmd.Parameters.AddWithValue("id", appointmentId);
|
||||
|
||||
var reader = cmd.ExecuteReader();
|
||||
|
||||
while (reader.Read())
|
||||
{
|
||||
var appointment = new Appointment(reader.GetInt32(0), reader.GetInt32(1), reader.GetString(2), reader.GetString(3));
|
||||
reader.Close();
|
||||
return appointment;
|
||||
}
|
||||
|
||||
throw new Exception("salutation does not exist");
|
||||
}
|
||||
|
||||
public static Appointment Create(int contactId, DateTime time, string description)
|
||||
{
|
||||
var cmd = Database.Execute($"INSERT INTO Appointment (contactId, time, description) VALUES (@contact, @time, @description)");
|
||||
|
||||
cmd.Parameters.AddWithValue("contact", contactId);
|
||||
cmd.Parameters.AddWithValue("time", time);
|
||||
cmd.Parameters.AddWithValue("description", description);
|
||||
|
||||
cmd.ExecuteNonQuery();
|
||||
|
||||
int appointmentId = (int)cmd.LastInsertedId;
|
||||
|
||||
return new Appointment(appointmentId, contactId, time.ToString(), description);
|
||||
}
|
||||
|
||||
public void Save()
|
||||
{
|
||||
var cmd = Database.Execute("UPDATE Appointment SET contactId = @contact, time = @time, description = @description WHERE appointmentId = @id");
|
||||
|
||||
cmd.Parameters.AddWithValue("contact", Contact.ContactId);
|
||||
cmd.Parameters.AddWithValue("time", Time);
|
||||
cmd.Parameters.AddWithValue("description", Description);
|
||||
|
||||
cmd.Parameters.AddWithValue("id", AppointmentId);
|
||||
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
public void Delete()
|
||||
{
|
||||
var cmd = Database.Execute("DELETE FROM Appointment WHERE appointmentId = @id");
|
||||
|
||||
cmd.Parameters.AddWithValue("id", AppointmentId);
|
||||
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
}
|
||||
87
AppointmentsManager/AppointmentsLib/Models/Company.cs
Normal file
87
AppointmentsManager/AppointmentsLib/Models/Company.cs
Normal file
@@ -0,0 +1,87 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using MySqlConnector;
|
||||
|
||||
namespace AppointmentsLib.Models
|
||||
{
|
||||
public class Company
|
||||
{
|
||||
public readonly int CompanyId;
|
||||
public string Label;
|
||||
|
||||
internal Company(int companyId, string label)
|
||||
{
|
||||
CompanyId = companyId;
|
||||
Label = label;
|
||||
}
|
||||
|
||||
public static IEnumerable<Company> GetCompanies()
|
||||
{
|
||||
var reader = Database.Execute("SELECT companyId, label FROM Company;").ExecuteReader();
|
||||
|
||||
List<Company> companies = new();
|
||||
|
||||
while (reader.Read())
|
||||
{
|
||||
companies.Add(new Company(reader.GetInt32(0), reader.GetString(1)));
|
||||
}
|
||||
|
||||
reader.Close();
|
||||
return companies;
|
||||
}
|
||||
|
||||
public static Company GetCompanyById(int companyId)
|
||||
{
|
||||
var cmd = Database.Execute($"SELECT companyId, label FROM Company WHERE companyId = @id;");
|
||||
|
||||
cmd.Parameters.AddWithValue("id", companyId);
|
||||
|
||||
var reader = cmd.ExecuteReader();
|
||||
|
||||
while (reader.Read())
|
||||
{
|
||||
var company = new Company(reader.GetInt32(0), reader.GetString(1));
|
||||
reader.Close();
|
||||
return company;
|
||||
}
|
||||
|
||||
throw new Exception("company does not exist");
|
||||
}
|
||||
|
||||
public static Company Create(string label)
|
||||
{
|
||||
var cmd = Database.Execute($"INSERT INTO Company (label) VALUES (@label)");
|
||||
|
||||
cmd.Parameters.AddWithValue("label", label);
|
||||
|
||||
cmd.ExecuteNonQuery();
|
||||
|
||||
int companyId = (int)cmd.LastInsertedId;
|
||||
|
||||
return new Company(companyId, label);
|
||||
}
|
||||
|
||||
public void Save()
|
||||
{
|
||||
var cmd = Database.Execute("UPDATE Company SET label = @label WHERE companyId = @companyId");
|
||||
|
||||
cmd.Parameters.AddWithValue("label", Label);
|
||||
cmd.Parameters.AddWithValue("companyId", CompanyId);
|
||||
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
public void Delete()
|
||||
{
|
||||
var cmd = Database.Execute("DELETE FROM Company WHERE companyId = @companyId");
|
||||
|
||||
cmd.Parameters.AddWithValue("companyId", CompanyId);
|
||||
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
}
|
||||
134
AppointmentsManager/AppointmentsLib/Models/Contact.cs
Normal file
134
AppointmentsManager/AppointmentsLib/Models/Contact.cs
Normal file
@@ -0,0 +1,134 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AppointmentsLib.Models
|
||||
{
|
||||
public class Contact
|
||||
{
|
||||
private static string primaryKey = "contactId";
|
||||
private static List<string> fields = new()
|
||||
{
|
||||
"salutationId",
|
||||
"firstname",
|
||||
"surname",
|
||||
"street",
|
||||
"postalId",
|
||||
"phone",
|
||||
"phoneTypeId",
|
||||
"mobil",
|
||||
"companyId",
|
||||
"department"
|
||||
};
|
||||
|
||||
public readonly int ContactId;
|
||||
public Salutation Salutation;
|
||||
public string FirstName;
|
||||
public string LastName;
|
||||
public string Street;
|
||||
public Postal Postal;
|
||||
public string Phone;
|
||||
public PhoneType PhoneType;
|
||||
public string Mobil;
|
||||
public Company Company;
|
||||
public string Department;
|
||||
|
||||
internal Contact(int contactId, int salutationId, string firstName, string lastName, string street, int postalId, string phone, int phoneTypeId, string mobil, int companyId, string department)
|
||||
{
|
||||
ContactId = contactId;
|
||||
FirstName = firstName;
|
||||
LastName = lastName;
|
||||
Street = street;
|
||||
Phone = phone;
|
||||
Mobil = mobil;
|
||||
Department = department;
|
||||
|
||||
Company = Company.GetCompanyById(companyId);
|
||||
}
|
||||
|
||||
public static IEnumerable<Contact> GetContacts()
|
||||
{
|
||||
var reader = Database.Execute($"SELECT {primaryKey}, {string.Join(", ", fields.ToArray())} FROM Contact;").ExecuteReader();
|
||||
|
||||
List<Contact> contacts = new();
|
||||
|
||||
while (reader.Read())
|
||||
{
|
||||
contacts.Add(new Contact(reader.GetInt32(0), reader.GetInt32(1), reader.GetString(2), reader.GetString(3), reader.GetString(4), reader.GetInt32(4), reader.GetString(5), reader.GetInt32(6), reader.GetString(7), reader.GetInt32(8), reader.GetString(9)));
|
||||
}
|
||||
|
||||
reader.Close();
|
||||
return contacts;
|
||||
}
|
||||
|
||||
public static Contact GetContactById(int contactId)
|
||||
{
|
||||
var cmd = Database.Execute($"SELECT {primaryKey}, {string.Join(", ", fields.ToArray())} FROM Contact WHERE {primaryKey} = @id;");
|
||||
|
||||
cmd.Parameters.AddWithValue("id", contactId);
|
||||
|
||||
var reader = cmd.ExecuteReader();
|
||||
|
||||
while (reader.Read())
|
||||
{
|
||||
var contact = new Contact(reader.GetInt32(0), reader.GetInt32(1), reader.GetString(2), reader.GetString(3), reader.GetString(4), reader.GetInt32(4), reader.GetString(5), reader.GetInt32(6), reader.GetString(7), reader.GetInt32(8), reader.GetString(9));
|
||||
reader.Close();
|
||||
return contact;
|
||||
}
|
||||
|
||||
throw new Exception("contact does not exist");
|
||||
}
|
||||
|
||||
public static Contact Create(int salutationId, string firstName, string lastName, string street, int postalId, string phone, int phoneTypeId, string mobil, int companyId, string department)
|
||||
{
|
||||
var cmd = Database.Execute($"INSERT INTO Contact ({string.Join(", ", fields.ToArray())}) VALUES ({string.Join(", ", fields.Select(delegate(string name) { return $"@{name}"; }).ToArray())})");
|
||||
|
||||
cmd.Parameters.AddWithValue("salutationId", salutationId);
|
||||
cmd.Parameters.AddWithValue("firstname", firstName);
|
||||
cmd.Parameters.AddWithValue("surname", lastName);
|
||||
cmd.Parameters.AddWithValue("street", street);
|
||||
cmd.Parameters.AddWithValue("postalId", postalId);
|
||||
cmd.Parameters.AddWithValue("phone", phone);
|
||||
cmd.Parameters.AddWithValue("phoneTypeId", phoneTypeId);
|
||||
cmd.Parameters.AddWithValue("mobil", mobil);
|
||||
cmd.Parameters.AddWithValue("companyId", companyId);
|
||||
cmd.Parameters.AddWithValue("department", department);
|
||||
|
||||
cmd.ExecuteNonQuery();
|
||||
|
||||
int contactId = (int)cmd.LastInsertedId;
|
||||
|
||||
return new Contact(contactId, salutationId, firstName, lastName, street, postalId, phone, phoneTypeId, mobil, companyId, department);
|
||||
}
|
||||
|
||||
public void Save()
|
||||
{
|
||||
var cmd = Database.Execute($"UPDATE Company SET {string.Join(", ", fields.Select(delegate(string name) { return $"@{name}"; }).ToArray())} WHERE {primaryKey} = @id");
|
||||
|
||||
cmd.Parameters.AddWithValue("id", ContactId);
|
||||
cmd.Parameters.AddWithValue("salutationId", Salutation.SalutationId); // Salutation.salutationId
|
||||
cmd.Parameters.AddWithValue("firstname", FirstName);
|
||||
cmd.Parameters.AddWithValue("surname", LastName);
|
||||
cmd.Parameters.AddWithValue("street", Street);
|
||||
cmd.Parameters.AddWithValue("postalId", Postal.PostalId); // Postal.postalId
|
||||
cmd.Parameters.AddWithValue("phone", Phone);
|
||||
cmd.Parameters.AddWithValue("phoneTypeId", PhoneType.PhoneTypeId); // PhoneType.phoneTypeId
|
||||
cmd.Parameters.AddWithValue("mobil", Mobil);
|
||||
cmd.Parameters.AddWithValue("companyId", Company.CompanyId);
|
||||
cmd.Parameters.AddWithValue("department", Department);
|
||||
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
public void Delete()
|
||||
{
|
||||
var cmd = Database.Execute($"DELETE FROM Contact WHERE {primaryKey} = @id");
|
||||
|
||||
cmd.Parameters.AddWithValue("id", ContactId);
|
||||
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
}
|
||||
85
AppointmentsManager/AppointmentsLib/Models/PhoneType.cs
Normal file
85
AppointmentsManager/AppointmentsLib/Models/PhoneType.cs
Normal file
@@ -0,0 +1,85 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AppointmentsLib.Models
|
||||
{
|
||||
public class PhoneType
|
||||
{
|
||||
public readonly int PhoneTypeId;
|
||||
public string Label;
|
||||
|
||||
internal PhoneType(int phoneTypeId, string label)
|
||||
{
|
||||
PhoneTypeId = phoneTypeId;
|
||||
Label = label;
|
||||
}
|
||||
|
||||
public static IEnumerable<PhoneType> GetPhoneTypes()
|
||||
{
|
||||
var reader = Database.Execute("SELECT phoneTypeId, label FROM PhoneType;").ExecuteReader();
|
||||
|
||||
List<PhoneType> phoneTypes = new();
|
||||
|
||||
while (reader.Read())
|
||||
{
|
||||
phoneTypes.Add(new PhoneType(reader.GetInt32(0), reader.GetString(1)));
|
||||
}
|
||||
|
||||
reader.Close();
|
||||
return phoneTypes;
|
||||
}
|
||||
|
||||
public static PhoneType GetPhoneTypeById(int companyId)
|
||||
{
|
||||
var cmd = Database.Execute($"SELECT phoneTypeId, label FROM PhoneType WHERE phoneTypeId = @id;");
|
||||
|
||||
cmd.Parameters.AddWithValue("id", companyId);
|
||||
|
||||
var reader = cmd.ExecuteReader();
|
||||
|
||||
while (reader.Read())
|
||||
{
|
||||
var phoneType = new PhoneType(reader.GetInt32(0), reader.GetString(1));
|
||||
reader.Close();
|
||||
return phoneType;
|
||||
}
|
||||
|
||||
throw new Exception("phonetype does not exist");
|
||||
}
|
||||
|
||||
public static PhoneType Create(string label)
|
||||
{
|
||||
var cmd = Database.Execute($"INSERT INTO PhoneType (label) VALUES (@label)");
|
||||
|
||||
cmd.Parameters.AddWithValue("label", label);
|
||||
|
||||
cmd.ExecuteNonQuery();
|
||||
|
||||
int phoneTypeId = (int)cmd.LastInsertedId;
|
||||
|
||||
return new PhoneType(phoneTypeId, label);
|
||||
}
|
||||
|
||||
public void Save()
|
||||
{
|
||||
var cmd = Database.Execute("UPDATE PhoneType SET label = @label WHERE phoneTypeId = @id");
|
||||
|
||||
cmd.Parameters.AddWithValue("label", Label);
|
||||
cmd.Parameters.AddWithValue("id", PhoneTypeId);
|
||||
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
public void Delete()
|
||||
{
|
||||
var cmd = Database.Execute("DELETE FROM PhoneType WHERE phoneTypeId = @id");
|
||||
|
||||
cmd.Parameters.AddWithValue("id", PhoneTypeId);
|
||||
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
}
|
||||
85
AppointmentsManager/AppointmentsLib/Models/Postal.cs
Normal file
85
AppointmentsManager/AppointmentsLib/Models/Postal.cs
Normal file
@@ -0,0 +1,85 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AppointmentsLib.Models
|
||||
{
|
||||
public class Postal
|
||||
{
|
||||
public readonly int PostalId;
|
||||
public string Label;
|
||||
|
||||
internal Postal(int postalId, string label)
|
||||
{
|
||||
PostalId = postalId;
|
||||
Label = label;
|
||||
}
|
||||
|
||||
public static IEnumerable<Postal> GetPostals()
|
||||
{
|
||||
var reader = Database.Execute("SELECT postalId, label FROM Postal;").ExecuteReader();
|
||||
|
||||
List<Postal> postals = new();
|
||||
|
||||
while (reader.Read())
|
||||
{
|
||||
postals.Add(new Postal(reader.GetInt32(0), reader.GetString(1)));
|
||||
}
|
||||
|
||||
reader.Close();
|
||||
return postals;
|
||||
}
|
||||
|
||||
public static Postal GetPostalById(int postalId)
|
||||
{
|
||||
var cmd = Database.Execute($"SELECT postalId, label FROM Postal WHERE postalId = @id;");
|
||||
|
||||
cmd.Parameters.AddWithValue("id", postalId);
|
||||
|
||||
var reader = cmd.ExecuteReader();
|
||||
|
||||
while (reader.Read())
|
||||
{
|
||||
var postal = new Postal(reader.GetInt32(0), reader.GetString(1));
|
||||
reader.Close();
|
||||
return postal;
|
||||
}
|
||||
|
||||
throw new Exception("postal does not exist");
|
||||
}
|
||||
|
||||
public static Postal Create(string label)
|
||||
{
|
||||
var cmd = Database.Execute($"INSERT INTO Postal (label) VALUES (@label)");
|
||||
|
||||
cmd.Parameters.AddWithValue("label", label);
|
||||
|
||||
cmd.ExecuteNonQuery();
|
||||
|
||||
int postalId = (int)cmd.LastInsertedId;
|
||||
|
||||
return new Postal(postalId, label);
|
||||
}
|
||||
|
||||
public void Save()
|
||||
{
|
||||
var cmd = Database.Execute("UPDATE Postal SET label = @label WHERE postalId = @id");
|
||||
|
||||
cmd.Parameters.AddWithValue("label", Label);
|
||||
cmd.Parameters.AddWithValue("id", PostalId);
|
||||
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
public void Delete()
|
||||
{
|
||||
var cmd = Database.Execute("DELETE FROM Postal WHERE postalId = @id");
|
||||
|
||||
cmd.Parameters.AddWithValue("id", PostalId);
|
||||
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
}
|
||||
85
AppointmentsManager/AppointmentsLib/Models/Salutation.cs
Normal file
85
AppointmentsManager/AppointmentsLib/Models/Salutation.cs
Normal file
@@ -0,0 +1,85 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AppointmentsLib.Models
|
||||
{
|
||||
public class Salutation
|
||||
{
|
||||
public readonly int SalutationId;
|
||||
public string Label;
|
||||
|
||||
internal Salutation(int salutationId, string label)
|
||||
{
|
||||
SalutationId = salutationId;
|
||||
Label = label;
|
||||
}
|
||||
|
||||
public static IEnumerable<Salutation> GetSalutations()
|
||||
{
|
||||
var reader = Database.Execute("SELECT salutationId, label FROM Salutation;").ExecuteReader();
|
||||
|
||||
List<Salutation> salutations = new();
|
||||
|
||||
while (reader.Read())
|
||||
{
|
||||
salutations.Add(new Salutation(reader.GetInt32(0), reader.GetString(1)));
|
||||
}
|
||||
|
||||
reader.Close();
|
||||
return salutations;
|
||||
}
|
||||
|
||||
public static Salutation GetSalutationById(int salutationId)
|
||||
{
|
||||
var cmd = Database.Execute($"SELECT salutationId, label FROM Salutation WHERE salutationId = @id;");
|
||||
|
||||
cmd.Parameters.AddWithValue("id", salutationId);
|
||||
|
||||
var reader = cmd.ExecuteReader();
|
||||
|
||||
while (reader.Read())
|
||||
{
|
||||
var salutation = new Salutation(reader.GetInt32(0), reader.GetString(1));
|
||||
reader.Close();
|
||||
return salutation;
|
||||
}
|
||||
|
||||
throw new Exception("salutation does not exist");
|
||||
}
|
||||
|
||||
public static Salutation Create(string label)
|
||||
{
|
||||
var cmd = Database.Execute($"INSERT INTO Salutation (label) VALUES (@label)");
|
||||
|
||||
cmd.Parameters.AddWithValue("label", label);
|
||||
|
||||
cmd.ExecuteNonQuery();
|
||||
|
||||
int salutationId = (int)cmd.LastInsertedId;
|
||||
|
||||
return new Salutation(salutationId, label);
|
||||
}
|
||||
|
||||
public void Save()
|
||||
{
|
||||
var cmd = Database.Execute("UPDATE Salutation SET label = @label WHERE salutationId = @id");
|
||||
|
||||
cmd.Parameters.AddWithValue("label", Label);
|
||||
cmd.Parameters.AddWithValue("id", SalutationId);
|
||||
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
public void Delete()
|
||||
{
|
||||
var cmd = Database.Execute("DELETE FROM Salutation WHERE salutationId = @id");
|
||||
|
||||
cmd.Parameters.AddWithValue("id", SalutationId);
|
||||
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user