1 package Newslash::Model::Events;
2 use Newslash::Model::Base -base;
4 use Mojo::JSON qw(to_json from_json);
5 use Encode qw(encode_utf8 decode_utf8);
8 use constant EVENTS_TABLE_NAME => '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",
29 return 1 if $self->check_readonly;
31 my $table_name = EVENTS_TABLE_NAME;
32 if (!$self->table_exists($table_name)) {
33 my $dbh = $self->connect_db;
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',
46 ) DEFAULT CHARSET=utf8mb4
48 my $rs = $dbh->do($sql, undef);
53 $self->warn("cannot create table: $table_name");
63 return $self->generic_select(parmas => $params);
70 return if $self->check_readonly;
72 my $event_type = $params->{event_type};
73 return if !$event_type;
75 my $table_name = EVENTS_TABLE_NAME;
77 INSERT INTO $table_name
78 (event_type, event_subtype, related_id, source_user_id, priority, active)
83 my @values = ($event_type,
84 $params->{event_subtype},
85 $params->{related_id},
86 $params->{source_user_id},
88 defined $params->{active} ? $params->{active} : 1);
89 my $dbh = $self->connect_db;
90 my $rs = $dbh->do($sql, undef, @values);
92 $self->set_error("insert failed", $dbh->{mysql_errorno});
96 my $event_id = $dbh->last_insert_id(undef, undef, undef, undef);
105 my $event_id = $params->{event_id};
106 return if !$event_id;
108 my $table_name = EVENTS_TABLE_NAME;
110 DELETE FROM $table_name WHERE event_id = ?
113 my $dbh = $self->connect_db;
114 my $rs = $dbh->do($sql, undef, $event_id);
116 $self->set_error("delete failed", $dbh->{mysql_errorno});
117 $self->disconnect_db;
120 $self->disconnect_db;
128 return if $self->check_readonly;
130 my $id = $params->{event_id} || $params->{id};
134 if (defined $params->{active}) {
135 push @cl, "active = ?";
136 push @val, $params->{active};
138 if (defined $params->{result}) {
139 push @cl, "result = ?";
140 push @val, $params->{result};
142 if (defined $params->{priority}) {
143 push @cl, "priority = ?";
144 push @val, $params->{priority};
146 my $set_clause = join(", ", @cl);
147 return if !$set_clause;
150 my $table = EVENTS_TABLE_NAME;
152 UPDATE $table SET $set_clause WHERE event_id = ?
154 my $dbh = $self->connect_db;
155 my $rs = $dbh->do($sql, undef, @val);
157 $self->set_error("delete failed", $dbh->{mysql_errorno});
158 $self->disconnect_db;
161 $self->disconnect_db;
167 return $self->generic_select(params => {@_});
172 my $rs = $self->select(order_by => "created_timestamp", limit => 1, active => 1);
180 my ($self, $max_count) = @_;
182 my $rs = $self->select(order_by => { DESC => "priority",
183 DESC => "created_timestamp" },
194 my $table = EVENTS_TABLE_NAME;
195 my $sql = "DELETE FROM $table";
197 my $dbh = $self->connect_db;
198 my $rs = $dbh->do($sql);
200 $self->set_error("delete failed", $dbh->{mysql_errorno});
201 $self->disconnect_db;
204 $self->disconnect_db;