def handleFighterUpdateMember(self, fighter_id): if "userID" not in self.sessionData: self.send_response(401) self.end_headers() return length = self.headers["Content-Length"] body = self.rfile.read(int(length)).decode("utf-8") print("the RAW body:", body) parsed_body = parse_qs(body) print("the PARSED body:", parsed_body) name = parsed_body["name"][0] color = parsed_body["color"][0] style = parsed_body["style"] [0] stock = parsed_body["stock"] [0] hp = parsed_body["hp"] [0] db = FightersDB() db.updateFighter(name, color, style, stock, hp, fighter_id ) self.send_response(200) self.end_headers()
def handleFighterDeleteMember(self, fighter_id): if "userID" not in self.sessionData: self.send_response(401) self.end_headers() return db = FightersDB() db.deleteFighter(fighter_id) self.send_response(200) self.end_headers()
def handleUserCreate(self): length = self.headers["Content-Length"] body = self.rfile.read(int(length)).decode("utf-8") pars_body = parse_qs(body) print("Parsed body:", pars_body) first_name = pars_body["first_name"] [0] last_name = pars_body["last_name"] [0] email = pars_body["email"] [0] password = pars_body["password"] [0] db = FightersDB() user = db.getUserEmail(email) if user == None: hash_pass = bcrypt.hash(password) db = FightersDB() db.registerUser(first_name, last_name, email, hash_pass) user = db.getUserEmail(email) self.send_response(201) self.end_headers() else: self.send_response(422) self.end_headers()
def handleFighterRetrieveMember(self, fighter_id): if "userID" not in self.sessionData: self.send_response(401) self.end_headers() return db = FightersDB() fighter = db.getOneFighter(fighter_id) if fighter: self.send_response(200) self.send_header("Content-Type", "application/json") self.end_headers() self.wfile.write(bytes(json.dumps(fighter),"utf-8")) else: self.handleNotFound()
def handleFighterRetrieveCollection(self): if "userID" not in self.sessionData: self.send_response(401) self.end_headers() print("userID is not GET") return db = FightersDB() fighter = db.getAllFighters() if fighter: self.send_response(200) self.send_header("Content-Type", "application/json") self.end_headers() self.wfile.write(bytes(json.dumps(fighter), "utf-8")) else: self.handleNotFound()
def handleSessionCreate(self): length = self.headers["Content-Length"] body = self.rfile.read(int(length)).decode('utf-8') pars_body = parse_qs(body) email = pars_body['email'][0] user_password = pars_body['password'][0] db = FightersDB() user = db.getUserEmail(email) print(user) if user != None: if bcrypt.verify(user_password, user['password']): self.sessionData['userID'] = user['id'] self.send_response(201) self.end_headers() else: self.send_response(401) self.end_headers() else: self.send_response(401) self.end_headers()