OSDN Git Service

Model::Base: remove deprecated / unused functions
[newslash/newslash.git] / src / newslash_web / lib / Newslash / Model / Events.pm
1 package Newslash::Model::Events;
2 use Newslash::Model::Base -base;
3
4 use Mojo::JSON qw(to_json from_json);
5 use Encode qw(encode_utf8 decode_utf8);
6 use Mojo::Log;
7
8 use constant EVENTS_TABLE_NAME => 'ns_events';
9
10 sub key_definition {
11     return {
12             table => "ns_events",
13             primary => "event_id",
14             datetime => [qw(created_timestamp)],
15             timestamp => "created_timestamp",
16             other => [qw(event_type event_subtype
17                          related_id source_user_id
18                          priority active result)],
19             aliases => { event_id => "id",
20                          create_time => "created_timestamp",
21                        }
22            };
23 }
24
25 sub on_start_up {
26     my $self = shift;
27
28     # create tables
29     return 1 if $self->check_readonly;
30
31     my $table_name = EVENTS_TABLE_NAME;
32     if (!$self->table_exists($table_name)) {
33         my $dbh = $self->connect_db;
34
35         my $sql = <<"EOSQL";
36 CREATE TABLE IF NOT EXISTS $table_name (
37   event_id          mediumint(8) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
38   event_type        varchar(32)           NOT NULL,
39   event_subtype     varchar(32),
40   related_id        mediumint(8) unsigned,
41   source_user_id    mediumint(8) unsigned,
42   created_timestamp timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
43   priority          tinyint(4)           DEFAULT '0',
44   active            tinyint(4)           NOT NULL DEFAULT '1',
45   result            tinyint(4)
46 ) DEFAULT CHARSET=utf8mb4
47 EOSQL
48         my $rs = $dbh->do($sql, undef);
49         $self->disconnect_db;
50         if ($rs) {
51             return 1;
52         }
53         $self->warn("cannot create table: $table_name");
54     }
55
56     return 1;
57 }
58
59 sub select {
60     my $self = shift;
61     my $params = {@_};
62
63     return $self->generic_select(parmas => $params);
64 }
65
66
67 sub create {
68     my $self = shift @_;
69     my $params = {@_};
70     return if $self->check_readonly;
71
72     my $event_type = $params->{event_type};
73     return if !$event_type;
74
75     my $table_name = EVENTS_TABLE_NAME;
76     my $sql = <<"EOSQL";
77 INSERT INTO $table_name
78     (event_type, event_subtype, related_id, source_user_id, priority, active)
79   VALUES
80     (?,          ?,             ?,          ?,              ?,        ?)
81 EOSQL
82
83     my @values = ($event_type,
84                   $params->{event_subtype},
85                   $params->{related_id},
86                   $params->{source_user_id},
87                   $params->{priority},
88                   defined $params->{active} ? $params->{active} : 1);
89     my $dbh = $self->connect_db;
90     my $rs = $dbh->do($sql, undef, @values);
91     if (!$rs) {
92         $self->set_error("insert failed", $dbh->{mysql_errorno});
93         $self->disconnect_db;
94         return;
95     }
96     my $event_id = $dbh->last_insert_id(undef, undef, undef, undef);
97     $self->disconnect_db;
98
99     return $event_id;
100 }
101
102 sub delete {
103     my $self = shift @_;
104     my $params = {@_};
105     my $event_id = $params->{event_id};
106     return if !$event_id;
107
108     my $table_name = EVENTS_TABLE_NAME;
109     my $sql = <<"EOSQL";
110 DELETE FROM $table_name WHERE event_id = ?
111 EOSQL
112
113     my $dbh = $self->connect_db;
114     my $rs = $dbh->do($sql, undef, $event_id);
115     if (!defined $rs) {
116         $self->set_error("delete failed", $dbh->{mysql_errorno});
117         $self->disconnect_db;
118         return;
119     }
120     $self->disconnect_db;
121
122     return $rs;
123 }
124
125 sub update {
126     my $self = shift;
127     my $params = {@_};
128     return if $self->check_readonly;
129
130     my $id = $params->{event_id} || $params->{id};
131     return if !$id;
132     my @cl;
133     my @val;
134     if (defined $params->{active}) {
135         push @cl, "active = ?";
136         push @val, $params->{active};
137     }
138     if (defined $params->{result}) {
139         push @cl, "result = ?";
140         push @val, $params->{result};
141     }
142     if (defined $params->{priority}) {
143         push @cl, "priority = ?";
144         push @val, $params->{priority};
145     }
146     my $set_clause = join(", ", @cl);
147     return if !$set_clause;
148     push @val, $id;
149
150     my $table = EVENTS_TABLE_NAME;
151     my $sql = <<"EOSQL";
152 UPDATE $table SET $set_clause WHERE event_id = ?
153 EOSQL
154     my $dbh = $self->connect_db;
155     my $rs = $dbh->do($sql, undef, @val);
156     if (!defined $rs) {
157         $self->set_error("delete failed", $dbh->{mysql_errorno});
158         $self->disconnect_db;
159         return;
160     }
161     $self->disconnect_db;
162     return $rs;
163 }
164
165 sub select {
166     my $self = shift;
167     return $self->generic_select(params => {@_});
168 }
169
170 sub oldest {
171     my $self = shift;
172     my $rs = $self->select(order_by => "created_timestamp", limit => 1, active => 1);
173     if ($rs) {
174         return $rs->[0];
175     }
176     return;
177 }
178
179 sub fetch {
180     my ($self, $max_count) = @_;
181     $max_count ||= 10;
182     my $rs = $self->select(order_by => { DESC => "priority",
183                                          DESC => "created_timestamp" },
184                            limit => $max_count,
185                            active => 1);
186     if ($rs) {
187         return $rs;
188     }
189     return;
190 }
191
192 sub delete_all {
193     my $self = shift;
194     my $table = EVENTS_TABLE_NAME;
195     my $sql = "DELETE FROM $table";
196
197     my $dbh = $self->connect_db;
198     my $rs = $dbh->do($sql);
199     if (!defined $rs) {
200         $self->set_error("delete failed", $dbh->{mysql_errorno});
201         $self->disconnect_db;
202         return;
203     }
204     $self->disconnect_db;
205     return $rs;
206 }
207
208 1;