Showing 4 changed files with 71 additions and 12 deletions
+1 -1
cpanfile
... ...
@@ -5,7 +5,7 @@ requires 'DBIx::Custom', '== 0.28';
5 5
 requires 'Config::Tiny', '== 2.14';
6 6
 requires 'Time::HiRes', '== 1.9725';
7 7
 requires 'Test::Simple', '== 0.98';
8
-requires 'Validator::Custom', '== 0.20';
8
+requires 'Validator::Custom', '== 0.22';
9 9
 requires 'DBIx::Connector', '== 0.53';
10 10
 requires 'Module::Build', '== 0.4003';
11 11
 requires 'Test::Harness', '== 3.26';
+2 -1
lib/Gitprep.pm
... ...
@@ -102,7 +102,8 @@ sub startup {
102 102
   my $models = [
103 103
     {table => 'user', primary_key => 'id'},
104 104
     {table => 'project', primary_key => ['user_id', 'name']},
105
-    {table => 'number', primary_key => 'key'}
105
+    {table => 'number', primary_key => 'key'},
106
+    {table => 'collaboration', primary_key => ['user_id', 'project_name', 'collaborator_id']}
106 107
   ];
107 108
   $dbi->create_model($_) for @$models;
108 109
 
+22
lib/Gitprep/Manager.pm
... ...
@@ -346,6 +346,28 @@ EOS
346 346
     croak $error;
347 347
   }
348 348
 
349
+  # Create collaboration table
350
+  eval {
351
+    my $sql = <<"EOS";
352
+create table collaboration (
353
+  row_id integer primary key autoincrement,
354
+  user_id not null unique default '',
355
+  project_name not null unique default '',
356
+  collaborator_id not null unique default '',
357
+  unique(user_id, project_name, collaborator_id)
358
+);
359
+EOS
360
+    $dbi->execute($sql);
361
+  };
362
+  
363
+  # Check collaboration table
364
+  eval { $dbi->select([qw/row_id user_id project_name collaborator_id/], table => 'collaboration') };
365
+  if ($@) {
366
+    my $error = "Can't create collaboration table properly: $@";
367
+    $self->app->log->error($error);
368
+    croak $error;
369
+  }
370
+
349 371
   # Create number table
350 372
   eval {
351 373
     my $sql = <<"EOS";
+46 -10
templates/settings/collaboration.html.ep
... ...
@@ -2,10 +2,11 @@
2 2
   # API
3 3
   my $api = gitprep_api;
4 4
   my $manager = app->manager;
5
-  
5
+
6 6
   # Parameters
7 7
   my $op = param('op') || '';
8 8
   my $user = param('user') || '';
9
+  my $project = param('project');
9 10
   
10 11
   # Authentication
11 12
   unless ($api->logined($user)) {
... ...
@@ -17,17 +18,52 @@
17 18
   my $git = app->git;
18 19
   my $errors;
19 20
   if ($op eq 'add' && lc $self->req->method eq 'post') {
20
-    my $collaborator = param('collaborator');
21
-    
22
-    eval { $git->description($user, $project) };
23
-    if (my $e = $@) {
24
-      app->log->error(url_with . ": $e");
25
-      $errors = ['Internal Error'];
21
+    my $params = $api->params;
22
+    my $rule = [
23
+      collaborator => [
24
+        ['not_blank' => 'collaborator is empty.'],
25
+        # Check user
26
+        sub {
27
+          my $collaborator = shift || '';
28
+          
29
+          if ($collaborator eq $user) {
30
+            return {result => 0, message => "User $collaborator is yourself"};
31
+          }
32
+          else {
33
+            my $row = app->dbi->model('user')->select(id => $collaborator)->one;
34
+            
35
+            return $row ? 1 : {result => 0, message => "User $collaborator don't exists"};
36
+          }
37
+        }
38
+      ]
39
+    ];
40
+    my $vresult = app->validator->validate($params, $rule);
41
+    if ($vresult->is_ok) {
42
+      my $safe_params = $vresult->data;
43
+      my $collaborator = $safe_params->{collaborator};
44
+      
45
+      # Insert
46
+      eval {
47
+        app->dbi->model('collaboration')->insert(
48
+          {
49
+            user_id => $user,
50
+            project_name => $project,
51
+            collaborator_id => $collaborator
52
+          }
53
+        );
54
+      };
55
+      if (my $e = $@) {
56
+        app->log->error(url_with . ": $e");
57
+        $errors = ['Internal Error'];
58
+      }
59
+      else {
60
+        flash(message => "Collaborator $collaborator is added.");
61
+        $self->redirect_to('current');
62
+        return;
63
+      }
26 64
     }
27 65
     else {
28
-      flash(message => 'Collaborator is added.');
29
-      $self->redirect_to('current');
30
-      return;
66
+      $errors = $vresult->messages;
31 67
     }
32 68
   }
33 69
 %>