def up_del_todo(request, user_id, task_id): if request.method == 'PUT': json_data = json.loads(request.body) todo = Todo.objects.filter(id=task_id, user_id=user_id).first() if not todo: return Response.bad_request(message="Task not found!") todo = Todo(id=task_id, task=json_data['task'], user_id=user_id) todo.save() return Response.ok(values=transformer.single_transform(todo), message='Task updated!') elif request.method == 'DELETE': todo = Todo.objects.filter(id=task_id, user_id=user_id).first() todo.delete() if not todo: return Response.bad_request(message='Task not found!') return Response.ok(message='Task deleted!') else: return Response.bad_request(message='Invalid method!')
def index(request): # Show all user if request.method == 'GET': user = Users.objects.all() user = transformer.transform(user) return Response.ok(values=user) else: return Response.bad_request(message='Invalid Method!')
def index(request): if request.method == 'GET': user = Users.objects.all() user = transformer.transform(user) return Response.ok(user) elif request.method == 'POST': json_data = json.loads(request.body) jumlah = [json_data["food"]] user = Users() user.foods = Foods.objects.get(id=json_data["food"]) return Response.ok(values=transformer.singleTransform(user), message="Added!")
def register(request): # Add user (Register) if request.method == 'POST': json_data = json.loads(request.body) user = Users() user.name = json_data['name'] user.email = json_data['email'] user.password = make_password(password=json_data['password']) user.save() return Response.ok(values=transformer.single_transform(user), message="Added!") else: return Response.bad_request(message='Invalid Method!')
def indexfood(request): if request.method == 'GET': user = Foods.objects.all() user = transformer.foodtransform(user) return Response.ok(values=user) elif request.method == 'POST': json_data = json.loads(request.body) user = Foods() user.name = json_data['name'] user.price = json_data['harga'] user.isready = json_data['isready'] user.save() return Response.ok(values=transformer.foodTransform(user), message="Added!")
def pesanansindex(request): if request.method == 'GET': user = Pesanans.objects.all() user = transformer.pesananstransform(user) return Response.ok(user) elif request.method == 'POST': json_data = json.loads(request.body) user = Pesanans() user.nama = json_data['nama'] user.nomeja = json_data['nomor meja'] user.keranjang = Users.objects.get(id=json_data['keranjang']) user.save() return Response.ok(values=transformer.pesanansTransform(user), message="Added!")
def index(request): if request.method == 'GET': user = Users.objects.all() user = transformer.transform(user) return Response.ok(values=user) elif request.method == 'POST': json_data = json.loads(request.body) user = Users() user.name = json_data['name'] user.email = json_data['email'] user.password = make_password(password=json_data['password']) user.save() return Response.ok(values=transformer.singleTransform(user), message="Added!")
def showfood(request, id): if request.method == 'GET': user = Foods.objects.filter(id=id).first() if not user: return Response.badRequest(message='Pengguna tidak ditemukan!') user = transformer.foodTransform(user) return Response.badRequest(values=user) elif request.method == 'PUT': json_data = json.loads(request.body) user = Foods.objects.filter(id=id).first() if not user: return Response.badRequest(message="Pengguna tidak ditemukan") user.name = json_data['name'] user.price = json_data['harga'] user.isready = json_data['isready'] user.save() return Response.ok(values=transformer.foodTransform(user), message="Updated!") elif request.method == 'DELETE': user = Foods.objects.filter(id=id).first() if not user: return Response.badRequest(message="Pengguna tidak ditemukan") user.delete() return Response.ok(message="Deleted!") else: return Response.badRequest(message="Invalid method!")
def show(request, id): if request.method == 'GET': user = Users.objects.filter(id=id).first() if not user: return Response.badRequest(message='Pengguna tidak ditemukan!') user = transformer.singleTransform(user) return Response.badRequest(values=user) elif request.method == 'PUT': json_data = json.loads(request.body) user = Users.objects.filter(id=id).first() if not user: return Response.badRequest(message="Pengguna tidak ditemukan") user.jumlah = json_data['jumlah'] user.foods = Foods.objects.get(id=json_data["food"]) user.save() return Response.ok(values=transformer.singleTransform(user), message="Updated!") elif request.method == 'DELETE': user = Users.objects.filter(id=id).first() if not user: return Response.badRequest(message="Pengguna tidak ditemukan") user.delete() return Response.ok(message="Deleted!") else: return Response.badRequest(message="Invalid method!")
def pesanansshow(request, id): if request.method == 'GET': user = Pesanans.objects.filter(id=id).first() if not user: return Response.badRequest(message='Pengguna tidak ditemukan!') user = transformer.pesanansTransform(user) return Response.badRequest(values=user) elif request.method == 'PUT': json_data = json.loads(request.body) user = Pesanans.objects.filter(id=id).first() if not user: return Response.badRequest(message="Pengguna tidak ditemukan") user.nama = json_data['nama'] user.nomeja = json_data['nomor meja'] user.keranjang = Users.objects.all() user.save() return Response.ok(values=transformer.pesanansTransform(user), message="Updated!") elif request.method == 'DELETE': user = Pesanans.objects.filter(id=id).first() if not user: return Response.badRequest(message="Pengguna tidak ditemukan") user.delete() return Response.ok(message="Deleted!") else: return Response.badRequest(message="Invalid method!")
def show(request, id): if request.method == 'GET': user = Users.objects.filter(id=id).first() if not user: return Response.badRequest(message='Pengguna tidak ditemukan!') user = transformer.singleTransform(user) return Response.badRequest(values=user) elif request.method == 'PUT': json_data = json.loads(request.body) user = Users.objects.filter(id=id).first() if not user: return Response.badRequest(message="Pengguna tidak ditemukan") user.name = json_data['name'] user.email = json_data['email'] user.password = make_password(password=json_data['password']) user.save() return Response.ok(values=transformer.singleTransform(user), message="Updated!") elif request.method == 'DELETE': user = Users.objects.filter(id=id).first() if not user: return Response.badRequest(message="Pengguna tidak ditemukan") user.delete() return Response.ok(message="Deleted!") else: return Response.badRequest(message="Invalid method!")
def auth(request): if request.method == 'POST': json_data = json.loads(request.body) email = json_data['email'] user = Users.objects.filter(email=email).first() if not user: return Response.badRequest(message='Pengguna tidak ditemukan!') if not check_password(json_data['password'], user.password): return Response.badRequest( message="Password atau email yang kamu masukkan salah!") user = transformer.singleTransform(user) jwt = JWTAuth() user['token'] = jwt.encode({"id": user['id']}) return Response.ok(values=user, message="Berhasil masuk!")
def view_add_todo(request, user_id): # Read todolist if request.method == 'GET': todo = Todo.objects.filter(user_id=user_id) todo = transformer.transform(todo) return Response.ok(values=todo) # Add todolist elif request.method == 'POST': json_data = json.loads(request.body) todo = Todo(task=json_data['task'], user_id=user_id) todo.save() return Response.ok(values=transformer.single_transform(todo), message='Task added successfully!') else: return Response.bad_request(message='Invalid method')
def login(request): # User login if request.method == 'POST': json_data = json.loads(request.body) #request.POST email = json_data['email'] user = Users.objects.filter(email=email).first() if not user: return Response.bad_request(message='User not found!') if not check_password(json_data['password'], user.password): return Response.bad_request('Invalid email or password!') user = transformer.single_transform(user) jwt = JWTAuth() user['token'] = jwt.encode({"id": user['id']}) return Response.ok(values=user, message='Login success!') else: return Response.bad_request(message='Invalid Method!')
def show(request, id): # Read user if request.method == 'GET': user = Users.objects.filter(id=id).first() if not user: return Response.bad_request(message='User not found!') user = transformer.single_transform(user) return Response.ok(values=user) # Update user elif request.method == 'PUT': json_data = json.loads(request.body) user = Users.objects.filter(id=id).first() if not user: return Response.bad_request(message='User not found!') user.name = json_data['name'] user.email = json_data['email'] user.password = make_password(password=json_data['password']) user.save() return Response.ok(values=transformer.single_transform(user), message="Updated!") # Delete user elif request.method == 'DELETE': user = Users.objects.filter(id=id).first() if not user: return Response.bad_request(message='User not found!') user.delete() return Response.ok(message='Deleted!') else: return Response.bad_request(message='Invalid Method!')
def wrapper(*args, **kwargs): try: decode(args[0].headers.get('Authorization')) except Exception: return Response.unauthorized(message='Unauthorized!') return fn(*args, **kwargs)