| 1 | |
| 2 | |
| 3 | |
| 4 | use crate::database::{Database, foreign_key_violation, tx_with_retry}; |
| 5 | use crate::db::field_update::FieldUpdate; |
| 6 | use crate::db::page::PagedList; |
| 7 | use crate::domain::course::CourseId; |
| 8 | use crate::domain::course_note::{CourseNote, CourseNoteContent, CourseNoteId, CourseNoteTitle}; |
| 9 | use crate::domain::course_note_file::{ |
| 10 | CourseNoteFile, CourseNoteFileId, FileContentType, FileName, |
| 11 | }; |
| 12 | use crate::domain::user::UserId; |
| 13 | use crate::error::AppError; |
| 14 | |
| 15 | pub async fn create( |
| 16 | db: &Database, |
| 17 | course: &CourseId, |
| 18 | author: &UserId, |
| 19 | title: CourseNoteTitle, |
| 20 | content: CourseNoteContent, |
| 21 | ) -> Result<CourseNote, AppError> { |
| 22 | let note = CourseNote { |
| 23 | id: CourseNoteId::generate(), |
| 24 | course: course.clone(), |
| 25 | author: author.clone(), |
| 26 | title, |
| 27 | content, |
| 28 | }; |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | let created = sqlx::query_as!( |
| 36 | CourseNote, |
| 37 | r#"INSERT INTO course_note (id, course, author, title, content) |
| 38 | VALUES ($1, $2, $3, $4, $5) |
| 39 | RETURNING id AS "id: CourseNoteId", course AS "course: CourseId", |
| 40 | author AS "author: UserId", title AS "title: CourseNoteTitle", |
| 41 | content AS "content: CourseNoteContent""#, |
| 42 | note.id.uuid(), |
| 43 | note.course.uuid(), |
| 44 | note.author.uuid(), |
| 45 | note.title.as_str(), |
| 46 | note.content.as_str() |
| 47 | ) |
| 48 | .fetch_one(db) |
| 49 | .await; |
| 50 | match created { |
| 51 | Ok(row) => Ok(row), |
| 52 | Err(err) if foreign_key_violation(&err) => Err(AppError::NotFound), |
| 53 | Err(err) => Err(err.into()), |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | pub async fn read(db: &Database, id: &CourseNoteId) -> Result<Option<CourseNote>, AppError> { |
| 58 | let note = sqlx::query_as!( |
| 59 | CourseNote, |
| 60 | r#"SELECT id AS "id: CourseNoteId", course AS "course: CourseId", |
| 61 | author AS "author: UserId", title AS "title: CourseNoteTitle", |
| 62 | content AS "content: CourseNoteContent" FROM course_note WHERE id = $1"#, |
| 63 | id.uuid() |
| 64 | ) |
| 65 | .fetch_optional(db) |
| 66 | .await?; |
| 67 | Ok(note) |
| 68 | } |
| 69 | |
| 70 | pub async fn list_for_course( |
| 71 | db: &Database, |
| 72 | course: &CourseId, |
| 73 | limit: Option<i64>, |
| 74 | offset: i64, |
| 75 | ) -> Result<(Vec<CourseNote>, i64), AppError> { |
| 76 | PagedList::new("course_note WHERE course = $1", "ORDER BY id DESC") |
| 77 | .bind(course.uuid()) |
| 78 | .run::<CourseNote>(limit, offset, db) |
| 79 | .await |
| 80 | } |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | pub async fn update( |
| 89 | db: &Database, |
| 90 | note: CourseNote, |
| 91 | title: Option<CourseNoteTitle>, |
| 92 | content: Option<CourseNoteContent>, |
| 93 | ) -> Result<CourseNote, AppError> { |
| 94 | FieldUpdate::new("course_note", note.id.uuid()) |
| 95 | .set("title", title.map(|title| title.as_str().to_string())) |
| 96 | .set( |
| 97 | "content", |
| 98 | content.map(|content| content.as_str().to_string()), |
| 99 | ) |
| 100 | .run::<CourseNote>(db) |
| 101 | .await |
| 102 | } |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | pub async fn delete( |
| 117 | db: &Database, |
| 118 | note: CourseNote, |
| 119 | ) -> Result<(CourseNote, Vec<CourseNoteFile>), AppError> { |
| 120 | tx_with_retry(db, true, async move |conn| { |
| 121 | sqlx::query!("DELETE FROM rag_output WHERE course_note = $1", note.id.uuid()) |
| 122 | .execute(&mut *conn) |
| 123 | .await?; |
| 124 | let files = sqlx::query_as!( |
| 125 | CourseNoteFile, |
| 126 | r#"DELETE FROM course_note_file WHERE course_note = $1 |
| 127 | RETURNING id AS "id: CourseNoteFileId", |
| 128 | course_note AS "course_note: CourseNoteId", name AS "name: FileName", |
| 129 | content_type AS "content_type: FileContentType", size"#, |
| 130 | note.id.uuid() |
| 131 | ) |
| 132 | .fetch_all(&mut *conn) |
| 133 | .await?; |
| 134 | let gone = sqlx::query_as!( |
| 135 | CourseNote, |
| 136 | r#"DELETE FROM course_note WHERE id = $1 RETURNING id AS "id: CourseNoteId", course AS "course: CourseId", |
| 137 | author AS "author: UserId", title AS "title: CourseNoteTitle", |
| 138 | content AS "content: CourseNoteContent""#, |
| 139 | note.id.uuid() |
| 140 | ) |
| 141 | .fetch_optional(&mut *conn) |
| 142 | .await?; |
| 143 | let note = gone.ok_or(AppError::NotFound)?; |
| 144 | Ok((note, files)) |
| 145 | }) |
| 146 | .await |
| 147 | } |
| 148 | |
| 149 | #[cfg(test)] |
| 150 | mod tests { |
| 151 | use super::*; |
| 152 | use crate::domain::course::{CourseDescription, CourseKind, CourseTitle}; |
| 153 | use crate::domain::course_note_file::{FileContentType, FileName}; |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | #[tokio::test] |
| 159 | async fn delete_returns_the_attachment_rows_it_removed() { |
| 160 | let db = crate::database::init_mem().await.unwrap(); |
| 161 | let creator = crate::domain::user::UserId::generate(); |
| 162 | let course = crate::db::course::create( |
| 163 | &db, |
| 164 | &creator, |
| 165 | CourseTitle::try_new("Math").unwrap(), |
| 166 | CourseDescription::try_new("").unwrap(), |
| 167 | CourseKind::try_new("course").unwrap(), |
| 168 | None, |
| 169 | None, |
| 170 | ) |
| 171 | .await |
| 172 | .unwrap(); |
| 173 | let note = create( |
| 174 | &db, |
| 175 | course.get_id(), |
| 176 | &creator, |
| 177 | CourseNoteTitle::try_new("a").unwrap(), |
| 178 | CourseNoteContent::try_new("body").unwrap(), |
| 179 | ) |
| 180 | .await |
| 181 | .unwrap(); |
| 182 | |
| 183 | let early = crate::db::course_note_file::insert( |
| 184 | &db, |
| 185 | CourseNoteFile::new( |
| 186 | note.get_id(), |
| 187 | FileName::try_new("early.pdf").unwrap(), |
| 188 | FileContentType::try_new("application/pdf").unwrap(), |
| 189 | 3, |
| 190 | ), |
| 191 | ) |
| 192 | .await |
| 193 | .unwrap(); |
| 194 | let (snapshot, _) = crate::db::course_note_file::list_for(&db, note.get_id(), None, 0) |
| 195 | .await |
| 196 | .unwrap(); |
| 197 | assert_eq!(snapshot.len(), 1); |
| 198 | |
| 199 | let late = crate::db::course_note_file::insert( |
| 200 | &db, |
| 201 | CourseNoteFile::new( |
| 202 | note.get_id(), |
| 203 | FileName::try_new("late.pdf").unwrap(), |
| 204 | FileContentType::try_new("application/pdf").unwrap(), |
| 205 | 3, |
| 206 | ), |
| 207 | ) |
| 208 | .await |
| 209 | .unwrap(); |
| 210 | |
| 211 | let (gone, files) = delete(&db, note).await.unwrap(); |
| 212 | assert_eq!(gone.get_title().as_str(), "a"); |
| 213 | let mut keys: Vec<_> = files |
| 214 | .iter() |
| 215 | .map(|file| file.get_id().key().to_string()) |
| 216 | .collect(); |
| 217 | keys.sort(); |
| 218 | let mut want = vec![ |
| 219 | early.get_id().key().to_string(), |
| 220 | late.get_id().key().to_string(), |
| 221 | ]; |
| 222 | want.sort(); |
| 223 | assert_eq!(keys, want); |
| 224 | assert!( |
| 225 | crate::db::course_note_file::list_for(&db, gone.get_id(), None, 0) |
| 226 | .await |
| 227 | .unwrap() |
| 228 | .0 |
| 229 | .is_empty() |
| 230 | ); |
| 231 | } |
| 232 | } |