Skip to content

ricmtz/ApiTriviaCrack

Repository files navigation

ApiTriviaCrack

Table of contents

Description

ApiTriviaCrack is a web-app where users answer questions from multiple programming topics, competing against other users in duels.

Modules

  • Users: Represents a player or admin, who has a unique nickname and a global score, can play games and submit questions.
    • Friends: Users can add friends to easily start duels.
    • Emails: Users can have multiple emails besides a main one.
  • Games: Based on duels between friends or random players, each game consists of 10 randomly selected questions per player (not necessarily the same questions).
    • GamesQuestions: Each of the questions is assigned to a player and it stores the answer and whether or not it is correct.
  • Questions: Each question can have three options, only one being the correct one. Questions are related to the user that submitted it, and whether or not it has been approved.
    • Categories: Questions are selected based on the category they fall on.

Installation

Dependencies:

$ sudo apt-get install git-core
$ curl https://raw.githubusercontent.com/creationix/nvm/v0.30.2/install.sh | bash
$ nvm install node

Get the sources:

$ git clone https://github.com/ricmtz/ApiTriviaCrack.git

Install node dependencies:

$ npm install

Setting up the .env file

To run this app is needed a .env file, this file must have the following structure:

DB_HOST=localhost
DB_NAME=namedbapi
DB_USER=userdbapi
DB_PORT=7777
DB_PASSWORD=sampledbpassword
PORT=3000
SESSION_TIME=5
SALT_ROUNDS=15
SECRET=sampleSecret
MAIL_HOST=smtp.ethereal.email
MAIL_PORT=587
MAIL_USER=[email protected]
MAIL_PASS=samplepasswordetheral

Run the app:

$ node index.js

Data base creation

This application has a data base implementation based on PostgreSQL. The following scripts describe the estructure of all the data base tables.

Users table.

CREATE TABLE users
(
    id SERIAL,
    nickname TEXT NOT NULL,
    password TEXT NOT NULL,
    email TEXT NOT NULL,
    score INTEGER DEFAULT 0,
    avatar TEXT DEFAULT ''::TEXT,
    lastlogin TIMESTAMP,
    verified BOOLEAN DEFAULT false,
    deleted BOOLEAN DEFAULT false,
    admin BOOLEAN DEFAULT false,
    CONSTRAINT users_pkey PRIMARY KEY (id),
    CONSTRAINT users_nickname_key UNIQUE (nickname),
    CONSTRAINT users_principal_email_key UNIQUE (email)
);

Emails table.

CREATE TABLE emails
(
    id SERIAL,
    userid INTEGER,
    email TEXT,
    deleted BOOLEAN DEFAULT false,
    CONSTRAINT emails_email_key UNIQUE (email),
    CONSTRAINT emails_id_user_fkey FOREIGN KEY (userid)
        REFERENCES users (id) MATCH SIMPLE
        ON UPDATE NO ACTION
        ON DELETE NO ACTION
);

Friends table.

CREATE TABLE friends
(
    id SERIAL,
    user1 INTEGER NOT NULL,
    user2 INTEGER NOT NULL,
    friendshipdate TIMESTAMP NOT NULL,
    deleted BOOLEAN DEFAULT false,
    CONSTRAINT friends_pkey PRIMARY KEY (id),
    CONSTRAINT friends_id_user_1_fkey FOREIGN KEY (user1)
        REFERENCES users (id) MATCH SIMPLE
        ON UPDATE NO ACTION
        ON DELETE NO ACTION,
    CONSTRAINT friends_id_user_2_fkey FOREIGN KEY (user2)
        REFERENCES users (id) MATCH SIMPLE
        ON UPDATE NO ACTION
        ON DELETE NO ACTION
);

Games table.

CREATE TABLE games
(
    id SERIAL,
    player1 INTEGER,
    player2 INTEGER,
    scoreplayer1 INTEGER DEFAULT '-1'::INTEGER,
    scoreplayer2 INTEGER DEFAULT '-1'::INTEGER,
    createdate TIMESTAMP NOT NULL,
    finished BOOLEAN DEFAULT false,
    deleted BOOLEAN DEFAULT false,
    CONSTRAINT games_pkey PRIMARY KEY (id),
    CONSTRAINT games_player_1_fkey FOREIGN KEY (player1)
        REFERENCES users (id) MATCH SIMPLE
        ON UPDATE NO ACTION
        ON DELETE NO ACTION,
    CONSTRAINT games_player_2_fkey FOREIGN KEY (player2)
        REFERENCES users (id) MATCH SIMPLE
        ON UPDATE NO ACTION
        ON DELETE NO ACTION
);

Categories table.

CREATE TABLE categories
(
    id SERIAL,
    name TEXT NOT NULL,
    color TEXT NOT NULL,
    icon TEXT DEFAULT ''::TEXT,
    deleted BOOLEAN DEFAULT false,
    CONSTRAINT categories_pkey PRIMARY KEY (id)
);

Questions table.

CREATE TABLE questions
(
    id SERIAL,
    category INTEGER,
    question TEXT NOT NULL,
    option1 TEXT NOT NULL,
    option2 TEXT NOT NULL,
    optioncorrect TEXT NOT NULL,
    approved BOOLEAN DEFAULT false,
    deleted BOOLEAN DEFAULT false,
    userid INTEGER,
    createdate TIMESTAMP NOT NULL,
    approveddate TIMESTAMP,
    CONSTRAINT questions_pkey PRIMARY KEY (id),
    CONSTRAINT questions_category_fkey FOREIGN KEY (category)
        REFERENCES categories (id) MATCH SIMPLE
        ON UPDATE NO ACTION
        ON DELETE NO ACTION,
    CONSTRAINT questions_userid_fkey FOREIGN KEY (userid)
        REFERENCES users (id) MATCH SIMPLE
        ON UPDATE NO ACTION
        ON DELETE NO ACTION
);

Answers table.

CREATE TABLE answers
(
    id SERIAL,
    game INTEGER NOT NULL,
    question INTEGER NOT NULL,
    player INTEGER NOT NULL,
    option TEXT,
    correct BOOLEAN,
    deleted BOOLEAN DEFAULT false,
    CONSTRAINT answers_pkey PRIMARY KEY (id),
    CONSTRAINT answers_game_fkey FOREIGN KEY (game)
        REFERENCES games (id) MATCH SIMPLE
        ON UPDATE NO ACTION
        ON DELETE NO ACTION,
    CONSTRAINT answers_player_fkey FOREIGN KEY (player)
        REFERENCES users (id) MATCH SIMPLE
        ON UPDATE NO ACTION
        ON DELETE NO ACTION,
    CONSTRAINT answers_question_fkey FOREIGN KEY (question)
        REFERENCES questions (id) MATCH SIMPLE
        ON UPDATE NO ACTION
        ON DELETE NO ACTION
);

Tokens table.

CREATE TABLE tokens
(
    id SERIAL,
    token TEXT NOT NULL,
    createdat TIMESTAMP NOT NULL,
    expires TIMESTAMP NOT NULL,
    type CHARACTER(1) NOT NULL,
    status CHARACTER(1) NOT NULL,
    userid INTEGER NOT NULL,
    CONSTRAINT tokens_pkey PRIMARY KEY (id),
    CONSTRAINT tokens_userid_fkey FOREIGN KEY (userid)
        REFERENCES users (id) MATCH SIMPLE
        ON UPDATE NO ACTION
        ON DELETE NO ACTION
);

Demo URL

Run in Postman

App URL

Collaborators

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •