Skip to content

Commit

Permalink
Merge pull request #10 from csci5117s24/backend
Browse files Browse the repository at this point in the history
Holy Cannoli That's A-Lotta Changes
  • Loading branch information
Conner-DeJong authored Apr 17, 2024
2 parents 4b9dad8 + b45674c commit fd01394
Show file tree
Hide file tree
Showing 20 changed files with 321 additions and 96 deletions.
154 changes: 152 additions & 2 deletions api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -497,13 +497,41 @@ app.http('checkUser', {

// ***TODO***
// FUNCTION NAME: getExpiringFoods
// DESCRIPTION: get list of foods expiring soon
// DESCRIPTION: get list of foods expiring soon - next week ?
// ROUTE: ??
// RETURN:

// ***TODO***
// FUNCTION NAME: getLowQuantityFoods
// DESCRIPTION: get list of foods low in quantity
// RETURN:
// ROUTE: ??
// RETURN: []
app.http('getLowQuantityFoods', {
methods: ['GET'],
authLevel: 'anonymous',
route: 'food',
handler: async (request, context) => {
const auth_header = request.headers.get('X-MS-CLIENT-PRINCIPAL');
let token = null;
if (auth_header) {
token = Buffer.from(auth_header, "base64");
token = JSON.parse(token.toString());
context.log("token= " + JSON.stringify(token));
const userId = token.userId;
const client = await mongoClient.connect(process.env.AZURE_MONGO_DB);
const lowQuantityFoods = await client.db("LetMeCookDB").collection("foods").find({ userId: userId, quantity: { $lte: 3 } }).toArray();
client.close();
context.log("Foods that are low in quantity: ", lowQuantityFoods);
return {
// jsonBody: { data : lowQuantityFoods }
jsonBody: {data: []}
}
}
},
});





app.http('searchRecipesFromIngredients', {
Expand All @@ -529,4 +557,126 @@ app.http('searchRecipesFromIngredients', {
});


app.http('editFood', {
methods: ['POST'],
authLevel: 'anonymous',
route: 'food/edit/{id}',
handler: async (request, context) => {
const _id = request.params.id;
if (ObjectId.isValid(_id)) {
const auth_header = request.headers.get('X-MS-CLIENT-PRINCIPAL');
let token = null;
if (auth_header) {
token = Buffer.from(auth_header, "base64");
token = JSON.parse(token.toString());
context.log("token= " + JSON.stringify(token));
const userId = token.userId;
const body = await request.json();
if (body) {
const name = body.name;
const image = body.image;
const quantity = body.quantity;
const expirationDate = body.expirationDate;
const client = await mongoClient.connect(process.env.AZURE_MONGO_DB);
const result = await client.db("LetMeCookDB").collection("foods").updateOne({_id: new ObjectId(_id), userId: userId}, {$set: {name: name, image: image, quantity: quantity, expirationDate: expirationDate}});
client.close();
if (result.matchedCount > 0) {
return {
status: 201,
jsonBody: {_id: _id}
}
}
return {
status: 403,
jsonBody: {error: "Authorization failed for editing food with id: " + _id}
}
}
return {
status: 405,
jsonBody: {error: "Empty content sent to edit food with id: " + _id}
}
}
return {
status: 403,
jsonBody: {error: "Authorization failed for editing food with id: " + _id}
}
}
return {
status: 404,
jsonBody: {error: "Unknown food with id: " + _id}
}
},
});

app.http('editRecipe', {
methods: ['POST'],
authLevel: 'anonymous',
route: 'recipe/edit/{id}',
handler: async (request, context) => {
const _id = request.params.id;
if (ObjectId.isValid(_id)) {
const auth_header = request.headers.get('X-MS-CLIENT-PRINCIPAL');
let token = null;
if (auth_header) {
token = Buffer.from(auth_header, "base64");
token = JSON.parse(token.toString());
context.log("token= " + JSON.stringify(token));
const userId = token.userId;
const body = await request.json();
const name = body.name;
const image = body.image;
const instructions = body.instructions;
const client = await mongoClient.connect(process.env.AZURE_MONGO_DB);
const result = await client.db("LetMeCookDB").collection("recipes").updateOne({_id: new ObjectId(_id), userId: userId}, {$set: {name: name, image: image, instructions: instructions}});
client.close();
if (result.matchedCount > 0) {
return {
status: 201,
jsonBody: {_id: _id}
}
}
return {
status: 403,
jsonBody: {error: "Authorization failed for editing recipe with id: " + _id}
}
}
return {
status: 403,
jsonBody: {error: "Authorization failed for editing recipe with id: " + _id}
}
}
return {
status: 404,
jsonBody: {error: "Unknown recipe with id: " + _id}
}
},
});

app.http('addRecipeToQueue', {
methods: ['POST'],
authLevel: 'anonymous',
route: 'recipe/queue/{id}',
handler: async (request, context) => {
const _id = request.params.id;
if (ObjectId.isValid(_id)) {
const auth_header = request.headers.get('X-MS-CLIENT-PRINCIPAL');
let token = null;
if (auth_header) {
token = Buffer.from(auth_header, "base64");
token = JSON.parse(token.toString());
context.log("token= " + JSON.stringify(token));
const userId = token.userId;
}
return {
status: 403,
jsonBody: {error: "Authorization failed for adding to queue with recipe id: " + _id}
}
}
return {
status: 404,
jsonBody: {error: "Unknown recipe with id: " + _id}
}
},
});

// GET https://api.spoonacular.com/recipes/findByIngredients?ingredients=apples,+flour,+sugar&number=2
16 changes: 9 additions & 7 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import { Link, Outlet } from "react-router-dom";
import NavBar from "./components/NavBar";
import SaveButton from './components/SaveButton'
import AddButton from './components/AddButton'
import ItemHeader from './components/ItemHeader'
import ItemHeaderEditable from './components/ItemHeaderEditable'

function App() {
return (
<div>
<Link to="">Landing</Link><br />
<Link to="/ingredients">Ingredients</Link><br />
<Link to="/food/1">Food</Link><br />
<SaveButton></SaveButton>
<AddButton></AddButton>
<Link to="/food/edit/1">Food Edit</Link><br />
<Link to="/food/create">Food Create</Link><br />
<Link to="/recipes">Recipes</Link><br />
<Link to="/recipe/1">Recipe indiviual</Link><br />
<Link to="/recipe/edit/1">Recipes</Link><br />
<Link to="/recipe/create">Recipes create</Link><br />
<Link to="/queue">Queue</Link><br />

{/* <ItemHeader src = "https://upload.wikimedia.org/wikipedia/commons/8/89/Tomato_je.jpg"
alt = "tomato" title = "Tomato"></ItemHeader>
<ItemHeaderEditable src = "https://upload.wikimedia.org/wikipedia/commons/8/89/Tomato_je.jpg"
Expand Down
7 changes: 3 additions & 4 deletions src/components/AddButton.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import styles from '../css/Buttons.module.css';
export default function AddButton({...props}){
return( <div>
<button className={styles.addButton} {...props}> Add </button>
</div>
)
return <div>
<button className={styles.addButton} {...props}> Add </button>
</div>
}
9 changes: 4 additions & 5 deletions src/components/EditButton.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import styles from '../css/Buttons.module.css';
export default function EditButton(props){
return ( <div>
<button className={styles.editButton}> Edit </button>
</div>
)
export default function EditButton({...props}){
return <div>
<button className={styles.editButton} {...props}> Edit </button>
</div>
}
17 changes: 7 additions & 10 deletions src/components/ItemHeader.jsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import styles from '../css/ItemHeader.module.css';
export default function ItemHeader(props){
return( <div className={styles.headerContainer} >
<div className={styles.imageContainer} >
<h4 className={styles.title}>{props.title}</h4>
<img className={styles.image} src={props.src} alt={props.alt} ></img>


</div>
</div>
)
export default function ItemHeader({ name, image, alt }){
return <div className={styles.headerContainer} >
<div className={styles.imageContainer} >
<h4 className={styles.title}>{name}</h4>
<img className={styles.image} src={image} alt={alt} ></img>
</div>
</div>
}
16 changes: 5 additions & 11 deletions src/components/ItemHeaderEditable.jsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import styles from '../css/ItemHeader.module.css';

export default function ItemHeaderEditable(props){
return( <div className={styles.headerContainer} >
export default function ItemHeaderEditable({ name, image, alt, updateName }){
return <div className={styles.headerContainer} >
<div className={styles.imageContainer} >
<input className={styles.inputTitle} value={props.title}></input>


<img className={styles.image} src={props.src} alt={props.alt} ></img>
<input className={styles.inputTitle} value={name} onInput={(e)=>updateName(e.target.value)}/>
<img className={styles.image} src={image} alt={alt} ></img>
<button className={styles.editButton}>Edit</button>


</div>
</div>
)

}
}
8 changes: 4 additions & 4 deletions src/components/ListItem.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import styles from '../css/ListItem.module.css'
export default function ListItem({name, description, image, viewLink, editLink}) {
const navigate = useNavigate()
return (
<Row className={styles.listItem} onClick={()=>navigate(viewLink)}>
<Row className={styles.listItem}>
{image ?
<Col>
<Col onClick={()=>navigate(viewLink)}>
<img src={image} alt="food" />
</Col>
: <></>}
<Col>{name}</Col>
<Col>{description}</Col>
<Col onClick={()=>navigate(viewLink)}>{name}</Col>
<Col onClick={()=>navigate(viewLink)}>{description}</Col>
<Col>
<Link to={editLink}>Edit</Link>
</Col>
Expand Down
17 changes: 8 additions & 9 deletions src/components/NavBar.jsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import Nav from 'react-bootstrap/Nav';
import Navbar from 'react-bootstrap/Navbar';
import { Link } from 'react-router-dom';
import 'bootstrap/dist/css/bootstrap.min.css';
import styles from "../css/NavBar.module.css"

function NavBar() {
return (
<div className={styles.nav}>
<Navbar expand="lg" fixed="bottom" fluid="true">
<Nav justify variant="tabs" className={styles.meAuto}>
<Nav.Link as={Link} to="/" className={styles.navbar}>Food</Nav.Link>
<Nav.Link as={Link} to="/myform" className={styles.navbar}>Recipe</Nav.Link>
<Nav.Link as={Link} to="/myform" className={styles.navbar}>Queue</Nav.Link>
<Nav.Link as={Link} to="/myform" className={styles.navbar}>My Account</Nav.Link>
</Nav>
</Navbar>

<div className={styles.meAuto}>
<Link to="/" className={styles.navbar}>Food</Link>
<Link to="/recipe" className={styles.navbar}>Recipe</Link>
<Link to="/food" className={styles.navbar}>Queue</Link>
<Link to="/queue" className={styles.navbar}>My Account</Link>
</div>

</div>
);
}
Expand Down
22 changes: 22 additions & 0 deletions src/components/NavBarOld2.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import Nav from 'react-bootstrap/Nav';
import Navbar from 'react-bootstrap/Navbar';
import { Link } from 'react-router-dom';
import 'bootstrap/dist/css/bootstrap.min.css';
import styles from "../css/NavBar.module.css"

function NavBar() {
return (
<div className={styles.nav}>
<Navbar expand="lg" fixed="bottom" fluid="true">
<Nav justify variant="tabs" className={styles.meAuto}>
<Nav.Link as={Link} to="/" className={styles.navbar}>Food</Nav.Link>
<Nav.Link as={Link} to="/myform" className={styles.navbar}>Recipe</Nav.Link>
<Nav.Link as={Link} to="/myform" className={styles.navbar}>Queue</Nav.Link>
<Nav.Link as={Link} to="/myform" className={styles.navbar}>My Account</Nav.Link>
</Nav>
</Navbar>
</div>
);
}

export default NavBar;
6 changes: 2 additions & 4 deletions src/components/QueueItem.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
export default function Queue(props){
return( <div>
return <div>


</div>
)
</div>
}
6 changes: 2 additions & 4 deletions src/components/RemoveButton.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import styles from '../css/Buttons.module.css';
export default function RemoveButton(props){
return( <div>

return <div>
<button className={styles.saveButton}> Remove </button>
</div>
)
</div>
}
9 changes: 4 additions & 5 deletions src/components/SaveButton.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import styles from '../css/Buttons.module.css';
export default function SaveButton(props) {
return ( <div>
<button className={styles.saveButton}> Save </button>
</div>
)
export default function SaveButton({...props}) {
return <div>
<button className={styles.saveButton} {...props}> Save </button>
</div>
}
6 changes: 2 additions & 4 deletions src/components/SearchButton.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
export default function SearchButton(props){
return( <div>
return <div>


</div>
)
</div>
}
Loading

0 comments on commit fd01394

Please sign in to comment.