Showing 4 changed files with 50 additions and 43 deletions
+2 -2
lib/Gitprep.pm
... ...
@@ -197,8 +197,8 @@ sub startup {
197 197
       table => 'collaboration',
198 198
       primary_key => 'row_id',
199 199
       join => [
200
-        'left join user on collaboration.collaborator = user.row_id',
201
-        'left join project on collaboration.project = project.row_id'
200
+        'left join user on collaboration.user = user.row_id',
201
+        'left join project on collaboration.project = project.row_id',
202 202
       ]
203 203
     },
204 204
     {
+1 -1
old/copy_database_v1_to_v2
... ...
@@ -175,7 +175,7 @@ unless ($new_collaboration_count) {
175 175
       where => {id => $old_collaboration->{collaborator_id}}
176 176
     )->value;
177 177
     if (defined $user_row_id) {
178
-      $new_collaboration->{collaborator} = $user_row_id;
178
+      $new_collaboration->{user} = $user_row_id;
179 179
     }
180 180
     else {
181 181
       next;
+8 -13
setup_database
... ...
@@ -65,8 +65,7 @@ EOS
65 65
   eval { $dbi->select([qw/row_id id admin password salt email name/], table => 'user') };
66 66
   if ($@) {
67 67
     my $error = "Can't create user table properly: $@";
68
-    $self->app->log->error($error);
69
-    croak $error;
68
+    die $error;
70 69
   }
71 70
   
72 71
   # Create project table
... ...
@@ -103,8 +102,7 @@ EOS
103 102
   };
104 103
   if ($@) {
105 104
     my $error = "Can't create project table properly: $@";
106
-    $self->app->log->error($error);
107
-    croak $error;
105
+    die $error;
108 106
   }
109 107
 
110 108
   # Create ssh_public_key table
... ...
@@ -131,8 +129,7 @@ EOS
131 129
   eval { $dbi->select([qw/row_id user key title/], table => 'ssh_public_key') };
132 130
   if ($@) {
133 131
     my $error = "Can't create ssh_public_key table properly: $@";
134
-    $self->app->log->error($error);
135
-    croak $error;
132
+    die $error;
136 133
   }
137 134
 
138 135
   # Create collaboration table
... ...
@@ -141,19 +138,18 @@ EOS
141 138
 create table collaboration (
142 139
   row_id integer primary key autoincrement,
143 140
   project integer not null default 0,
144
-  collaborator integer not null default 0,
145
-  unique(project, collaborator)
141
+  user integer not null default 0,
142
+  unique(project, user)
146 143
 );
147 144
 EOS
148 145
     $dbi->execute($sql);
149 146
   };
150 147
   
151 148
   # Check collaboration table
152
-  eval { $dbi->select([qw/row_id project collaborator/], table => 'collaboration') };
149
+  eval { $dbi->select([qw/row_id project user/], table => 'collaboration') };
153 150
   if ($@) {
154 151
     my $error = "Can't create collaboration table properly: $@";
155
-    $self->app->log->error($error);
156
-    croak $error;
152
+    die $error;
157 153
   }
158 154
 
159 155
   # Create pull_request table
... ...
@@ -185,7 +181,6 @@ EOS
185 181
   eval { $dbi->select([qw/row_id project branch1 branch2 title open open_time open_user/], table => 'pull_request') };
186 182
   if ($@) {
187 183
     my $error = "Can't create pull_request table properly: $@";
188
-    $self->app->log->error($error);
189
-    croak $error;
184
+    die $error;
190 185
   }
191 186
 }
+39 -27
templates/settings/collaboration.html.ep
... ...
@@ -5,19 +5,18 @@
5 5
 
6 6
   # Parameters
7 7
   my $op = param('op') || '';
8
-  my $user = param('user') || '';
8
+  my $user_id = param('user') || '';
9 9
   my $project_id = param('project');
10 10
   
11 11
   # Authentication
