Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rating #118

Merged
merged 13 commits into from
May 26, 2024
Prev Previous commit
Next Next commit
changed how post request works
  • Loading branch information
Noahkmm committed May 23, 2024
commit b3fecf22a686dce867b1288338ccd264683c4118
43 changes: 32 additions & 11 deletions app/locations/[location]/page.tsx
Original file line number Diff line number Diff line change
@@ -9,21 +9,18 @@ interface RestrictionImageMap {
[key: string]: string;
}

function kms(location: Location) {
// response.data["locations"].map(kms)
axios
.post("http://localhost:8000/myapi/db_update/", { dh_name: location.name })
.then((response) => {//get diff?
console.log(response.data);
})
.catch((error) => {
console.log(error);
});
function calc_average(food_name: string, foods: {}) {
return 0;
}

function get_user_rating() {

}

export default function Page({ params }: { params: { location: number } }) {
const [location, setLocation] = useState<Location>();
const [showCategories, setShowCategories] = useState<boolean[]>([]);
const [food_reviews, setReviews] = useState({});

const restrictionImageMap: RestrictionImageMap = {
eggs: "/Images/egg.jpg",
@@ -50,7 +47,14 @@ export default function Page({ params }: { params: { location: number } }) {
const locations = response.data.locations;
const location = locations[params.location];
setLocation(location);
kms(location);
axios
.post("http://localhost:8000/myapi/db_update/", { dh_name: location.name })
.then((response) => {//get diff?
console.log(response.data);
})
.catch((error) => {
console.log(error);
});

// Get current hour
const currentHour = new Date().getHours();
@@ -72,6 +76,22 @@ export default function Page({ params }: { params: { location: number } }) {
}
}),
);
let user_id = sessionStorage.getItem("token");
if (user_id == null) user_id = "";

axios
.get("http://localhost:8000/myapi/get_ratings/", //gets the user rating
) //need to get global

.then((response) => {//get diff?
setReviews(response.data);
console.log(food_reviews);
})
.catch((error) => {
console.log(error);
});


})
.catch((error) => {
console.log(error);
@@ -148,6 +168,7 @@ export default function Page({ params }: { params: { location: number } }) {
{subCategory.foods.map((food, k) => (
<LocationFood
key={k}
food_average={calc_average(food.name, food_reviews)}
food_name={food.name}
restriction_images={food.restrictions.map(
(restriction) => restrictionImageMap[restriction],
8 changes: 8 additions & 0 deletions backend/myapi/model_logic/foods/actions.py
Original file line number Diff line number Diff line change
@@ -18,6 +18,7 @@ def set_food(
"ratings": [],
"comments": [],
"images": [],
"average": 0,
}
foods_collection.insert_one(food)
return food
@@ -76,6 +77,13 @@ def update_food(
# update the ratings in the db
foods_collection.update_one({"name": name}, {"$set": {"ratings": ratings}})

#update average
total = 0
for user_rating in food["ratings"]:
total+= user_rating["rating"]
# print(user_rating['rating'])
food["average"] = total/ len(food["ratings"])

# update comment
if comment is not None and user_id is not None:
# get the current comments
63 changes: 54 additions & 9 deletions backend/myapi/model_logic/foods/views.py
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@
from urllib.parse import unquote

from ..locations.actions import get_locations, find_food_in_location
from ..users.actions import update_user


# Get request that takes parameter of food name and returns the food object
@@ -60,11 +61,6 @@ def get_food(request, name: str):
return Response(food)


# "name": name,
# "restrictions": restrictions,
# "ratings": [],
# "comments": [],
# "images": [],

##bulk food get?
@api_view(["POST"])
@@ -98,13 +94,62 @@ def bulk_update_db(request):
return Response(200)

## Ratings
# update_user

# @api_view(["POST"])
# def get_ratings(request):

# # get the food name from the request
# food_name = request.data.get("food_item")
# print("-"*20 + food_name + "-"*20)
# food = get_food_db( food_name)
# if(food ==None):
# return Response (404)

# rating = food['allergies']
# print(rating)

# return Response(200)
from ...models import foods_collection

@api_view(["GET"])
def get_ratings(request):

foods = {}
for n in foods_collection.find():
if(n == None) :return Response(404)
foods[n['name']] = n['ratings']

return Response(foods)

@api_view(["POST"])
def update_food_rating(request):
food_name = request.data.get()
def user_rating_update(request):
# get the food name from the request
food_name = request.data.get("food_name")
# get the user id from the request
user_id = request.data.get("user_id")

food_rating = request.data.get("food_rating")
if(food_rating == None ):
print("none?")
print("Food: "+ food_name)
print("User: "+ user_id)
print(f"", end = "")
print({food_rating})

if(get_food_db(food_name) == None): #a bit redundant, no?
print("Food: "+ food_name)
return Response(status = 404 )

if(update_food_db(food_name, [], user_id, food_rating) == None):
print("No user")
return Response(status = 404 )

if(update_user(user_id, food_name, food_rating) == None):
return Response(status = 404 )

return Response(food_rating)

update_food_db(food_name, )
return Response(200)

## Comments
@api_view(["POST"])
1 change: 1 addition & 0 deletions backend/myapi/model_logic/users/actions.py
Original file line number Diff line number Diff line change
@@ -35,6 +35,7 @@ def update_user(id: str, food_name: str, rating: int | None = None) -> None:
user["ratings"][food_name] = rating
# update user
users_collection.update_one({"id": id}, {"$set": {"ratings": user["ratings"]}})



def delete_user(id: str) -> None:
4 changes: 2 additions & 2 deletions backend/myapi/urls.py
Original file line number Diff line number Diff line change
@@ -10,6 +10,6 @@
path("logout/", views.current_logout, name="current_logout"),
path("foods/<str:name>/", foods_views.get_food, name="foods"),
path("comments/", foods_views.add_comment, name="comments"),
path("user_rating_get/", user_funcs.get_user, name = "user_rating_get"),
path("user_rating_update/", user_funcs.update_user, name = "user_update_rating")
path("rating_update/", foods_views.user_rating_update, name = "rating_update"),
path("get_ratings/", foods_views.get_ratings, name = "get_ratings")
]
14 changes: 7 additions & 7 deletions backend/myapi/views.py
Original file line number Diff line number Diff line change
@@ -133,11 +133,11 @@ def current_logout(request):
return JsonResponse({"message": "User has been logged out"})


@api_view(["GET"])
def get_rating(request):
user = get_rating(request.user_id)
if(user==None):
return Response({"message": "No user found"})
else:
return Response({"message": "User found: {user}", "user data": user})
# @api_view(["GET"])
# def get_user_rating(request):
# user = get_rating(request.user_ids)
# if(user==None):
# return Response({"message": "No user found"})
# else:
# return Response({"message": "User found: {user}", "user data": user})

12 changes: 9 additions & 3 deletions components/location/food.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import axios from "axios";
import Link from "next/link";

import Rating_bar from "@/components/review";
import Rating_bar from "@/components/rating";
import Review_bar from "@/components/review";


export default function LocationFood({
food_name,
food_average,
restriction_images,
}: {
food_name: string;
food_average: number;
restriction_images: string[]; // Change the type to string array
}) {

// const [user_rating, setUserRating] = useState(0);
let user_id = sessionStorage.getItem("token");
if (user_id == null) user_id = "";

return (
<Link href={`/foods/${encodeURIComponent(food_name)}`}>
@@ -29,11 +34,12 @@ export default function LocationFood({
</ul>
<div>
<h4>Review</h4>
<Rating_bar name={"wo"} />
{food_average}
{/* <Rating_bar food_name={food_name} user_id={'0'} /> */}
</div>
<div>
<h4>Rating</h4>
<Rating_bar name={"wo"} />
<Review_bar food_name={food_name} user_id={'0'} />
</div>
</div>
</div>
21 changes: 21 additions & 0 deletions components/rating.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"use client";
import Link from "next/link";
import axios from "axios";
import { useState } from "react";


export default function Rating_bar({
food_name,
user_id
}: {
food_name: string;
user_id: string;
}) {


return (
<div className="flex items-center justify-center pb-4 ">
rating - -
</div>
);
}
37 changes: 20 additions & 17 deletions components/review.tsx
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
"use client";
import Link from "next/link";
import axios from "axios";
import { useState } from "react";



export default function Rating_bar({
name,
export default function Review_bar({
food_name,
user_id
}: {
name: string;

food_name: string;
user_id: string;
}) {
return (
<div className="flex items-center justify-center pb-4 ">
rating
</div>
);
}

export function Review_bar({
name,
}: {
name: string;
}) {
// post get_rating
// const [averageRating, setAverage] = useState<string>();
// axios
// .post("http://localhost:8000/myapi/average_rating/", //gets the user rating
// { food_item: food_name }) //need to get global

// .then((response) => {//get diff?
// setAverage(response.data)
// console.log(response.data);
// })
// .catch((error) => {
// console.log(error);
// });

return (
<div className="flex items-center justify-center pb-4 ">
review

</div>
);
}