Skip to content

Commit

Permalink
✨ can create cults
Browse files Browse the repository at this point in the history
  • Loading branch information
jayy-lmao committed Jan 30, 2020
1 parent 254570d commit ce7ecf1
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 23 deletions.
18 changes: 18 additions & 0 deletions api/src/data/cult/create_cult.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

use crate::type_defs::{Cult,NewCult};
use crate::db::get_db_conn;

pub fn create_cult(data: NewCult) -> Cult {
let conn = get_db_conn();
let res = &conn
.query(
"INSERT INTO cults (name) VALUES ($1) RETURNING id, name;",
&[&data.name],
)
.unwrap();
let row = res.iter().next().unwrap();
Cult {
id: row.get(0),
name: row.get(1),
}
}
6 changes: 5 additions & 1 deletion api/src/data/cult/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::type_defs::Cult;
use crate::type_defs::{Cult, NewCult};

pub mod get_cult_by_id;
pub mod create_cult;
use get_cult_by_id::{get_loader, CultLoader};

#[derive(Clone)]
Expand All @@ -17,4 +18,7 @@ impl CultData {
pub async fn cult_by_id(&self, id: i32) -> Cult {
self.cult_by_id.load(id).await.unwrap()
}
pub async fn create_cult(&self, data: NewCult) -> Cult {
create_cult::create_cult(data)
}
}
19 changes: 19 additions & 0 deletions api/src/data/person/create_person.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

use crate::type_defs::{Person,NewPerson};
use crate::db::get_db_conn;

pub fn create_person(data: NewPerson) -> Person {
let conn = get_db_conn();
let res = &conn
.query(
"INSERT INTO persons (name, cult) VALUES ($1, $2) RETURNING id, name, cult;",
&[&data.name, &data.cult],
)
.unwrap();
let row = res.iter().next().unwrap();
Person {
id: row.get(0),
name: row.get(1),
cult: row.get(2)
}
}
16 changes: 0 additions & 16 deletions api/src/data/person/get_person_by_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,6 @@ pub fn get_person_by_ids(hashmap: &mut HashMap<i32, Person>, ids: Vec<i32>) {
}
}

// pub fn create_person(data: NewPerson) -> Person {
// let conn = get_db_conn();
// let res = &conn
// .query(
// "INSERT INTO persons (name, cult) VALUES ($1, $2) RETURNING id, name, cult;",
// &[&data.name, &data.cult],
// )
// .unwrap();
// let row = res.iter().next().unwrap();
// Person {
// id: row.get(0),
// name: row.get(1),
// cult: row.get(2)
// }
// }


pub struct PersonBatcher;

Expand Down
6 changes: 5 additions & 1 deletion api/src/data/person/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::type_defs::Person;
use crate::type_defs::{Person, NewPerson};

pub mod get_person_by_id;
pub mod get_persons_by_cult_id;
pub mod create_person;

#[derive(Clone)]
pub struct PersonData {
Expand All @@ -22,4 +23,7 @@ impl PersonData {
pub async fn persons_by_cult_id(&self, id: i32) -> Vec<Person> {
self.persons_by_cult_id.load(id).await.unwrap()
}
pub async fn create_person(&self, data: NewPerson) -> Person {
create_person::create_person(data)
}
}
12 changes: 8 additions & 4 deletions api/src/graphql/mutation.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
use super::schema::Context;
use crate::type_defs::{Cult, NewCult, NewPerson, Person};
use juniper::FieldResult;

pub struct Mutation;

#[juniper::graphql_object(Context = Context)]
impl Mutation {
// not really needed, but graphiql bug if this is empty…
pub fn nothing(_name: String) -> i32 {
0
pub async fn create_person(ctx: &Context, data: NewPerson) -> FieldResult<Person> {
Ok(ctx.person_data.create_person(data).await)
}
}
pub async fn create_cult(ctx: &Context, data: NewCult) -> FieldResult<Cult> {
Ok(ctx.cult_data.create_cult(data).await)
}
}
6 changes: 6 additions & 0 deletions api/src/type_defs/cults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ pub struct Cult {
pub name: String,
}

#[derive(juniper::GraphQLInputObject, Debug, Clone)]
#[graphql(name="NewPerson", description="A creating a person!")]
pub struct NewCult {
pub name: String,
}

#[juniper::graphql_object(Context = Context)]
impl Cult {
pub fn id(&self) -> i32 {
Expand Down
5 changes: 4 additions & 1 deletion api/src/type_defs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ mod cults;
mod person;

pub use cults::Cult;
pub use person::Person;
pub use cults::NewCult;

pub use person::Person;
pub use person::NewPerson;
7 changes: 7 additions & 0 deletions api/src/type_defs/person.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ pub struct Person {
pub cult: Option<i32>,
}

#[derive(juniper::GraphQLInputObject, Debug, Clone)]
#[graphql(name="NewPerson", description="A creating a person!")]
pub struct NewPerson {
pub name: String,
pub cult: Option<i32>,
}

#[juniper::graphql_object(Context = Context)]
impl Person {
pub fn id(&self) -> i32 {
Expand Down

0 comments on commit ce7ecf1

Please sign in to comment.