12
-  unless ($api->logined($user)) {
12
+  unless ($api->logined($user_id)) {
13 13
     $self->redirect_to('/');
14 14
     return;
15 15
   }
16 16
   
17
-  my $user_row_id = app->dbi->model('user')->select('row_id', where => {id => $user})->value;
18
-  my $project_id_row_id = app->dbi->model('project')->model('project')->select(
19
-    'row_id',
20
-    where => {user => $user_row_id, id => $project_id}
17
+  my $project_row_id = app->dbi->model('project')->select(
18
+    'project.row_id',
19
+    where => {'user.id' => $user_id, 'project.id' => $project_id}
21 20
   )->value;
22 21
   
23 22
   # Rename project
... ...
@@ -25,7 +24,7 @@
25 24
   my $errors;
26 25
   if (lc $self->req->method eq 'post') {
27 26
     if ($op eq 'add') {
28
-      my $collaborator = param('collaborator');
27
+      my $collaborator_id = param('collaborator');
29 28
       
30 29
       # Validator
31 30
       my $vc = app->vc;
... ...
@@ -34,29 +33,39 @@
34 33
       my $validation = $vc->validation;
35 34
       
36 35
       # collaborator check
37
-      if (!length $collaborator) {
36
+      if (!length $collaborator_id) {
38 37
         $validation->add_failed(collaborator => "collaborator is empty");
39 38
       }
40
-      elsif ($collaborator eq $user) {
41
-        $validation->add_failed(collaborator => "User $collaborator is yourself");
39
+      elsif ($collaborator_id eq $user_id) {
40
+        $validation->add_failed(collaborator => "User $collaborator_id is yourself");
42 41
       }
43 42
       else {
44 43
         my $row = app->dbi->model('user')->select(
45
-          where => {id => $collaborator}
44
+          where => {id => $collaborator_id}
46 45
         )->one;
47 46
         if (!$row) {
48
-          $validation->add_failed(collaborator => "User $collaborator don't exists");
47
+          $validation->add_failed(collaborator => "User $collaborator_id don't exists");
48
+        }
49
+        else {
50
+          my $row = app->dbi->model('collaboration')->select(
51
+            where => {project => $project_row_id, 'user.id' => $collaborator_id}
52
+          )->one;
53
+          if ($row) {
54
+            $validation->add_failed(collaborator => "Collaborator $collaborator_id already exists");
55
+          }
49 56
         }
50 57
       }
51 58
       
52 59
       if ($validation->is_valid) {
53 60
         
61
+        my $collaborator_row_id = $api->get_user_row_id($collaborator_id);
62
+        
54 63
         # Insert
55 64
         eval {
56 65
           app->dbi->model('collaboration')->insert(
57 66
             {
58
-              project => $project_id_row_id,
59
-              collaborator => $collaborator
67
+              project => $project_row_id,
68
+              user => $collaborator_row_id
60 69
             }
61 70
           );
62 71
         };
... ...
@@ -65,7 +74,7 @@
65 74
           $errors = ['Internal Error'];
66 75
         }
67 76
         else {
68
-          flash(message => "Collaborator $collaborator is added.");
77
+          flash(message => "Collaborator $collaborator_id is added.");
69 78
           $self->redirect_to('current');
70 79
           return;
71 80
         }
... ...
@@ -75,15 +84,16 @@
75 84
       }
76 85
     }
77 86
     elsif ($op eq 'remove') {
78
-      my $collaborator = param('collaborator');
87
+      my $collaborator_id = param('collaborator');
88
+      
89
+      my $collaborator_row_id = $api->get_user_row_id($collaborator_id);
79 90
       
80 91
       # Delete
81 92
       eval {
82 93
         app->dbi->model('collaboration')->delete(
83 94
           where => {
84
-            user_id => $user,
85
-            project_name => $project_id,
86
-            collaborator_id => $collaborator
95
+            project => $project_row_id,
96
+            user => $collaborator_row_id
87 97
           }
88 98
         );
89 99
       };
... ...
@@ -92,7 +102,7 @@
92 102
         $errors = ['Internal Error'];
93 103
       }
94 104
       else {
95
-        flash(message => "Collaborator $collaborator is removed.");
105
+        flash(message => "Collaborator $collaborator_id is removed.");
96 106
         $self->redirect_to('current');
97 107
         return;
98 108
       }
... ...
@@ -100,10 +110,12 @@
100 110
   }
101 111
   
102 112
   my $collaborators = app->dbi->model('collaboration')->select(
103
-    'collaborator_id',
104
-    where => {user_id => $user, project_name => $project_id},
105
-    append => 'order by collaborator_id'
106
-  )->values;
113
+    {user => ['id']},
114
+    where => {project => $project_row_id},
115
+    append => 'order by collaboration.user'
116
+  )->all;
117
+  
118
+  use D;d [$user_id, $project_id, $collaborators];
107 119
 %>
108 120
 
109 121
 % layout 'common', title => 'Collaboration';
... ...
@@ -117,7 +129,7 @@
117 129
     <div class="project-settings">
118 130
       <div class="left">
119 131
         <ul>
120
-          <li><a href="<%= url_for("/$user/$project_id/settings") %>">Options</a></li>
132
+          <li><a href="<%= url_for("/$user_id/$project_id/settings") %>">Options</a></li>
121 133
           <li><b>Collaborators</b></li>
122 134
         </ul>
123 135
       </div>
... ...
@@ -129,9 +141,9 @@
129 141
                 % for my $collaborator (@$collaborators) {
130 142
                   <tr>
131 143
                     <td>
132
-                      <a href="<%= url_for("/$collaborator") %>"><%= $collaborator %></a>
144
+                      <a href="<%= url_for("/$collaborator->{'user.id'}") %>"><%= $collaborator->{'user.id'} %></a>
133 145
                       <form action="<%= url_for->query(op => 'remove') %>" method="post" style="display:inline-block">
134
-                        <%= hidden_field 'collaborator' => $collaborator %>
146
+                        <%= hidden_field 'collaborator' => $collaborator->{'user.id'} %>
135 147
                         (<a href="javascript:void(0)" onclick="$(this).closest('form').submit();" style="color:red">remove</a>)
136 148
                       </form>
137 149
                     </td>