REST API for a simple marketplace.
PostgreSQL is used for persistence.
Entities: users, roles, a user–role mapping table, and advertisements.
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.
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.
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"
}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
}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
}
]Signing uses SigningMethodHS256.
- Register —
POST http://localhost:8080/register
{
"login": "third_new_new",
"password": "qwerty"
}- Login —
POST http://localhost:8080/login
{
"login": "third_new_new",
"password": "qwerty"
}Response:
{
"token": "<your-jwt-token>"
}- Create ad —
POST http://localhost:8080/add/advwith the token in theAuthorizationheader:
{
"title": "Example Ad",
"content": "This is an example ad.",
"image_path": "https://example.com/image.jpg",
"price": 12
}- List ads —
GET http://localhost:8080/show/advwith the sameAuthorizationheader.
