Skip to content

Commit 23067b3

Browse files
author
Kaitlyn Parkhurst
committed
Added tag creation.
1 parent a1dc2ee commit 23067b3

File tree

7 files changed

+230
-3
lines changed

7 files changed

+230
-3
lines changed

Web/lib/BlogDB/Web.pm

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,15 @@ sub startup ($self) {
9595

9696
# /new/
9797
$r->post ( '/new' )->to( 'Create#post_new' )->name( 'do_add_new_blog' );
98-
$r->get ( '/new/:name' )->to( 'Create#get_new_blog' )->name( 'update_new_blog' );
99-
$r->post ( '/new/:name' )->to( 'Create#post_new_blog' )->name( 'do_update_new_blog');
100-
$auth->post( '/new/:name/push' )->to( 'Create#post_push_blog')->name( 'do_push_new_blog' );
98+
$r->get ( '/new/*name' )->to( 'Create#get_new_blog' )->name( 'update_new_blog' );
99+
$r->post ( '/new/*name' )->to( 'Create#post_new_blog' )->name( 'do_update_new_blog');
101100

102101
# /tags/
103102
$r->get ( '/tags' )->to( 'Tags#get_tags' )->name( 'tags' );
104103
$auth->post( '/tags/suggest' )->to( 'Tags#post_suggest_tag')->name( 'do_suggest_tag' );
105104
$auth->post( '/tags/vote' )->to( 'Tags#post_vote_tag' )->name( 'do_vote_tag' );
106105
$auth->post( '/tags/delete' )->to( 'Tags#post_delete_tag' )->name( 'do_delete_tag' );
106+
$auth->post( '/tags/approve' )->to( 'Tags#post_approve_tag')->name( 'do_approve_tag' );
107107

108108
}
109109

