Skip to content
This repository was archived by the owner on Mar 25, 2022. It is now read-only.

Commit 4b0a175

Browse files
committed
- Store survey data in new table without user mapping
- Return Forbidden if the user has already submitted a survey - Increase version to 1.5.1
1 parent bd59de9 commit 4b0a175

File tree

6 files changed

+45
-14
lines changed

6 files changed

+45
-14
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "smartbeans-backend"
3-
version = "1.5.0"
3+
version = "1.5.1"
44
authors = ["Niklas Birth <[email protected]>"]
55
edition = "2018"
66
default-run = "backend"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP TABLE survey
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CREATE TABLE survey (
2+
id INTEGER PRIMARY KEY AUTO_INCREMENT NOT NULL,
3+
val TEXT NOT NULL
4+
)

src/routes/user.rs

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,10 @@ pub fn get_userdata(user: guards::User) -> Json<Value> {
2525
.first(&conn)
2626
.expect("Database error");
2727

28-
let survey_completed = users.filter(username.eq(&user.name))
29-
.select(survey)
30-
.first::<Option<String>>(&conn)
31-
.expect("Database error")
32-
.is_some();
33-
3428
Json(json!({
3529
"username": user.name,
3630
"first_login": the_first_login_that_is_not_the_table_column,
37-
"survey_completed": survey_completed
31+
"survey_completed": survey_completed(&user.name)
3832
}))
3933
}
4034

@@ -53,16 +47,40 @@ pub fn first_login_done(user: guards::User) -> Status {
5347

5448
#[post("/user/submit_survey", format = "text/plain", data = "<data>")]
5549
pub fn submit_survey(user: guards::User, data: rocket::Data) -> Status {
56-
use crate::schema::users::dsl::*;
50+
if survey_completed(&user.name) {
51+
return Status::Forbidden;
52+
}
5753

58-
diesel::update(users.filter(username.eq(&user.name)))
59-
.set(survey.eq(crate::data_to_string(data)))
60-
.execute(&crate::database::establish_connection())
61-
.expect("Database error");
54+
let conn = crate::database::establish_connection();
55+
56+
{
57+
use crate::schema::users::dsl::*;
58+
diesel::update(users.filter(username.eq(&user.name)))
59+
.set(survey.eq("1"))
60+
.execute(&conn)
61+
.expect("Database error");
62+
}
63+
64+
{
65+
use crate::schema::survey::dsl::*;
66+
diesel::insert_into(survey)
67+
.values(val.eq(crate::data_to_string(data)))
68+
.execute(&conn)
69+
.expect("Database error");
70+
}
6271

6372
Status::Ok
6473
}
6574

75+
fn survey_completed(uname: &str) -> bool {
76+
use crate::schema::users::dsl::*;
77+
users.filter(username.eq(uname))
78+
.select(survey)
79+
.first::<Option<String>>(&crate::database::establish_connection())
80+
.expect("Database error")
81+
.is_some()
82+
}
83+
6684
#[get("/level_data")]
6785
pub fn level_data(user: guards::User) -> Result<Json<Value>, Status> {
6886
let userdata = level::user_points(&user)?;

src/schema.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ table! {
6161
}
6262
}
6363

64+
table! {
65+
survey (id) {
66+
id -> Integer,
67+
val -> Text,
68+
}
69+
}
70+
6471
table! {
6572
system_messages (id) {
6673
id -> Integer,
@@ -90,6 +97,7 @@ allow_tables_to_appear_in_same_query!(
9097
error_reports,
9198
route_log,
9299
sessions,
100+
survey,
93101
system_messages,
94102
users,
95103
);

0 commit comments

Comments
 (0)