diff --git a/controllers/bugs.js b/controllers/bugs.js new file mode 100644 index 0000000..f7fed86 --- /dev/null +++ b/controllers/bugs.js @@ -0,0 +1,32 @@ +const Bugs = require('../models/bugs'); + +const { + v4: uuidv4 +} = require("uuid"); +const S3 = require("aws-sdk/clients/s3"); +const s3 = new S3(); // initialize the S3 constructor + +const BUCKET = process.env.BUCKET; + +module.exports = { + index +} + + + + +async function index(req, res) { + try { + // this populates the user when you find the bugs + // so you'll have access to the users information + // when you fetch teh bugs + const bugs = await Bugs.find({}).exec(); + res.status(200).json({ + bugs: bugs + }); + } catch (err) { + res.status(400).json({ + err + }); + } +} \ No newline at end of file diff --git a/controllers/fish.js b/controllers/fish.js new file mode 100644 index 0000000..5247ed2 --- /dev/null +++ b/controllers/fish.js @@ -0,0 +1,35 @@ +const Fish = require('../models/fish'); + +const { + v4: uuidv4 +} = require("uuid"); +const S3 = require("aws-sdk/clients/s3"); +const s3 = new S3(); // initialize the S3 constructor + +const BUCKET = process.env.BUCKET; + +module.exports = { + index, + +} + + + + +async function index(req, res) { + try { + // this populates the user when you find the fish + // so you'll have access to the users information + // when you fetch teh fish + const fish = await Fish.find({}); + // console.log(res.fish, 'fish') + // console.log(res.json, 'json') + res.status(200).json({ + fish: fish + }); + } catch (err) { + res.status(400).json({ + err + }); + } +} diff --git a/models/bugs.js b/models/bugs.js new file mode 100644 index 0000000..4547da0 --- /dev/null +++ b/models/bugs.js @@ -0,0 +1,80 @@ +const mongoose = require('mongoose'); + +const caughtSchema = mongoose.Schema({ + + username: String, + userId: { + type: mongoose.Schema.Types.ObjectId, + ref: 'User' + } +}); + +const bugsSchema = new mongoose.Schema({ + id: Number, + bug_name: String, + price: Number, + img: String, + location: String, + hemisphere: { + north: { + January: Boolean, + February: Boolean, + March: Boolean, + April: Boolean, + May: Boolean, + June: Boolean, + July: Boolean, + August: Boolean, + October: Boolean, + September: Boolean, + November: Boolean, + December: Boolean, + }, + south: { + January: Boolean, + February: Boolean, + March: Boolean, + April: Boolean, + May: Boolean, + June: Boolean, + July: Boolean, + August: Boolean, + October: Boolean, + September: Boolean, + November: Boolean, + December: Boolean, + } + }, + time: String, + time_available: { + h_0: Boolean, + h_1: Boolean, + h_2: Boolean, + h_3: Boolean, + h_4: Boolean, + h_4: Boolean, + h_5: Boolean, + h_6: Boolean, + h_7: Boolean, + h_8: Boolean, + h_9: Boolean, + h_10: Boolean, + h_11: Boolean, + h_12: Boolean, + h_13: Boolean, + h_14: Boolean, + h_15: Boolean, + h_16: Boolean, + h_17: Boolean, + h_18: Boolean, + h_19: Boolean, + h_20: Boolean, + h_21: Boolean, + h_22: Boolean, + h_23: Boolean + }, + caught: [caughtSchema] // < one post has many likes +}); + + +module.exports = mongoose.model('Bugs', bugsSchema); \ No newline at end of file diff --git a/models/fish.js b/models/fish.js new file mode 100644 index 0000000..7ee662a --- /dev/null +++ b/models/fish.js @@ -0,0 +1,69 @@ +const mongoose = require('mongoose'); + +const caughtSchema = mongoose.Schema({ + + username: String, + userId: { + type: mongoose.Schema.Types.ObjectId, + ref: 'User' + } +}); +const months = mongoose.Schema({ + January: Boolean, + February: Boolean, + March: Boolean, + April: Boolean, + May: Boolean, + June: Boolean, + July: Boolean, + August: Boolean, + October: Boolean, + September: Boolean, + November: Boolean, + December: Boolean +}) +const hours = mongoose.Schema({ + h_0: Boolean, + h_1: Boolean, + h_2: Boolean, + h_3: Boolean, + h_4: Boolean, + h_4: Boolean, + h_5: Boolean, + h_6: Boolean, + h_7: Boolean, + h_8: Boolean, + h_9: Boolean, + h_10: Boolean, + h_11: Boolean, + h_12: Boolean, + h_13: Boolean, + h_14: Boolean, + h_15: Boolean, + h_16: Boolean, + h_17: Boolean, + h_18: Boolean, + h_19: Boolean, + h_20: Boolean, + h_21: Boolean, + h_22: Boolean, + h_23: Boolean +}) + +const fishSchema = new mongoose.Schema({ + id: Number, + bug_name: String, + price: Number, + img: String, + shadow_size: String, + hemisphere: [{ + north: [months], + south: [months] + }], + time: String, + time_available: [hours], + caught: [caughtSchema] // < one post has many likes +}); + + +module.exports = mongoose.model('Fish', fishSchema); \ No newline at end of file diff --git a/routes/api/fish.js b/routes/api/fish.js new file mode 100644 index 0000000..80f619e --- /dev/null +++ b/routes/api/fish.js @@ -0,0 +1,13 @@ +const express = require('express'); +const router = express.Router(); +const fishCtrl = require('../../controllers/fish'); + + +// /*---------- Public Routes ----------*/ + +router.get('/', fishCtrl.index) + + + + +module.exports = router; \ No newline at end of file diff --git a/server.js b/server.js index 35f47d2..7835b64 100644 --- a/server.js +++ b/server.js @@ -20,17 +20,18 @@ app.use(express.static(path.join(__dirname, 'build'))); // this allows express t // Configure the auth middleware // This decodes the jwt token, and assigns // the user information to req.user -app.use(require('./config/auth')); +app.use(require('./config/auth')); // api routes must be before the "catch all" route app.use('/api/users', require('./routes/api/users')); +app.use('/api/fish', require('./routes/api/fish')); // "catch all" route -app.get('/*', function(req, res) { +app.get('/*', function (req, res) { res.sendFile(path.join(__dirname, 'build', 'index.html')); }); const port = process.env.PORT || 3001; -app.listen(port, function() { +app.listen(port, function () { console.log(`Express app listening on port ${port}`); -}); +}); \ No newline at end of file diff --git a/src/Data/bugData.js b/src/Data/bugData.js index 28f6205..5bf4f2c 100644 --- a/src/Data/bugData.js +++ b/src/Data/bugData.js @@ -446,6 +446,8 @@ const BugData = [{ "h_23": true } }, + + //now { "id": 26, "bug_name": "Brown Cicada", diff --git a/src/pages/Bugs/Bugs.jsx b/src/pages/Bugs/Bugs.jsx index 6a97a8a..8c1e8b8 100644 --- a/src/pages/Bugs/Bugs.jsx +++ b/src/pages/Bugs/Bugs.jsx @@ -48,7 +48,7 @@ export default function Bug({ currentHemisphere }) { Name Price Location - Avaliable + Available diff --git a/src/pages/Fish/Fish.jsx b/src/pages/Fish/Fish.jsx index 5ad3d6d..a033272 100644 --- a/src/pages/Fish/Fish.jsx +++ b/src/pages/Fish/Fish.jsx @@ -4,23 +4,33 @@ import { Table, Container } from "semantic-ui-react"; import FishTable from '../../components/fish/fishtable' import Loading from '../../components/Loader/Loader' import fishData from '../../Data/fishData' +import * as fishAPI from "../../utils/fishAPI"; export default function Fish({ currentHemisphere }) { const [currentFish, setCurrentFish] = useState(); const [loading, setLoading] = useState(true); - async function getData() { + + async function getFish() { var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; const today = new Date(); const currentHour = `h_${today.getHours()}`; const currentMonth = months[today.getMonth()]; - console.log(currentMonth, "month") - let filterFish = await fishData.filter(function (i) { - return i.time_available[currentHour] === true && - i.hemisphere[currentHemisphere][currentMonth] === true; - }); - console.log(filterFish, "filteredfish") - setCurrentFish(filterFish); - setLoading(() => false); + try { + const data = await fishAPI.getAll(); + console.log(data.fish, " this is data,"); + let filterFish = await data.fish.filter(function (i) { + return i.time_available[currentHour] === true && + i.hemisphere[currentHemisphere][currentMonth] === true; + }); + console.log(filterFish, "database fishies") + setCurrentFish(filterFish); + setLoading(() => false); + // setPosts([...data.posts]); + // setLoading(false); + } catch (err) { + console.log(err.message, " this is the error"); + // setError(err.message); + } } console.log(currentFish, "fish page") // const allFish = currentFish.map((m, i) => ) @@ -28,7 +38,7 @@ export default function Fish({ currentHemisphere }) { } useEffect(() => { - getData(); + getFish(); }, [currentHemisphere]); if (loading) { @@ -48,7 +58,7 @@ export default function Fish({ currentHemisphere }) { Name Price Shadow Size - Avaliable + Available diff --git a/src/utils/fishAPI.js b/src/utils/fishAPI.js new file mode 100644 index 0000000..acccc0c --- /dev/null +++ b/src/utils/fishAPI.js @@ -0,0 +1,16 @@ +import tokenService from "./tokenService" + +const BASE_URL = '/api/fish/' + + +export function getAll() { + return fetch(BASE_URL, { + headers: { + 'Authorization': 'Bearer ' + tokenService.getToken() + } + }) + .then(res => { + if (res.ok) return res.json() + throw new Error('Problem Fetching Gel All') + }) +} \ No newline at end of file