From 816aa060c802239055ffc24efd0059c9871206b3 Mon Sep 17 00:00:00 2001 From: Gryllida A Date: Wed, 16 Jan 2013 10:09:10 +1030 Subject: [PATCH] write a proper schema.sql --- lib/Ppolls.pm | 58 ++++++++++++++++++++++++++++------------------------------ schema.sql | 6 +++--- 2 files changed, 31 insertions(+), 33 deletions(-) diff --git a/lib/Ppolls.pm b/lib/Ppolls.pm index b353038..97df28c 100644 --- a/lib/Ppolls.pm +++ b/lib/Ppolls.pm @@ -9,9 +9,7 @@ use Crypt::SaltedHash; use Data::Dumper; use Dancer::Plugin::FlashMessage; -our $VERSION = '0.1'; - - +our $VERSION = '0.2'; set 'session' => 'Simple'; set 'template' => 'template_toolkit'; @@ -87,15 +85,7 @@ get '/register' => sub { template 'register', { path => params->{'path'} }; }; -post '/register' => sub { - my @arry; - $arry[0]=params->{'username'}; - $arry[1]=params->{'password'}; - $arry[2]="user"; - $arry[3]=0; - $arry[4]=params->{'email'}; - $arry[5]=params->{'fullname'}; - +post '/register' => sub { if (params->{'username'} =~ m/\W/){ flash error => 'Please use only [A-Za-z0-9] in the username. Thank you.'; redirect uri_for('/register'); @@ -121,7 +111,14 @@ post '/register' => sub { return; } else { my $user = database('users')->do( - "insert into users values(NULL, ?, ?, ?, ?, ?, ?)",{},@arry); + "insert into users values(NULL, ?, ?, ?, ?, ?, ?)",{}, + (params->{'username'}, + params->{'password'}, + "user", + 0, + params->{'email'}, + params->{'fullname'}) + ); flash error => 'Registration of user '. params->{username}.' successful.'; template 'login', { username => (params->{'username'}), @@ -198,11 +195,12 @@ post '/vote' => sub { # User hit this question. database('questions')->do ("UPDATE entries SET hits = hits + 1 WHERE id = ?",{},params->{'qid'}); - my @arry; - $arry[0]=params->{'qid'}; - $arry[1]=session('uid'); + # add more hit database('users')->do - ("UPDATE users SET hit = hit || ' ' || ? WHERE id = ?",{},@arry); + ("UPDATE users SET hit = hit || ' ' || ? WHERE id = ?",{},( + params->{'qid'}, + session('uid') + )); redirect uri_for('/show/'.params->{'qid'}); }; @@ -211,11 +209,6 @@ get '/add' => sub { }; post '/add' => sub { - my @arry; - push(@arry, params->{'type'}); - $arry[1] = params->{'title'}; - $arry[2] = params->{'content'}; - $arry[3] = session('uid'); if (params->{'title'} eq ""){ flash error => 'Error: empty title.'; redirect uri_for('/add'); @@ -229,20 +222,25 @@ post '/add' => sub { redirect uri_for('/add'); } database('questions')->do - ("insert into entries values(NULL, ?, ?, ?, ?, 0)",{},@arry); + ("insert into entries values(NULL, ?, ?, ?, ?, 0)",{},( + params->{'type'}, + params->{'title'}, + params->{'content'}, + session('uid'), + )); # TODO: get the id. - # my $qid = database('questions')->last_insert_id("",database('questions'),'entries','id'); - my $qid = (database('questions')->selectrow_hashref - ("select id from entries where text = ?",{},params->{'content'}))->{'id'}; + my $qid = database('questions')->last_insert_id("",database('questions'),'entries','id'); + #my $qid = (database('questions')->selectrow_hashref + # ("select id from entries where text = ?",{},params->{'content'}))->{'id'}; my $answer_choice; foreach $answer_choice (split /\n/, params->{'answers'}){ - my @arry2; $answer_choice =~ s/\n//; $answer_choice =~ s/\r//; - $arry2[0] = $qid; - $arry2[1] = $answer_choice; database('questions')->do - ("insert into answers values(NULL,?, ?, 0)",{},@arry2); + ("insert into answers values(NULL,?, ?, 0)",{},( + $qid, + $answer_choice + )); } redirect uri_for("show/$qid"); }; diff --git a/schema.sql b/schema.sql index 096d218..3b4f120 100644 --- a/schema.sql +++ b/schema.sql @@ -5,8 +5,8 @@ title string not null, text string not null, uid integer not null, hits integer not null); -insert into entries values (1,'radio','first question','Is this the first question?'); -insert into entries values (2,'checkbox','second question','Is this not the first question?'); +insert into entries values (1,'radio','first question','Is this the first question?',1,0); +insert into entries values (2,'checkbox','second question','Is this not the first question?',1,0); create table if not exists answers (id integer primary key autoincrement,qid integer,content string not null,hits integer default 0); insert into answers values (1,1,'yes',0); @@ -26,4 +26,4 @@ insert into answers values (6,2,'do not know...',0); fullname string ); - insert into users values (0,'a','c',0,""); + insert into users values (0,'a','c',0,"","e@ma.il","a full name"); -- 2.11.4.GIT