Categorygithub.com/emprius/emprius-app-backend
repositorypackage
0.0.0-20250101211409-263091dac0cd
Repository: https://github.com/emprius/emprius-app-backend.git
Documentation: pkg.go.dev

# Packages

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# README

Emprius App Backend

A RESTful API backend service for the Emprius tool sharing platform. This service provides endpoints for user management, tool sharing, and image handling.

Features

  • User authentication with JWT tokens
  • User profile management with avatar support
  • Tool management (add, edit, delete, search)
  • Image upload and retrieval
  • Location-based tool search
  • Community-based user organization

Prerequisites

  • Go 1.x
  • MongoDB
  • Docker (optional)

Installation

  1. Clone the repository
  2. Install dependencies:
go mod download
  1. Set up environment variables:
  • REGISTER_TOKEN: Token required for user registration
  • JWT_SECRET: Secret key for JWT token generation
  1. Run the server:
go run main.go

Or using Docker (for testing):

docker-compose up -d

API Documentation

The API uses JWT (JSON Web Token) for authentication. Most endpoints require a valid JWT token in the Authorization header:

Authorization: BEARER <your-jwt-token>

Public Endpoints

POST /register

Register a new user.

Request:

{
  "email": "[email protected]",
  "invitationToken": "required-token",
  "name": "Username",
  "community": "Community1",
  "location": {
    "latitude": 42202259,
    "longitude": 1815044
  },
  "password": "userpassword",
  "active": true
}

Response:

{
  "header": {
    "success": true
  },
  "data": {
    "token": "jwt-token",
    "expirity": "2024-01-01T00:00:00Z"
  }
}

POST /login

Authenticate user and get JWT token.

Request:

{
  "email": "[email protected]",
  "password": "userpassword"
}

Response:

{
  "header": {
    "success": true
  },
  "data": {
    "token": "jwt-token",
    "expirity": "2024-01-01T00:00:00Z"
  }
}

GET /info

Get general platform information.

Response:

{
  "header": {
    "success": true
  },
  "data": {
    "users": 100,
    "tools": 250,
    "categories": [
      {"id": 1, "name": "Category1"},
      {"id": 2, "name": "Category2"}
    ],
    "transports": [
      {"id": 1, "name": "Transport1"},
      {"id": 2, "name": "Transport2"}
    ]
  }
}

Protected Endpoints (Require Authentication)

GET /profile

Get user profile information.

Response:

{
  "header": {
    "success": true
  },
  "data": {
    "email": "[email protected]",
    "name": "Username",
    "community": "Community1",
    "location": {
      "latitude": 42202259,
      "longitude": 1815044
    },
    "active": true,
    "avatarHash": "image-hash"
  }
}

POST /profile

Update user profile.

Request:

{
  "name": "New Name",
  "community": "New Community",
  "location": {
    "latitude": 42202259,
    "longitude": 1815044
  },
  "avatar": "base64-encoded-image"
}

GET /users

List all users.

Response:

{
  "header": {
    "success": true
  },
  "data": {
    "users": [
      {
        "email": "[email protected]",
        "name": "User1",
        "community": "Community1"
      }
    ]
  }
}

POST /tools

Add a new tool.

Request:

{
  "id": 0,
  "title": "Tool Name",
  "description": "Tool Description",
  "mayBeFree": true,
  "askWithFee": false,
  "cost": 10,
  "images": [],
  "transportOptions": [1, 2],
  "category": 1,
  "location": {
    "latitude": 42202259,
    "longitude": 1815044
  },
  "estimatedValue": 20,
  "height": 30,
  "weight": 40
}

Response:

{
  "header": {
    "success": true
  },
  "data": {
    "id": 123456
  }
}

GET /tools

Get tools owned by the authenticated user.

Response:

{
  "header": {
    "success": true
  },
  "data": {
    "tools": [
      {
        "id": 123456,
        "title": "Tool Name",
        "description": "Tool Description",
        "mayBeFree": true,
        "askWithFee": false,
        "cost": 10,
        "images": [],
        "transportOptions": [1, 2],
        "category": 1,
        "location": {
          "latitude": 42202259,
          "longitude": 1815044
        },
        "estimatedValue": 20,
        "height": 30,
        "weight": 40
      }
    ]
  }
}

GET /tools/{id}

Get specific tool details.

PUT /tools/{id}

Update tool information.

Request:

{
  "description": "New Description",
  "cost": 20,
  "category": 2
}

DELETE /tools/{id}

Delete a tool.

GET /tools/search

Search for tools with filters.

Request:

{
  "categories": [1, 2],
  "maxCost": 100,
  "distance": 20000,
  "mayBeFree": true
}

Response:

