MarioAriasC / subscription-service

Application example with NestJS

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Subscription Service

Running

To run the application follow these steps:

  • Create a copy of the .envcopy file with the name .env
  • Change the values to your MySQL instance values (The user should have permissions to create tables). e.g;
DATABASE_USER=user
DATABASE_PASSWORD=p4ssw0rd
DATABASE_NAME=subscription
DATABASE_PORT=3306
DATABASE_HOST=localhost
JWT_SECRET=SUPER_SECRET_KEY
  • Run the command npm install
  • Run the command npm run start:dev

The application should be ready in your localhost port 3000

Using the application

The examples in this document will use curl, you can use any tool that you prefer to interact with the API

Listing all subscriptions

GET http://localhost:3000/subscription

curl -XGET 'localhost:3000/subscription'
[{"id":1,"name":"ZumCare","cost":10},{"id":2,"name":"ZumCare+","cost":20}]

Login

POST http://localhost:3000/auth/login

The application has 3 users that you can try:

Username Password
Mario pass123!
Andy pass123!
Camile pass123!

Use the username and password as body: {"username":"Mario", "password": "pass123!"}

curl -XPOST -H "Content-type: application/json" -d '{"username":"Mario", "password": "pass123!"}' 'localhost:3000/auth/login'

It will return an access token

{"access_token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6Ik1hcmlvIiwic3ViIjoxLCJpYXQiOjE2Njk2MDQ4NjgsImV4cCI6MTY2OTYwNTQ2OH0.-6ASD-kfCnr_OAnFF8McK-wqsqN-ZnKpuwRwH7yA--o"}

List user information

GET http://localhost:3000/users

Use the access token as authorisation bearer. The token contains the user id information, therefore doesn't need an additional parameter and add increased security

curl -XGET -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6Ik1hcmlvIiwic3ViIjoxLCJpYXQiOjE2Njk2MDQ4NjgsImV4cCI6MTY2OTYwNTQ2OH0.-6ASD-kfCnr_OAnFF8McK-wqsqN-ZnKpuwRwH7yA--o' -H "Content-type: application/json" 'localhost:3000/users'
{"id":1,"name":"Mario","subscription":null}

Purchase subscription

POST GET http://localhost:3000/users/purchase

Use the access token as authorisation bearer.
As the body use the subscription id {"subscription_id": 1}

curl -XPOST -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6Ik1hcmlvIiwic3ViIjoxLCJpYXQiOjE2Njk2MDU1NDcsImV4cCI6MTY2OTYwNjE0N30.xq4SMI4Ztz5Y8ZZwcpRdKHgpeMCEFFAOesCFrdkBOlk' -H "Content-type: application/json" -d '{"subscription_id":1}' 'localhost:3000/users/purchase'
{"id":1,"name":"Mario","subscription":{"id":1,"name":"ZumCare","cost":10}}

About

Application example with NestJS

License:Apache License 2.0


Languages

Language:TypeScript 96.4%Language:JavaScript 3.6%