Showing 1 changed files with 45 additions and 50 deletions
+45 -50
templates/auto/_new.html.ep
... ...
@@ -13,70 +13,65 @@
13 13
   
14 14
   my $errors;
15 15
   if ($op eq 'create') {
16
-    # API
17
-    my $api = gitprep_api;
16
+    # Parameters
17
+    my $project = param('project');
18
+    my $description = param('description');
19
+    my $readme = param('readme');
20
+    my $private = param('private');
18 21
     
19
-    # Validation
20
-    my $params = $api->params;
21
-    my $rule = [
22
-      project => [
23
-        ['not_blank' => 'Repository name is empty'],
24
-        ['project_name' => 'Invalid repository name']
25
-      ],
26
-      description => {require => 0} => [
27
-        'any'
28
-      ],
29
-      readme => {require => 0} => [
30
-        'any'
31
-      ],
32
-      private => {require => 0} => [
33
-        'any'
34
-      ]
35
-    ];
22
+    # Validator
36 23
     my $vc = app->vc;
37
-    my $vresult = $vc->validate($params, $rule);
24
+    
25
+    # Validation
26
+    my $validation = $vc->validation;
27
+    
28
+    # "project"
29
+    if (!(defined $project && length $project)) {
30
+      $validation->add_failed(project => 'Repository name is empty');
31
+    }
32
+    elsif (!$vc->check($project, 'project_name')) {
33
+      $validation->add_failed(project => 'Invalid repository name');
34
+    }
35
+    elsif (app->manager->exists_project($session_user, $project)) {
36
+      $validation->add_failed(project => 'Repository already exists');
37
+    }
38
+    
39
+    # "description"
40
+    $description ||= '';
41
+    
42
+    # "readme"
43
+    $readme = $readme ? 1 :0;
44
+    
45
+    # "private"
46
+    $private = $private ? 1 : 0;
38 47
     
39 48
     # Git
40
-    my $git = app->git;
41
-    if ($vresult->is_ok) {
49
+    if ($validation->is_valid) {
42 50
       # Not logined
43 51
       unless ($api->logined) {
44 52
         return $self->render_exception;
45 53
       }
46 54
       
47
-      my $data = $vresult->data;
48
-      my $project = $data->{project};
49
-      my $description = $data->{description};
50
-      $description = '' unless defined $description;
51
-      my $readme = $data->{readme};
52
-      my $private = $data->{private};
53
-      $private ||= 0;
54
-      
55 55
       my $manager = app->manager;
56
-      if ($manager->exists_project($session_user, $project)) {
57
-        $errors = ['Repository already exists'];
56
+      # Create repository
57
+      eval {
58
+        $manager->create_project(
59
+          $session_user,
60
+          $project,
61
+          {description => $description, readme => $readme, private => $private}
62
+        );
63
+      };
64
+      
65
+      if (my $e = $@) {
66
+        app->log->error(url_for . ": $e");
67
+        $errors = ['Internal error'];
58 68
       }
59 69
       else {
60
-        # Create repository
61
-        eval {
62
-          $manager->create_project(
63
-            $session_user,
64
-            $project,
65
-            {description => $description, readme => $readme, private => $private}
66
-          );
67
-        };
68
-        
69
-        if (my $e = $@) {
70
-          app->log->error(url_for . ": $e");
71
-          $errors = ['Internal error'];
72
-        }
73
-        else {
74
-          $self->redirect_to("/$session_user/$project");
75
-          return;
76
-        }
70
+        $self->redirect_to("/$session_user/$project");
71
+        return;
77 72
       }
78 73
     }
79
-    else { $errors = $vresult->messages }
74
+    else { $errors = $validation->messages }
80 75
   }
81 76
 %>
82 77