Skip to content

zardan4/petition-rest

Repository files navigation

GitHub Workflow Status (with event) GitHub go.mod Go version (subdirectory of monorepo) Go Report Card GitHub Repo stars Go Reference

REST for petitions

Handles requests for petitions, users and signatures. Can be used for writing small petitions interfaces
For this petitions branch use audit module: https://github.com/zardan4/petition-audit-rabbitmq

Enpoints. Requests/Responses

Auth

POST /signup. Create new user

CreateUser(user petitions.User) (int, error)

Request bodyad

{
    "name": "mark zuckerberg",
    "grade": "3",
    "password": "secretpassword123"
}

Response

{
    "id": newUserId
}

POST /signin. Sign in as old user

GenerateTokens(name, password, fingerprint string) (core.JWTPair, error)

Request body

{
    "name": "mark zuckerberg",
    "password": "secretpassword123",
    "fingerprint": "your_unique_device_fingerprint"
}

Response

{
    "access_token": usersJWT,
    "refresh_token": userRefreshToken
}

POST /refresh. Refresh user's tokens by refresh token. Delete previous refresh token

RefreshTokens(refreshToken, fingerprint string) (core.JWTPair, error)

Request body

{
    "fingerprint": "your_unique_device_fingerprint"
}

Cookie

{
    "refresh_token": "refresh_token_cookie"
}

Response

{
    "access_token": newUsersJWT,
    "refresh_token": newUserRefreshToken
}

POST /logout. Delete user's refresh session

Logout(refreshToken string) error

Cookie

{
    "refresh_token": "refresh_token_cookie"
}

Response

{
    "status": "ok"
}

Additional

  • Refresh session depends on fingerprint too so make unique refresh session from each user's device and don't use the same fingerprint(generate it here)
  • Follow this scheme to better understand how to use auth

Petitions. Only auth

GET /petitions. Get all petitions

GetAllPetitions() ([]petitions.Petition, error)

Response

{
    "data": [
        {
            "id": "id",
            "title": "title",
            "date": "date",
            "timeend": "timeend",
            "text": "text",
            "answer": "answer"
        }
    ]
}

POST /petitions. Create petition

CreatePetition(title, text string, authorId int) (int, error)

Request body

{
    "title": "title_example",
    "text": "text_example"
}

Response

{
    "id": "id"
}

GET /petitions/{id}. Get petition by id

GetPetition(petitionId int) (petitions.Petition, error)

Response

{
    "id": "id",
    "title": "title",
    "date": "date",
    "timeend": "timeend",
    "text": "text",
    "answer": "answer"
}

PUT /petitions/{id}. Update petition by id

UpdatePetition(petition petitions.UpdatePetitionInput, petitionId, userId int) error

Request body. Optional fields but at least one

{
    "id": "id",
    "title": "title",
    "date": "date",
    "timeend": "timeend",
    "text": "text",
    "answer": "answer"
}

Response

{
    "status": "ok"
}

DELETE /petitions/{id}. Delete petition by id

DeletePetition(petitionId, userId int) error

Response

{
    "status": "ok"
}

GET /petitions/{id}/signed. Get petition signed status by user

CheckSignatorie(petitionId, userId int) (bool, error)

Response

{
    "signed": bool
}

Signatures. Only auth

GET /petitions/{id}/subs. Get all signatures for petition

GetAllSubs(petitionId int) ([]petitions.Sub, error)

Response

{
    "data": [
        {
            "id": "id",
            "date": "date",
            "userId": "userId",
            "name": "username"
        }
    ]
}

POST /petitions/{id}/subs. Create signature for petition

CreateSub(petitionId, userId int) (int, error)

Request body

{}

Response

{
    "id": "signatureId"
}

DELETE /petitions/{id}/subs. Delete signature for petition by user

DeleteSub(subId, petitionId, userId int) error

Response

{
    "status": "ok"
}

Running:

Firstly, configure your .env

make run # run containers
make migrate # init tables
make swag # init swagger

TODO

  • docker-compose
  • unit tests
  • swagger
  • log out endpoint

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages