Final V2 Changes (#212)

* Redo dependencies, add rejection reasons, make notifications more readable

* Fix errors, add dependency route, finish PR

* Fix clippy errors
This commit is contained in:
Geometrically
2021-06-16 09:05:35 -07:00
committed by GitHub
parent 2a4caa856e
commit d2c2503cfa
39 changed files with 2365 additions and 1303 deletions

View File

@@ -2,6 +2,7 @@ use super::ids::*;
use crate::database::models::DatabaseError;
pub struct NotificationBuilder {
pub notification_type: Option<String>,
pub title: String,
pub text: String,
pub link: String,
@@ -16,6 +17,7 @@ pub struct NotificationActionBuilder {
pub struct Notification {
pub id: NotificationId,
pub user_id: UserId,
pub notification_type: Option<String>,
pub title: String,
pub text: String,
pub link: String,
@@ -64,6 +66,7 @@ impl NotificationBuilder {
Notification {
id,
user_id: user,
notification_type: self.notification_type.clone(),
title: self.title.clone(),
text: self.text.clone(),
link: self.link.clone(),
@@ -87,17 +90,18 @@ impl Notification {
sqlx::query!(
"
INSERT INTO notifications (
id, user_id, title, text, link
id, user_id, title, text, link, type
)
VALUES (
$1, $2, $3, $4, $5
$1, $2, $3, $4, $5, $6
)
",
self.id as NotificationId,
self.user_id as UserId,
&self.title,
&self.text,
&self.link
&self.link,
self.notification_type
)
.execute(&mut *transaction)
.await?;
@@ -118,7 +122,7 @@ impl Notification {
{
let result = sqlx::query!(
"
SELECT n.user_id, n.title, n.text, n.link, n.created, n.read,
SELECT n.user_id, n.title, n.text, n.link, n.created, n.read, n.type notification_type,
STRING_AGG(DISTINCT na.id || ', ' || na.title || ', ' || na.action_route || ', ' || na.action_route_method, ' ,') actions
FROM notifications n
LEFT OUTER JOIN notifications_actions na on n.id = na.notification_id
@@ -150,6 +154,7 @@ impl Notification {
Ok(Some(Notification {
id,
user_id: UserId(row.user_id),
notification_type: row.notification_type,
title: row.title,
text: row.text,
link: row.link,
@@ -174,7 +179,7 @@ impl Notification {
let notification_ids_parsed: Vec<i64> = notification_ids.into_iter().map(|x| x.0).collect();
sqlx::query!(
"
SELECT n.id, n.user_id, n.title, n.text, n.link, n.created, n.read,
SELECT n.id, n.user_id, n.title, n.text, n.link, n.created, n.read, n.type notification_type,
STRING_AGG(DISTINCT na.id || ', ' || na.title || ', ' || na.action_route || ', ' || na.action_route_method, ' ,') actions
FROM notifications n
LEFT OUTER JOIN notifications_actions na on n.id = na.notification_id
@@ -207,6 +212,7 @@ impl Notification {
Notification {
id,
user_id: UserId(row.user_id),
notification_type: row.notification_type,
title: row.title,
text: row.text,
link: row.link,
@@ -231,7 +237,7 @@ impl Notification {
sqlx::query!(
"
SELECT n.id, n.user_id, n.title, n.text, n.link, n.created, n.read,
SELECT n.id, n.user_id, n.title, n.text, n.link, n.created, n.read, n.type notification_type,
STRING_AGG(DISTINCT na.id || ', ' || na.title || ', ' || na.action_route || ', ' || na.action_route_method, ' ,') actions
FROM notifications n
LEFT OUTER JOIN notifications_actions na on n.id = na.notification_id
@@ -263,6 +269,7 @@ impl Notification {
Notification {
id,
user_id: UserId(row.user_id),
notification_type: row.notification_type,
title: row.title,
text: row.text,
link: row.link,
@@ -276,13 +283,10 @@ impl Notification {
.await
}
pub async fn remove<'a, 'b, E>(
pub async fn remove(
id: NotificationId,
exec: E,
) -> Result<Option<()>, sqlx::error::Error>
where
E: sqlx::Executor<'a, Database = sqlx::Postgres> + Copy,
{
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
) -> Result<Option<()>, sqlx::error::Error> {
sqlx::query!(
"
DELETE FROM notifications_actions
@@ -290,7 +294,7 @@ impl Notification {
",
id as NotificationId,
)
.execute(exec)
.execute(&mut *transaction)
.await?;
sqlx::query!(
@@ -300,7 +304,36 @@ impl Notification {
",
id as NotificationId,
)
.execute(exec)
.execute(&mut *transaction)
.await?;
Ok(Some(()))
}
pub async fn remove_many(
notification_ids: Vec<NotificationId>,
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
) -> Result<Option<()>, sqlx::error::Error> {
let notification_ids_parsed: Vec<i64> = notification_ids.into_iter().map(|x| x.0).collect();
sqlx::query!(
"
DELETE FROM notifications_actions
WHERE notification_id IN (SELECT * FROM UNNEST($1::bigint[]))
",
&notification_ids_parsed
)
.execute(&mut *transaction)
.await?;
sqlx::query!(
"
DELETE FROM notifications
WHERE id IN (SELECT * FROM UNNEST($1::bigint[]))
",
&notification_ids_parsed
)
.execute(&mut *transaction)
.await?;
Ok(Some(()))