MojoForum - A Forum in Mojolicious (Part-1)
Tags: mojolicious, perl
Published: 2020-04-25 19:15:41

# MojoForum - Project in Mojolicious

![banner](https://user-images.githubusercontent.com/12954663/80288582-751d1980-8756-11ea-872f-1d852ef0e1d7.jpg)

I believe that the good way to learn any technology is through the Project. Project based learning helps us to learn the critical parameters of the technology and we also know how to use them on the production environment.  Therefore I decided to write a series of articles, where I write on how to develop a forum (MojoForum) with the help of the Mojolicous. Project repository is on the Github. I am developing the forum during the series and then check in the code to the Github.

As we all know, Mojolicious is a light-weight web framework written in Perl. It is simple yet remarkably powerful to write complex web applications. It requires a few modules to start developing web projects.

We all know, the forum is a place where the user can request a question and exchange the views with other users.  Without further delay, let's jump into the series.

Before starting the Project in Mojolicous, the following are the prerequisites for this tutorial:

1. Perl version 5.24 or greater installed on your local machine. On Unix or Mac Operating System, Perl is available through the default OS installation packages, if you are using the Windows machine, install Activestate Perl from [**here**](http://www.activestate.com/activeperl/downloads) or strawberry Perl from [here](http://strawberryperl.com/download/5.30.2.1/strawberry-perl-5.30.2.1-64bit.msi).

2. Elementary knowledge of Perl (Not Object Oriented) is essential. If you are not accustomed to basic Perl, then you are not ready for this tutorial. I would recommend acquiring fundamental knowledge of Perl first and continue the tutorial.

3. MySQL 5.0 onwards, must be installed on your system. If not, install it according to your flavored OS:
   1.  [Windows Installation](https://dev.mysql.com/downloads/windows/) Packages.
   2. [Debian/Ubuntu Unix](https://support.rackspace.com/how-to/installing-mysql-server-on-ubuntu/) Installation.
   3. [Redhat/CentOS/Fedora](https://www.digitalocean.com/community/tutorials/how-to-install-mysql-on-centos-7) Installation.
   4. [macOS](https://dev.mysql.com/doc/refman/5.6/en/osx-installation-pkg.html) Installation.

4. MySQL fundamental knowledge.

5. Basic knowledge of HTML, CSS, Tailwind CSS framework.

I am using **Visual Studio Code** with Tabnine plugin for developing the forums.

# **Installation of Mojolicious**

You are over here!!! You fulfilled all the prerequisites to get started with Mojolicious Application. Great!!!

Let's start with the Mojolicious Installation.

I already write an article for Mojolicous Installation. Please use this link to install [Mojolicious](https://ashutosh.dev/blog/post/2020/03/how-to-install-mojolicious).

# **Start Project in Mojolicious**

It's reasonably fair to assume that installation is successful. Let's generate the app.

```shell
mojo generate app mojoForum
```

In Mojolicious one line is enough to create the Project. Isn’t is easy?

# **Application Launch**

```shell
cd mojoForum
morbo script/mojoForum
```

If you you receive a message

```shell
Server available at http://127.0.0.1:3000
```

You are good to. Go to the browser and launch the application http://localhost:3000

## Database seeding

A Minimum Viable Product specification of a forum is:

A Database to store forum information and columns and in the database create following tables:
 - Threads
 - Replies
 - Users

Once we set up the database, we need routes to view the data on our web application.

Let's start by creating the database in MySQL first.
We want our forum application DB isolated from other applications therefore, we create a new database in MySQL.

If you have phpmyadmin/MySQL Workbench/SQL pro, you can use these tools to set up a database and it's tables. Otherwise, use the Command line terminal.

Sign in to MySQL database from command prompt:

```shell
mysql -u root -p
```

Enter the administrative password and you will be at MySQL Command prompt.

Create a new database by typing the following command:

```mysql
CREATE DATABASE MojoForum;
```

Anyways, you are not restricted to use this name. You are liable to select any name for your application database.

Select the database for sequential operations.

```mysql
USE MojoForum;
```

Select the database for successive operations.

Next step is to create Users, Threads and Replies table.

We can understand the above table structure as:

1. Users may have threads.
2. Users may add replies to threads.
3. Replies belongs to thread.

We also need to establish a relationship between:

1. Users and Threads table
2. Users and Replies table
3. Threads and Replies table

Create Users table.

```mysql
CREATE TABLE 'users' (
 'id' int(11) unsigned NOT NULL AUTO_INCREMENT,
 'first_name' varchar(25) NOT NULL DEFAULT '',
 'middle_name' varchar(25) DEFAULT NULL,
 'last_name' varchar(25) NOT NULL DEFAULT '',
 'email' varchar(50) DEFAULT NULL,
 'password' varchar(255) DEFAULT NULL,
 PRIMARY KEY ('id')
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
```

Create threads table.

```mysql
CREATE TABLE 'threads' (
  'id' int(11) unsigned NOT NULL AUTO_INCREMENT,
  'user_id' int(11) unsigned DEFAULT NULL,
  'title' varchar(50) DEFAULT NULL,
  'body' mediumtext,
  PRIMARY KEY ('id'),
  KEY 'user_id' ('user_id'),
  CONSTRAINT 'threads_ibfk_1' FOREIGN KEY ('user_id') REFERENCES 'users' ('id') ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
```

Create replies table

```mysql
CREATE TABLE 'replies' (
  'id' int(11) unsigned NOT NULL AUTO_INCREMENT,
  'user_id' int(11) unsigned DEFAULT NULL,
  'thread_id' int(11) unsigned DEFAULT NULL,
  'body' mediumtext,
  PRIMARY KEY ('id'),
  KEY 'user_id' ('user_id'),
  KEY 'thread_id' ('thread_id'),
  CONSTRAINT 'replies_ibfk_1' FOREIGN KEY ('user_id') REFERENCES 'users' ('id') ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT 'replies_ibfk_2' FOREIGN KEY ('thread_id') REFERENCES 'threads' ('id') ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
```

We have successfully created the database and tables, and columns. We also establish the relationship between users, thread and replies table. 

That's all for this article. In this article we learn,

1. To install Mojolicious
2. Install MySQL
3. Create Database
4. Create users, threads and replies table.

In the next article, we learn to seed the database and setup the scaffolding of our Forums application.

Stay tuned.