aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHarald Eilertsen <haraldei@anduin.net>2019-03-21 10:39:16 +0100
committerHarald Eilertsen <haraldei@anduin.net>2019-03-21 10:39:16 +0100
commitb4a8149dc6ed595c0bd95f5824f7048799989bdb (patch)
treeffc490cb7e13a3abe55e176b0fd308c55da051e6
parent9c3a341e79581541fc39ceddeec28daae7dc28cf (diff)
downloadramaskrik-social-b4a8149dc6ed595c0bd95f5824f7048799989bdb.tar.gz
ramaskrik-social-b4a8149dc6ed595c0bd95f5824f7048799989bdb.tar.bz2
ramaskrik-social-b4a8149dc6ed595c0bd95f5824f7048799989bdb.zip
Actually fetch available rooms from the database.
-rw-r--r--Cargo.lock15
-rw-r--r--Cargo.toml6
-rw-r--r--src/controllers/room.rs13
-rw-r--r--src/db.rs1
-rw-r--r--src/lib.rs6
-rw-r--r--src/models.rs19
-rw-r--r--src/models/room.rs35
-rw-r--r--tests/room_tests.rs34
8 files changed, 119 insertions, 10 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 805a922..08f1c11 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -497,9 +497,13 @@ dependencies = [
name = "ramaskrik"
version = "0.1.0"
dependencies = [
+ "diesel 1.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
"dotenv 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rocket 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rocket_contrib 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde_derive 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@@ -650,6 +654,16 @@ version = "1.0.89"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
+name = "serde_derive"
+version = "1.0.89"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)",
+ "quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)",
+ "syn 0.15.27 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
name = "serde_json"
version = "1.0.39"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -935,6 +949,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum same-file 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8f20c4be53a8a1ff4c1f1b2bd14570d2f634628709752f0702ecdd2b3f9a5267"
"checksum scheduled-thread-pool 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1a2ff3fc5223829be817806c6441279c676e454cc7da608faf03b0ccc09d3889"
"checksum serde 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)" = "92514fb95f900c9b5126e32d020f5c6d40564c27a5ea6d1d7d9f157a96623560"
+"checksum serde_derive 1.0.89 (registry+https://github.com/rust-lang/crates.io-index)" = "bb6eabf4b5914e88e24eea240bb7c9f9a2cbc1bbbe8d961d381975ec3c6b806c"
"checksum serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)" = "5a23aa71d4a4d43fdbfaac00eff68ba8a06a51759a89ac3304323e800c4dd40d"
"checksum slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8"
"checksum smallvec 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)" = "c4488ae950c49d403731982257768f48fada354a5203fe81f9bb6f43ca9002be"
diff --git a/Cargo.toml b/Cargo.toml
index cb1b0de..53f348c 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -5,10 +5,16 @@ authors = ["haraldei"]
edition = "2018"
[dependencies]
+diesel = { version = "1.4.1", features = ["postgres"] }
dotenv = "0.13.0"
rocket = "0.4.0"
+serde = "1.0.89"
+serde_derive = "1.0.89"
[dependencies.rocket_contrib]
version = "0.4.0"
default-features = false
features = ["json", "serve", "diesel_postgres_pool"]
+
+[dev-dependencies]
+serde_json = "~1.0.39"
diff --git a/src/controllers/room.rs b/src/controllers/room.rs
index 27f6a41..df77414 100644
--- a/src/controllers/room.rs
+++ b/src/controllers/room.rs
@@ -16,14 +16,17 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
-use crate::db;
+use crate::{
+ db,
+ models::room,
+};
+
use rocket::get;
use rocket_contrib::{
- json,
- json::JsonValue,
+ json::Json,
};
#[get("/")]
-pub fn get_rooms(_db: db::Connection) -> JsonValue {
- json!([])
+pub fn get_rooms(db: db::Connection) -> Json<Vec<room::Room>> {
+ Json(room::get_all(&db).unwrap())
}
diff --git a/src/db.rs b/src/db.rs
index f427e14..b299911 100644
--- a/src/db.rs
+++ b/src/db.rs
@@ -23,4 +23,3 @@ use rocket_contrib::{
#[database("main")]
pub struct Connection(diesel::PgConnection);
-
diff --git a/src/lib.rs b/src/lib.rs
index 989a6ba..4e94f5a 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -17,8 +17,12 @@
*/
#![feature(proc_macro_hygiene, decl_macro)]
+#[macro_use] extern crate diesel;
+
mod controllers;
-mod db;
+pub mod db;
+pub mod models;
+pub mod schema;
use crate::controllers::room;
use rocket::routes;
diff --git a/src/models.rs b/src/models.rs
new file mode 100644
index 0000000..d8f9d5d
--- /dev/null
+++ b/src/models.rs
@@ -0,0 +1,19 @@
+/*
+ Social program for Ramaskrik.
+ Copyright (C) 2019 Harald Eilertsen <haraldei@anduin.net>
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Affero General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Affero General Public License for more details.
+
+ You should have received a copy of the GNU Affero General Public License
+ along with this program. If not, see <https://www.gnu.org/licenses/>.
+*/
+
+pub mod room;
diff --git a/src/models/room.rs b/src/models/room.rs
new file mode 100644
index 0000000..2ec3c7f
--- /dev/null
+++ b/src/models/room.rs
@@ -0,0 +1,35 @@
+/*
+ Social program for Ramaskrik.
+ Copyright (C) 2019 Harald Eilertsen <haraldei@anduin.net>
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Affero General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Affero General Public License for more details.
+
+ You should have received a copy of the GNU Affero General Public License
+ along with this program. If not, see <https://www.gnu.org/licenses/>.
+*/
+
+use crate::{
+ db::Connection,
+ schema::rooms,
+};
+use diesel::prelude::*;
+use serde_derive::{Deserialize, Serialize};
+
+#[derive(Deserialize, Identifiable, PartialEq, Serialize, Queryable)]
+pub struct Room {
+ pub id: i32,
+ pub name: String,
+}
+
+pub fn get_all(db: &Connection) -> QueryResult<Vec<Room>> {
+ use crate::schema::rooms::dsl::*;
+ rooms.load::<Room>(&**db)
+}
diff --git a/tests/room_tests.rs b/tests/room_tests.rs
index 292954a..1bd5649 100644
--- a/tests/room_tests.rs
+++ b/tests/room_tests.rs
@@ -17,16 +17,44 @@
*/
use ramaskrik;
+use ramaskrik::models::room::Room;
use rocket::http::ContentType;
+use rocket_contrib::databases::diesel::prelude::*;
+use serde_json;
#[test]
fn getting_rooms_from_api() {
- let db_url = dotenv::var("DATABASE_URL")
- .map_err(|_| "No database! Set DATABASE_URL env var and try again.").unwrap();
+ let db_url = dotenv::var("TEST_DATABASE_URL")
+ .map_err(|_| "No database! Set TEST_DATABASE_URL env var and try again.").unwrap();
let server = ramaskrik::build_rocket(&db_url).unwrap();
+ let db = ramaskrik::db::Connection::get_one(&server).expect("Could not get db connection");
+
+ diesel::dsl::sql_query("TRUNCATE TABLE rooms").execute(&*db).unwrap();
+
+ use ramaskrik::schema::rooms::dsl::*;
+
+ let new_rooms = vec![
+ name.eq("Main room"),
+ name.eq("Small room"),
+ name.eq("Neverland")];
+
+ diesel::insert_into(rooms)
+ .values(&new_rooms)
+ .execute(&*db)
+ .unwrap();
+
let client = rocket::local::Client::new(server).unwrap();
let mut response = client.get("/rooms").dispatch();
assert_eq!(response.content_type(), Some(ContentType::JSON));
- assert_eq!(response.body_string(), Some("[]".into()));
+
+ let fetched_rooms: Vec<Room> =
+ serde_json::from_str(&response.body_string().unwrap()).unwrap();
+
+ assert_eq!(fetched_rooms.len(), 3);
+
+ let room_names: Vec<&str> = fetched_rooms.iter().map(|room| room.name.as_str()).collect();
+ assert!(room_names.contains(&"Main room"));
+ assert!(room_names.contains(&"Small room"));
+ assert!(room_names.contains(&"Neverland"));
}