GithubHelp home page GithubHelp logo

sdkevorkian / cruddy-board-games-db Goto Github PK

View Code? Open in Web Editor NEW

This project forked from maria-christina-sevilla/cruddy-board-games-db

0.0 1.0 0.0 424 KB

License: Other

JavaScript 76.99% CSS 3.17% HTML 19.84%

cruddy-board-games-db's Introduction

Cruddy Board Games Database

This will expand on the Cruddy Board Games project. You can copy your solution into this new repository, or you can copy this completed solution and begin from there.

We will be replacing the games.json "database" with an actual database. We will use PostgreSQL and Sequelize.

screenshot

Full RESTful Routing (for reference)

Verb Path Action Used for
GET /games index display a list of all games
GET /games/new new return an HTML form for creating a new game
POST /games create create a new game (using form data from /games/new)
GET /games/:name show display a specific game
GET /games/:name/edit edit return an HTML form for editing a game
PUT /games/:name update update a specific game (using form data from /games/:name/edit)
DELETE /games/:name destroy deletes a specific game

Task

Set up sequelize

Refer to sequelize cheatsheet

  1. Install the modules we need.

    • npm install --save pg pg-hstore sequelize
  2. Create the database.

    • createdb cruddy_board_games_db
  3. Initialize.

  • sequelize init
  1. Configure your config.json file.

Create table and model

  1. Use the sequelize command line tool to create a game table in our database.
  • sequelize model:create --name game --attributes ...
  • What columns should be on game?
  1. Run the migrations.
  • sequelize db:migrate
  1. On the top of app.js file, require the models
  • var db = require("./models");
  1. Delete games.json, getGames(), and saveGames(). Replace calls to those functions with appropriate usages of db.game.

Example, for the game indexing route:

app.get('/games', function(req, res) {
    var games = getGames();

    res.render('games-index', { games: games });
});

turns into

app.get('/games', function(req, res) {
    db.game.findAll().then(function(games) {
        res.render('games-index', { games: games });
    }).catch(function(error) {
        res.status(404).send(error);
    });
});

Add a column

Add numberOfPlayers to the game model.

sequelize migration:create --name add-number-of-players-to-game

Refer to Sequelize migration documentation.

Edit the new migration file migrations/xxxxxxxxxxx-add-number-of-players-to-game.js.

  1. In the up function, use addColumn
  2. In the down function, use removeColumn
  3. Migrate! sequelize db:migrate
  4. Update the model file models/game.js. Add the property: numberOfPlayers: DataTypes.INTEGER

Make sure you also update the show, new game, and edit game pages to use the new numberOfPlayers property.

Validations

Add some validations to our data. For example: num players should be at least 1. Game name should be at least X characters long, etc ...

Refer to the git note book and sequelize's documentation.

Licensing

All content is licensed under a CC­BY­NC­SA 4.0 license. All software code is licensed under GNU GPLv3. For commercial use or alternative licensing, please contact [email protected].

cruddy-board-games-db's People

Contributors

connorjclark avatar sdkevorkian avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.