A code sharing platform built using spring boot, hibernate and JPA as ORM with PostgreSQL which also follows a RESTful architecture.
- Post Snippet and get a unique id
- Post Snippet with added restrictions
- Time restriction
- Views restriction
- or Both
- View top 10 latest unrestricted codes
- Api features
- Post
- View
- Delete
Before starting make sure gradle is downloaded. To get started with gradle click here.
After downloading the latest gradle distribution configure your PATH
environment variable to include the bin
directory of the unzipped distribution.
$ export PATH=$PATH:/opt/gradle/gradle-7.2/bin
In File Explorer right-click on the This PC
(or Computer
) icon, then click Properties
->
Advanced System Settings
-> Environmental Variables
.
Under System Variables
select Path, then click Edit
. Add an entry for C:\Gradle\gradle-7.2\bin
(make sure to unzip the content of the downloaded folder to C:\Gradle
). Click OK
to save.
To verify the installation open a console (or a Windows command prompt) and run gradle -v
to run gradle and display
the version, e.g.:
> gradle -v
------------------------------------------------------------
Gradle 7.2
------------------------------------------------------------
As gradle is already initialized, after downloading the project all that's left to do is build the project. To build the
project run the gradle build
command following the gradle clean
command.
> gradle clean
Starting a Gradle Daemon (subsequent builds will be faster)
BUILD SUCCESSFUL in 12s
1 actionable task: 1 executed
> gradle build
> Task :test
2021-08-19 13:56:06.991 INFO 12964 --- [ionShutdownHook] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2021-08-19 13:56:06.991 INFO 12964 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated...
2021-08-19 13:56:09.695 INFO 12964 --- [ionShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.
BUILD SUCCESSFUL in 1m 7s
7 actionable tasks: 7 executed
To run the app locally the following changes need to be made to the
application.properties
file:
# Set the database name as desired
spring.datasource.url=jdbc:postgresql://localhost:5432/snippet
spring.datasource.username=postgres
# Set the password as you desire
spring.datasource.password=Adnanrocks247
Now to set up the database in PostgreSQL
we first open the psql
shell (to install PostgreSQL, follow
this link). After opening the shell press Enter
four times and
when asked for the password, type in the password used whilst installing PostgreSQL
(refer to the previous link).
- Login to PostgreSQL
Server [localhost]:
Database [postgres]:
Port [5432]:
Username [postgres]:
Password for user postgres:
psql (13.4)
WARNING: Console code page (437) differs from Windows code page (1252)
8-bit characters might not work correctly. See psql reference
page "Notes for Windows users" for details.
Type "help" for help.
postgres=#
- Enter the command
CREATE DATABASE snippet;
- To make sure the desired database is created enter the
\l
command to view all the available databases
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+----------------------------+----------------------------+-----------------------
postgres | postgres | UTF8 | English_United States.1252 | English_United States.1252 |
snippet | postgres | UTF8 | English_United States.1252 | English_United States.1252 |
template0 | postgres | UTF8 | English_United States.1252 | English_United States.1252 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | English_United States.1252 | English_United States.1252 | =c/postgres +
| | | | | postgres=CTc/postgres
(4 rows)
- Now rebuild the project or simply use an IDE (preferably Intellij) to run the application and then to view the web
app use this link
http://localhost:8080/
.
To post a snippet open Postman
then open a Post
tab and paste the following link with the following end
points https://snap-snippet.herokuapp.com/api/code/new
.
Next in the body parameter type in the code with restrictions in the following JSON
format:
{
"code": "Secret code",
"time": 5000,
"views": 5
}
If the code is to be posted with no restrictions simply assign 0
to "time"
and "views"
:
{
"code": "Secret code",
"time": 0,
"views": 0
}
The reply should be in a json
format with a unique id:
{
"id": "983fca68-303c-4a52-b55e-87ac3c3b5fb2"
}
To get a snippet open Postman
then open a Get
tab and paste the following link with the following end points (with
the desired unique id) https://snap-snippet.herokuapp.com/api/code/983fca68-303c-4a52-b55e-87ac3c3b5fb2
.
The response would be in the following json
format:
{
"code": "Secret code",
"date": "2021/08/19 09:54:52",
"time": 3800,
"views": 4
}
To delete a snippet open Postman
then open a DEL
tab and paste the following link with the following end points (
with the desired unique id) https://snap-snippet.herokuapp.com/api/code/delete/983fca68-303c-4a52-b55e-87ac3c3b5fb2
.
The response will be a simple Deleted!
string message.