Navigation Menu

Skip to content

Commit

Permalink
day 10'
Browse files Browse the repository at this point in the history
  • Loading branch information
typester committed Dec 10, 2009
1 parent ae3f8d7 commit c058d6d
Show file tree
Hide file tree
Showing 9 changed files with 198 additions and 40 deletions.
46 changes: 40 additions & 6 deletions lib/Jobeet/Controller/Job.pm
Expand Up @@ -13,27 +13,61 @@ sub index :Path {
}

sub show :Path :Args(1) {
my ($self, $c, $job_id) = @_;
my ($self, $c, $job_token) = @_;

$c->stash->{job} = models('Schema::Job')->find({ token => $job_token })
or $c->detach('/default');
}

sub create :Local :Form('Jobeet::Form::Job') {
my ($self, $c) = @_;


if ($c->req->method eq 'POST' and $self->form->submitted_and_valid) {
my $job = models('Schema::Job')->create_from_form($self->form);
$c->redirect( $c->uri_for('/job', $job->token) );
}
}

sub job :Chained('/') :PathPart :CaptureArgs(1) {
my ($self, $c, $job_id) = @_;
$c->stash->{job_id} = $job_id;
my ($self, $c, $job_token) = @_;

$c->stash->{job} = models('Schema::Job')->find({ token => $job_token })
or $c->detach('/default');
}

sub edit :Chained('job') :PathPart :Form('Jobeet::Form::Job') {
my ($self, $c) = @_;

my $job = $c->stash->{job};

if ($c->req->method eq 'POST') {
if ($self->form->submitted_and_valid) {
$job->update_from_form($self->form);
$c->redirect( $c->uri_for('/job', $job->token) );
}
}
else {
$self->form->fill({
$job->get_columns,
category => $job->category->name,
});
}
}

