jrpespinas / togo

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Togo

Golang application which accepts a todo task and records it if the user has not yet reached the limited number of tasks per day.

Usage

I. Run server

Make sure you have Docker installed, otherwise you may download it from this link.

If you already have Docker, simply follow the steps to deploy the code.

  1. Clone the repository
$ git clone git@github.com:jrpespinas/togo.git
  1. Change directory
$ cd togo
  1. Run docker compose to deploy the application
$ docker compose up

By now you must be able to see Docker running the containers after building the images.

NOTE: For the sake of this exam, I included the .env file in my commits for you to observe successful results when testing and making a simple post request to the application. However, it is bad practice to commit the environment variables to the code repository.

II. Sample Request

  1. You need to register a new user to create tasks.
$ curl -X POST http://localhost:8080/registration -H 'Content-Type: application/json' -d '{"email":"admin@gmail.com","password":"password"}'
  1. Login to start your session
$ curl -X POST http://localhost:8080/login -H 'Content-Type: application/json' -d '{"email":"admin@gmail.com","password":"password"}'

Finally, make a simple post request to create a task.

$ curl -X POST http://localhost:8080/tasks -H 'Content-Type: application/json' -d '{"title":"sample title","description":"sample description"}'

You should have received a response such as this:

{
  "status": "Success",
  "code": 200,
  "message": {
    "id": "cabhte81hrh6mgum9d7g",
    "title": "sample title",
    "description": "sample description",
    "created_at": "2022-06-01T08:09:29.0564148Z"
  }
}

III. Run sample test

In a separater terminal, head to service folder

$ cd ~/togo/server/service

Run the following command to test the code

$ go test -v

You will see the following output

=== RUN   TestValidateEmptyTask
--- PASS: TestValidateEmptyTask (0.00s)
=== RUN   TestValidateEmptyTaskTitle
--- PASS: TestValidateEmptyTaskTitle (0.00s)
=== RUN   TestCreateTask
    task_service_test.go:78: PASS:      CreateTask()
--- PASS: TestCreateTask (0.00s)
=== RUN   TestGetLimitNoToken
--- PASS: TestGetLimitNoToken (0.00s)
=== RUN   TestValidateRegistration
--- PASS: TestValidateRegistration (0.00s)
=== RUN   TestValidateLogin
--- PASS: TestValidateLogin (2.70s)
=== RUN   TestValidateLoginIncorrectPassword
--- PASS: TestValidateLoginIncorrectPassword (2.55s)
=== RUN   TestGenerateJWT
--- PASS: TestGenerateJWT (0.00s)
PASS
ok      togo/service    5.265s

Solution

Deployment: Docker

This approach deploys the Golang Backend via Docker. Subsequently, I am able to utilize docker compose to run a MongoDB container which resolves compatibility issues.

Design Pattern: Repository Pattern

This approach uses the Repository Pattern.

Repositories are classes or components that encapsulate the logic required to access data sources. They centralize common data access functionality, providing better maintainability and decoupling the infrastructure or technology used to access databases from the domain model layer.

- Microsoft Documentation

This pattern is usually made up of three main layers: Repository, Service, and Controller.

The Controller layer is responsible for handling the request and for returning the response.

The Service layer is responsible for the business logic. This is the layer in which you manipulate the data. In this case, this is where the id and the JWT token is generated. This is also where the validation of tasks and users occur.

The Repository layer is responsible for the data access or the interaction with the database.

The main benefit of this approach is to divide the application by layers to encourage long-term maintainability of the codebase. By abstracting each layer from the other, you will be able to easily test and/or to easily refactor the code. For example, since the Repository layer is abstracted from the Service layer, the Service layer does not have to worry which database I use--in this case, MongoDB.

Things to improve

  • Follow file structure best practices
  • Learn about proper logging
  • implement CRUD

Total Working Time: 22 hours

About

License:MIT License


Languages

Language:Go 97.9%Language:Dockerfile 1.6%Language:Shell 0.4%