Skip to content

Commit

Permalink
Merge pull request #47 from ingalls/custom_style
Browse files Browse the repository at this point in the history
Custom Styles UI
  • Loading branch information
ingalls committed May 8, 2018
2 parents 82866d9 + 30d8188 commit fd359d8
Show file tree
Hide file tree
Showing 8 changed files with 419 additions and 117 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,18 @@ curl -X GET 'http://localhost:8000/api/user/create?ingalls&password=yeaheh&email

---

#### `GET` `/api/user/session`

Return a new session cookie and the `uid` given an Basic Authenticated request.

*Example*

```bash
curl -X GET 'http://username:password@localhost:8000/api/user/session
```
---
<h3 align='center'>Downloading via Boundaries</h3>
#### `GET` `/api/data/bounds/`
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ fn user_create_session(conn: DbConn, auth: user::Auth, mut cookies: Cookies) ->
match user::create_token(&conn.0, &uid) {
Ok(token) => {
cookies.add_private(Cookie::new("session", token));
Ok(Json(json!(true)))
Ok(Json(json!(uid)))
},
Err(err) => Err(status::Custom(HTTPStatus::BadRequest, err.to_string()))
}
Expand All @@ -252,7 +252,7 @@ fn style_create(conn: DbConn, auth: user::Auth, style: String) -> Result<Json, s
};

