Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

marketplace_rest

REST API for a simple marketplace.

Database

PostgreSQL is used for persistence.

Database schema

Entities: users, roles, a user–role mapping table, and advertisements.

Architecture

The project follows an MVC-style layout: services in service, models in model, controllers in controller. HTTP routes are registered with http.HandleFunc in main.go.

Login

The client sends JSON in the request body:

{
    "login": "Misha",
    "password": "qwerty"
}

Passwords and logins are stored as provided. On successful login a JWT is issued (see jwt) and returned as JSON:

{
    "token": "<your-jwt-token>"
}

If the user is not registered, the API returns an error.

Registration

If the user already exists, the same user is returned in the response; the database is unchanged.

For a new user: insert into users, ensure the USER role exists in role (create it if missing), then add a row to user_x_roles linking the user and role. The mapping table supports users with multiple roles. The response returns the created user.

A transaction wraps inserts into role and user_x_roles so failures roll back.

The JWT is not returned on registration; the client should call login afterward to obtain a token.

Request:

{
    "login": "third_new_new",
    "password": "qwerty"
}

Response:

{
    "id": "e69a4197-a0c2-48ed-9f77-445a29d31bb4",
    "login": "third_new_new",
    "password": "qwerty"
}

Creating advertisements

The token is read from the request headers. If it is valid, the ad is created.

Field validation uses limits defined in constant.

The response returns the created advertisement.

Request body:

{
    "title": "Example Ad",
    "content": "This is an example ad.",
    "image_path": "https://example.com/image.jpg",
    "price": 12
}

Header:

Authorization: <your-jwt-token>

Response:

{
    "title": "Example Ad",
    "content": "This is an example ad.",
    "image_path": "https://example.com/image.jpg",
    "price": 12
}

Listing advertisements

Ads are loaded, then (if the user is authenticated) the user id is resolved. Results are ordered by date ascending.

Header:

Authorization: <your-jwt-token>

Response:

[
    {
        "Adv": {
            "title": "Example Ad",
            "content": "This is an example ad.",
            "image_path": "https://example.com/image.jpg",
            "price": 12
        },
        "IsUserAdv": false
    },
    {
        "Adv": {
            "title": "Example ad another",
            "content": "This is an example ad.",
            "image_path": "https://example.com/img.jpg",
            "price": 14
        },
        "IsUserAdv": true
    }
]

JWT

Signing uses SigningMethodHS256.

Quick start

  1. RegisterPOST http://localhost:8080/register
{
    "login": "third_new_new",
    "password": "qwerty"
}
  1. LoginPOST http://localhost:8080/login
{
    "login": "third_new_new",
    "password": "qwerty"
}

Response:

{
    "token": "<your-jwt-token>"
}
  1. Create adPOST http://localhost:8080/add/adv with the token in the Authorization header:
{
    "title": "Example Ad",
    "content": "This is an example ad.",
    "image_path": "https://example.com/image.jpg",
    "price": 12
}
  1. List adsGET http://localhost:8080/show/adv with the same Authorization header.

About

REST-API for marketplace

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages