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 < ContactData > data = new ( ) ;
List < Contact > contacts = new ( ) ;
while ( reader . Read ( ) )
{
data . Add ( new ContactData ( )
{
ContactId = reader . GetInt32 ( 0 ) ,
SalutationId = reader . GetInt32 ( 1 ) ,
FirstName = reader . GetString ( 2 ) ,
LastName = reader . GetString ( 3 ) ,
Street = reader . GetString ( 4 ) ,
PostalId = reader . GetInt32 ( 5 ) ,
Phone = reader . GetString ( 6 ) ,
PhoneTypeId = reader . GetInt32 ( 7 ) ,
Mobil = reader . GetString ( 8 ) ,
CompanyId = reader . GetInt32 ( 9 ) ,
Department = reader . GetString ( 10 )
} ) ;
}
reader . Close ( ) ;
foreach ( ContactData cd in data )
{
contacts . Add ( cd . Finalize ( ) ) ;
}
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 data = new ContactData ( )
{
ContactId = reader . GetInt32 ( 0 ) ,
SalutationId = reader . GetInt32 ( 1 ) ,
FirstName = reader . GetString ( 2 ) ,
LastName = reader . GetString ( 3 ) ,
Street = reader . GetString ( 4 ) ,
PostalId = reader . GetInt32 ( 5 ) ,
Phone = reader . GetString ( 6 ) ,
PhoneTypeId = reader . GetInt32 ( 7 ) ,
Mobil = reader . GetString ( 8 ) ,
CompanyId = reader . GetInt32 ( 9 ) ,
Department = reader . GetString ( 10 )
} ;
reader . Close ( ) ;
return data . Finalize ( ) ;
}
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 ( ) ;
}
}
internal class ContactData
{
public int ContactId ;
public int SalutationId ;
public string FirstName ;
public string LastName ;
public string Street ;
public int PostalId ;
public string Phone ;
public int PhoneTypeId ;
public string Mobil ;
public int CompanyId ;
public string Department ;
public Contact Finalize ( )
{
return new Contact ( ContactId , SalutationId , FirstName , LastName , Street , PostalId , Phone , PhoneTypeId , Mobil , CompanyId , Department ) ;
}
}
}