match style::create(&conn.0, &uid, &style) {
Ok(created) => Ok(Json(json!(created))),
Ok(style_id) => Ok(Json(json!(style_id))),
Err(err) => Err(status::Custom(HTTPStatus::BadRequest, err.to_string()))
}
}
Expand Down
77 changes: 44 additions & 33 deletions src/style/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,20 @@ impl StyleError {
/// Creates a new GL JS Style under a given user account
///
/// By default styles are private and can only be accessed by a single user
pub fn create(conn: &r2d2::PooledConnection<r2d2_postgres::PostgresConnectionManager>, uid: &i64, style: &String) -> Result<bool, StyleError> {
match conn.execute("
INSERT into styles (name, style, uid, public)
pub fn create(conn: &r2d2::PooledConnection<r2d2_postgres::PostgresConnectionManager>, uid: &i64, style: &String) -> Result<i64, StyleError> {
match conn.query("
INSERT INTO styles (name, style, uid, public)
VALUES (
COALESCE($1::TEXT::JSON->>'name', 'New Style')::TEXT,
COALESCE($1::TEXT::JSON->'style', '{}'::JSON),
$2,
false
);
)
RETURNING id;
", &[&style, &uid]) {
Ok(updated) => {
if updated == 0 {
Err(StyleError::NotFound)
} else {
Ok(true)
}
Ok(rows) => {
let id = rows.get(0).get(0);
Ok(id)
},
Err(err) => {
match err.as_db() {
Expand All @@ -56,16 +54,20 @@ pub fn get(conn: &r2d2::PooledConnection<r2d2_postgres::PostgresConnectionManage
FROM (
SELECT
styles.id AS id,
styles.uid AS uid,
styles.name AS name,
styles.style AS style
styles.style AS style,
users.username AS username
FROM
styles
styles,
users
WHERE
id = $1
styles.id = $1
AND (
public IS true
OR uid = $2
styles.public IS true
OR styles.uid = $2
)
AND users.id = styles.uid
) t
", &[&style_id, &uid]) {
Ok(rows) => {
Expand Down Expand Up @@ -169,15 +171,18 @@ pub fn list_user(conn: &r2d2::PooledConnection<r2d2_postgres::PostgresConnection
COALESCE(JSON_Agg(row_to_json(t)), '[]'::JSON)
FROM (
SELECT
id,
name,
public,
uid
styles.id,
styles.name,
styles.public,
styles.uid,
users.username
FROM
styles
styles,
users
WHERE
uid = $1
ORDER BY id
AND users.id = uid
ORDER BY styles.id
) t;
", &[&uid]) {
Ok(rows) => {
Expand All @@ -204,16 +209,19 @@ pub fn list_user_public(conn: &r2d2::PooledConnection<r2d2_postgres::PostgresCon
COALESCE(JSON_Agg(row_to_json(t)), '[]'::JSON)
FROM (
SELECT
id,
name,
public,
uid
styles.id,
styles.name,
styles.public,
styles.uid,
users.username
FROM
styles
styles,
users
WHERE
uid = $1
AND public IS TRUE
ORDER BY id
AND uid = users.id
ORDER BY styles.id
) t;
", &[&uid]) {
Ok(rows) => {
Expand All @@ -239,15 +247,18 @@ pub fn list_public(conn: &r2d2::PooledConnection<r2d2_postgres::PostgresConnecti
COALESCE(JSON_Agg(row_to_json(t)), '[]'::JSON)
FROM (
SELECT
id,
name,
public,
uid
styles.id,
styles.name,
styles.public,
styles.uid,
users.username
FROM
styles
styles,
users
WHERE
public IS true
ORDER BY id
AND uid = users.id
ORDER BY styles.id
) t;
", &[]) {
Ok(rows) => {
Expand Down
2 changes: 1 addition & 1 deletion src/user/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ pub fn auth(conn: &r2d2::PooledConnection<r2d2_postgres::PostgresConnectionManag
AND password = crypt($2, password)
", &[ &username, &password ]) {
Ok(res) => {
if res.len() == 0 { return None; }
if res.len() != 1 { return None; }
let uid: i64 = res.get(0).get(0);

Some(uid)
Expand Down
20 changes: 10 additions & 10 deletions tests/styles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ mod test {
.send()
.unwrap();

assert_eq!(resp.text().unwrap(), "true");
assert_eq!(resp.text().unwrap(), "1");
assert!(resp.status().is_success());
}

Expand All @@ -77,7 +77,7 @@ mod test {
.basic_auth("ingalls", Some("yeaheh"))
.send()
.unwrap();
assert_eq!(resp.text().unwrap(), r#"{"id":1,"name":"Awesome Style","style":"I am a style"}"#);
assert_eq!(resp.text().unwrap(), r#"{"id":1,"name":"Awesome Style","style":"I am a style","uid":1,"username":"ingalls"}"#);
assert!(resp.status().is_success());
}

Expand Down Expand Up @@ -112,7 +112,7 @@ mod test {
.basic_auth("ingalls", Some("yeaheh"))
.send()
.unwrap();
assert_eq!(resp.text().unwrap(), r#"{"id":1,"name":"Modified Awesome Style","style":"I am a style"}"#);
assert_eq!(resp.text().unwrap(), r#"{"id":1,"name":"Modified Awesome Style","style":"I am a style","uid":1,"username":"ingalls"}"#);
assert!(resp.status().is_success());
}

Expand All @@ -138,7 +138,7 @@ mod test {
.basic_auth("ingalls", Some("yeaheh"))
.send()
.unwrap();
assert_eq!(resp.text().unwrap(), r#"{"id":1,"name":"Modified Awesome Style","style":"I am a modified style"}"#);
assert_eq!(resp.text().unwrap(), r#"{"id":1,"name":"Modified Awesome Style","style":"I am a modified style","uid":1,"username":"ingalls"}"#);
assert!(resp.status().is_success());
}

Expand Down Expand Up @@ -183,7 +183,7 @@ mod test {
.send()
.unwrap();

assert_eq!(resp.text().unwrap(), "true");
assert_eq!(resp.text().unwrap(), "2");
assert!(resp.status().is_success());
}

Expand All @@ -199,7 +199,7 @@ mod test {
.send()
.unwrap();

assert_eq!(resp.text().unwrap(), "true");
assert_eq!(resp.text().unwrap(), "3");
assert!(resp.status().is_success());
}

Expand All @@ -225,19 +225,19 @@ mod test {
let mut resp = client.get("http://localhost:8000/api/style/2")
.send()
.unwrap();
assert_eq!(resp.text().unwrap(), r#"{"id":2,"name":"Style 1","style":"I am a style"}"#);
assert_eq!(resp.text().unwrap(), r#"{"id":2,"name":"Style 1","style":"I am a style","uid":1,"username":"ingalls"}"#);
assert!(resp.status().is_success());
}

{ //Get List of Public Styles - Style 1 should now appear, since it is now public
let mut resp = reqwest::get("http://localhost:8000/api/styles").unwrap();
assert_eq!(resp.text().unwrap(), r#"[{"id":2,"name":"Style 1","public":true,"uid":1}]"#);
assert_eq!(resp.text().unwrap(), r#"[{"id":2,"name":"Style 1","public":true,"uid":1,"username":"ingalls"}]"#);
assert!(resp.status().is_success());
}

{ //Get User List of Public Styles - Style 1 should now appear, since it is now public
let mut resp = reqwest::get("http://localhost:8000/api/styles/1").unwrap();
assert_eq!(resp.text().unwrap(), r#"[{"id":2,"name":"Style 1","public":true,"uid":1}]"#);
assert_eq!(resp.text().unwrap(), r#"[{"id":2,"name":"Style 1","public":true,"uid":1,"username":"ingalls"}]"#);
assert!(resp.status().is_success());
}

Expand All @@ -247,7 +247,7 @@ mod test {
.basic_auth("ingalls", Some("yeaheh"))
.send()
.unwrap();
assert_eq!(resp.text().unwrap(), r#"[{"id":2,"name":"Style 1","public":true,"uid":1},{"id":3,"name":"Style 2","public":false,"uid":1}]"#);
assert_eq!(resp.text().unwrap(), r#"[{"id":2,"name":"Style 1","public":true,"uid":1,"username":"ingalls"},{"id":3,"name":"Style 2","public":false,"uid":1,"username":"ingalls"}]"#);
assert!(resp.status().is_success());
}

Expand Down
2 changes: 1 addition & 1 deletion tests/users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ mod test {
.unwrap();

assert!(resp.status().is_success());
assert_eq!(resp.text().unwrap(), "true");
assert_eq!(resp.text().unwrap(), "1");
//TODO test for cookie existence - reqwest is currently working on adding better cookie
//support
}
Expand Down

0 comments on commit fd359d8

Please sign in to comment.