{
  "header": {
    "success": true
  },
  "data": {
    "tools": [
      {
        "id": 123456,
        "title": "Tool Name",
        "description": "Tool Description",
        "cost": 10,
        "category": 1
      }
    ]
  }
}

POST /bookings

Create a new booking request.

Request:

{
  "toolId": "123456",
  "startDate": 1735734735,
  "endDate": 1735821135,
  "contact": "[email protected]",
  "comments": "I need this tool for a weekend project"
}

Response:

{
  "header": {
    "success": true
  },
  "data": {
    "id": "6773e44f06307bedd602fbd2",
    "toolId": "123456",
    "fromUserId": "user123",
    "toUserId": "owner456",
    "startDate": 1735734735,
    "endDate": 1735821135,
    "contact": "[email protected]",
    "comments": "I need this tool for a weekend project",
    "bookingStatus": "pending",
    "createdAt": "2024-01-01T00:00:00Z",
    "updatedAt": "2024-01-01T00:00:00Z"
  }
}

GET /bookings/requests

Get list of booking requests for tools you own.

Response:

{
  "header": {
    "success": true
  },
  "data": {
    "bookings": [
      {
        "id": "6773e44f06307bedd602fbd2",
        "toolId": "123456",
        "fromUserId": "user123",
        "toUserId": "owner456",
        "startDate": 1735734735,
        "endDate": 1735821135,
        "contact": "[email protected]",
        "comments": "I need this tool for a weekend project",
        "bookingStatus": "pending",
        "createdAt": "2024-01-01T00:00:00Z",
        "updatedAt": "2024-01-01T00:00:00Z"
      }
    ]
  }
}

GET /bookings/petitions

Get list of your booking requests.

Response:

{
  "header": {
    "success": true
  },
  "data": {
    "bookings": [
      {
        "id": "6773e44f06307bedd602fbd2",
        "toolId": "123456",
        "fromUserId": "user123",
        "toUserId": "owner456",
        "startDate": 1735734735,
        "endDate": 1735821135,
        "contact": "[email protected]",
        "comments": "I need this tool for a weekend project",
        "bookingStatus": "pending",
        "createdAt": "2024-01-01T00:00:00Z",
        "updatedAt": "2024-01-01T00:00:00Z"
      }
    ]
  }
}

GET /bookings/{bookingId}

Get specific booking details.

Response:

{
  "header": {
    "success": true
  },
  "data": {
    "id": "6773e44f06307bedd602fbd2",
    "toolId": "123456",
    "fromUserId": "user123",
    "toUserId": "owner456",
    "startDate": 1735734735,
    "endDate": 1735821135,
    "contact": "[email protected]",
    "comments": "I need this tool for a weekend project",
    "bookingStatus": "pending",
    "createdAt": "2024-01-01T00:00:00Z",
    "updatedAt": "2024-01-01T00:00:00Z"
  }
}

POST /bookings/{bookingId}/return

Mark a tool as returned (tool owner only).

Response:

{
  "header": {
    "success": true
  }
}

GET /bookings/rates

Get list of pending ratings.

Response:

{
  "header": {
    "success": true
  },
  "data": {
    "ratings": [
      {
        "id": "6773e44f06307bedd602fbd2",
        "bookingId": "6773e44f06307bedd602fbd2",
        "fromUserId": "user123",
        "toUserId": "owner456",
        "isPending": true,
        "ratingType": "tool"
      }
    ]
  }
}

POST /bookings/rates

Submit a rating for a booking.

Request:

{
  "bookingId": "6773e44f06307bedd602fbd2",
  "rating": 5
}

Response:

{
  "header": {
    "success": true
  }
}

POST /images

Upload an image.

Request:

{
  "name": "image-name",
  "content": "base64-encoded-image"
}

Response:

{
  "header": {
    "success": true
  },
  "data": {
    "hash": "image-hash"
  }
}

GET /images/{hash}

Get an image by its hash.

Error Responses

All endpoints return errors in the following format:

{
  "header": {
    "success": false,
    "message": "Error description",
    "errorCode": 123
  }
}

Common error messages:

Authentication & General:

  • Invalid register auth token
  • Invalid request body data
  • Could not insert to database
  • Wrong password or email
  • Invalid JSON body

Resources:

  • Invalid hash
  • Image not found
  • Tool not found

Bookings:

  • Booking dates conflict with existing booking
  • Unauthorized booking operation
  • Invalid booking dates
  • Booking not found
  • Only tool owner can mark as returned
  • Booking already marked as returned
  • Invalid rating value
  • Booking already rated

Development

Running Tests

go test ./...

API Testing Script

A test script (test.sh) is provided to demonstrate API usage. Run it with:

./test.sh

License

This project is licensed under the terms of the LICENSE file included in the repository.