Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace "presentations" with "events" and "locations" table #58

Merged
merged 2 commits into from
Apr 21, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/MCT.pm
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ sub _routes {
my $presentations = $conf->any('/presentations')->to('presentation#');
$presentations->get('/')->to('#list')->name('presentations'); # TODO

my $presentation = $presentations->any('/:url_name');
my $presentation = $presentations->any('/:pid');
$presentation->get('/')->to('#show')->name('presentation');
$presentation->get('/edit')->to('#edit')->name('presentation.edit');
$presentation->post('/edit')->to('#store')->name('presentation.update');
Expand Down
21 changes: 7 additions & 14 deletions lib/MCT/Controller/Presentation.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ use Mojo::Base 'Mojolicious::Controller';

sub show {
my $c = shift;
my $p = $c->model->presentation(
conference => $c->stash('conference')->identifier,
url_name => $c->stash('url_name'),
);
my $p = $c->model->presentation(conference => $c->stash('conference')->identifier, identifier => $c->stash('pid'));

$c->delay(
sub { $p->load(shift->begin) },
sub {
Expand All @@ -20,10 +18,7 @@ sub show {

sub edit {
my $c = shift;
my $p = $c->model->presentation(
conference => $c->stash('conference')->identifier,
id => $c->stash('url_name'),
);
my $p = $c->model->presentation(conference => $c->stash('conference')->identifier, id => $c->stash('pid'));

$c->delay(
sub { $p->load(shift->begin) },
Expand All @@ -39,12 +34,10 @@ sub edit {
sub store {
my $c = shift;
my $validation = $c->validation;

my $id = $c->param('id');
my $p = $c->model->presentation(
conference => $c->stash('conference')->identifier,
$id ? (id => $id) : (),
);
my $p = $c->model->presentation(conference => $c->stash('conference')->identifier);

$p->id($id) if $id;

# if validation fails, render the edit page
if ($p->validate($validation)->has_error) {
Expand All @@ -66,7 +59,7 @@ sub store {
my ($delay, $err) = @_;
die $err if $err;
return $c->render('presentation/edit', p => $p, saved => 1) if $id and !$c->param('view');
return $c->redirect_to('presentation', url_name => $p->url_name);
return $c->redirect_to('presentation', pid => $p->identifier);
},
);
}
Expand Down
20 changes: 10 additions & 10 deletions lib/MCT/Model/Conference.pm
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,19 @@ sub presentations {

my $sql = <<' SQL';
SELECT
p.id,
p.duration,
p.status,
p.url_name as url_name,
p.title as title,
p.abstract as abstract,
e.id,
e.duration,
e.status,
e.identifier as identifier,
e.title as title,
e.description as description,
u.username as author,
u.name as author_name
FROM presentations p
JOIN conferences c ON c.id=p.conference_id
JOIN users u ON u.id=p.user_id
FROM events e
JOIN conferences c ON c.id=e.conference_id
JOIN users u ON u.id=e.user_id
WHERE c.identifier=?
ORDER BY p.title
ORDER BY e.title
SQL

Mojo::IOLoop->delay(
Expand Down
72 changes: 40 additions & 32 deletions lib/MCT/Model/Presentation.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ use MCT::Model -row;
use Text::Markdown ();
use Mojo::Util 'xml_escape';

my @VALID_STATUS = qw( waiting accepted rejected confirmed );
my @VALID_STATUS = qw( TENTATIVE CONFIRMED CANCELLED NEEDS-ACTION COMPLETED IN-PROCESS );

col abstract => '';
col description => '';
col duration => 20;
col status => 'waiting';
col external_url => '';
col identifier => '';
col sequence => 0;
col start_time => undef,
col status => 'TENTATIVE';
col title => '';
col url_name => '';
col type => 'talk';

has author_name => '';
has author => '';
Expand All @@ -26,14 +30,14 @@ sub change_status {
}

my $sql = <<' SQL';
UPDATE presentations p
UPDATE events e
SET status=?
FROM conferences c
WHERE c.identifier=? AND p.url_name=?
WHERE c.identifier=? AND e.identifier=?
SQL

Mojo::IOLoop->delay(
sub { $self->_query($sql, $status, $self->conference, $self->url_name, shift->begin) },
sub { $self->_query($sql, $status, $self->conference, $self->identifier, shift->begin) },
sub {
my ($delay, $err, $res) = @_;
$self->status($status) unless $err;
Expand All @@ -44,9 +48,9 @@ sub change_status {
return $self;
}

sub abstract_to_html {
sub description_to_html {
my ($self, $args) = @_;
my $dom = Mojo::DOM->new(Text::Markdown::markdown(xml_escape $self->abstract));
my $dom = Mojo::DOM->new(Text::Markdown::markdown(xml_escape $self->description));

if (my $level = $args->{headings}) {
for my $e ($dom->find('h1,h2,h3,h4,h5,h6')->each) {
Expand All @@ -64,65 +68,69 @@ sub validate {
my ($self, $validation) = @_;

$validation->required('title');
$validation->required('abstract');
$validation->optional('url_name');
$validation->required('description');
$validation->optional('identifier');

my $output = $validation->output;
if (my $title = $output->{title} and not $output->{url_name}) {
if (my $title = $output->{title} and not $output->{identifier}) {
$title =~ s/\s+/-/g;
$title =~ s/(\W)/$1 eq '-' ? '-' : ''/eg;
$output->{url_name} = lc $title;
$output->{identifier} = lc $title;
}

return $validation;
}

sub _load_sst {
my $self = shift;
my $key = $self->id ? 'id' : 'url_name';
my $key = $self->id ? 'id' : 'identifier';

<<" SQL", map { $self->$_ } 'conference', $key;
SELECT
p.id,
e.id,
c.identifier as conference,
c.name as conference_name,
u.username as author,
u.name as author_name,
p.duration,
p.status,
p.url_name,
p.title,
p.abstract
FROM presentations p
JOIN conferences c ON c.id=p.conference_id
JOIN users u ON u.id=p.user_id
e.duration as duration,
e.external_url as external_url,
e.status as status,
e.identifier as identifier,
e.title as title,
e.description as description,
e.type as type
FROM events e
JOIN conferences c ON c.id=e.conference_id
JOIN users u ON u.id=e.user_id
WHERE
c.identifier=?
AND p.$key=?
AND e.$key=?
SQL
}

sub _insert_sst {
my $self = shift;
# http://sqlfiddle.com/#!15/e1168/1/3
<<' SQL', map { $self->$_ } qw( conference author duration url_name title abstract );
INSERT INTO presentations (conference_id, user_id, duration, url_name, title, abstract)
<<' SQL', map { $self->$_ } qw( conference author duration identifier title description external_url type );
INSERT INTO events (conference_id, user_id, duration, identifier, title, description, external_url, type)
VALUES(
(SELECT c.id FROM conferences c WHERE c.identifier=?),
(SELECT u.id FROM users u WHERE u.username=?),
?, ?, ?, ?
?, ?, ?, ?, ?, ?
)
RETURNING id
SQL
}

sub _next_sequence { shift->sequence + 1 }

sub _update_sst {
my $self = shift;
<<' SQL', map { $self->$_ } qw( url_name title abstract duration conference id );
UPDATE presentations p
SET url_name=?, title=?, abstract=?, duration=?
<<' SQL', map { $self->$_ } qw( identifier title description duration _next_sequence external_url type conference id );
UPDATE events e
SET identifier=?, title=?, description=?, duration=?, sequence=?, external_url=?, type=?
FROM conferences c
WHERE c.identifier=? AND p.id=?
WHERE c.identifier=? AND e.id=?
SQL
}

Expand All @@ -136,7 +144,7 @@ sub user_can_update {

sub TO_JSON {
my $self = shift;
return { map { ($_, $self->$_) } qw( url_name title abstract author author_name conference id ) };
return { map { ($_, $self->$_) } qw( identifier title description author author_name conference id ) };
}

1;
22 changes: 11 additions & 11 deletions lib/MCT/Model/User.pm
Original file line number Diff line number Diff line change
Expand Up @@ -117,21 +117,21 @@ sub presentations {

my $sql = <<' SQL';
SELECT
p.id,
e.id,
c.identifier as conference,
c.name as conference_name,
u.username as author,
u.name as author_name,
p.duration,
p.status,
p.url_name as url_name,
p.title as title,
p.abstract as abstract
FROM presentations p
JOIN conferences c ON c.id=p.conference_id
JOIN users u ON u.id=p.user_id
WHERE p.user_id=?
ORDER BY c.created DESC, p.title
e.duration,
e.status,
e.identifier as identifier,
e.title as title,
e.description as description
FROM events e
JOIN conferences c ON c.id=e.conference_id
JOIN users u ON u.id=e.user_id
WHERE e.user_id=?
ORDER BY c.created DESC, e.title
SQL

Mojo::IOLoop->delay(
Expand Down
47 changes: 47 additions & 0 deletions mct.sql
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,37 @@ CREATE TABLE user_roles (
UNIQUE(user_id, conference_id, role)
);
DROP TABLE IF EXISTS user_conferences; -- replaced by user_roles
-- 7 up
CREATE TABLE locations (
id SERIAL PRIMARY KEY,
conference_id INTEGER REFERENCES conferences (id),
name TEXT NOT NULL,
location TEXT NOT NULL DEFAULT '',
latitude REAL,
longitude REAL,
UNIQUE (conference_id, name)
);
CREATE TABLE events (
id SERIAL PRIMARY KEY,
conference_id INTEGER NOT NULL REFERENCES conferences (id),
user_id INTEGER NOT NULL REFERENCES users (id),
location_id INTEGER REFERENCES locations (id),
type VARCHAR(16) NOT NULL,
identifier TEXT NOT NULL, -- /:cid/events/:identifier
title TEXT NOT NULL,
description TEXT NOT NULL DEFAULT '',
external_url TEXT NOT NULL DEFAULT '',
sequence INTEGER DEFAULT 0, -- http://www.kanzaki.com/docs/ical/sequence.html
status VARCHAR(16) NOT NULL DEFAULT 'TENTATIVE', -- http://www.kanzaki.com/docs/ical/status.html
start_time TIMESTAMP,
duration INTEGER DEFAULT 20,
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
last_modified TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
UNIQUE (conference_id, identifier)
);
INSERT INTO events (id, conference_id, user_id, title, description, identifier, duration, type)
SELECT id, conference_id, user_id, title, abstract, url_name, duration, 'talk' FROM presentations;
DROP TABLE IF EXISTS presentations;
-- 1 down
DROP TABLE IF EXISTS presentations;
DROP TABLE IF EXISTS conferences;
Expand Down Expand Up @@ -124,3 +155,19 @@ DROP TABLE IF EXISTS conference_products;
ALTER TABLE user_products DROP COLUMN status;
-- 6 down
DROP TABLE IF EXISTS user_roles;
-- 7 down
CREATE TABLE presentations (
id SERIAL PRIMARY KEY,
conference_id INTEGER REFERENCES conferences (id) ON UPDATE CASCADE,
user_id INTEGER NOT NULL REFERENCES users (id),
title TEXT NOT NULL,
abstract TEXT,
url_name TEXT NOT NULL,
duration INTEGER DEFAULT 20,
status VARCHAR(16) DEFAULT 'waiting', -- waiting,accepted,rejected,confirmed
UNIQUE (url_name, conference_id)
);
INSERT INTO presentations (id, conference_id, user_id, title, abstract, url_name, duration)
SELECT id, conference_id, user_id, title, description, identifier, duration FROM events;
DROP TABLE IF EXISTS events;
DROP TABLE IF EXISTS locations;
3 changes: 2 additions & 1 deletion t/Helper.pm
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ my $_test_table = sub {
};

sub t {
my ($class, %args) = @_;
Test::More::plan(skip_all => "TEST_ONLINE=postgresql://@{[scalar getpwuid $<]}\@/mct_test") unless $ENV{TEST_ONLINE};
$ENV{MCT_SKIP_MIGRATION} //= 1;
$ENV{MCT_DATABASE_DSN} = $ENV{TEST_ONLINE};
$ENV{MCT_MOCK} //= 1;
my $t = Test::Mojo->new('MCT');
$t->app->migrations->migrate(0)->migrate;
$t->app->migrations->migrate(0)->migrate($args{migrate_to});
$t;
}

Expand Down
4 changes: 2 additions & 2 deletions t/admin-presentations.t
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ my $t = t::Helper->t;
my $conference = $t->app->model->conference(name => 'Devops')->save;

$t->get_ok('/user/connect', form => {code => 42})->status_is(302);
$t->post_ok('/devops/user/presentations', form => {title => 't1', abstract => 'a1'})->status_is(302);
$t->post_ok('/devops/user/presentations', form => {title => 't1', description => 'a1'})->status_is(302);

$t->get_ok('/devops/user/admin/presentations')->status_is(302);

Expand All @@ -16,7 +16,7 @@ $t->get_ok('/devops/user/admin/presentations')
[
['a[href="/devops/presentations/t1"]', 't1'],
'20',
'waiting',
'TENTATIVE',
]
]);

Expand Down
32 changes: 32 additions & 0 deletions t/migrate-presentations.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use t::Helper;

my $t = t::Helper->t(migrate_to => 6);
my $app = $t->app;

$app->model->conference(country => 'NO', name => 'mipre')->save;
$app->model->user(username => 'bruce', name => 'Bruce')->save;

$t->app->model->db->query(<<'SQL');
INSERT INTO presentations
(conference_id, user_id, duration, url_name, title, abstract)
VALUES(
(SELECT c.id FROM conferences c WHERE c.identifier='mipre'),
(SELECT u.id FROM users u WHERE u.username='bruce'),
20, 'my-talk', 'My Talk', 'Too cool talk.'
)
SQL

ok eval { $t->app->migrations->migrate }, 'migrate' or diag "migrate failed: $@";
my $p_new = $app->model->presentation(author => 'bruce', conference => 'mipre', identifier => 'my-talk')->load;
is $p_new->author, 'bruce', 'author';
is $p_new->author_name, 'Bruce', 'author_name';
is $p_new->description, 'Too cool talk.', 'description';
is $p_new->duration, '20', 'duration';
is $p_new->external_url, '', 'external_url';
is $p_new->identifier, 'my-talk', 'identifier';
is $p_new->start_time, undef, 'start_time';
is $p_new->status, 'TENTATIVE', 'status';
is $p_new->title, 'My Talk', 'title';
is $p_new->type, 'talk', 'type';

done_testing;
Loading