diff --git a/backend/src/app.py b/backend/src/app.py index 822088c..49b4da4 100644 --- a/backend/src/app.py +++ b/backend/src/app.py @@ -162,7 +162,7 @@ def register(): return ret, 409 -@app.route('/umfrage', methods=['POST']) +@app.route('/api/protected/umfrage', methods=['POST']) @flask_praetorian.auth_required def survey(): req = flask.request.get_json(force=True) @@ -172,18 +172,66 @@ def survey(): # get data from the survey and write it to the database + # placeholder if the survey is valid + is_survey_valid = True + + # Check age if it's numeric age = req.get('age', None) + if not age.isnumeric(): + is_survey_valid = False + + # check gender possibilites gender = req.get('gender', None) + possible_genders = [ + "male", + "female", + "divers", + "sex_na" + ] + if gender not in possible_genders: + is_survey_valid = False + + # check education possibilities education = req.get('education', None) + possible_education = [ + "edu_lehre", + "edu_gymnasium", + "edu_berufsmatura", + "edu_bachelor", + "edu_Master", + "edu_na" + ] + if education not in possible_education: + is_survey_valid = False + + # check skills values skills = req.get('skills', None) - user_db = User.query.filter_by(id=id_req).first() - user_db.age = age - user_db.gender = gender - user_db.education = education - user_db.skills = skills - db.session.commit() - ret = {'message': 'Umfrage freigestellt'} - return ret, 200 + possible_skills = [ + "skills_sehr_gut", + "skills_gut", + "skills_mittel", + "skills_nicht_so_good", + "skills_garnicht", + "skills_na" + ] + + if skills not in possible_skills: + is_survey_valid = False + + if is_survey_valid: + user_db = User.query.filter_by(id=id_req).first() + user_db.age = age + user_db.gender = gender + user_db.education = education + user_db.skills = skills + db.session.commit() + print(req) + ret = {'message': 'Vielen Dank für das Ausfüllen der Umfrage.'} + return ret, 200 + else: + ret = { + 'message': 'Einige der Felder stimmen nicht überein und müssen angepasst werden.'} + return ret, 400 @app.route('/time') diff --git a/frontend/src/components/ErrorMessage.js b/frontend/src/components/ErrorMessage.js new file mode 100644 index 0000000..41c2798 --- /dev/null +++ b/frontend/src/components/ErrorMessage.js @@ -0,0 +1,10 @@ +import React from "react"; +import "../App.css"; + +export default function ErrorMessage(props) { + return ( + <> +

{props.message}

+ + ); +} \ No newline at end of file diff --git a/frontend/src/components/pages/Umfrage.js b/frontend/src/components/pages/Umfrage.js index c810ea1..fc83439 100644 --- a/frontend/src/components/pages/Umfrage.js +++ b/frontend/src/components/pages/Umfrage.js @@ -6,15 +6,33 @@ import Footer from "../../Footer"; import InputField from "../InputField"; import SubmitField from "../SubmitField"; import { authFetch } from "../../auth/AuthProvider"; +import ErrorMessage from "../ErrorMessage"; export default function Umfrage() { + /* values from survey */ const [age, setAge] = useState(""); const [gender, setGender] = useState(""); const [education, setEducation] = useState(""); const [skills, setSkills] = useState(""); - let surveyIsValid = true; - let errorMessage = ""; + /* error messages */ + const [ageErrorMessage, setAgeErrorMessage] = useState(""); + const [genderErrorMessage, setGenderErrorMessage] = useState(""); + const [educationErrorMessage, setEducationErrorMessage] = useState(""); + const [skillsErrorMessage, setSkillsErrorMessage] = useState(""); + + const [backendErrorMessage, setBackendErrorMessage] = useState(""); + const [backendSuccessMessage, setBackendSuccessMessage] = useState(""); + const [isSurveyValidBackend, setIsSurveyValidBackend] = useState(""); + + /* boolean if the values are ok */ + const [isAgeOk, setIsAgeOk] = useState(""); + const [isGenderOk, setIsGenderOk] = useState(""); + const [isEducationOk, setIsEducationOk] = useState(""); + const [isSkillsOk, setIsSkillsOk] = useState(""); + + /* genereal check if survey is valid */ + const [isSurveyValid, setIsSurveyValid] = useState(""); const onSubmitClick = (e) => { e.preventDefault(); @@ -25,32 +43,56 @@ export default function Umfrage() { skills: skills, }; + setIsAgeOk(true); + setIsGenderOk(true); + setIsEducationOk(true); + setIsSkillsOk(true); + if (!Number(age)) { - errorMessage += "age is not a number\n"; - surveyIsValid = false; + setAgeErrorMessage("Das Alter muss als Zahl angegeben werden."); + setIsSurveyValid(false); + setIsAgeOk(false); } if (gender === "DEFAULT" || gender === "") { - errorMessage += "choose gender\n"; - surveyIsValid = false; + setGenderErrorMessage( + "Bitte wähle eine der Optionen für dein Geschlecht aus." + ); + setIsSurveyValid(false); + setIsGenderOk(false); } if (education === "DEFAULT" || education === "") { - errorMessage += "choose edu\n"; - surveyIsValid = false; + setEducationErrorMessage( + "Bitte wähle eine der Optionen zu deiner Ausbildung aus." + ); + setIsSurveyValid(false); + setIsEducationOk(false); } if (skills === "DEFAULT" || skills === "") { - errorMessage += "choose skills\n"; - surveyIsValid = false; + setSkillsErrorMessage( + "Bitte wähle eine der Optionen zu den Fähigkeiten aus" + ); + setIsSurveyValid(false); + setIsSkillsOk(false); } - if (surveyIsValid === true) { - console.log(opts); - - authFetch("/umfrage", { + setIsSurveyValid(true); + if (isSurveyValid === true) { + authFetch("/api/protected/umfrage", { method: "post", body: JSON.stringify(opts), + }).then((response) => { + if (response.status === 200) { + response.json().then((r) => { + setBackendSuccessMessage(r.message); + setIsSurveyValidBackend(true); + }); + } else { + response.json().then((r) => { + setBackendErrorMessage(r.message); + setIsSurveyValidBackend(false); + }); + } }); - } else { - console.log(errorMessage); } }; @@ -96,6 +138,7 @@ export default function Umfrage() { className="input-field" LabelName="Wie alt bist du? (Alter in Jahren)" /> + {!isAgeOk && }

+ {!isGenderOk && }
+ {!isEducationOk && }
+ {!isSkillsOk && } + {!isSurveyValidBackend ? ( + + ) : ( +

{backendSuccessMessage}

+ )}