« lib/Mojolicious/Plugin/Fondation/User/Schema/Result/User.pm
↑ Summary
lib/Mojolicious/Plugin/Fondation/User.pm »
| 1 | package Mojolicious::Plugin::Fondation::User::Schema::ResultSet::User; | ||
| 2 | # ABSTRACT: DBIx::Class ResultSet class for users table | ||
| 3 | $Mojolicious::Plugin::Fondation::User::Schema::ResultSet::User::VERSION = '0.04'; | ||
| 4 | use base 'DBIx::Class::ResultSet'; | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | use DateTime; | ||
| 9 | |||
| 10 | # Example of useful method: active users (if you add an 'active' field) | ||
| 11 | sub active { | ||
| 12 | shift->search({ active => 1 }); | ||
| 13 | } | ||
| 14 | |||
| 15 | # Example: users created today | ||
| 16 | sub created_today { | ||
| 17 | my $self = shift; | ||
| 18 | my $today = DateTime->today->strftime('%Y-%m-%d'); | ||
| 19 | return $self->search({ | ||
| 20 | created_at => { '>=' => $today, '<' => DateTime->today->add( days => 1 )->strftime('%Y-%m-%d') }, | ||
| 21 | }); | ||
| 22 | } | ||
| 23 | |||
| 24 | # Example: search by email or username (case insensitive) | ||
| 25 | sub search_by_login { | ||
| 26 | my ($self, $login) = @_; | ||
| 27 | return $self->search([ | ||
| 28 | { username => { -like => "%$login%" } }, | ||
| 29 | { email => { -like => "%$login%" } }, | ||
| 30 | ]); | ||
| 31 | } | ||
| 32 | |||
| 33 | # Example: latest registered user (sorted by created_at DESC) | ||
| 34 | sub latest { | ||
| 35 | shift->search(undef, { order_by => { -desc => 'created_at' }, rows => 10 }); | ||
| 36 | } | ||
| 37 | |||
| 38 | 1; | ||
| 39 | |||
| 40 | __END__ |