PageRenderTime 189ms CodeModel.GetById 2ms app.highlight 179ms RepoModel.GetById 1ms app.codeStats 0ms

/OnlineAddressBook/server.rb

https://github.com/DerekFangming/ArchivedProjects
Ruby | 1008 lines | 974 code | 23 blank | 11 comment | 127 complexity | d78fa0204ae848bbb1ce757fce90206c MD5 | raw file
   1##
   2# This is a simple Web server, mainly for serving static content with some JavaScript
   3# in order to get started building a Web site.
   4#
   5# gpollice
   6##
   7#add the lib folder to the path
   8$: << File.expand_path(File.dirname(__FILE__) + "/lib")
   9require 'sinatra'
  10require 'pgdb'
  11enable :sessions
  12
  13set :public_folder, File.dirname(__FILE__) + '/public'
  14get '/' do
  15  session["cookie"] ||= nil
  16  erb :index
  17end
  18
  19get '/register' do
  20  erb :register
  21end
  22
  23 post '/signup' do
  24  username = params[:usernamesignup]
  25  password = params[:passwordsignup]
  26  password_confirm = params[:passwordsignup_confirm]
  27  usage = 0;
  28  conn = connectToDB(ENV['DATABASE_URL'])
  29  password_indb = "SELECT password FROM users_table WHERE username = '#{username}'"
  30  results = conn.exec(password_indb)
  31  if password_confirm == password
  32    if results.ntuples() !=0
  33      @message = "&nbsp;&nbsp;Username already exists!"
  34      erb :register
  35    else
  36      pass = "INSERT INTO users_table VALUES('#{params[:firstnamesignup]}','#{params[:lastnamesignup]}','#{username}','#{password}','#{params[:emailsignup]}','#{params[:year]}','#{params[:month]}','#{params[:day]}','#{params[:Gender]}','#{usage}')"
  37      result = conn.exec(pass)
  38      session["cookie"] = username
  39      time = Time.now.hour
  40      if time >=12 && time < 18
  41        @greeting="Good afternoon,"
  42      elsif time >=6 && time <12
  43        @greeting="Good morning,"
  44      else
  45        @greeting="It's sleep time,"
  46      end
  47      gender = "SELECT gender FROM users_table WHERE username = '#{username}'"
  48      gender_result = conn.exec(gender)
  49      if gender_result.getvalue(0,0).eql? "male"
  50        @call = "Mr.&nbsp;"
  51      else
  52        @call = "Mrs.&nbsp;"
  53      end
  54      lname = "SELECT lastname FROM users_table WHERE username = '#{username}'"
  55      lname_result = conn.exec(lname)
  56      if lname_result.getvalue(0,0).eql? ""
  57        @name = "Foo"
  58      else
  59        @name = lname_result.getvalue(0,0)
  60      end
  61      usg = "SELECT usage FROM users_table WHERE username = '#{username}'"
  62    usg_result = conn.exec(usg)
  63    usg_value = usg_result.getvalue(0,0)
  64    if usg_value.eql? "1" or usg_value.eql? "0"
  65      @usageMsg = usg_value + " contact"
  66    else
  67      @usageMsg = usg_value + " contacts"
  68    end
  69    if !usg_value.eql? "0"
  70      name_array = "SELECT name FROM contacts_table WHERE username = '#{username}'"
  71    phone_array = "SELECT phonenum FROM contacts_table WHERE username = '#{username}'"
  72    email_array = "SELECT email FROM contacts_table WHERE username = '#{username}'"
  73    msn_array = "SELECT msn FROM contacts_table WHERE username = '#{username}'"
  74    gender_array = "SELECT gender FROM contacts_table WHERE username = '#{username}'"
  75    name_array_result= conn.exec(name_array)
  76    phone_array_result= conn.exec(phone_array)
  77    email_array_result= conn.exec(email_array)
  78    msn_array_result= conn.exec(msn_array)
  79    gender_array_result= conn.exec(gender_array)
  80    $i = 0;
  81    @contactCode=""
  82    while $i < usg_value.to_i do
  83      @contactCode += "<tr class='solid'><td class='solid'>" + name_array_result.getvalue($i,0) + "</td>"
  84      @contactCode += "<td class='solid'>" + gender_array_result.getvalue($i,0) + "</td>"
  85      @contactCode += "<td class='solid'>" + phone_array_result.getvalue($i,0) + "</td>"
  86      @contactCode += "<td class='solid'>" + email_array_result.getvalue($i,0) + "</td>"
  87      @contactCode += "<td class='solid'>" + msn_array_result.getvalue($i,0) + "</td></tr>"
  88      $i += 1
  89    end
  90    end
  91      erb :home
  92    end
  93  else
  94    @message = "&nbsp;&nbsp;Try again. Two passwords are not the same."
  95    erb :register
  96  end
  97end
  98
  99post '/login' do
 100  username = params[:username]
 101  password = params[:password]
 102  conn = connectToDB(ENV['DATABASE_URL'])
 103  password_indb = "SELECT password FROM users_table WHERE username = '#{username}'"
 104  results = conn.exec(password_indb)
 105  if results.ntuples() == 0
 106    @message = "Try again. User doesn't exist!"
 107    erb :index
 108  elsif results.getvalue(0,0) == password
 109    session["cookie"] = username
 110    time = Time.now.hour
 111    if time >=12 && time < 18
 112      @greeting="Good afternoon,"
 113    elsif time >=6 && time <12
 114      @greeting="Good morning,"
 115    else
 116      @greeting="It's sleep time,"
 117    end
 118    gender = "SELECT gender FROM users_table WHERE username = '#{username}'"
 119    gender_result = conn.exec(gender)
 120    if gender_result.getvalue(0,0).eql? "male"
 121      @call = "Mr.&nbsp;"
 122    else
 123      @call = "Mrs.&nbsp;"
 124    end
 125    lname = "SELECT lastname FROM users_table WHERE username = '#{username}'"
 126    lname_result = conn.exec(lname)
 127    if lname_result.getvalue(0,0).eql? ""
 128      @name = "Foo"
 129    else
 130      @name = lname_result.getvalue(0,0)
 131    end
 132    usg = "SELECT usage FROM users_table WHERE username = '#{username}'"
 133    usg_result = conn.exec(usg)
 134    usg_value = usg_result.getvalue(0,0)
 135    if usg_value.eql? "1" or usg_value.eql? "0"
 136      @usageMsg = usg_value + " contact"
 137    else
 138      @usageMsg = usg_value + " contacts"
 139    end
 140    if !usg_value.eql? "0"
 141    name_array = "SELECT name FROM contacts_table WHERE username = '#{username}'"
 142    phone_array = "SELECT phonenum FROM contacts_table WHERE username = '#{username}'"
 143    email_array = "SELECT email FROM contacts_table WHERE username = '#{username}'"
 144    msn_array = "SELECT msn FROM contacts_table WHERE username = '#{username}'"
 145    gender_array = "SELECT gender FROM contacts_table WHERE username = '#{username}'"
 146    name_array_result= conn.exec(name_array)
 147    phone_array_result= conn.exec(phone_array)
 148    email_array_result= conn.exec(email_array)
 149    msn_array_result= conn.exec(msn_array)
 150    gender_array_result= conn.exec(gender_array)
 151    $i = 0;
 152    @contactCode=""
 153    while $i < usg_value.to_i do
 154      @contactCode += "<tr class='solid'><td class='solid'>" + name_array_result.getvalue($i,0) + "</td>"
 155      @contactCode += "<td class='solid'>" + gender_array_result.getvalue($i,0) + "</td>"
 156      @contactCode += "<td class='solid'>" + phone_array_result.getvalue($i,0) + "</td>"
 157      @contactCode += "<td class='solid'>" + email_array_result.getvalue($i,0) + "</td>"
 158      @contactCode += "<td class='solid'>" + msn_array_result.getvalue($i,0) + "</td></tr>"
 159      $i += 1
 160    end
 161    end
 162    erb :home
 163  else
 164    @message = "Try again. Password incorrest"
 165    erb :index
 166  end
 167
 168end
 169
 170get '/changeprofile' do
 171  if session["cookie"] == nil
 172    erb :index
 173    # should be somewhere else
 174  else
 175    username = session["cookie"]
 176    @usr = username
 177    conn = connectToDB(ENV['DATABASE_URL'])
 178    firstname = "SELECT firstname FROM users_table WHERE username = '#{username}'"
 179    firstname_result = conn.exec(firstname)
 180    if !firstname_result.getvalue(0,0).eql? "na"
 181      @firstname = firstname_result.getvalue(0,0)
 182    end
 183    lastname = "SELECT lastname FROM users_table WHERE username = '#{username}'"
 184    lastname_result = conn.exec(lastname)
 185    if !lastname_result.getvalue(0,0).eql? "na"
 186      @lastname = lastname_result.getvalue(0,0)
 187    end
 188    email = "SELECT email FROM users_table WHERE username = '#{username}'"
 189    email_result = conn.exec(email)
 190    if !email_result.getvalue(0,0).eql? "na"
 191      @email = email_result.getvalue(0,0)
 192    end
 193    month = "SELECT month FROM users_table WHERE username = '#{username}'"
 194    month_result = conn.exec(month)
 195    if !month_result.getvalue(0,0).eql? "na"
 196      monthNum = month_result.getvalue(0,0)
 197      if monthNum.eql? "1"
 198        @jan = "selected"
 199      elsif monthNum.eql? "2"
 200        @feb = "selected"
 201      elsif monthNum.eql? "3"
 202        @mar = "selected"
 203      elsif monthNum.eql? "4"
 204        @apr = "selected"
 205      elsif monthNum.eql? "5"
 206        @may = "selected"
 207      elsif monthNum.eql? "6"
 208        @jun = "selected"
 209      elsif monthNum.eql? "7"
 210        @jul = "selected"
 211      elsif monthNum.eql? "8"
 212        @aug = "selected"
 213      elsif monthNum.eql? "9"
 214        @sep = "selected"
 215      elsif monthNum.eql? "10"
 216        @oct = "selected"
 217      elsif monthNum.eql? "11"
 218        @nov = "selected"
 219      elsif monthNum.eql? "12"
 220        @dec = "selected"
 221      end
 222    end
 223    day = "SELECT day FROM users_table WHERE username = '#{username}'"
 224    day_result = conn.exec(day)
 225    if !day_result.getvalue(0,0).eql? "na"
 226      @day = day_result.getvalue(0,0)
 227      @dayselected = "selected"
 228    end
 229    year = "SELECT year FROM users_table WHERE username = '#{username}'"
 230    year_result = conn.exec(year)
 231    if !year_result.getvalue(0,0).eql? "na"
 232      @year = year_result.getvalue(0,0)
 233      @yearselected = "selected"
 234    end
 235    gender = "SELECT gender FROM users_table WHERE username = '#{username}'"
 236    gender_result = conn.exec(gender)
 237    if !gender_result.getvalue(0,0).eql? "na"
 238      if gender_result.getvalue(0,0).eql? "male"
 239        @male = "selected"
 240      elsif gender_result.getvalue(0,0).eql? "female"
 241        @female = "selected"
 242      end
 243    end
 244    erb :update
 245  end
 246end
 247
 248post '/saveprofileupdate' do
 249  username = session["cookie"]
 250  conn = connectToDB(ENV['DATABASE_URL'])
 251  pass = "UPDATE users_table SET firstname = '#{params[:firstnamesignup]}', lastname = '#{params[:lastnamesignup]}', email = '#{params[:emailsignup]}', year = '#{params[:year]}', month = '#{params[:month]}', day = '#{params[:day]}', gender = '#{params[:Gender]}' WHERE username = '#{username}'"
 252  result = conn.exec(pass)
 253  
 254  time = Time.now.hour
 255    if time >=12 && time < 18
 256      @greeting="Good afternoon,"
 257    elsif time >=6 && time <12
 258      @greeting="Good morning,"
 259    else
 260      @greeting="It's sleep time,"
 261    end
 262    gender = "SELECT gender FROM users_table WHERE username = '#{username}'"
 263    gender_result = conn.exec(gender)
 264    if gender_result.getvalue(0,0).eql? "male"
 265      @call = "Mr.&nbsp;"
 266    else
 267      @call = "Mrs.&nbsp;"
 268    end
 269    lname = "SELECT lastname FROM users_table WHERE username = '#{username}'"
 270    lname_result = conn.exec(lname)
 271    if lname_result.getvalue(0,0).eql? ""
 272      @name = "Foo"
 273    else
 274      @name = lname_result.getvalue(0,0)
 275    end
 276    usg = "SELECT usage FROM users_table WHERE username = '#{username}'"
 277    usg_result = conn.exec(usg)
 278    usg_value = usg_result.getvalue(0,0)
 279    if usg_value.eql? "1" or usg_value.eql? "0"
 280      @usageMsg = usg_value + " contact"
 281    else
 282      @usageMsg = usg_value + " contacts"
 283    end
 284    if !usg_value.eql? "0"
 285      name_array = "SELECT name FROM contacts_table WHERE username = '#{username}'"
 286    phone_array = "SELECT phonenum FROM contacts_table WHERE username = '#{username}'"
 287    email_array = "SELECT email FROM contacts_table WHERE username = '#{username}'"
 288    msn_array = "SELECT msn FROM contacts_table WHERE username = '#{username}'"
 289    gender_array = "SELECT gender FROM contacts_table WHERE username = '#{username}'"
 290    name_array_result= conn.exec(name_array)
 291    phone_array_result= conn.exec(phone_array)
 292    email_array_result= conn.exec(email_array)
 293    msn_array_result= conn.exec(msn_array)
 294    gender_array_result= conn.exec(gender_array)
 295    $i = 0;
 296    @contactCode=""
 297    while $i < usg_value.to_i do
 298      @contactCode += "<tr class='solid'><td class='solid'>" + name_array_result.getvalue($i,0) + "</td>"
 299      @contactCode += "<td class='solid'>" + gender_array_result.getvalue($i,0) + "</td>"
 300      @contactCode += "<td class='solid'>" + phone_array_result.getvalue($i,0) + "</td>"
 301      @contactCode += "<td class='solid'>" + email_array_result.getvalue($i,0) + "</td>"
 302      @contactCode += "<td class='solid'>" + msn_array_result.getvalue($i,0) + "</td></tr>"
 303      $i += 1
 304    end
 305    end
 306    erb :home
 307end
 308
 309post '/changepasswordupdate' do
 310  username = session["cookie"]
 311  password = params[:oldpassword]
 312  newpassword = params[:passwordsignup]
 313  newpassword_confirm = params[:passwordsignup_confirm]
 314  conn = connectToDB(ENV['DATABASE_URL'])
 315  password_indb = "SELECT password FROM users_table WHERE username = '#{username}'"
 316  results = conn.exec(password_indb)
 317  if results.getvalue(0,0) != password
 318    @message = "Incorrect old password!"
 319    erb :update
 320  elsif newpassword != newpassword_confirm
 321    @message = "New passwords are not the same!"
 322    erb :update
 323  else 
 324    pass = "UPDATE users_table SET password = '#{newpassword}' WHERE username = '#{username}'"
 325    result = conn.exec(pass)
 326    time = Time.now.hour
 327    if time >=12 && time < 18
 328      @greeting="Good afternoon,"
 329    elsif time >=6 && time <12
 330      @greeting="Good morning,"
 331    else
 332      @greeting="It's sleep time,"
 333    end
 334    gender = "SELECT gender FROM users_table WHERE username = '#{username}'"
 335    gender_result = conn.exec(gender)
 336    if gender_result.getvalue(0,0).eql? "male"
 337      @call = "Mr.&nbsp;"
 338    else
 339      @call = "Mrs.&nbsp;"
 340    end
 341    lname = "SELECT lastname FROM users_table WHERE username = '#{username}'"
 342    lname_result = conn.exec(lname)
 343    if lname_result.getvalue(0,0).eql? ""
 344      @name = "Foo"
 345    else
 346      @name = lname_result.getvalue(0,0)
 347    end
 348    usg = "SELECT usage FROM users_table WHERE username = '#{username}'"
 349    usg_result = conn.exec(usg)
 350    usg_value = usg_result.getvalue(0,0)
 351    if usg_value.eql? "1" or usg_value.eql? "0"
 352      @usageMsg = usg_value + " contact"
 353    else
 354      @usageMsg = usg_value + " contacts"
 355    end
 356    if !usg_value.eql? "0"
 357      name_array = "SELECT name FROM contacts_table WHERE username = '#{username}'"
 358    phone_array = "SELECT phonenum FROM contacts_table WHERE username = '#{username}'"
 359    email_array = "SELECT email FROM contacts_table WHERE username = '#{username}'"
 360    msn_array = "SELECT msn FROM contacts_table WHERE username = '#{username}'"
 361    gender_array = "SELECT gender FROM contacts_table WHERE username = '#{username}'"
 362    name_array_result= conn.exec(name_array)
 363    phone_array_result= conn.exec(phone_array)
 364    email_array_result= conn.exec(email_array)
 365    msn_array_result= conn.exec(msn_array)
 366    gender_array_result= conn.exec(gender_array)
 367    $i = 0;
 368    @contactCode=""
 369    while $i < usg_value.to_i do
 370      @contactCode += "<tr class='solid'><td class='solid'>" + name_array_result.getvalue($i,0) + "</td>"
 371      @contactCode += "<td class='solid'>" + gender_array_result.getvalue($i,0) + "</td>"
 372      @contactCode += "<td class='solid'>" + phone_array_result.getvalue($i,0) + "</td>"
 373      @contactCode += "<td class='solid'>" + email_array_result.getvalue($i,0) + "</td>"
 374      @contactCode += "<td class='solid'>" + msn_array_result.getvalue($i,0) + "</td></tr>"
 375      $i += 1
 376    end
 377    end
 378    erb :home
 379  end
 380end
 381
 382 post '/addcontact' do
 383  username = session["cookie"]
 384  name = params[:name]
 385  phonenum = params[:phonenum]
 386  email = params[:email]
 387  msn = params[:msn]
 388  gender = params[:Gender]
 389  conn = connectToDB(ENV['DATABASE_URL'])
 390  check_name ="SELECT phonenum FROM contacts_table WHERE username = '#{username}' AND name = '#{name}'"
 391  check_result = conn.exec(check_name)
 392  if check_result.ntuples() !=0
 393    time = Time.now.hour
 394    if time >=12 && time < 18
 395      @greeting="Good afternoon,"
 396    elsif time >=6 && time <12
 397      @greeting="Good morning,"
 398    else
 399      @greeting="It's sleep time,"
 400    end
 401    gender = "SELECT gender FROM users_table WHERE username = '#{username}'"
 402    gender_result = conn.exec(gender)
 403    if gender_result.getvalue(0,0).eql? "male"
 404      @call = "Mr.&nbsp;"
 405    else
 406      @call = "Mrs.&nbsp;"
 407    end
 408    lname = "SELECT lastname FROM users_table WHERE username = '#{username}'"
 409    lname_result = conn.exec(lname)
 410    if lname_result.getvalue(0,0).eql? ""
 411      @name = "Foo"
 412    else
 413      @name = lname_result.getvalue(0,0)
 414    end
 415    usg = "SELECT usage FROM users_table WHERE username = '#{username}'"
 416    usg_result = conn.exec(usg)
 417    usg_value = usg_result.getvalue(0,0)
 418    if usg_value.eql? "1" or usg_value.eql? "0"
 419      @usageMsg = usg_value + " contact"
 420    else
 421      @usageMsg = usg_value + " contacts"
 422    end
 423    if !usg_value.eql? "0"
 424      name_array = "SELECT name FROM contacts_table WHERE username = '#{username}'"
 425      phone_array = "SELECT phonenum FROM contacts_table WHERE username = '#{username}'"
 426      email_array = "SELECT email FROM contacts_table WHERE username = '#{username}'"
 427      msn_array = "SELECT msn FROM contacts_table WHERE username = '#{username}'"
 428      gender_array = "SELECT gender FROM contacts_table WHERE username = '#{username}'"
 429      name_array_result= conn.exec(name_array)
 430      phone_array_result= conn.exec(phone_array)
 431      email_array_result= conn.exec(email_array)
 432      msn_array_result= conn.exec(msn_array)
 433      gender_array_result= conn.exec(gender_array)
 434      $i = 0;
 435      @contactCode=""
 436      while $i < usg_value.to_i do
 437        @contactCode += "<tr class='solid'><td class='solid'>" + name_array_result.getvalue($i,0) + "</td>"
 438        @contactCode += "<td class='solid'>" + gender_array_result.getvalue($i,0) + "</td>"
 439        @contactCode += "<td class='solid'>" + phone_array_result.getvalue($i,0) + "</td>"
 440        @contactCode += "<td class='solid'>" + email_array_result.getvalue($i,0) + "</td>"
 441        @contactCode += "<td class='solid'>" + msn_array_result.getvalue($i,0) + "</td></tr>"
 442        $i += 1
 443      end
 444    end
 445    @createResult = "Name already exists!"
 446    erb :home
 447  else
 448    usage = "SELECT usage FROM users_table WHERE username = '#{username}'"
 449    usage_result = conn.exec(usage)
 450    newusg = usage_result.getvalue(0,0).to_i + 1
 451    updateusg = "UPDATE users_table SET usage = '#{newusg}' WHERE username = '#{username}'"
 452    result = conn.exec(updateusg)
 453
 454    create = "INSERT INTO contacts_table VALUES('#{username}','#{name}','#{phonenum}','#{email}','#{msn}','#{gender}')"
 455    result = conn.exec(create)
 456
 457    time = Time.now.hour
 458    if time >=12 && time < 18
 459      @greeting="Good afternoon,"
 460    elsif time >=6 && time <12
 461      @greeting="Good morning,"
 462    else
 463      @greeting="It's sleep time,"
 464    end
 465    gender = "SELECT gender FROM users_table WHERE username = '#{username}'"
 466    gender_result = conn.exec(gender)
 467    if gender_result.getvalue(0,0).eql? "male"
 468      @call = "Mr.&nbsp;"
 469    else
 470      @call = "Mrs.&nbsp;"
 471    end
 472    lname = "SELECT lastname FROM users_table WHERE username = '#{username}'"
 473    lname_result = conn.exec(lname)
 474    if lname_result.getvalue(0,0).eql? ""
 475      @name = "Foo"
 476    else
 477      @name = lname_result.getvalue(0,0)
 478    end
 479    usg = "SELECT usage FROM users_table WHERE username = '#{username}'"
 480    usg_result = conn.exec(usg)
 481    usg_value = usg_result.getvalue(0,0)
 482    if usg_value.eql? "1" or usg_value.eql? "0"
 483      @usageMsg = usg_value + " contact"
 484    else
 485      @usageMsg = usg_value + " contacts"
 486    end
 487    if !usg_value.eql? "0"
 488      name_array = "SELECT name FROM contacts_table WHERE username = '#{username}'"
 489      phone_array = "SELECT phonenum FROM contacts_table WHERE username = '#{username}'"
 490      email_array = "SELECT email FROM contacts_table WHERE username = '#{username}'"
 491      msn_array = "SELECT msn FROM contacts_table WHERE username = '#{username}'"
 492      gender_array = "SELECT gender FROM contacts_table WHERE username = '#{username}'"
 493      name_array_result= conn.exec(name_array)
 494      phone_array_result= conn.exec(phone_array)
 495      email_array_result= conn.exec(email_array)
 496      msn_array_result= conn.exec(msn_array)
 497      gender_array_result= conn.exec(gender_array)
 498      $i = 0;
 499      @contactCode=""
 500      while $i < usg_value.to_i do
 501        @contactCode += "<tr class='solid'><td class='solid'>" + name_array_result.getvalue($i,0) + "</td>"
 502        @contactCode += "<td class='solid'>" + gender_array_result.getvalue($i,0) + "</td>"
 503        @contactCode += "<td class='solid'>" + phone_array_result.getvalue($i,0) + "</td>"
 504        @contactCode += "<td class='solid'>" + email_array_result.getvalue($i,0) + "</td>"
 505        @contactCode += "<td class='solid'>" + msn_array_result.getvalue($i,0) + "</td></tr>"
 506        $i += 1
 507      end
 508    end
 509    erb :home
 510  end
 511end
 512
 513post '/searchcontact' do
 514  username = session["cookie"]
 515  name = params[:contactname]
 516  conn = connectToDB(ENV['DATABASE_URL'])
 517  check_name ="SELECT phonenum FROM contacts_table WHERE username = '#{username}' AND name = '#{name}'"
 518  check_result = conn.exec(check_name)
 519  if check_result.ntuples() == 0
 520    time = Time.now.hour
 521    if time >=12 && time < 18
 522      @greeting="Good afternoon,"
 523    elsif time >=6 && time <12
 524      @greeting="Good morning,"
 525    else
 526      @greeting="It's sleep time,"
 527    end
 528    gender = "SELECT gender FROM users_table WHERE username = '#{username}'"
 529    gender_result = conn.exec(gender)
 530    if gender_result.getvalue(0,0).eql? "male"
 531      @call = "Mr.&nbsp;"
 532    else
 533      @call = "Mrs.&nbsp;"
 534    end
 535    lname = "SELECT lastname FROM users_table WHERE username = '#{username}'"
 536    lname_result = conn.exec(lname)
 537    if lname_result.getvalue(0,0).eql? ""
 538      @name = "Foo"
 539    else
 540      @name = lname_result.getvalue(0,0)
 541    end
 542    usg = "SELECT usage FROM users_table WHERE username = '#{username}'"
 543    usg_result = conn.exec(usg)
 544    usg_value = usg_result.getvalue(0,0)
 545    if usg_value.eql? "1" or usg_value.eql? "0"
 546      @usageMsg = usg_value + " contact"
 547    else
 548      @usageMsg = usg_value + " contacts"
 549    end
 550    if !usg_value.eql? "0"
 551      name_array = "SELECT name FROM contacts_table WHERE username = '#{username}'"
 552      phone_array = "SELECT phonenum FROM contacts_table WHERE username = '#{username}'"
 553      email_array = "SELECT email FROM contacts_table WHERE username = '#{username}'"
 554      msn_array = "SELECT msn FROM contacts_table WHERE username = '#{username}'"
 555      gender_array = "SELECT gender FROM contacts_table WHERE username = '#{username}'"
 556      name_array_result= conn.exec(name_array)
 557      phone_array_result= conn.exec(phone_array)
 558      email_array_result= conn.exec(email_array)
 559      msn_array_result= conn.exec(msn_array)
 560      gender_array_result= conn.exec(gender_array)
 561      $i = 0;
 562      @contactCode=""
 563      while $i < usg_value.to_i do
 564        @contactCode += "<tr class='solid'><td class='solid'>" + name_array_result.getvalue($i,0) + "</td>"
 565        @contactCode += "<td class='solid'>" + gender_array_result.getvalue($i,0) + "</td>"
 566        @contactCode += "<td class='solid'>" + phone_array_result.getvalue($i,0) + "</td>"
 567        @contactCode += "<td class='solid'>" + email_array_result.getvalue($i,0) + "</td>"
 568        @contactCode += "<td class='solid'>" + msn_array_result.getvalue($i,0) + "</td></tr>"
 569        $i += 1
 570      end
 571    end
 572    @searchResult = "Name does not exist!"
 573    erb :home
 574  else  
 575    time = Time.now.hour
 576    if time >=12 && time < 18
 577      @greeting="Good afternoon,"
 578    elsif time >=6 && time <12
 579      @greeting="Good morning,"
 580    else
 581      @greeting="It's sleep time,"
 582    end
 583    gender = "SELECT gender FROM users_table WHERE username = '#{username}'"
 584    gender_result = conn.exec(gender)
 585    if gender_result.getvalue(0,0).eql? "male"
 586      @call = "Mr.&nbsp;"
 587    else
 588      @call = "Mrs.&nbsp;"
 589    end
 590    lname = "SELECT lastname FROM users_table WHERE username = '#{username}'"
 591    lname_result = conn.exec(lname)
 592    if lname_result.getvalue(0,0).eql? ""
 593      @name = "Foo"
 594    else
 595      @name = lname_result.getvalue(0,0)
 596    end
 597    usg = "SELECT usage FROM users_table WHERE username = '#{username}'"
 598    usg_result = conn.exec(usg)
 599    usg_value = usg_result.getvalue(0,0)
 600    if usg_value.eql? "1" or usg_value.eql? "0"
 601      @usageMsg = usg_value + " contact"
 602    else
 603      @usageMsg = usg_value + " contacts"
 604    end
 605    if !usg_value.eql? "0"
 606      phone_array = "SELECT phonenum FROM contacts_table WHERE username = '#{username}' AND name = '#{name}'"
 607      email_array = "SELECT email FROM contacts_table WHERE username = '#{username}' AND name = '#{name}'"
 608      msn_array = "SELECT msn FROM contacts_table WHERE username = '#{username}' AND name = '#{name}'"
 609      gender_array = "SELECT gender FROM contacts_table WHERE username = '#{username}' AND name = '#{name}'"
 610      phone_array_result= conn.exec(phone_array)
 611      email_array_result= conn.exec(email_array)
 612      msn_array_result= conn.exec(msn_array)
 613      gender_array_result= conn.exec(gender_array)
 614      @contactCode=""
 615        @contactCode += "<tr class='solid'><td class='solid'>" + name + "</td>"
 616        @contactCode += "<td class='solid'>" + gender_array_result.getvalue(0,0) + "</td>"
 617        @contactCode += "<td class='solid'>" + phone_array_result.getvalue(0,0) + "</td>"
 618        @contactCode += "<td class='solid'>" + email_array_result.getvalue(0,0) + "</td>"
 619        @contactCode += "<td class='solid'>" + msn_array_result.getvalue(0,0) + "</td></tr>"
 620    end
 621    @buttonCode = "
 622      <form action='return' method='get'>
 623        <span style='float: right;'><button type='submit'>Return to all contacts</button>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 624          </span>
 625      </form>"
 626    erb :home
 627  end
 628end
 629
 630get '/return' do
 631  username = session["cookie"]
 632  conn = connectToDB(ENV['DATABASE_URL'])
 633  time = Time.now.hour
 634    if time >=12 && time < 18
 635      @greeting="Good afternoon,"
 636    elsif time >=6 && time <12
 637      @greeting="Good morning,"
 638    else
 639      @greeting="It's sleep time,"
 640    end
 641    gender = "SELECT gender FROM users_table WHERE username = '#{username}'"
 642    gender_result = conn.exec(gender)
 643    if gender_result.getvalue(0,0).eql? "male"
 644      @call = "Mr.&nbsp;"
 645    else
 646      @call = "Mrs.&nbsp;"
 647    end
 648    lname = "SELECT lastname FROM users_table WHERE username = '#{username}'"
 649    lname_result = conn.exec(lname)
 650    if lname_result.getvalue(0,0).eql? ""
 651      @name = "Foo"
 652    else
 653      @name = lname_result.getvalue(0,0)
 654    end
 655    usg = "SELECT usage FROM users_table WHERE username = '#{username}'"
 656    usg_result = conn.exec(usg)
 657    usg_value = usg_result.getvalue(0,0)
 658    if usg_value.eql? "1" or usg_value.eql? "0"
 659      @usageMsg = usg_value + " contact"
 660    else
 661      @usageMsg = usg_value + " contacts"
 662    end
 663    if !usg_value.eql? "0"
 664      name_array = "SELECT name FROM contacts_table WHERE username = '#{username}'"
 665      phone_array = "SELECT phonenum FROM contacts_table WHERE username = '#{username}'"
 666      email_array = "SELECT email FROM contacts_table WHERE username = '#{username}'"
 667      msn_array = "SELECT msn FROM contacts_table WHERE username = '#{username}'"
 668      gender_array = "SELECT gender FROM contacts_table WHERE username = '#{username}'"
 669      name_array_result= conn.exec(name_array)
 670      phone_array_result= conn.exec(phone_array)
 671      email_array_result= conn.exec(email_array)
 672      msn_array_result= conn.exec(msn_array)
 673      gender_array_result= conn.exec(gender_array)
 674      $i = 0;
 675      @contactCode=""
 676      while $i < usg_value.to_i do
 677        @contactCode += "<tr class='solid'><td class='solid'>" + name_array_result.getvalue($i,0) + "</td>"
 678        @contactCode += "<td class='solid'>" + gender_array_result.getvalue($i,0) + "</td>"
 679        @contactCode += "<td class='solid'>" + phone_array_result.getvalue($i,0) + "</td>"
 680        @contactCode += "<td class='solid'>" + email_array_result.getvalue($i,0) + "</td>"
 681        @contactCode += "<td class='solid'>" + msn_array_result.getvalue($i,0) + "</td></tr>"
 682        $i += 1
 683      end
 684    end
 685    erb :home
 686end
 687
 688post '/editcontact' do
 689  username = session["cookie"]
 690  name = params[:contactname]
 691  conn = connectToDB(ENV['DATABASE_URL'])
 692  check_name ="SELECT phonenum FROM contacts_table WHERE username = '#{username}' AND name = '#{name}'"
 693  check_result = conn.exec(check_name)
 694  if check_result.ntuples() == 0
 695    time = Time.now.hour
 696    if time >=12 && time < 18
 697      @greeting="Good afternoon,"
 698    elsif time >=6 && time <12
 699      @greeting="Good morning,"
 700    else
 701      @greeting="It's sleep time,"
 702    end
 703    gender = "SELECT gender FROM users_table WHERE username = '#{username}'"
 704    gender_result = conn.exec(gender)
 705    if gender_result.getvalue(0,0).eql? "male"
 706      @call = "Mr.&nbsp;"
 707    else
 708      @call = "Mrs.&nbsp;"
 709    end
 710    lname = "SELECT lastname FROM users_table WHERE username = '#{username}'"
 711    lname_result = conn.exec(lname)
 712    if lname_result.getvalue(0,0).eql? ""
 713      @name = "Foo"
 714    else
 715      @name = lname_result.getvalue(0,0)
 716    end
 717    usg = "SELECT usage FROM users_table WHERE username = '#{username}'"
 718    usg_result = conn.exec(usg)
 719    usg_value = usg_result.getvalue(0,0)
 720    if usg_value.eql? "1" or usg_value.eql? "0"
 721      @usageMsg = usg_value + " contact"
 722    else
 723      @usageMsg = usg_value + " contacts"
 724    end
 725    if !usg_value.eql? "0"
 726      name_array = "SELECT name FROM contacts_table WHERE username = '#{username}'"
 727      phone_array = "SELECT phonenum FROM contacts_table WHERE username = '#{username}'"
 728      email_array = "SELECT email FROM contacts_table WHERE username = '#{username}'"
 729      msn_array = "SELECT msn FROM contacts_table WHERE username = '#{username}'"
 730      gender_array = "SELECT gender FROM contacts_table WHERE username = '#{username}'"
 731      name_array_result= conn.exec(name_array)
 732      phone_array_result= conn.exec(phone_array)
 733      email_array_result= conn.exec(email_array)
 734      msn_array_result= conn.exec(msn_array)
 735      gender_array_result= conn.exec(gender_array)
 736      $i = 0;
 737      @contactCode=""
 738      while $i < usg_value.to_i do
 739        @contactCode += "<tr class='solid'><td class='solid'>" + name_array_result.getvalue($i,0) + "</td>"
 740        @contactCode += "<td class='solid'>" + gender_array_result.getvalue($i,0) + "</td>"
 741        @contactCode += "<td class='solid'>" + phone_array_result.getvalue($i,0) + "</td>"
 742        @contactCode += "<td class='solid'>" + email_array_result.getvalue($i,0) + "</td>"
 743        @contactCode += "<td class='solid'>" + msn_array_result.getvalue($i,0) + "</td></tr>"
 744        $i += 1
 745      end
 746    end
 747    @editResult = "Name does not exist!"
 748    erb :home
 749  else  
 750    time = Time.now.hour
 751    if time >=12 && time < 18
 752      @greeting="Good afternoon,"
 753    elsif time >=6 && time <12
 754      @greeting="Good morning,"
 755    else
 756      @greeting="It's sleep time,"
 757    end
 758    gender = "SELECT gender FROM users_table WHERE username = '#{username}'"
 759    gender_result = conn.exec(gender)
 760    if gender_result.getvalue(0,0).eql? "male"
 761      @call = "Mr.&nbsp;"
 762    else
 763      @call = "Mrs.&nbsp;"
 764    end
 765    lname = "SELECT lastname FROM users_table WHERE username = '#{username}'"
 766    lname_result = conn.exec(lname)
 767    if lname_result.getvalue(0,0).eql? ""
 768      @name = "Foo"
 769    else
 770      @name = lname_result.getvalue(0,0)
 771    end
 772    usg = "SELECT usage FROM users_table WHERE username = '#{username}'"
 773    usg_result = conn.exec(usg)
 774    usg_value = usg_result.getvalue(0,0)
 775    if usg_value.eql? "1" or usg_value.eql? "0"
 776      @usageMsg = usg_value + " contact"
 777    else
 778      @usageMsg = usg_value + " contacts"
 779    end
 780    if !usg_value.eql? "0"
 781      phone_array = "SELECT phonenum FROM contacts_table WHERE username = '#{username}' AND name = '#{name}'"
 782      email_array = "SELECT email FROM contacts_table WHERE username = '#{username}' AND name = '#{name}'"
 783      msn_array = "SELECT msn FROM contacts_table WHERE username = '#{username}' AND name = '#{name}'"
 784      gender_array = "SELECT gender FROM contacts_table WHERE username = '#{username}' AND name = '#{name}'"
 785      phone_array_result= conn.exec(phone_array)
 786      email_array_result= conn.exec(email_array)
 787      msn_array_result= conn.exec(msn_array)
 788      gender_array_result= conn.exec(gender_array)
 789      @contactCode=""
 790        @contactCode += "<tr class='solid'><td class='solid'>" + name + "</td>"
 791        @contactCode += "<td class='solid'>" + gender_array_result.getvalue(0,0) + "</td>"
 792        @contactCode += "<td class='solid'>" + phone_array_result.getvalue(0,0) + "</td>"
 793        @contactCode += "<td class='solid'>" + email_array_result.getvalue(0,0) + "</td>"
 794        @contactCode += "<td class='solid'>" + msn_array_result.getvalue(0,0) + "</td></tr>"
 795    end
 796    phone = phone_array_result.getvalue(0,0)
 797    email = email_array_result.getvalue(0,0)
 798    msn = msn_array_result.getvalue(0,0)
 799    if gender_array_result.getvalue(0,0).eql? "male"
 800      maleselected = "selected"
 801      femaleselected = ""
 802    else
 803      femaleselected = "selected"
 804      maleselected = ""
 805    end
 806    @buttonCode = "
 807      <form action='saveeditchange' method='post'>
 808        <center><p>
 809          <label for='lname' data-icon='u'><span class='red'>*</span>Name:&nbsp;&nbsp;&nbsp;</label>
 810          <input size='20' maxlength='10' id='lname' name='lname'type='text' required='required' placeholder='Name' readonly value ='"+name+"' />
 811                </p>
 812                <p>
 813          <label for='phonenum' data-icon='u'><span class='red'>*</span>Cal:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 814            &nbsp;&nbsp;</label>
 815          <input size='20' maxlength='10' id='phonenum' name='phonenum'type='text' required='required' placeholder='Phone number' value ='" + phone +"'/>
 816                </p>
 817                <p>
 818          <label for='email' data-icon='u'>&nbsp;&nbsp;Email:&nbsp;&nbsp;&nbsp;</label>
 819          <input size='20' maxlength='20'id='email' name='email'type='email' placeholder='sample@domin.com' value ='"+email+"'/>
 820                </p>
 821                <p>
 822          <label for='msn' data-icon='u'>&nbsp;&nbsp;MSN:&nbsp;&nbsp;&nbsp;&nbsp;</label>
 823          <input size='20' maxlength='10' id='msn' name='msn' placeholder='MSN' value ='"+msn+"'/>
 824                </p>
 825                <p>
 826          <label for='gender'>&nbsp;&nbsp;Gender:</label>
 827          <select name='Gender'>
 828                    <option value='male' "+maleselected+">Male</option>
 829                    <option value='female' "+femaleselected+">Female</option>
 830                    </select>
 831                </p></center>
 832        <span style='float: right;'><button type='submit'>Save change</button>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 833          &nbsp;&nbsp;&nbsp;</span>
 834      </form>
 835      <br /><br />
 836      <form action='deletecontact' method='get'>
 837        <span style='float: right;'><button type='submit'  style='color: #FF1919;'>Delete this contact</button>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 838          &nbsp;&nbsp;&nbsp;</span>
 839      </form>
 840      <br /><br />
 841      <form action='return' method='get'>
 842        <span style='float: right;'><button type='submit'>Return to all contacts</button>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 843          &nbsp;&nbsp;&nbsp;</span>
 844      </form>
 845      "
 846      session["name"] = params[:contactname]
 847    erb :home
 848  end
 849end
 850
 851 post '/saveeditchange' do
 852  username = session["cookie"]
 853  name = params[:lname]
 854  phonenum = params[:phonenum]
 855  email = params[:email]
 856  msn = params[:msn]
 857  gender = params[:Gender]
 858  conn = connectToDB(ENV['DATABASE_URL'])
 859  update = "UPDATE contacts_table SET phonenum = '#{phonenum}', email = '#{email}', msn = '#{msn}', gender = '#{gender}' WHERE username = '#{username}' AND name = '#{name}'"
 860  update_result = conn.exec(update)
 861
 862  time = Time.now.hour
 863  if time >=12 && time < 18
 864    @greeting="Good afternoon,"
 865  elsif time >=6 && time <12
 866    @greeting="Good morning,"
 867  else
 868    @greeting="It's sleep time,"
 869  end
 870  gender = "SELECT gender FROM users_table WHERE username = '#{username}'"
 871  gender_result = conn.exec(gender)
 872  if gender_result.getvalue(0,0).eql? "male"
 873    @call = "Mr.&nbsp;"
 874  else
 875    @call = "Mrs.&nbsp;"
 876  end
 877  lname = "SELECT lastname FROM users_table WHERE username = '#{username}'"
 878  lname_result = conn.exec(lname)
 879  if lname_result.getvalue(0,0).eql? ""
 880    @name = "Foo"
 881  else
 882    @name = lname_result.getvalue(0,0)
 883  end
 884  usg = "SELECT usage FROM users_table WHERE username = '#{username}'"
 885  usg_result = conn.exec(usg)
 886  usg_value = usg_result.getvalue(0,0)
 887  if usg_value.eql? "1" or usg_value.eql? "0"
 888    @usageMsg = usg_value + " contact"
 889  else
 890    @usageMsg = usg_value + " contacts"
 891  end
 892  if !usg_value.eql? "0"
 893    name_array = "SELECT name FROM contacts_table WHERE username = '#{username}'"
 894    phone_array = "SELECT phonenum FROM contacts_table WHERE username = '#{username}'"
 895    email_array = "SELECT email FROM contacts_table WHERE username = '#{username}'"
 896    msn_array = "SELECT msn FROM contacts_table WHERE username = '#{username}'"
 897    gender_array = "SELECT gender FROM contacts_table WHERE username = '#{username}'"
 898    name_array_result= conn.exec(name_array)
 899    phone_array_result= conn.exec(phone_array)
 900    email_array_result= conn.exec(email_array)
 901    msn_array_result= conn.exec(msn_array)
 902    gender_array_result= conn.exec(gender_array)
 903    $i = 0;
 904    @contactCode=""
 905    while $i < usg_value.to_i do
 906      @contactCode += "<tr class='solid'><td class='solid'>" + name_array_result.getvalue($i,0) + "</td>"
 907      @contactCode += "<td class='solid'>" + gender_array_result.getvalue($i,0) + "</td>"
 908      @contactCode += "<td class='solid'>" + phone_array_result.getvalue($i,0) + "</td>"
 909      @contactCode += "<td class='solid'>" + email_array_result.getvalue($i,0) + "</td>"
 910      @contactCode += "<td class='solid'>" + msn_array_result.getvalue($i,0) + "</td></tr>"
 911      $i += 1
 912    end
 913  end
 914  erb :home
 915end
 916
 917get '/deletecontact' do
 918  name = session["name"]
 919  username = session["cookie"]
 920  conn = connectToDB(ENV['DATABASE_URL'])
 921  update = "DELETE FROM contacts_table WHERE username = '#{username}' AND name = '#{name}'"
 922  update_result = conn.exec(update)
 923  usage = "SELECT usage FROM users_table WHERE username = '#{username}'"
 924    usage_result = conn.exec(usage)
 925    newusg = usage_result.getvalue(0,0).to_i - 1
 926    updateusg = "UPDATE users_table SET usage = '#{newusg}' WHERE username = '#{username}'"
 927    result = conn.exec(updateusg)
 928
 929    time = Time.now.hour
 930    if time >=12 && time < 18
 931      @greeting="Good afternoon,"
 932    elsif time >=6 && time <12
 933      @greeting="Good morning,"
 934    else
 935      @greeting="It's sleep time,"
 936    end
 937    gender = "SELECT gender FROM users_table WHERE username = '#{username}'"
 938    gender_result = conn.exec(gender)
 939    if gender_result.getvalue(0,0).eql? "male"
 940      @call = "Mr.&nbsp;"
 941    else
 942      @call = "Mrs.&nbsp;"
 943    end
 944    lname = "SELECT lastname FROM users_table WHERE username = '#{username}'"
 945    lname_result = conn.exec(lname)
 946    if lname_result.getvalue(0,0).eql? ""
 947      @name = "Foo"
 948    else
 949      @name = lname_result.getvalue(0,0)
 950    end
 951    usg = "SELECT usage FROM users_table WHERE username = '#{username}'"
 952    usg_result = conn.exec(usg)
 953    usg_value = usg_result.getvalue(0,0)
 954    if usg_value.eql? "1" or usg_value.eql? "0"
 955      @usageMsg = usg_value + " contact"
 956    else
 957      @usageMsg = usg_value + " contacts"
 958    end
 959    if !usg_value.eql? "0"
 960      name_array = "SELECT name FROM contacts_table WHERE username = '#{username}'"
 961      phone_array = "SELECT phonenum FROM contacts_table WHERE username = '#{username}'"
 962      email_array = "SELECT email FROM contacts_table WHERE username = '#{username}'"
 963      msn_array = "SELECT msn FROM contacts_table WHERE username = '#{username}'"
 964      gender_array = "SELECT gender FROM contacts_table WHERE username = '#{username}'"
 965      name_array_result= conn.exec(name_array)
 966      phone_array_result= conn.exec(phone_array)
 967      email_array_result= conn.exec(email_array)
 968      msn_array_result= conn.exec(msn_array)
 969      gender_array_result= conn.exec(gender_array)
 970      $i = 0;
 971      @contactCode=""
 972      while $i < usg_value.to_i do
 973        @contactCode += "<tr class='solid'><td class='solid'>" + name_array_result.getvalue($i,0) + "</td>"
 974        @contactCode += "<td class='solid'>" + gender_array_result.getvalue($i,0) + "</td>"
 975        @contactCode += "<td class='solid'>" + phone_array_result.getvalue($i,0) + "</td>"
 976        @contactCode += "<td class='solid'>" + email_array_result.getvalue($i,0) + "</td>"
 977        @contactCode += "<td class='solid'>" + msn_array_result.getvalue($i,0) + "</td></tr>"
 978        $i += 1
 979      end
 980    end
 981    erb :home
 982end
 983
 984get '/exit' do
 985  session["cookie"] = nil
 986  erb :index
 987end
 988
 989# this route tests the database connection
 990get '/test_db' do
 991  testDBConnection(ENV['DATABASE_URL'])
 992end
 993# this route displays the SQL input form
 994get '/db_manager' do
 995  runDBShell(ENV['DATABASE_URL'])
 996end
 997# this route receives input from the SQL input form
 998post '/db_manager' do
 999  runDBShell(ENV['DATABASE_URL'], params)
1000end
1001
1002get '/env' do
1003   ENV
1004end
1005
1006get '*' do
1007  "Path: " + request.fullpath()
1008end