-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from gamemstr/17-session-endpoints
17 session endpoints
- Loading branch information
Showing
14 changed files
with
284 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
-- This file should undo anything in `up.sql` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
-- Your SQL goes here | ||
CREATE TABLE sessions ( | ||
id VARCHAR PRIMARY KEY, | ||
name VARCHAR NOT NULL, | ||
description VARCHAR NOT NULL, | ||
campaign_id VARCHAR NOT NULL, | ||
notes JSONB NOT NULL, | ||
plan JSONB NOT NULL, | ||
recap JSONB NOT NULL | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod sessions; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use diesel::prelude::*; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::schema::sessions; | ||
|
||
#[derive(Debug, Serialize, Deserialize, Queryable, Insertable, AsChangeset)] | ||
#[diesel(table_name = sessions)] | ||
pub struct Session { | ||
pub id: String, | ||
pub name: String, | ||
pub campaign_id: String, | ||
pub description: String, | ||
pub notes: serde_json::Value, | ||
pub plan: serde_json::Value, | ||
pub recap: serde_json::Value, | ||
} | ||
|
||
impl super::super::super::Model for Session { | ||
type Entity = gamemstr_common::world::campaign::session::Session; | ||
|
||
fn new(entity: Self::Entity) -> Self { | ||
Self { | ||
id: entity.id, | ||
name: entity.name, | ||
campaign_id: entity.campaign_id, | ||
description: entity.description, | ||
notes: serde_json::to_value(entity.notes).unwrap(), | ||
plan: serde_json::to_value(entity.plan).unwrap(), | ||
recap: serde_json::to_value(entity.recap).unwrap(), | ||
} | ||
} | ||
|
||
fn to_entity(&self) -> Self::Entity { | ||
Self::Entity { | ||
id: self.id.clone(), | ||
name: self.name.clone(), | ||
campaign_id: self.campaign_id.clone(), | ||
description: self.description.clone(), | ||
notes: serde_json::from_value(self.notes.clone()).unwrap(), | ||
plan: serde_json::from_value(self.plan.clone()).unwrap(), | ||
recap: serde_json::from_value(self.recap.clone()).unwrap(), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod sessions; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
use super::super::super::Result; | ||
use crate::{ | ||
models::{self, Model}, | ||
schema, | ||
}; | ||
use diesel::prelude::*; | ||
use gamemstr_common::world::campaign::session::Session; | ||
use rocket::{ | ||
delete, get, post, | ||
response::status::{Accepted, Created, NotFound}, | ||
serde::json::Json, | ||
Either, | ||
}; | ||
use rocket_dyn_templates::{context, Template}; | ||
|
||
#[get("/worlds/<_world_id>/campaigns/<campaign_id>/sessions")] | ||
pub fn list_sessions(_world_id: String, campaign_id: String) -> Template { | ||
let connection = &mut crate::services::establish_connection_pg(); | ||
let sessions = schema::sessions::dsl::sessions | ||
.filter(schema::sessions::dsl::campaign_id.eq(campaign_id)) | ||
.load::<models::worlds::campaigns::sessions::Session>(connection) | ||
.expect("Error loading sessions") | ||
.iter() | ||
.map(|session| session.to_entity()) | ||
.collect::<Vec<_>>(); | ||
Template::render( | ||
"sessions", | ||
context! { | ||
sessions: &sessions, | ||
count: sessions.len() | ||
}, | ||
) | ||
} | ||
|
||
#[get("/worlds/<_world_id>/campaigns/<_campaign_id>/sessions/<id>")] | ||
pub fn get_session(_world_id: String, _campaign_id: String, id: String) -> Result<Json<Session>> { | ||
let connection = &mut crate::services::establish_connection_pg(); | ||
let session = schema::sessions::dsl::sessions | ||
.find(id) | ||
.first::<models::worlds::campaigns::sessions::Session>(connection) | ||
.expect("Error loading session") | ||
.to_entity(); | ||
Ok(Json(session)) | ||
} | ||
|
||
#[delete("/worlds/<_world_id>/campaigns/<_campaign_id>/sessions/<id>")] | ||
pub fn delete_session(_world_id: String, _campaign_id: String, id: String) -> Result<Json<Session>> { | ||
let connection = &mut crate::services::establish_connection_pg(); | ||
let session = schema::sessions::dsl::sessions | ||
.find(&id) | ||
.first::<models::worlds::campaigns::sessions::Session>(connection) | ||
.expect("Error loading session") | ||
.to_entity(); | ||
diesel::delete(schema::sessions::dsl::sessions.find(&id)) | ||
.execute(connection) | ||
.expect("Error deleting session"); | ||
Ok(Json(session)) | ||
} | ||
|
||
#[post( | ||
"/worlds/<_world_id>/campaigns/<_campaign_id>/sessions/add", | ||
rank = 1, | ||
format = "json", | ||
data = "<session>" | ||
)] | ||
pub fn create_session( | ||
_world_id: String, | ||
_campaign_id: String, | ||
session: Json<Session>, | ||
) -> Result<Created<Json<Session>>> { | ||
let connection = &mut crate::services::establish_connection_pg(); | ||
let new_session = models::worlds::campaigns::sessions::Session::new(session.clone().0); | ||
diesel::insert_into(schema::sessions::dsl::sessions) | ||
.values(&new_session) | ||
.execute(connection) | ||
.expect("Error creating session"); | ||
Ok(Created::new("/").body(session)) | ||
} | ||
|
||
#[post( | ||
"/worlds/<_world_id>/campaigns/<_campaign_id>/sessions/<id>", | ||
rank = 2, | ||
format = "json", | ||
data = "<session>" | ||
)] | ||
pub fn update_session( | ||
_world_id: String, | ||
_campaign_id: String, | ||
id: String, | ||
session: Json<Session>, | ||
) -> Either<Result<Accepted<Json<Session>>>, Result<NotFound<String>>> { | ||
let connection = &mut crate::services::establish_connection_pg(); | ||
let new_session = models::worlds::campaigns::sessions::Session::new(session.clone().0); | ||
let result = diesel::update(schema::sessions::dsl::sessions.find(&id)) | ||
.set(&new_session) | ||
.execute(connection); | ||
match result { | ||
Ok(_) => Either::Left(Ok(Accepted(Some(session)))), | ||
Err(_) => Either::Right(Ok(NotFound(format!("Session not found with id {}", id)))), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
<title>Sessions</title> | ||
</head> | ||
<body> | ||
<section id = "hello"> | ||
<h1>Sessions</h1> | ||
{{#each sessions as |session|}} | ||
<ul> | ||
<li>ID: {{session.id}}</li> | ||
<li>Name: {{session.name}}</li> | ||
<li>Description: {{session.description}}</li> | ||
<li>Campaign ID: {{session.campaign_id}}</li> | ||
<li>Notes: {{session.notes}}</li> | ||
<li>Plan: {{session.plan}}</li> | ||
<li>Recap: {{session.recap}}</li> | ||
</ul> | ||
{{/each}} | ||
</section> | ||
</body> | ||
</html> |