Web/lib/BlogDB/Web/Controller/Create.pm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ sub post_new ($c) {
77

88
sub get_new_blog ($c) {
99
$c->set_template( 'new/index' );
10+
11+
$c->stash->{blog_url} = $c->param('name');
1012
}
1113

1214
sub post_new_blog ($c) {

Web/lib/BlogDB/Web/Controller/Root.pm

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ use Mojo::Base 'Mojolicious::Controller', -signatures;
33
use Try::Tiny;
44
use Data::UUID;
55

6+
sub get_homepage ($c) {
7+
$c->set_template( 'index' );
8+
9+
}
10+
611
sub get_register ($c) {
712
$c->set_template( 'register' );
813

Web/lib/BlogDB/Web/Controller/Tags.pm

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,108 @@ use Mojo::Base 'Mojolicious::Controller', -signatures;
33

44
sub get_tags ($c) {
55
$c->set_template( 'tags/index' );
6+
7+
push @{$c->stash->{tags}}, $c->db->resultset('Tag')->all;
8+
push @{$c->stash->{pending_tags}}, $c->db->resultset('PendingTag')->all;
9+
610
}
711

812
sub post_suggest_tag ($c) {
913
$c->set_template( 'tags/index' );
14+
15+
push @{$c->stash->{tags}}, $c->db->resultset('Tag')->all;
16+
push @{$c->stash->{pending_tags}}, $c->db->resultset('PendingTag')->all;
17+
18+
my $tag_name = $c->stash->{form_tag} = $c->param('tag');
19+
my $is_adult = $c->stash->{form_adult} = $c->param('is_adult');
20+
21+
push @{$c->stash->{errors}}, "Tag names must start with a letter, and may only contain letters and numbers."
22+
unless $tag_name =~ m/^[a-zA-Z][a-zA-Z0-9]+$/;
23+
24+
return 0 if $c->stash->{errors};
25+
26+
my $tag_exists = $c->db->resultset('Tag' )->search({ name => $tag_name })->first;
27+
my $pending_tag_exists = $c->db->resultset('PendingTag')->search({ name => $tag_name })->first;
28+
29+
push @{$c->stash->{errors}}, "There is already a tag with that name."
30+
if $tag_exists;
31+
32+
push @{$c->stash->{errors}}, "There is already a pending tag with that name."
33+
if $pending_tag_exists;
34+
35+
return 0 if $c->stash->{errors};
36+
37+
$c->db->resultset('PendingTag')->create({
38+
name => $tag_name,
39+
});
40+
41+
$c->stash->{success} = 1;
42+
$c->stash->{tag_name} = $tag_name;
1043
}
1144

1245
sub post_vote_tag ($c) {
1346
$c->set_template( 'tags/index' );
47+
48+
push @{$c->stash->{tags}}, $c->db->resultset('Tag')->all;
49+
push @{$c->stash->{pending_tags}}, $c->db->resultset('PendingTag')->all;
50+
51+
my $tag_name = $c->stash->{form_tag} = $c->param('tag');
52+
53+
my $tag = $c->db->resultset('PendingTag')->search({ name => $tag_name })->first;
54+
55+
push @{$c->stash->{errors}}, "No such tag?"
56+
unless $tag;
57+
58+
return 0 if $c->stash->{errors};
59+
60+
# Find out if the user already voted -- in which case we are toggling the vote.
61+
my $vote = $c->db->resultset('TagVote')->search( {
62+
tag_id => $tag->id,
63+
person_id => $c->stash->{person}->id,
64+
});
65+
66+
if ( $vote ) {
67+
$vote->vote( ! $vote->vote );
68+
$vote->update;
69+
} else {
70+
$c->stash->{person}->create_related( 'tag_votes', {
71+
tag_id => $tag->id,
72+
});
73+
}
74+
75+
$c->stash->{success} = 1;
1476
}
1577

1678
sub post_delete_tag ($c) {
1779
$c->set_template( 'tags/index' );
80+
81+
push @{$c->stash->{tags}}, $c->db->resultset('Tag')->all;
82+
push @{$c->stash->{pending_tags}}, $c->db->resultset('PendingTag')->all;
83+
84+
my $tag_name = $c->stash->{form_tag} = $c->param('tag');
85+
}
86+
87+
sub post_approve_tag ($c) {
88+
$c->set_template( 'tags/index' );
89+
90+
push @{$c->stash->{tags}}, $c->db->resultset('Tag')->all;
91+
push @{$c->stash->{pending_tags}}, $c->db->resultset('PendingTag')->all;
92+
93+
my $tag_name = $c->stash->{form_tag} = $c->param('tag');
94+
95+
my $tag = $c->db->resultset('PendingTag')->search({ name => $tag_name })->first;
96+
97+
push @{$c->stash->{errors}}, "No such tag?"
98+
unless $tag;
99+
100+
return 0 if $c->stash->{errors};
101+
102+
$c->db->resultset('Tag')->create({
103+
name => $tag->name,
104+
is_adult => $tag->is_adult,
105+
});
106+
107+
$c->stash->{success} = 1;
18108
}
19109

20110
1;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
%% cascade default::_::layout { title => 'Home',
2+
%%
3+
%% }
4+
5+
%% override panel -> {
6+
7+
<!-- Add New Blog Section -->
8+
<div class="d-flex justify-content-center">
9+
<form method="post" class="row row-cols-lg-auto align-items-center" action="[% $c.url_for( 'do_add_blog' ) %]">
10+
<div class="col-12">
11+
<input placeholder="http://reallycoolblog.com" type="text" name="url" value="[% $form_blog_url %]">
12+
</div>
13+
<div class="col-12">
14+
<button type="submit" class="btn btn-primary float-end">Next</button>
15+
</div>
16+
</form>
17+
</div>
18+
19+
%% }
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
%% cascade default::_::layout { title => 'Edit ' ~ $blog_url,
2+
%%
3+
%% }
4+
5+
%% override panel -> {
6+
7+
<h3>[% $blog_url %]</h3>
8+
9+
<div class="row">
10+
<!-- Error Handling On LHS -->
11+
<div class="col">
12+
%% if ( $errors.size() ) {
13+
<div style="margin-top: 2em" class="alert alert-danger" role="alert">
14+
There were errors with your request that could not be resolved:
15+
<ul>
16+
%% for $errors -> $error {
17+
<li>[% $error %]</li>
18+
%% }
19+
</ul>
20+
</div>
21+
%% }
22+
</div>
23+
</div>
24+
25+
<div class="row">
26+
<div class="col">
27+
<form method="post" action="[% $c.url_for( 'do_register' ) %]">
28+
29+
%% include 'default/_/form/input.tx' { type => 'text', name => 'title',
30+
%% title => 'Title',
31+
%% help => 'The title of the blog',
32+
%% value => $form_title,
33+
%% };
34+
35+
%% include 'default/_/form/input.tx' { type => 'text', name => 'tagline',
36+
%% title => 'Tagline',
37+
%% help => 'The tagline of the blog',
38+
%% value => $form_tagline,
39+
%% };
40+
41+
<textarea name="description" rows="4">[% $form_description %]</textarea>
42+
43+
<button type="submit" class="btn btn-primary float-end">Register</button>
44+
45+
</form>
46+
</div>
47+
</div>
48+
49+
%% }
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
%% cascade default::_::layout { title => 'Tags',
2+
%%
3+
%% }
4+
5+
%% override panel -> {
6+
7+
%% if ( $errors.size() ) {
8+
<div style="margin-top: 2em" class="alert alert-danger" role="alert">
9+
There were errors with your request that could not be resolved:
10+
<ul>
11+
%% for $errors -> $error {
12+
<li>[% $error %]</li>
13+
%% }
14+
</ul>
15+
</div>
16+
%% }
17+
</div>
18+
19+
<!-- Add New Tag Section -->
20+
<div class="d-flex justify-content-center">
21+
<form method="post" class="row row-cols-lg-auto align-items-center" action="[% $c.url_for( 'do_suggest_tag' ) %]">
22+
<div class="col-12">
23+
<div class="input-group mb-3">
24+
<span class="input-group-text" id="basic-addon1">#</span>
25+
<input type="text" class="form-control" placeholder="newTag" name="tag" value="[% $form_tag %]" aria-label="New Tag Name" aria-describedby="basic-addon1">
26+
</div>
27+
</div>
28+
<div class="col-12">
29+
<div class="form-check form-switch">
30+
<input name="is_adult" [% $form_adult ? " checked" : "" %] class="form-check-input" type="checkbox" id="flexSwitchCheckDefault">
31+
<label class="form-check-label" for="flexSwitchCheckDefault">Adult Tag?</label>
32+
</div>
33+
</div>
34+
<div class="col-12">
35+
<button type="submit" class="btn btn-primary float-end">Suggest</button>
36+
</div>
37+
</form>
38+
</div>
39+
40+
%% if ( $success ) {
41+
<div class="d-flex justify-content-center">
42+
<p>Suggested #[% $tag_name %]!</p>
43+
</div>
44+
%% }
45+
46+
47+
<!-- List Pending Tags -->
48+
49+
%% for $pending_tags -> $tag {
50+
<p>[% $tag.name %] - [% $tag.id %]</p>
51+
%% }
52+
53+
54+
<!-- List Tags -->
55+
56+
%% for $tags -> $tag {
57+
<p>[% $tag.name %] - [% $tag.id %]</p>
58+
59+
%% }
60+
61+
62+
%% }

0 commit comments

Comments
 (0)