Skip to content
This repository has been archived by the owner on Nov 1, 2020. It is now read-only.

Commit

Permalink
add collaborator register logic
Browse files Browse the repository at this point in the history
  • Loading branch information
yuki-kimoto committed Nov 17, 2013
1 parent 5d4e445 commit 72df1af
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 12 deletions.
2 changes: 1 addition & 1 deletion cpanfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ requires 'DBIx::Custom', '== 0.28';
requires 'Config::Tiny', '== 2.14';
requires 'Time::HiRes', '== 1.9725';
requires 'Test::Simple', '== 0.98';
requires 'Validator::Custom', '== 0.20';
requires 'Validator::Custom', '== 0.22';
requires 'DBIx::Connector', '== 0.53';
requires 'Module::Build', '== 0.4003';
requires 'Test::Harness', '== 3.26';
Expand Down
3 changes: 2 additions & 1 deletion lib/Gitprep.pm
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ sub startup {
my $models = [
{table => 'user', primary_key => 'id'},
{table => 'project', primary_key => ['user_id', 'name']},
{table => 'number', primary_key => 'key'}
{table => 'number', primary_key => 'key'},
{table => 'collaboration', primary_key => ['user_id', 'project_name', 'collaborator_id']}
];
$dbi->create_model($_) for @$models;

Expand Down
22 changes: 22 additions & 0 deletions lib/Gitprep/Manager.pm
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,28 @@ EOS
croak $error;
}

# Create collaboration table
eval {
my $sql = <<"EOS";
create table collaboration (
row_id integer primary key autoincrement,
user_id not null unique default '',
project_name not null unique default '',
collaborator_id not null unique default '',
unique(user_id, project_name, collaborator_id)
);
EOS
$dbi->execute($sql);
};

# Check collaboration table
eval { $dbi->select([qw/row_id user_id project_name collaborator_id/], table => 'collaboration') };
if ($@) {
my $error = "Can't create collaboration table properly: $@";
$self->app->log->error($error);
croak $error;
}

# Create number table
eval {
my $sql = <<"EOS";
Expand Down
56 changes: 46 additions & 10 deletions templates/settings/collaboration.html.ep
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
# API
my $api = gitprep_api;
my $manager = app->manager;

# Parameters
my $op = param('op') || '';
my $user = param('user') || '';
my $project = param('project');

# Authentication
unless ($api->logined($user)) {
Expand All @@ -17,17 +18,52 @@
my $git = app->git;
my $errors;
if ($op eq 'add' && lc $self->req->method eq 'post') {
my $collaborator = param('collaborator');

eval { $git->description($user, $project) };
if (my $e = $@) {
app->log->error(url_with . ": $e");
$errors = ['Internal Error'];
my $params = $api->params;
my $rule = [
collaborator => [
['not_blank' => 'collaborator is empty.'],
# Check user
sub {
my $collaborator = shift || '';

if ($collaborator eq $user) {
return {result => 0, message => "User $collaborator is yourself"};
}
else {
my $row = app->dbi->model('user')->select(id => $collaborator)->one;

return $row ? 1 : {result => 0, message => "User $collaborator don't exists"};
}
}
]
];
my $vresult = app->validator->validate($params, $rule);
if ($vresult->is_ok) {
my $safe_params = $vresult->data;
my $collaborator = $safe_params->{collaborator};

# Insert
eval {
app->dbi->model('collaboration')->insert(
{
user_id => $user,
project_name => $project,
collaborator_id => $collaborator
}
);
};
if (my $e = $@) {
app->log->error(url_with . ": $e");
$errors = ['Internal Error'];
}
else {
flash(message => "Collaborator $collaborator is added.");
$self->redirect_to('current');
return;
}
}
else {
flash(message => 'Collaborator is added.');
$self->redirect_to('current');
return;
$errors = $vresult->messages;
}
}
%>
Expand Down

0 comments on commit 72df1af

Please sign in to comment.