- Generate error messages based on whether or not data was supplied in a form submit, checking
req.body
to do so. - Prevent duplicate data from being added into the database. Generate error messages if certain data submitted through a form already exists in the database.
- Fork/Clone this repo
- Read through this document to get familiar with the concepts
- Generate a new express app, making sure to install
pg
andknex
:npm install --save pg knex
- Install the dependencies
- Initialize
knex
within the root directory:
$ knex init
- Update the code in knexfile.js with:
module.exports = {
development: {
client: 'postgresql',
connection: 'postgresql://localhost/knex_people',
migrations: {
directory: __dirname + '/src/server/db/migrations'
},
}
};
- Create a new database:
$ createdb knex_people
- Create a new migrations file:
$ knex migrate:make create_people
- In your text editor, open up the file you generated within the migrations folder and create this table schema:
exports.up = (knex, Promise) => {
return knex.schema.createTable('people', (table) => {
table.increments();
table.string('username').unique().notNullable();
table.string('hobby')notNullable();
table.timestamp('created_at').defaultTo(knex.fn.now());
});
};
exports.down = (knex, Promise) => {
return knex.schema.dropTable('people');
};
- Apply the migration:
$ knex migrate:latest
Did this work? Find out!
When a user goes to the '/' route
they should be redirected to the '/people' GET route
all of the people from the database should be displayed on the people.html page in a table
When a user goes to the site's homepage
And clicks on the anchor tag "Add a Person"
The user will be taken to a page with a create person form
When the user fills out the form
And clicks "Create Person" as long as the form was filled out properly
the person should be inserted into the database and the user should be redirected to '/people'
Then a user should see the created person on the people people.html ('/people') page
And a success message should appear
When a user fills out a the create person form
And clicks "Create Person"
When any of the form fields are blank
Then the 'new' template should be re-rendered and relevant error messages should appear
(the error messages should be added into an error array so you only see the errors that currently apply)
When a user fills in the username field with a username that already exists
in the database
And clicks "Create Person"
Then the 'new' template should be re-rendered and an error message
should appear that read "Username is already taken"
- Add update and delete functionality
- Properly validate the update operations so that proper data isn't replaced with invalid data and so that error messages will appear when invalid data is submitted
- When data is updated or deleted, display success messages unique to each operation
- Validate that datatypes submitted through the form are the correct datatypes for that input field. For example: don't allow a number to be inserted as a username. Create error messages for those circumstances as well and re-render the page to display those error messages.