def listening(owner_email, listening_email): if owner_email == listening_email: return abort(400, "can't listen yourself") listening_state = session.query(Listen).\ filter(Listen.listeningEmail == listening_email).\ filter(Listen.listenerEmail == owner_email).first() if listening_state: return abort(400, "already listening") listening_user = session.query(User).filter( User.email == listening_email).first() if listening_user: try: new_listening = Listen(listeningEmail=listening_email, listenerEmail=owner_email) session.add(new_listening) session.commit() return {"message": "Success"} except SQLAlchemyError: session.rollback() return abort(418, "db_error") else: return abort(404, "No such user")
def post(title, content): new_post = Posts(title=title, content=content) session.add(new_post) session.commit() return {'message': 'Create post successfully'}, 201
def create_notice(title, content, user_email): new_notice = Notice(title=title, content=content, user_email=user_email) session.add(new_notice) session.commit() # sendMessage(title="새로운 공지사항", body=title) return {"message": "success"}, 201
def create_new_user(email, password, nickname, age): new_user = User(email=email, password=generate_password_hash(password), nickname=nickname, age=age) session.add(new_user) session.commit()
def delete_post(post_id): del_data = session.query(Posts).filter(Posts.id == post_id).first() if del_data: session.delete(del_data) session.commit() return {'message': 'Delete post successfully'} else: return abort(404, 'There is no post you looking for')
def logout(email): user = session.query(User).filter(User.email == email).first() if user: user.refresh_token = None session.commit() return {"message": "logout successfully"} else: return abort(401, "cannot find token user")
def update_post(post_id, title, content): udt_post = session.query(Posts).filter(Posts.id == post_id).first() if udt_post: udt_post.title = title udt_post.content = content session.commit() return {'message': 'Update post successfully'} else: return abort(404, 'There is no post you looking for')
def withdrawal(email): del_user = session.query(User).filter(User.email == email).first() if del_user: token = Redis.get(email) if token: Redis.delete(email) session.delete(del_user) session.commit() return {"message": "success"}, 204 else: abort(401, 'could not find user')
def delete_notice(notice_id, user_email): del_notice = session.query(Notice).filter(Notice.id == notice_id).first() if del_notice: if del_notice.user_email == user_email: session.delete(del_notice) session.commit() return {"message": "success"}, 204 else: abort(403, 'could not delete notice created by others') else: abort(404, 'could not find notice matching this id')
def sign_up(email, password, name, major): origin_user = session.query(User).filter(User.email == email).first() if origin_user: abort(409, "this email is already in use") else: add_user = User(email=email, password=generate_password_hash(password), name=name, major=major, email_status=1) session.add(add_user) session.commit() return {"message": "success"}, 201
def change_password(email, code, password): if check_reset_code(email, code): user = session.query(User).filter(User.email == email).first() if user: user.password = generate_password_hash(password) session.commit() return {"message": "Successfully password changed"} else: return abort(404, "The email is incorrect") else: return abort(401, "The verification code is not correct")
def sign_up(email, password, name, phone_number, gender): user = session.query(User).filter(User.email == email).first() if user: return abort(409, 'This email already signed up') add_user = User(email=email, password=generate_password_hash(password), name=name, phone_number=phone_number, gender=gender) session.add(add_user) session.commit() return {'message': 'Sign up successfully'}, 201
def login(email, password): user = session.query(User).filter(User.email == email).first() check_user_pw = check_password_hash(user.passward, password) if user else None if check_user_pw: access_token = create_access_token(identity=email) refresh_token = create_refresh_token(identity=email) user.refresh_token = refresh_token session.commit() return {"access_token": access_token, "refresh_token": refresh_token} else: return abort(400, "The email or password is incorrect")