|
1 | 1 | //! This module defines an abstraction layer over the database. |
2 | 2 |
|
3 | 3 | use std::sync::Arc; |
4 | | -#[cfg(not(test))] |
5 | | -use std::time::Duration; |
6 | 4 |
|
7 | 5 | use anyhow::Result; |
8 | 6 | use async_trait::async_trait; |
9 | | -#[cfg(not(test))] |
10 | | -use cached::proc_macro::cached; |
11 | 7 | use deadpool_postgres::{Pool, Transaction}; |
12 | 8 | #[cfg(test)] |
13 | 9 | use mockall::automock; |
14 | | -use tokio_postgres::{Client, types::Json}; |
| 10 | +use tokio_postgres::types::Json; |
15 | 11 | use uuid::Uuid; |
16 | 12 |
|
17 | 13 | use crate::{ |
@@ -43,6 +39,9 @@ pub(crate) trait DB { |
43 | 39 | /// Get pending status checks. |
44 | 40 | async fn get_pending_status_checks(&self) -> Result<Vec<CheckVoteInput>>; |
45 | 41 |
|
| 42 | + /// Get vote by id. |
| 43 | + async fn get_vote(&self, vote_id: Uuid) -> Result<Option<Vote>>; |
| 44 | + |
46 | 45 | /// Check if the issue/pr provided has a vote. |
47 | 46 | async fn has_vote(&self, repository_full_name: &str, issue_number: i64) -> Result<bool>; |
48 | 47 |
|
@@ -248,6 +247,16 @@ impl DB for PgDB { |
248 | 247 | Ok(inputs) |
249 | 248 | } |
250 | 249 |
|
| 250 | + /// [`DB::get_vote`] |
| 251 | + async fn get_vote(&self, vote_id: Uuid) -> Result<Option<Vote>> { |
| 252 | + let db = self.pool.get().await?; |
| 253 | + let vote = db |
| 254 | + .query_opt("select * from vote where vote_id = $1::uuid", &[&vote_id]) |
| 255 | + .await? |
| 256 | + .map(|row| Vote::from(&row)); |
| 257 | + Ok(vote) |
| 258 | + } |
| 259 | + |
251 | 260 | /// [`DB::has_vote`] |
252 | 261 | async fn has_vote(&self, repository_full_name: &str, issue_number: i64) -> Result<bool> { |
253 | 262 | let db = self.pool.get().await?; |
@@ -289,36 +298,22 @@ impl DB for PgDB { |
289 | 298 |
|
290 | 299 | /// [`DB::list_votes`] |
291 | 300 | async fn list_votes(&self, repository_full_name: &str) -> Result<Vec<Vote>> { |
292 | | - #[cfg_attr( |
293 | | - not(test), |
294 | | - cached( |
295 | | - time = 900, |
296 | | - key = "String", |
297 | | - convert = r#"{ repository_full_name.to_owned() }"#, |
298 | | - sync_writes = "by_key", |
299 | | - result = true |
| 301 | + let db = self.pool.get().await?; |
| 302 | + let votes = db |
| 303 | + .query( |
| 304 | + " |
| 305 | + select * |
| 306 | + from vote |
| 307 | + where repository_full_name = $1::text |
| 308 | + order by created_at desc |
| 309 | + ", |
| 310 | + &[&repository_full_name], |
300 | 311 | ) |
301 | | - )] |
302 | | - async fn inner(client: &Client, repository_full_name: &str) -> Result<Vec<Vote>> { |
303 | | - let votes = client |
304 | | - .query( |
305 | | - " |
306 | | - select * |
307 | | - from vote |
308 | | - where repository_full_name = $1::text |
309 | | - order by created_at desc |
310 | | - ", |
311 | | - &[&repository_full_name], |
312 | | - ) |
313 | | - .await? |
314 | | - .iter() |
315 | | - .map(Vote::from) |
316 | | - .collect(); |
317 | | - Ok(votes) |
318 | | - } |
319 | | - |
320 | | - let client = self.pool.get().await?; |
321 | | - inner(client.as_ref(), repository_full_name).await |
| 312 | + .await? |
| 313 | + .iter() |
| 314 | + .map(Vote::from) |
| 315 | + .collect(); |
| 316 | + Ok(votes) |
322 | 317 | } |
323 | 318 |
|
324 | 319 | /// [`DB::store_vote`] |
|
0 commit comments