Skip to content

Commit

Permalink
using database instead of local file for fish
Browse files Browse the repository at this point in the history
  • Loading branch information
Trishadring committed Mar 7, 2022
1 parent 3c9a272 commit 6c77905
Show file tree
Hide file tree
Showing 10 changed files with 274 additions and 16 deletions.
32 changes: 32 additions & 0 deletions controllers/bugs.js
Original file line number Diff line number Diff line change
@@ -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
});
}
}
35 changes: 35 additions & 0 deletions controllers/fish.js
Original file line number Diff line number Diff line change
@@ -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
});
}
}
80 changes: 80 additions & 0 deletions models/bugs.js
Original file line number Diff line number Diff line change
@@ -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);
69 changes: 69 additions & 0 deletions models/fish.js
Original file line number Diff line number Diff line change
@@ -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);
13 changes: 13 additions & 0 deletions routes/api/fish.js
Original file line number Diff line number Diff line change
@@ -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;
9 changes: 5 additions & 4 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
});
});
2 changes: 2 additions & 0 deletions src/Data/bugData.js
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,8 @@ const BugData = [{
"h_23": true
}
},

//now
{
"id": 26,
"bug_name": "Brown Cicada",
Expand Down
2 changes: 1 addition & 1 deletion src/pages/Bugs/Bugs.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default function Bug({ currentHemisphere }) {
<Table.HeaderCell>Name</Table.HeaderCell>
<Table.HeaderCell className="mobile hidden">Price</Table.HeaderCell>
<Table.HeaderCell>Location</Table.HeaderCell>
<Table.HeaderCell className="mobile hidden">Avaliable</Table.HeaderCell>
<Table.HeaderCell className="mobile hidden">Available</Table.HeaderCell>
</Table.Row>
</Table.Header>
<Table.Body>
Expand Down
32 changes: 21 additions & 11 deletions src/pages/Fish/Fish.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,41 @@ 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) => <FishTable key={m.id} {...m} />)
function addCaught() {

}
useEffect(() => {
getData();
getFish();
}, [currentHemisphere]);

if (loading) {
Expand All @@ -48,7 +58,7 @@ export default function Fish({ currentHemisphere }) {
<Table.HeaderCell>Name</Table.HeaderCell>
<Table.HeaderCell className="mobile hidden">Price</Table.HeaderCell>
<Table.HeaderCell className="mobile hidden">Shadow Size</Table.HeaderCell>
<Table.HeaderCell className="mobile hidden">Avaliable</Table.HeaderCell>
<Table.HeaderCell className="mobile hidden">Available</Table.HeaderCell>
</Table.Row>
</Table.Header>
<Table.Body>
Expand Down
16 changes: 16 additions & 0 deletions src/utils/fishAPI.js
Original file line number Diff line number Diff line change
@@ -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')
})
}

0 comments on commit 6c77905

Please sign in to comment.