Skip to content

Commit

Permalink
Added tag creation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaitlyn Parkhurst committed Oct 23, 2021
1 parent a1dc2ee commit 23067b3
Show file tree
Hide file tree
Showing 7 changed files with 230 additions and 3 deletions.
6 changes: 3 additions & 3 deletions Web/lib/BlogDB/Web.pm
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,15 @@ sub startup ($self) {

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

# /tags/
$r->get ( '/tags' )->to( 'Tags#get_tags' )->name( 'tags' );
$auth->post( '/tags/suggest' )->to( 'Tags#post_suggest_tag')->name( 'do_suggest_tag' );
$auth->post( '/tags/vote' )->to( 'Tags#post_vote_tag' )->name( 'do_vote_tag' );
$auth->post( '/tags/delete' )->to( 'Tags#post_delete_tag' )->name( 'do_delete_tag' );
$auth->post( '/tags/approve' )->to( 'Tags#post_approve_tag')->name( 'do_approve_tag' );

}

Expand Down
2 changes: 2 additions & 0 deletions Web/lib/BlogDB/Web/Controller/Create.pm
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ sub post_new ($c) {

sub get_new_blog ($c) {
$c->set_template( 'new/index' );

$c->stash->{blog_url} = $c->param('name');
}

sub post_new_blog ($c) {
Expand Down
5 changes: 5 additions & 0 deletions Web/lib/BlogDB/Web/Controller/Root.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ use Mojo::Base 'Mojolicious::Controller', -signatures;
use Try::Tiny;
use Data::UUID;

sub get_homepage ($c) {
$c->set_template( 'index' );

}

sub get_register ($c) {
$c->set_template( 'register' );

Expand Down
90 changes: 90 additions & 0 deletions Web/lib/BlogDB/Web/Controller/Tags.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,108 @@ use Mojo::Base 'Mojolicious::Controller', -signatures;

sub get_tags ($c) {
$c->set_template( 'tags/index' );

push @{$c->stash->{tags}}, $c->db->resultset('Tag')->all;
push @{$c->stash->{pending_tags}}, $c->db->resultset('PendingTag')->all;

}

sub post_suggest_tag ($c) {
$c->set_template( 'tags/index' );

push @{$c->stash->{tags}}, $c->db->resultset('Tag')->all;
push @{$c->stash->{pending_tags}}, $c->db->resultset('PendingTag')->all;

my $tag_name = $c->stash->{form_tag} = $c->param('tag');
my $is_adult = $c->stash->{form_adult} = $c->param('is_adult');

push @{$c->stash->{errors}}, "Tag names must start with a letter, and may only contain letters and numbers."
unless $tag_name =~ m/^[a-zA-Z][a-zA-Z0-9]+$/;

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

my $tag_exists = $c->db->resultset('Tag' )->search({ name => $tag_name })->first;
my $pending_tag_exists = $c->db->resultset('PendingTag')->search({ name => $tag_name })->first;

push @{$c->stash->{errors}}, "There is already a tag with that name."
if $tag_exists;

push @{$c->stash->{errors}}, "There is already a pending tag with that name."
if $pending_tag_exists;

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

$c->db->resultset('PendingTag')->create({
name => $tag_name,
});

$c->stash->{success} = 1;
$c->stash->{tag_name} = $tag_name;
}

sub post_vote_tag ($c) {
$c->set_template( 'tags/index' );

push @{$c->stash->{tags}}, $c->db->resultset('Tag')->all;
push @{$c->stash->{pending_tags}}, $c->db->resultset('PendingTag')->all;

my $tag_name = $c->stash->{form_tag} = $c->param('tag');

my $tag = $c->db->resultset('PendingTag')->search({ name => $tag_name })->first;

push @{$c->stash->{errors}}, "No such tag?"
unless $tag;

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

# Find out if the user already voted -- in which case we are toggling the vote.
my $vote = $c->db->resultset('TagVote')->search( {
tag_id => $tag->id,
person_id => $c->stash->{person}->id,
});

if ( $vote ) {
$vote->vote( ! $vote->vote );
$vote->update;
} else {
$c->stash->{person}->create_related( 'tag_votes', {
tag_id => $tag->id,
});
}

$c->stash->{success} = 1;
}

sub post_delete_tag ($c) {
$c->set_template( 'tags/index' );

push @{$c->stash->{tags}}, $c->db->resultset('Tag')->all;
push @{$c->stash->{pending_tags}}, $c->db->resultset('PendingTag')->all;

my $tag_name = $c->stash->{form_tag} = $c->param('tag');
}

sub post_approve_tag ($c) {
$c->set_template( 'tags/index' );

push @{$c->stash->{tags}}, $c->db->resultset('Tag')->all;
push @{$c->stash->{pending_tags}}, $c->db->resultset('PendingTag')->all;

my $tag_name = $c->stash->{form_tag} = $c->param('tag');

my $tag = $c->db->resultset('PendingTag')->search({ name => $tag_name })->first;

push @{$c->stash->{errors}}, "No such tag?"
unless $tag;

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

$c->db->resultset('Tag')->create({
name => $tag->name,
is_adult => $tag->is_adult,
});

$c->stash->{success} = 1;
}

1;
Expand Down
19 changes: 19 additions & 0 deletions Web/templates/default/index.html.tx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
%% cascade default::_::layout { title => 'Home',
%%
%% }

%% override panel -> {

<!-- Add New Blog Section -->
<div class="d-flex justify-content-center">
<form method="post" class="row row-cols-lg-auto align-items-center" action="[% $c.url_for( 'do_add_blog' ) %]">
<div class="col-12">
<input placeholder="http://reallycoolblog.com" type="text" name="url" value="[% $form_blog_url %]">
</div>
<div class="col-12">
<button type="submit" class="btn btn-primary float-end">Next</button>
</div>
</form>
</div>

%% }
49 changes: 49 additions & 0 deletions Web/templates/default/new/index.html.tx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
%% cascade default::_::layout { title => 'Edit ' ~ $blog_url,
%%
%% }

%% override panel -> {

<h3>[% $blog_url %]</h3>

<div class="row">
<!-- Error Handling On LHS -->
<div class="col">
%% if ( $errors.size() ) {
<div style="margin-top: 2em" class="alert alert-danger" role="alert">
There were errors with your request that could not be resolved:
<ul>
%% for $errors -> $error {
<li>[% $error %]</li>
%% }
</ul>
</div>
%% }
</div>
</div>

<div class="row">
<div class="col">
<form method="post" action="[% $c.url_for( 'do_register' ) %]">

%% include 'default/_/form/input.tx' { type => 'text', name => 'title',
%% title => 'Title',
%% help => 'The title of the blog',
%% value => $form_title,
%% };

%% include 'default/_/form/input.tx' { type => 'text', name => 'tagline',
%% title => 'Tagline',
%% help => 'The tagline of the blog',
%% value => $form_tagline,
%% };

<textarea name="description" rows="4">[% $form_description %]</textarea>

<button type="submit" class="btn btn-primary float-end">Register</button>

</form>
</div>
</div>

%% }
62 changes: 62 additions & 0 deletions Web/templates/default/tags/index.html.tx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
%% cascade default::_::layout { title => 'Tags',
%%
%% }

%% override panel -> {

%% if ( $errors.size() ) {
<div style="margin-top: 2em" class="alert alert-danger" role="alert">
There were errors with your request that could not be resolved:
<ul>
%% for $errors -> $error {
<li>[% $error %]</li>
%% }
</ul>
</div>
%% }
</div>

<!-- Add New Tag Section -->
<div class="d-flex justify-content-center">
<form method="post" class="row row-cols-lg-auto align-items-center" action="[% $c.url_for( 'do_suggest_tag' ) %]">
<div class="col-12">
<div class="input-group mb-3">
<span class="input-group-text" id="basic-addon1">#</span>
<input type="text" class="form-control" placeholder="newTag" name="tag" value="[% $form_tag %]" aria-label="New Tag Name" aria-describedby="basic-addon1">
</div>
</div>
<div class="col-12">
<div class="form-check form-switch">
<input name="is_adult" [% $form_adult ? " checked" : "" %] class="form-check-input" type="checkbox" id="flexSwitchCheckDefault">
<label class="form-check-label" for="flexSwitchCheckDefault">Adult Tag?</label>
</div>
</div>
<div class="col-12">
<button type="submit" class="btn btn-primary float-end">Suggest</button>
</div>
</form>
</div>

%% if ( $success ) {
<div class="d-flex justify-content-center">
<p>Suggested #[% $tag_name %]!</p>
</div>
%% }


<!-- List Pending Tags -->

%% for $pending_tags -> $tag {
<p>[% $tag.name %] - [% $tag.id %]</p>
%% }


<!-- List Tags -->

%% for $tags -> $tag {
<p>[% $tag.name %] - [% $tag.id %]</p>

%% }


%% }

0 comments on commit 23067b3

Please sign in to comment.