Skip to content

Commit 9af0514

Browse files
author
Kaitlyn Parkhurst
committed
Tags - additional UI.
1 parent 23067b3 commit 9af0514

File tree

5 files changed

+111
-22
lines changed

5 files changed

+111
-22
lines changed

DB/etc/schema.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ CREATE TABLE blog_tag_map (
8989
CREATE TABLE pending_tag (
9090
id serial PRIMARY KEY,
9191
name text not null unique,
92+
is_adult boolean not null default false,
9293
created_at timestamptz not null default current_timestamp
9394
);
9495

DB/lib/BlogDB/DB/Result/PendingTag.pm

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ __PACKAGE__->table("pending_tag");
4949
data_type: 'text'
5050
is_nullable: 0
5151
52+
=head2 is_adult
53+
54+
data_type: 'boolean'
55+
default_value: false
56+
is_nullable: 0
57+
5258
=head2 created_at
5359
5460
data_type: 'timestamp with time zone'
@@ -67,6 +73,8 @@ __PACKAGE__->add_columns(
6773
},
6874
"name",
6975
{ data_type => "text", is_nullable => 0 },
76+
"is_adult",
77+
{ data_type => "boolean", default_value => \"false", is_nullable => 0 },
7078
"created_at",
7179
{
7280
data_type => "timestamp with time zone",
@@ -119,8 +127,8 @@ __PACKAGE__->has_many(
119127
);
120128

121129

122-
# Created by DBIx::Class::Schema::Loader v0.07049 @ 2021-10-06 18:19:48
123-
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:HZogqHRh8JLwFRYnoaflyA
130+
# Created by DBIx::Class::Schema::Loader v0.07049 @ 2021-10-25 18:09:10
131+
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:Lax0e42Qy8BWDsYZfKmZyw
124132

125133

126134
# You can replace this text with custom code or comments, and it will be preserved on regeneration

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

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package BlogDB::Web::Controller::Tags;
22
use Mojo::Base 'Mojolicious::Controller', -signatures;
3+
use Try::Tiny;
34

45
sub get_tags ($c) {
56
$c->set_template( 'tags/index' );
@@ -35,11 +36,11 @@ sub post_suggest_tag ($c) {
3536
return 0 if $c->stash->{errors};
3637

3738
$c->db->resultset('PendingTag')->create({
38-
name => $tag_name,
39+
name => $tag_name,
40+
is_adult => $is_adult ? 1 : 0,
3941
});
4042

41-
$c->stash->{success} = 1;
42-
$c->stash->{tag_name} = $tag_name;
43+
$c->redirect_to( $c->url_for( 'tags' ) );
4344
}
4445

4546
sub post_vote_tag ($c) {
@@ -61,32 +62,53 @@ sub post_vote_tag ($c) {
6162
my $vote = $c->db->resultset('TagVote')->search( {
6263
tag_id => $tag->id,
6364
person_id => $c->stash->{person}->id,
64-
});
65+
})->first;
6566

6667
if ( $vote ) {
67-
$vote->vote( ! $vote->vote );
68+
$vote->vote( $vote->vote ? 0 : 1 );
6869
$vote->update;
6970
} else {
7071
$c->stash->{person}->create_related( 'tag_votes', {
7172
tag_id => $tag->id,
7273
});
7374
}
7475

75-
$c->stash->{success} = 1;
76+
$c->redirect_to( $c->url_for( 'tags' ) );
7677
}
7778

7879
sub post_delete_tag ($c) {
7980
$c->set_template( 'tags/index' );
80-
81+
8182
push @{$c->stash->{tags}}, $c->db->resultset('Tag')->all;
8283
push @{$c->stash->{pending_tags}}, $c->db->resultset('PendingTag')->all;
8384

8485
my $tag_name = $c->stash->{form_tag} = $c->param('tag');
86+
87+
my $tag = $c->db->resultset('PendingTag')->search({ name => $tag_name })->first;
88+
89+
push @{$c->stash->{errors}}, "No such tag?"
90+
unless $tag;
91+
92+
return 0 if $c->stash->{errors};
93+
94+
try {
95+
$c->db->storage->schema->txn_do( sub {
96+
$c->db->resultset('TagVote')->search({
97+
tag_id => $tag->id,
98+
})->delete;
99+
$tag->delete;
100+
});
101+
} catch {
102+
push @{$c->stash->{errors}}, "The tag could not be deleted $_";
103+
};
104+
return 0 if $c->stash->{errors};
105+
106+
$c->redirect_to( $c->url_for( 'tags' ) );
85107
}
86108

87109
sub post_approve_tag ($c) {
88110
$c->set_template( 'tags/index' );
89-
111+
90112
push @{$c->stash->{tags}}, $c->db->resultset('Tag')->all;
91113
push @{$c->stash->{pending_tags}}, $c->db->resultset('PendingTag')->all;
92114

@@ -99,12 +121,23 @@ sub post_approve_tag ($c) {
99121

100122
return 0 if $c->stash->{errors};
101123

102-
$c->db->resultset('Tag')->create({
103-
name => $tag->name,
104-
is_adult => $tag->is_adult,
105-
});
106-
107-
$c->stash->{success} = 1;
124+
try {
125+
$c->db->storage->schema->txn_do( sub {
126+
$c->db->resultset('Tag')->create({
127+
name => $tag->name,
128+
is_adult => $tag->is_adult,
129+
});
130+
$c->db->resultset('TagVote')->search({
131+
tag_id => $tag->id,
132+
})->delete;
133+
$tag->delete;
134+
});
135+
} catch {
136+
push @{$c->stash->{errors}}, "The tag could not be created: $_";
137+
};
138+
return 0 if $c->stash->{errors};
139+
140+
$c->redirect_to( $c->url_for( 'tags' ) );
108141
}
109142

110143
1;

Web/templates/default/_/layout.tx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
<button class="btn btn-outline-success" type="submit">Login</button>
5757
</form>
5858
<div class="container-fluid">
59-
<p style="margin-right: 0em;"><a href="[% $c.url_for('register') %]">Register</a> | <a href="[% $c.url_for( 'forgot' ) %]">Forgot</a></p>
59+
<p style="margin-right: 0em;"><a href="[% $c.url_for('register') %]">Register</a> | <a href="[% $c.url_for( 'forgot_password' ) %]">Forgot</a></p>
6060
</div>
6161
</div>
6262
%% }

Web/templates/default/tags/index.html.tx

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
%% cascade default::_::layout { title => 'Tags',
22
%%
3-
%% }
3+
%% };
4+
5+
46

57
%% override panel -> {
68

@@ -14,7 +16,6 @@
1416
</ul>
1517
</div>
1618
%% }
17-
</div>
1819

1920
<!-- Add New Tag Section -->
2021
<div class="d-flex justify-content-center">
@@ -45,16 +46,62 @@
4546

4647

4748
<!-- List Pending Tags -->
48-
49+
<h2>Pending Tags</h2>
4950
%% for $pending_tags -> $tag {
50-
<p>[% $tag.name %] - [% $tag.id %]</p>
51+
<p style="display: inline">
52+
53+
#[% $tag.name %]
54+
55+
<small>([% $tag.search_related('tag_votes', { vote => 1 } ).count() %] Votes)</small>
56+
57+
%% if ( $tag.is_adult ) {
58+
<small>Adult</small>
59+
%% }
60+
61+
<!-- Upvote Button -->
62+
<form style="display: inline" method="post" action="[% $c.url_for( 'do_vote_tag') %]">
63+
<input type="hidden" name="tag" value="[% $tag.name %]">
64+
<button type="submit">
65+
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-up-circle" viewBox="0 0 16 16">
66+
<path fill-rule="evenodd" d="M1 8a7 7 0 1 0 14 0A7 7 0 0 0 1 8zm15 0A8 8 0 1 1 0 8a8 8 0 0 1 16 0zm-7.5 3.5a.5.5 0 0 1-1 0V5.707L5.354 7.854a.5.5 0 1 1-.708-.708l3-3a.5.5 0 0 1 .708 0l3 3a.5.5 0 0 1-.708.708L8.5 5.707V11.5z"/>
67+
</svg>
68+
</button>
69+
</form>
70+
71+
<!-- Delete Button -->
72+
<form style="display: inline" method="post" action="[% $c.url_for( 'do_delete_tag') %]">
73+
<input type="hidden" name="tag" value="[% $tag.name %]">
74+
<button type="submit">
75+
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-trash" viewBox="0 0 16 16">
76+
<path d="M5.5 5.5A.5.5 0 0 1 6 6v6a.5.5 0 0 1-1 0V6a.5.5 0 0 1 .5-.5zm2.5 0a.5.5 0 0 1 .5.5v6a.5.5 0 0 1-1 0V6a.5.5 0 0 1 .5-.5zm3 .5a.5.5 0 0 0-1 0v6a.5.5 0 0 0 1 0V6z"/>
77+
<path fill-rule="evenodd" d="M14.5 3a1 1 0 0 1-1 1H13v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V4h-.5a1 1 0 0 1-1-1V2a1 1 0 0 1 1-1H6a1 1 0 0 1 1-1h2a1 1 0 0 1 1 1h3.5a1 1 0 0 1 1 1v1zM4.118 4 4 4.059V13a1 1 0 0 0 1 1h6a1 1 0 0 0 1-1V4.059L11.882 4H4.118zM2.5 3V2h11v1h-11z"/>
78+
</svg>
79+
</button>
80+
</form>
81+
82+
<!-- Approve Button -->
83+
<form style="display: inline" method="post" action="[% $c.url_for( 'do_approve_tag') %]">
84+
<input type="hidden" name="tag" value="[% $tag.name %]">
85+
<button type="submit">
86+
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-plus-circle" viewBox="0 0 16 16">
87+
<path d="M8 15A7 7 0 1 1 8 1a7 7 0 0 1 0 14zm0 1A8 8 0 1 0 8 0a8 8 0 0 0 0 16z"/>
88+
<path d="M8 4a.5.5 0 0 1 .5.5v3h3a.5.5 0 0 1 0 1h-3v3a.5.5 0 0 1-1 0v-3h-3a.5.5 0 0 1 0-1h3v-3A.5.5 0 0 1 8 4z"/>
89+
</svg>
90+
</button>
91+
</form>
92+
</p>
93+
5194
%% }
5295

5396

5497
<!-- List Tags -->
5598

99+
<h2>Existing Tags</h2>
56100
%% for $tags -> $tag {
57-
<p>[% $tag.name %] - [% $tag.id %]</p>
101+
<p>
102+
#[% $tag.name %]
103+
[% $tag.is_adult ? mark_raw("<small>(Adult)</small>") : "" %]
104+
</p>
58105

59106
%% }
60107

0 commit comments

Comments
 (0)