sub edit :Chained('job') :PathPart {
sub publish :Chained('job') :PathPart {
my ($self, $c) = @_;

my $job = $c->stash->{job};

$job->publish;
$c->redirect( $c->uri_for('/job', $job->token) );
}

# /job/{job_id}/delete (削除)
sub delete :Chained('job') :PathPart {
my ($self, $c) = @_;

$c->stash->{job}->delete;
$c->redirect( $c->uri_for('/job') );
}

1;
Expand Down
6 changes: 3 additions & 3 deletions lib/Jobeet/Schema/Result/Category.pm
Expand Up @@ -44,10 +44,10 @@ sub get_active_jobs {
my $attr = shift || {};

$self->jobs(
{ expires_at => { '>=', DateTime->now } },
{ expires_at => { '>=', DateTime->now }, is_activated => 1 },
{ order_by => { -desc => 'created_at' },
defined $attr->{rows} ? (rows => $attr->{rows}) : (),
defined $attr->{page} ? (page => $attr->{page}) : (),
defined $attr->{rows} ? ( rows => $attr->{rows} ) : (),
defined $attr->{page} ? ( page => $attr->{page} ) : (),
}
);
}
Expand Down
25 changes: 25 additions & 0 deletions lib/Jobeet/Schema/Result/Job.pm
Expand Up @@ -6,6 +6,9 @@ use base 'Jobeet::Schema::ResultBase';
use DateTime;
use Jobeet::Models;

use Digest::SHA1 qw/sha1_hex/;
use Data::UUID;

__PACKAGE__->table('jobeet_job');

__PACKAGE__->add_columns(
Expand Down Expand Up @@ -107,8 +110,30 @@ __PACKAGE__->belongs_to( category => 'Jobeet::Schema::Result::Category', 'catego
sub insert {
my $self = shift;

$self->token( sha1_hex(Data::UUID->new->create) );

$self->expires_at( DateTime->now->add( days => models('conf')->{active_days} ) );
$self->next::method(@_);
}

sub publish {
my ($self) = @_;
$self->update({ is_activated => 1 });
}

sub is_expired {
my ($self) = @_;
$self->days_before_expired < 0;
}

sub days_before_expired {
my ($self) = @_;
($self->expires_at - DateTime->now)->in_units('days');
}

sub expires_soon {
my ($self) = @_;
$self->days_before_expired < 5;
}

1;
22 changes: 22 additions & 0 deletions lib/Jobeet/Schema/ResultSet/Job.pm
Expand Up @@ -3,5 +3,27 @@ use strict;
use warnings;
use base 'DBIx::Class::ResultSet';

use Jobeet::Models;

sub create_from_form {
my ($self, $form) = @_;

my $txn_guard = $self->result_source->schema->txn_scope_guard;

my $category_name = delete $form->params->{category};
my $category = models('Schema::Category')->find({ slug => $category_name })
or die 'no such category_name: ', $category_name;

my $job = $self->create({
category_id => $category->id,
%{ $form->params },
});

$txn_guard->commit;

$job;
}


1;

4 changes: 2 additions & 2 deletions root/common/base.mt
Expand Up @@ -19,7 +19,7 @@
<div class="post">
<h2>Ask for people</h2>
<div>
<a href="<?= $c->uri_for('/job/new') ?>">Post a Job</a>
<a href="<?= $c->uri_for('/job/create') ?>">Post a Job</a>
</div>
</div>

Expand Down Expand Up @@ -60,4 +60,4 @@
</div>
</div>
</body>
</html>
</html>
30 changes: 1 addition & 29 deletions root/job/create.mt
Expand Up @@ -6,35 +6,7 @@

<h1>New Job</h1>

<form method="post">

<table id="job_form">
<tfoot>
<tr>
<td colspan="2">
<input type="submit" value="Preview your job" />
</td>
</tr>
</tfoot>
<tbody>
? for my $field (qw/category type company url position location description how_to_apply email/) {
<tr>
<th><?= raw_string $form->label($field) ?></th>
<td>
? if ($form->is_error($field)) {
<ul class="error_list">
? for my $err (@{ $form->error_messages($field) }) {
<li><?= raw_string $err ?></li>
? } # endfor $err
</ul>
? } # endif
<?= raw_string $form->input($field) ?>
</td>
</tr>
? } # endfor $field
</tbody>
</table>
</form>
?= include('job/partial_form', $form);

? } # endblock content

12 changes: 12 additions & 0 deletions root/job/edit.mt
@@ -0,0 +1,12 @@
? my $form = $c->stash->{form};

? extends 'common/jobs_base';

? block content => sub {

<h1>Edit Job</h1>

?= include('job/partial_form', $form);

? } # endblock content

31 changes: 31 additions & 0 deletions root/job/partial_form.mt
@@ -0,0 +1,31 @@
? my $form = shift;

<form method="post">
<table id="job_form">
<tfoot>
<tr>
<td colspan="2">
<input type="submit" value="Preview your job" />
</td>
</tr>
</tfoot>
<tbody>
? for my $field (qw/category type company url position location description how_to_apply email/) {
<tr>
<th><?= raw_string $form->label($field) ?></th>
<td>
? if ($form->is_error($field)) {
<ul class="error_list">
? for my $err (@{ $form->error_messages($field) }) {
<li><?= raw_string $err ?></li>
? } # endfor $err
</ul>
? } # endif
<?= raw_string $form->input($field) ?>
</td>
</tr>
? } # endfor $field
</tbody>
</table>
</form>

62 changes: 62 additions & 0 deletions root/job/show.mt
@@ -0,0 +1,62 @@
? my $job = $c->stash->{job};

? extends 'common/base';

? block stylesheets => sub {
<link rel="stylesheet" type="text/css" href="<?= $c->uri_for('/css/main.css') ?>" />
<link rel="stylesheet" type="text/css" href="<?= $c->uri_for('/css/job.css') ?>" />
? }

? block content => sub {

<div id="job_actions">
<h3>Admin</h3>
<ul>
? if (!$job->is_activated) {
<li><a href="<?= $c->uri_for('/job', $job->token, 'edit') ?>">Edit</a></li>
<li><a href="<?= $c->uri_for('/job', $job->token, 'publish') ?>">Publish</a></li>
? }
<li><a href="<?= $c->uri_for('/job', $job->token, 'delete') ?>" onclick="return confirm('Are you sure?')">Delete</a></li>

? if ($job->is_activated) {
<li<?= $job->expires_soon ? ' class="expires_soon"' : '' ?>>
? if ($job->is_expired) {
Expired
? } else {
Expires in <strong><?= $job->days_before_expired ?></strong> days
? }

? if ($job->expires_soon) {
- <a href="">Extend</a> for another <?= Jobeet::Models->get('conf')->{active_days} ?> days
? }
</li>
? } else {
<li>
[Bookmark this <a href="<?= $c->req->uri ?>">URL</a> to manage this job in the future.]
</li>
? }
</ul>
</div>

<div id="job">
<h1><?= $job->company ?></h1>
<h2><?= $job->location ?></h2>
<h3>
<?= $job->category->name ?> <small> - <?= $job->type ?></small>
</h3>

<div class="description">
<?= $job->description ?>
</div>

<h4>How to apply?</h4>

<p class="how_to_apply"><?= $job->how_to_apply ?></p>

<div class="meta">
<small>posted on <?= $job->created_at->ymd ?></small>
</div>
</div>

? } # endblock content

0 comments on commit c058d6d

Please sign in to comment.