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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
|
use crate::db::get_client;
use crate::errors::{AppError, AppErrorType};
use chrono::{DateTime, Local};
use deadpool_postgres::Pool;
use serde::{Deserialize, Serialize};
use tokio_pg_mapper::FromTokioPostgresRow;
use tokio_pg_mapper_derive::PostgresMapper;
#[derive(Serialize, Deserialize, PostgresMapper)]
#[pg_mapper(table = "commit")]
/// Commit model
pub struct Commit {
pub hash: String,
pub tree: Option<String>,
pub text: String,
pub date: DateTime<Local>,
pub author_email: String, // Reference to Email
pub author_name: String,
pub committer_email: String, // Reference to Email
pub committer_name: String,
pub repository_url: String, // Reference to Repository
}
/// Model used for 'most authors' function
#[derive(Serialize, Deserialize)]
pub struct CommitNumAuthor {
pub num: i64,
pub author_email: String,
pub author_name: String,
}
impl Commit {
/// Find all commits. Order them by descrescent `date` field
/// `commit_hash` is used to search commit that matches with some sha codes
pub async fn find_all(
pool: Pool,
commit_hash: &String,
) -> Result<Vec<Commit>, AppError> {
let client = get_client(pool.clone()).await.unwrap();
let query;
let hash;
if commit_hash != "" {
hash = format!("%{}%", commit_hash);
query = "SELECT * FROM commit WHERE hash LIKE $1 ORDER BY date DESC LIMIT 300";
} else {
hash = String::new();
query = "SELECT * FROM commit ORDER BY date DESC LIMIT 300"
}
let statement = client.prepare(query).await?;
let commits;
if hash != "" {
commits = client.query(&statement, &[&hash]).await?;
} else {
commits = client.query(&statement, &[]).await?;
}
let result = commits
.iter()
.map(|row| Commit::from_row_ref(row).unwrap())
.collect::<Vec<Commit>>();
Ok(result)
}
/// Find all repository url' commits
pub async fn find_by_repository(
pool: Pool,
repository_url: String,
) -> Result<Vec<Commit>, AppError> {
let client = get_client(pool.clone()).await.unwrap();
let statement = client
.prepare(
"SELECT * FROM commit
WHERE repository_url = $1
ORDER BY date DESC
LIMIT 1000",
)
.await?;
let commits = client
.query(&statement, &[&repository_url])
.await?
.iter()
.map(|row| Commit::from_row_ref(row).unwrap())
.collect::<Vec<Commit>>();
Ok(commits)
}
// Find a commit that it has an hash equals to `hash`
pub async fn find(pool: Pool, hash: String) -> Result<Commit, AppError> {
let client = get_client(pool.clone()).await.unwrap();
let statement = client
.prepare("SELECT * FROM commit WHERE hash = $1")
.await?;
let commit = client
.query_opt(&statement, &[&hash])
.await?
.map(|row| Commit::from_row_ref(&row).unwrap());
match commit {
Some(commit) => Ok(commit),
None => Err(AppError {
error_type: AppErrorType::NotFoundError,
cause: None,
message: Some("Commit not found".to_string()),
}),
}
}
/// Find a commit and delete it, but before check if "Authorization"
/// matches with SECRET_KEY
pub async fn delete(
pool: Pool,
hash: &String,
) -> Result<Commit, AppError> {
let client = get_client(pool.clone()).await.unwrap();
let statement = client
.prepare(
"
DELETE FROM commit
WHERE hash=$1
RETURNING *
",
)
.await?;
let commit = client
.query_opt(&statement, &[&hash])
.await?
.map(|row| Commit::from_row_ref(&row).unwrap());
match commit {
Some(commit) => Ok(commit),
None => Err(AppError {
error_type: AppErrorType::NotFoundError,
cause: None,
message: Some("Commit not found".to_string()),
}),
}
}
/// Create commits from an array
pub async fn create(
pool: Pool,
commits: Vec<Commit>,
) -> Result<Vec<Commit>, AppError> {
let client = get_client(pool.clone()).await.unwrap();
let mut raw_query = "INSERT INTO commit VALUES".to_string();
for commit in commits {
let tree = match commit.tree {
Some(t) => format!("'{}'", t),
None => "NULL".to_string(),
};
raw_query += &format!(
"('{}', {}, E'{}', '{}', '{}', E'{}', '{}', E'{}', '{}'),",
commit.hash,
tree,
commit.text.replace("\\'", "'").replace("'", "\\'"),
commit.date,
commit.author_email,
commit.author_name.replace("\\'", "'").replace("'", "\\'"),
commit.committer_email,
commit
.committer_name
.replace("\\'", "'")
.replace("'", "\\'"),
commit.repository_url
)[..]
}
// Remove the last `,`
let _ = raw_query.pop();
raw_query += " RETURNING *";
// TODO: write query with &commits and parameter. Need to implement
// ToSql trait for `Commit` model
// let statement = client.prepare(&query[..]).await?;
// client.query(&statement, &[&commits]
let statement = client.prepare(&raw_query[..]).await?;
let result = client
.query(&statement, &[])
.await?
.iter()
.map(|row| Commit::from_row_ref(row).unwrap())
.collect::<Vec<Commit>>();
Ok(result)
}
/// Returns a ranking of authors of his commits number
pub async fn most_authors(
pool: Pool,
) -> Result<Vec<CommitNumAuthor>, AppError> {
let client = get_client(pool.clone()).await.unwrap();
let statement = client.prepare(
"SELECT COUNT(hash) as num, author_email, author_name FROM commit
GROUP BY author_email, author_name ORDER BY COUNT(hash) DESC"
).await?;
let authors = client
.query(&statement, &[])
.await?
.iter()
.map(|row| CommitNumAuthor {
num: row.get(0),
author_email: row.get(1),
author_name: row.get(2),
})
.collect::<Vec<CommitNumAuthor>>();
Ok(authors)
}
}
|