-
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 #20 from gamemstr/1-campaign
1 campaign
- Loading branch information
Showing
11 changed files
with
276 additions
and
13 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1 +1 @@ | ||
-- This file should undo anything in `up.sql` | ||
DROP TABLE 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 |
---|---|---|
@@ -1,10 +1,9 @@ | ||
-- Your SQL goes here | ||
CREATE TABLE sessions ( | ||
id VARCHAR PRIMARY KEY, | ||
name VARCHAR NOT NULL, | ||
description VARCHAR NOT NULL, | ||
id VARCHAR PRIMARY KEY, | ||
name VARCHAR NOT NULL, | ||
campaign_id VARCHAR NOT NULL, | ||
description VARCHAR NOT NULL, | ||
notes JSONB NOT NULL, | ||
plan 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DROP TABLE campaigns; |
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,7 @@ | ||
CREATE TABLE campaigns ( | ||
id VARCHAR PRIMARY KEY, | ||
name VARCHAR NOT NULL, | ||
description VARCHAR NOT NULL, | ||
world_id VARCHAR NOT NULL, | ||
players 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,41 @@ | ||
pub mod sessions; | ||
use diesel::prelude::*; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::schema::campaigns; | ||
|
||
pub mod sessions; | ||
|
||
#[derive(Debug, Serialize, Deserialize, Queryable, Insertable, AsChangeset)] | ||
#[diesel(table_name = campaigns)] | ||
pub struct Campaign { | ||
pub id: String, | ||
pub name: String, | ||
pub description: String, | ||
pub world_id: String, | ||
pub players: serde_json::Value, | ||
} | ||
|
||
impl crate::models::Model for Campaign { | ||
type Entity = gamemstr_common::world::campaign::Campaign; | ||
|
||
fn new(entity: Self::Entity) -> Self { | ||
Self { | ||
id: entity.id, | ||
name: entity.name, | ||
description: entity.description, | ||
world_id: entity.world_id, | ||
players: serde_json::to_value(entity.players).unwrap(), | ||
} | ||
} | ||
|
||
fn to_entity(&self) -> Self::Entity { | ||
Self::Entity { | ||
id: self.id.clone(), | ||
name: self.name.clone(), | ||
description: self.description.clone(), | ||
world_id: self.world_id.clone(), | ||
players: serde_json::from_value(self.players.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,102 @@ | ||
pub mod sessions; | ||
use crate::{ | ||
models::{self, Model}, | ||
schema, | ||
services::Result, | ||
}; | ||
use diesel::prelude::*; | ||
use gamemstr_common::world::campaign::Campaign; | ||
use rocket::{ | ||
delete, get, post, | ||
response::status::{Accepted, Created, NotFound}, | ||
serde::json::Json, | ||
Either, | ||
}; | ||
use rocket_dyn_templates::{context, Template}; | ||
|
||
pub mod sessions; | ||
|
||
#[get("/worlds/<id>/campaigns")] | ||
pub fn list_campaigns(id: String) -> Template { | ||
let connection = &mut super::super::establish_connection_pg(); | ||
let campaigns = schema::campaigns::dsl::campaigns | ||
.filter(schema::campaigns::dsl::world_id.eq(id)) | ||
.load::<models::worlds::campaigns::Campaign>(connection) | ||
.expect("Error loading campaigns") | ||
.iter() | ||
.map(|campaign| campaign.to_entity()) | ||
.collect::<Vec<_>>(); | ||
Template::render( | ||
"campaigns", | ||
context! { | ||
campaigns: &campaigns, | ||
count: campaigns.len() | ||
}, | ||
) | ||
} | ||
|
||
#[get("/worlds/<_world_id>/campaigns/<id>")] | ||
pub fn get_campaign(_world_id: String, id: String) -> Result<Json<Campaign>> { | ||
let connection = &mut super::super::establish_connection_pg(); | ||
let campaign = schema::campaigns::dsl::campaigns | ||
.find(id) | ||
.first::<models::worlds::campaigns::Campaign>(connection) | ||
.expect("Error loading campaign") | ||
.to_entity(); | ||
Ok(Json(campaign)) | ||
} | ||
|
||
#[delete("/worlds/<_world_id>/campaigns/<id>")] | ||
pub fn delete_campaign(_world_id: String, id: String) -> Result<Json<Campaign>> { | ||
let connection = &mut super::super::establish_connection_pg(); | ||
let campaign = schema::campaigns::dsl::campaigns | ||
.find(&id) | ||
.first::<models::worlds::campaigns::Campaign>(connection) | ||
.expect("Error loading campaign") | ||
.to_entity(); | ||
diesel::delete(schema::campaigns::dsl::campaigns.find(&id)) | ||
.execute(connection) | ||
.expect("Error deleting campaign"); | ||
Ok(Json(campaign)) | ||
} | ||
|
||
#[post( | ||
"/worlds/<_world_id>/campaigns/add", | ||
rank = 1, | ||
format = "json", | ||
data = "<campaign>" | ||
)] | ||
pub fn create_campaign( | ||
_world_id: String, | ||
campaign: Json<Campaign>, | ||
) -> Result<Created<Json<Campaign>>> { | ||
let connection = &mut super::super::establish_connection_pg(); | ||
let new_campaign = models::worlds::campaigns::Campaign::new(campaign.clone().0); | ||
diesel::insert_into(schema::campaigns::dsl::campaigns) | ||
.values(&new_campaign) | ||
.execute(connection) | ||
.expect("Error creating campaign"); | ||
Ok(Created::new("/").body(campaign)) | ||
} | ||
|
||
#[post( | ||
"/worlds/<_world_id>/campaigns/<id>", | ||
rank = 2, | ||
format = "json", | ||
data = "<campaign>" | ||
)] | ||
pub fn update_campaign( | ||
_world_id: String, | ||
id: String, | ||
campaign: Json<Campaign>, | ||
) -> Either<Result<Accepted<Json<Campaign>>>, Result<NotFound<String>>> { | ||
let connection = &mut super::super::establish_connection_pg(); | ||
let result = diesel::update(schema::campaigns::dsl::campaigns.find(&id)) | ||
.set(&models::worlds::campaigns::Campaign::new( | ||
campaign.clone().0, | ||
)) | ||
.execute(connection); | ||
match result { | ||
Ok(_) => Either::Left(Ok(Accepted(Some(campaign)))), | ||
Err(_) => Either::Right(Ok(NotFound(format!("Campaign 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,23 @@ | ||
<!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>Campaigns</title> | ||
</head> | ||
<body> | ||
<section id = "hello"> | ||
<h1>Campaigns</h1> | ||
{{#each campaigns as |campaign|}} | ||
<ul> | ||
<li>ID: {{campaign.id}}</li> | ||
<li>Name: {{campaign.name}}</li> | ||
<li>Description: {{campaign.description}}</li> | ||
<li>World ID: {{campaign.world_id}}</li> | ||
<li>Players: {{campaign.players}}</li> | ||
</ul> | ||
{{/each}} | ||
</section> | ||
</body> | ||
</html> |