Skip to content

Commit

Permalink
feat: added cults
Browse files Browse the repository at this point in the history
  • Loading branch information
jayy-lmao committed Jan 9, 2020
1 parent aacb989 commit 1f7e589
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
29 changes: 29 additions & 0 deletions api/src/cult.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
extern crate postgres;
use postgres::{Connection, TlsMode};
use std::env;

pub fn get_cult_all(vec: &mut Vec<crate::Cult>) {
let pg_connection_string = env::var("DATABASE_URI").expect("need a db uri");
println!("Connecting to {}", pg_connection_string);
let conn = Connection::connect(&pg_connection_string[..], TlsMode::None) .unwrap();
println!("Connection is fine");
for row in &conn.query("SELECT id, name FROM cults", &[]).unwrap() {
let cult = crate::Cult {
id: row.get(0),
name: row.get(1)
};
vec.push(cult);
}
}

pub fn get_cult_by_id(vec: &mut Vec<crate::Cult>, id: i32) {
let pg_connection_string = env::var("DATABASE_URI").expect("need a db uri");
let conn = Connection::connect(&pg_connection_string[..], TlsMode::None) .unwrap();
for row in &conn.query("SELECT id, name FROM cults WHERE id = $1", &[&id]).unwrap() {
let cult = crate::Cult {
id: row.get(0),
name: row.get(1)
};
vec.push(cult);
}
}
23 changes: 23 additions & 0 deletions api/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,26 @@ use actix_web::{web, App, HttpServer, Responder};
use serde::Serialize;

mod person;
mod cult;

#[derive(Serialize)]
pub struct Cult {
pub id: i32,
pub name: String
}

fn get_cult_list() -> impl Responder {
let mut vec:Vec<Cult> = Vec::new();
cult::get_cult_all(&mut vec);
web::Json(vec)
}

fn get_cult(info: web::Path<i32>) -> impl Responder {
let mut vec:Vec<Cult> = Vec::new();
cult::get_cult_by_id(&mut vec, info.into_inner());
web::Json(vec)
}

#[derive(Serialize)]
pub struct Person {
pub person_id: i32,
Expand Down Expand Up @@ -36,6 +55,10 @@ fn main() {
.route(web::get().to(get_person)))
// .route(web::delete().to(delete_person))
// .route(web::put().to(update_person)))
.service(web::resource("/cults/{cult_id}")
.route(web::get().to(get_cult)))
.service(web::resource("/cults")
.route(web::get().to(get_cult_list)))
.service(web::resource("/persons")
.route(web::get().to(get_person_list)))
// .route(web::post().to(create_person)))
Expand Down
10 changes: 10 additions & 0 deletions database/create_cults.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CREATE TABLE cults
(
id serial NOT NULL,
name VARCHAR(100)
);

-- ALTER TABLE CULTS owner TO jayylmao;

INSERT INTO cults (name)
VALUES ('Church of the Latter day dudes');
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: "2.3"
services:
api:
build:
context: ./readspacer-api
context: ./api
dockerfile: Dockerfile
ports:
- "8000:8000"
Expand Down

0 comments on commit 1f7e589

Please sign in to comment.