def post(self): """ Register a new user --- tags: - User API parameters: - name: username in: body type: string required: true description: the username - name: password in: body type: string required: true description: the password - name: email in: body type: string required: true description: the email adress - name: phonenumber in: body type: string required: true description: the phonenumber of the new user responses: 200: description: user sucessfully registered schema: id: RegisterResponse properties: message: type: string description: user successful created 400: description: the user already exists or not all required data provided schema: id: RegisterResponse properties: message: type: string description: user exists or not all data provided """ data = _user_parser.parse_args() if UserModel.find_user_by_username(data["username"]): return {"message": "User exists!"}, 400 if not data["username"] or not data["password"] or not data[ "email"] or not data["phonenumber"]: return {"message": "Not all required data provided!"}, 400 user = UserModel(data["username"], data["password"], data["email"], data["phonenumber"]) user.save_to_db() return {"message": "User {} created!".format(data["username"])}
def post(self): """ Login as a user --- tags: - User API parameters: - name: username in: body type: string required: true description: the username - name: password in: body type: string required: true description: the password responses: 200: description: user sucessfully logged in schema: id: TokenRefresh properties: accessToken: type: string description: new token for the current user id: type: string description: id of the logged in user 401: description: the credentials are invalid schema: id: LoginResponse properties: message: type: string description: Invalid credentials """ data = _user_parser.parse_args() user = UserModel.find_user_by_username(data["username"]) if user and user.password == data["password"]: access_token = create_access_token( identity=user.id, fresh=True) # Puts User ID as Identity in JWT return {"accessToken": access_token, "id": str(user.id)}, 200 return {"message": "Invalid credentials!"}, 401
def put(self): """ Change an entry --- tags: - Entry API parameters: - name: JWT Token in: header type: string required: true description: The JWT Token with format "Authorization Bearer <JWT Token>" - name: id in: body type: string required: true description: the id of the entry that you want to change - name: title in: body type: string required: true description: the title of the entry - name: text in: body type: string required: true description: the text of the entry - name: tags in: body type: array required: true description: the tags for a entry responses: 200: description: Entry changed! 404: description: Entry not found! 403: description: You are not allowed to change the entry! Only the Author can change it """ data = _entry_parser.parse_args() entry = EntryModel.find_entry_by_id(data["id"]) if entry: if UserModel.find_user_by_id(get_jwt_identity()) is not UserModel.find_user_by_username(entry.json()["author"]): return { "message": "You are not allowed to change the entry!" }, 403 else: tags_to_db = "" counter = 0 for i in data['tags']: counter += 1 tags_to_db += str(i) if counter == len(data['tags']): pass else: tags_to_db += ";" data["tags"] = tags_to_db entry.title = data["title"] entry.text = data["text"] entry.date = str(datetime.now().strftime("%Y-%m-%d %H:%M:%S")) entry.tags = tags_to_db entry.save_to_db() return { "message": "Entry changed!" }, 200 return { "message": "Entry not found!" }, 404