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

add Attraction model/migration #2

Merged
merged 3 commits into from
May 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ Desktop.ini
.env

# project-specific
/logo
/logo
2 changes: 2 additions & 0 deletions client/src/components/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import "../assets/scss/main.scss";
import RegistrationForm from "./registration/RegistrationForm";
import SignInForm from "./authentication/SignInForm";
import TopBar from "./layout/TopBar";
import AttractionsList from "./AttractionsList";

const App = (props) => {
const [currentUser, setCurrentUser] = useState(undefined);
Expand All @@ -32,6 +33,7 @@ const App = (props) => {
</Route>
<Route exact path="/users/new" component={RegistrationForm} />
<Route exact path="/user-sessions/new" component={SignInForm} />
<Route exact path="/attractions" component={AttractionsList} />
</Switch>
</Router>
);
Expand Down
11 changes: 11 additions & 0 deletions client/src/components/AttractionTile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React from "react";

const AttractionTile = ({ name }) => {
return (
<div>
<h3> {name} </h3>
</div>
);
};

export default AttractionTile;
37 changes: 37 additions & 0 deletions client/src/components/AttractionsList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import React, { useState, useEffect } from "react";
import AttractionTile from "./AttractionTile";

const AttractionsList = () => {
const [attractions, setAttractions] = useState([]);

const getAttractions = async () => {
try {
const response = await fetch("/api/v1/attractions");
if (!response.ok) {
const errorMessage = `${response.status} (${response.statusText})`;
const error = new Error(errorMessage);
throw error;
}
const parsedResponse = await response.json();
setAttractions(parsedResponse.attractions);
} catch (err) {
console.err(`Error in fetch: ${err.message}`);
}
};

useEffect(() => {
getAttractions();
}, []);

const attractionTileComponents = attractions.map((attractionObject) => {
return <AttractionTile key={attractionObject.id} {...attractionObject} />;
});

return (
<div className="callout">
Attractions
{attractionTileComponents}
</div>
);
};
export default AttractionsList;
3 changes: 3 additions & 0 deletions server/src/db/Seeder.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
/* eslint-disable no-console */
import { connection } from "../boot.js"
import AttractionSeeder from "./seeders/AttractionSeeder.js"

class Seeder {
static async seed() {
// include individual seed commands here
console.log("seeding attractions...");
await AttractionSeeder.seed();

console.log("Done!")
await connection.destroy()
Expand Down
23 changes: 23 additions & 0 deletions server/src/db/migrations/20220503141649_createAttractions.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @typedef {import("knex")} Knex
*/

/**
* @param {Knex} knex
*/
exports.up = async (knex) => {
return knex.schema.createTable("attractions", (table) => {
table.bigIncrements("id")
table.string("name").notNullable()
table.text("description")
table.timestamp("createdAt").notNullable().defaultTo(knex.fn.now())
table.timestamp("updatedAt").notNullable().defaultTo(knex.fn.now())
})
}

/**
* @param {Knex} knex
*/
exports.down = (knex) => {
return knex.schema.dropTableIfExists("attractions")
}
28 changes: 28 additions & 0 deletions server/src/db/seeders/AttractionSeeder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Attraction } from "../../models/index.js";

class AttractionSeeder {
static async seed() {
const attractionsData = [
{
name: "Baha'i Gardens",
description: "A very beautiful garden by the ocean."
},
{
name: "Jean's House",
description: "Home to million dollar apps and the world's greatest Overwatch player."
},
{
name: "Launch Academy",
description: "Jean-Free Zone"
}
]
for(const singleAttractionData of attractionsData) {
const currentAttraction = await Attraction.query().findOne(singleAttractionData);
if(!currentAttraction) {
await Attraction.query().insert(singleAttractionData);
}
}
}
}

export default AttractionSeeder;
20 changes: 20 additions & 0 deletions server/src/models/Attraction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const Model = require("./Model");

class Attraction extends Model {
static get tableName() {
return "attractions";
}

static get jsonSchema() {
return {
type: "object",
required: ["name"],
properties: {
name: { type: "string", minLength: 1 },
description: { type: "string" },
},
};
}
}

module.exports = Attraction;
3 changes: 2 additions & 1 deletion server/src/models/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// include all of your models here using CommonJS requires
const User = require("./User.js")
const Attraction = require("./Attraction.js");

module.exports = {User};
module.exports = { Attraction, User };
15 changes: 15 additions & 0 deletions server/src/routes/api/v1/attractionsRouter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import express from "express";
import { Attraction } from "../../../models/index.js";

const attractionsRouter = new express.Router();

attractionsRouter.get("/", async (req, res) => {
try {
const attractions = await Attraction.query();
return res.status(200).json({ attractions });
} catch (error) {
return res.status(500).json({ error });
}
});

export default attractionsRouter;
2 changes: 1 addition & 1 deletion server/src/routes/clientRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import getClientIndexPath from "../config/getClientIndexPath.js";

const router = new express.Router();

const clientRoutes = ["/", "/user-sessions/new", "/users/new"];
const clientRoutes = ["/", "/user-sessions/new", "/users/new", "/attractions"];
router.get(clientRoutes, (req, res) => {
res.sendFile(getClientIndexPath());
});
Expand Down
4 changes: 3 additions & 1 deletion server/src/routes/rootRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import express from "express";
import userSessionsRouter from "./api/v1/userSessionsRouter.js";
import usersRouter from "./api/v1/usersRouter.js";
import clientRouter from "./clientRouter.js";
import attractionsRouter from './api/v1/attractionsRouter.js'
const rootRouter = new express.Router();
rootRouter.use("/", clientRouter);

rootRouter.use("/api/v1/user-sessions", userSessionsRouter);
rootRouter.use("/api/v1/users/sessions", userSessionsRouter);
rootRouter.use("/api/v1/users", usersRouter); //place your server-side routes here
rootRouter.use("/api/v1/attractions", attractionsRouter)

export default rootRouter;