root/veekun/trunk/lib/Vee/Schema/Forums.pm @ 406

Revision 406, 2.0 KB (checked in by eevee, 22 months ago)

Database refactoring. Renamed columns and tables to be more consistent and more readable. (#58)

Line 
1package Vee::Schema::Forums;
2
3use strict;
4use warnings;
5use base 'DBIx::Class';
6
7=head1 NAME
8
9Vee::Schema::Forums - DBIC class for the C<forums> table
10
11=cut
12
13__PACKAGE__->load_components(qw/ ResultSetManager Core /);
14__PACKAGE__->table('forums');
15__PACKAGE__->add_columns(qw/
16    id name last_post_id thread_count post_count flags accessibility description
17/);
18__PACKAGE__->set_primary_key('id');
19
20__PACKAGE__->has_many(threads => 'Vee::Schema::Threads', 'forum_id');
21__PACKAGE__->might_have(last_post => 'Vee::Schema::Posts', { 'foreign.id' => 'self.last_post_id' });
22
23=head2 unread_ids($user)
24
25Returns a list of all forum ids the given user has not read.
26
27=cut
28
29sub unread_ids : ResultSet {
30    my ($self, $user) = @_;
31
32    # ->get_column doesn't seem to work here; it tries 'SELECT id' and I get
33    # ambiguity errors
34    return map { $_->id } $self->search( {
35        'threads.last_post_time'   => { '>' => $user->thread_view_cutoff },
36        'thread_views.last_viewed' => [ undef, \'< threads.last_post_time' ],
37    }, {
38        # XXX: This is very naughty, but the only way to stuff the user id in
39        # the ON clause
40        from     => [
41            { me => 'forums' },
42            [
43                [
44                    { threads => 'threads' },
45                    [
46                        {
47                            thread_views => 'thread_views',
48                            -join_type => 'left'
49                        }, { # ON
50                            'thread_views.thread_id' => 'threads.id',
51                            'thread_views.user_id' => $user->id,
52                        },
53                    ],
54                ],
55                { 'me.id' => 'threads.forum_id' },
56            ],
57        ],
58        group_by => 'me.id',
59        columns  => [qw[ me.id ]],
60    } )->all;
61}
62
63=head1 SEE ALSO
64
65L<Vee::Schema>, L<DBIx::Class>
66
67=head1 AUTHOR
68
69Maintainer: Alex "Eevee" Munroe (C<veekun@veekun.com>)
70
71See the included F<AUTHORS> file for a full list of contributers.
72
73=head1 LICENSE
74
75See the included F<LICENSE> file.
76
77=cut
78
791;
Note: See TracBrowser for help on using the browser.