Skip to content

CassivsGabriellis/crypto-vote-interface

Repository files navigation

Crypto Vote API

This is a simple RESTful API implementation for managing cryptocurrencies using MySQL as the database, by which allows users to perform various operations, such as fetching all the most well known cryptocurrencies available, getting a cryptocurrency by ID, creating a new cryptocurrency, upvoting or downvoting a cryptocurrency, and deleting a cryptocurrency.

Table of Contents

Components

Here's a brief summary of the main components in the code:

  • go.sum and go.mod: These files specify the dependencies of the project along with their versions.

  • main.go: This is the main entry point of the application. It sets up the server, initializes the database, and registers the API endpoints for handling different requests.

  • database.go: This file contains the logic to initialize the database connection. It uses the Go MySQL Driver package to connect to a MySQL database.

  • crypto_currency_model.go: This file defines the CryptoCurrency struct, which represents the structure of a cryptocurrency entry.

  • crypto_currency_service.go: This file contains the main business logic for handling various API requests related to cryptocurrencies. It implements the CRUD (Create, Read, Update, Delete) operations for cryptocurrencies and interacts with the database to perform these operations.

  • crypto_currency_service_test.go: This file contains unit tests for the CryptoCurrencyService methods. It uses the Go SQLmock package to mock the database and test the service's functionality.

Database Schema

The MySQL database schema for the Crypto Vote API is as follows:

Field        | Type         | Null | Key | Default          | Extra
-------------------------------------------------------------------------
id           | int          | NO   | PRI | NOT NULL         | auto_increment
name         | varchar(255) | NO   |     | NOT NULL         |
up_vote      | int          | YES  |     | 0                |
down_vote    | int          | YES  |     | 0                |
total_votes  | int          | YES  |     | 0                |

Endpoints specification

Below are the available endpoints and their functionalities:

Get All Crypto Currencies

  • Endpoint: GET /v1/cryptovote

  • Description: This endpoint returns a list of all registered cryptocurrencies along with their voting statistics.

  • Response: The response will be a JSON array containing objects representing each cryptocurrency and its properties (ID, name, up votes, down votes, and total votes).

Get Crypto Currency by ID

  • Endpoint: GET /v1/cryptovote/{id}

  • Description: This endpoint retrieves a specific cryptocurrency by its unique ID.

  • Response: The response will be a JSON object representing the cryptocurrency with the given ID, along with its properties (ID, name, up votes, down votes, and total votes).

Create Crypto Currency

  • Endpoint: POST /v1/cryptovote

  • Description: This endpoint allows you to create a new cryptocurrency entry in the database.

  • Request Body: The request should contain a JSON object representing the cryptocurrency to be created. The only required field is the name.

  • Response: The response will be a JSON object representing the newly created cryptocurrency, including its automatically assigned ID.

Up Vote Crypto Currency

  • Endpoint: PUT /v1/cryptovote/{id}/upvote

  • Description: This endpoint lets you cast an upvote for a specific cryptocurrency.

  • Response: The response will be a JSON object representing the cryptocurrency with the updated voting statistics after the upvote.

Down Vote Crypto Currency

  • Endpoint: PUT /v1/cryptovote/{id}/downvote

  • Description: This endpoint lets you cast a downvote for a specific cryptocurrency.

  • Response: The response will be a JSON object representing the cryptocurrency with the updated voting statistics after the downvote.

Delete Crypto Currency

  • Endpoint: DELETE /v1/cryptovote/{id}

  • Description: This endpoint allows you to delete a cryptocurrency from the database based on its ID.

  • Response: If the cryptocurrency is successfully deleted, the response will have a status code of 204 (No Content) with an empty body.

API Start and Usage

To start the Crypto Vote API, follow these steps:

  1. Make sure you have Go (Golang) and MySQL installed on your system.

  2. Clone the repository or download the source code.

  3. Navigate to the project directory.

  4. Install the project dependencies by running the following command:

go mod download
  1. Set up the MySQL database and configure the connection details in the database.go file.

  2. Run the following command to start the server:

go run .

The server will start running on http://localhost:8080.

To use these endpoints, you can make HTTP requests to the server hosting the crypto-vote application.

You can interact with it using a tool for testing APIs, such as Postman or cURL.

Here are some examples of using curl to interact with the endpoints:

  • Get All Crypto Currencies:
curl -X GET http://localhost:8080/v1/cryptovote
  • Get Crypto Currency by ID

Replace {id} with the desired cryptocurrency ID:

curl -X GET http://localhost:8080/v1/cryptovote/{id}
  • Create Crypto Currency

Replace {"name": "Bitcoin"} with the desired cryptocurrency data:

curl -X POST -H "Content-Type: application/json" -d '{"name": "Bitcoin"}' http://localhost:8080/v1/cryptovote
  • Up Vote Crypto Currency

Replace {id} with the desired cryptocurrency ID:

curl -X PUT http://localhost:8080/v1/cryptovote/{id}/upvote
  • Down Vote Crypto Currency

Replace {id} with the desired cryptocurrency ID:

curl -X PUT http://localhost:8080/v1/cryptovote/{id}/downvote
  • Delete Crypto Currency

Replace {id} with the desired cryptocurrency ID:

curl -X DELETE http://localhost:8080/v1/cryptovote/{id}

Remember to replace localhost:8080 with the actual address and port you want your server to run. Additionally, for endpoints that require a request body (e.g., creating a cryptocurrency), make sure to provide valid JSON data in the -d parameter.

About

An API with Golang using some database that exposes an Upvote service endpoints.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages