1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#![allow(clippy::implicit_hasher)]
use diesel::pg::PgConnection;
use diesel::result::Error;
use diesel::*;
use diesel::{delete, insert_into};
use super::{Corpus, Service};
use crate::concerns::{CortexDeletable, CortexInsertable};
use crate::schema::tasks;
#[derive(Identifiable, Queryable, AsChangeset, Clone, Debug, PartialEq, Eq, QueryableByName)]
#[table_name = "tasks"]
pub struct Task {
pub id: i64,
pub service_id: i32,
pub corpus_id: i32,
pub status: i32,
pub entry: String,
}
#[derive(Insertable, Debug, Clone)]
#[table_name = "tasks"]
pub struct NewTask {
pub service_id: i32,
pub corpus_id: i32,
pub status: i32,
pub entry: String,
}
impl CortexInsertable for NewTask {
fn create(&self, connection: &PgConnection) -> Result<usize, Error> {
insert_into(tasks::table).values(self).execute(connection)
}
}
impl CortexDeletable for Task {
fn delete_by(&self, connection: &PgConnection, field: &str) -> Result<usize, Error> {
match field {
"entry" => self.delete_by_entry(connection),
"service_id" => self.delete_by_service_id(connection),
"id" => self.delete_by_id(connection),
_ => Err(Error::QueryBuilderError(
format!("unknown Task model field: {}", field).into(),
)),
}
}
}
impl Task {
pub fn delete_by_entry(&self, connection: &PgConnection) -> Result<usize, Error> {
use crate::schema::tasks::dsl::entry;
delete(tasks::table.filter(entry.eq(&self.entry))).execute(connection)
}
pub fn delete_by_service_id(&self, connection: &PgConnection) -> Result<usize, Error> {
use crate::schema::tasks::dsl::service_id;
delete(tasks::table.filter(service_id.eq(&self.service_id))).execute(connection)
}
pub fn delete_by_id(&self, connection: &PgConnection) -> Result<usize, Error> {
use crate::schema::tasks::dsl::id;
delete(tasks::table.filter(id.eq(self.id))).execute(connection)
}
pub fn find(taskid: i64, connection: &PgConnection) -> Result<Task, Error> {
tasks::table.find(taskid).first(connection)
}
pub fn find_by_entry(entry: &str, connection: &PgConnection) -> Result<Task, Error> {
tasks::table
.filter(tasks::entry.eq(entry))
.first(connection)
}
pub fn find_by_name(
name: &str,
corpus: &Corpus,
service: &Service,
connection: &PgConnection,
) -> Result<Task, Error>
{
use crate::schema::tasks::dsl::{corpus_id, service_id};
tasks::table
.filter(corpus_id.eq(corpus.id))
.filter(service_id.eq(service.id))
.filter(tasks::entry.like(&format!("%{}.zip", name)))
.first(connection)
}
}
impl CortexDeletable for NewTask {
fn delete_by(&self, connection: &PgConnection, field: &str) -> Result<usize, Error> {
match field {
"entry" => self.delete_by_entry(connection),
"service_id" => self.delete_by_service_id(connection),
_ => Err(Error::QueryBuilderError(
format!("unknown Task model field: {}", field).into(),
)),
}
}
}
impl NewTask {
fn delete_by_entry(&self, connection: &PgConnection) -> Result<usize, Error> {
use crate::schema::tasks::dsl::entry;
delete(tasks::table.filter(entry.eq(&self.entry))).execute(connection)
}
fn delete_by_service_id(&self, connection: &PgConnection) -> Result<usize, Error> {
use crate::schema::tasks::dsl::service_id;
delete(tasks::table.filter(service_id.eq(&self.service_id))).execute(connection)
}
pub fn create_if_new(&self, connection: &PgConnection) -> Result<usize, Error> {
insert_into(tasks::table)
.values(self)
.on_conflict_do_nothing()
.execute(connection)
}
}