api

package
v0.0.0-...-e158c48 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 3, 2025 License: AGPL-3.0 Imports: 29 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidRegisterAuthToken = fmt.Errorf("invalid register auth token")
	ErrInvalidRequestBodyData   = fmt.Errorf("invalid request body data")
	ErrCouldNotInsertToDatabase = fmt.Errorf("could not insert to database")
	ErrWrongLogin               = fmt.Errorf("wrong password or email")
	ErrInvalidHash              = fmt.Errorf("invalid hash")
	ErrImageNotFound            = fmt.Errorf("image not found")
	ErrInvalidImageFormat       = fmt.Errorf("invalid image format")
	ErrInvalidJSON              = fmt.Errorf("invalid json body")
	ErrBookingDatesConflict     = fmt.Errorf("booking dates conflict with existing booking")
	ErrToolNotFound             = fmt.Errorf("tool not found")
	ErrUnauthorizedBooking      = fmt.Errorf("unauthorized booking operation")
	ErrInvalidBookingDates      = fmt.Errorf("invalid booking dates")
	ErrBookingNotFound          = fmt.Errorf("booking not found")
	ErrOnlyOwnerCanReturn       = fmt.Errorf("only tool owner can mark as returned")
	ErrBookingAlreadyReturned   = fmt.Errorf("booking already marked as returned")
	ErrInvalidRating            = fmt.Errorf("invalid rating value")
	ErrBookingAlreadyRated      = fmt.Errorf("booking already rated")
)

Functions

This section is empty.

Types

type API

type API struct {
	Router *chi.Mux
	// contains filtered or unexported fields
}

API type represents the API HTTP server with JWT authentication capabilities.

func New

func New(secret, registerAuthToken string, database *db.Database) *API

New creates a new API HTTP server. It does not start the server. Use Start() for that.

func (*API) EnablePrometheusMetrics

func (a *API) EnablePrometheusMetrics(prometheusID string)

EnablePrometheusMetrics enables go-chi prometheus metrics under specified ID. If ID empty, the default "gochi_http" is used.

func (*API) HandleCreateBooking

func (a *API) HandleCreateBooking(r *Request) (interface{}, error)

HandleCreateBooking handles POST /bookings

func (*API) HandleGetBooking

func (a *API) HandleGetBooking(r *Request) (interface{}, error)

HandleGetBooking handles GET /bookings/{bookingId}

func (*API) HandleGetBookingPetitions

func (a *API) HandleGetBookingPetitions(r *Request) (interface{}, error)

HandleGetBookingPetitions handles GET /bookings/petitions

func (*API) HandleGetBookingRequests

func (a *API) HandleGetBookingRequests(r *Request) (interface{}, error)

HandleGetBookingRequests handles GET /bookings/requests

func (*API) HandleGetPendingRatings

func (a *API) HandleGetPendingRatings(r *Request) (interface{}, error)

HandleGetPendingRatings handles GET /bookings/rates

func (*API) HandleRateBooking

func (a *API) HandleRateBooking(r *Request) (interface{}, error)

HandleRateBooking handles POST /bookings/rates

func (*API) HandleReturnBooking

func (a *API) HandleReturnBooking(r *Request) (interface{}, error)

HandleReturnBooking handles POST /bookings/{bookingId}/return

func (*API) Start

func (a *API) Start(host string, port int)

Start starts the API HTTP server (non blocking).

type BookingResponse

type BookingResponse struct {
	ID            string    `json:"id"`
	ToolID        string    `json:"toolId"`
	FromUserID    string    `json:"fromUserId"`
	ToUserID      string    `json:"toUserId"`
	StartDate     int64     `json:"startDate"`
	EndDate       int64     `json:"endDate"`
	Contact       string    `json:"contact"`
	Comments      string    `json:"comments"`
	BookingStatus string    `json:"bookingStatus"`
	CreatedAt     time.Time `json:"createdAt"`
	UpdatedAt     time.Time `json:"updatedAt"`
}

BookingResponse represents the API response for a booking

type CreateBookingRequest

type CreateBookingRequest struct {
	ToolID    string `json:"toolId"`
	StartDate int64  `json:"startDate"`
	EndDate   int64  `json:"endDate"`
	Contact   string `json:"contact"`
	Comments  string `json:"comments"`
}

CreateBookingRequest represents the request to create a new booking

type HTTPContext

type HTTPContext struct {
	Writer  http.ResponseWriter
	Request *http.Request
}

HTTPContext is the Context for an HTTP request.

func (*HTTPContext) Send

func (h *HTTPContext) Send(msg []byte, httpStatusCode int) error

Send replies the request with the provided message.

func (*HTTPContext) URLParam

func (h *HTTPContext) URLParam(key string) string

URLParam is a wrapper around go-chi to get a URL parameter (specified in the path pattern as {key})

type HTTPError

type HTTPError struct {
	Code    int
	Message string
}

HTTPError represents an error with an HTTP status code

func (*HTTPError) Error

func (e *HTTPError) Error() string

type Info

type Info struct {
	Users      int               `json:"users"`
	Tools      int               `json:"tools"`
	Categories []db.ToolCategory `json:"categories"`
	Transports []db.Transport    `json:"transports"`
}

type Login

type Login struct {
	Email    string `json:"email"`
	Password string `json:"password"`
}

type LoginResponse

type LoginResponse struct {
	Token    string    `json:"token"`
	Expirity time.Time `json:"expirity"`
}

type RateRequest

type RateRequest struct {
	Rating    int    `json:"rating"`
	BookingID string `json:"bookingId"`
}

RateRequest represents the request body for rating a booking

type Register

type Register struct {
	UserEmail         string `json:"email"`
	RegisterAuthToken string `json:"invitationToken"`
	UserProfile
}

type Request

type Request struct {
	Data    []byte
	Path    []string
	Context *HTTPContext
	UserID  string
}

Request represents an HTTP request to the API. It contains the request Body data, the URL path and the HTTP context. The context can be used for obtaining URL parameters and sending responses.

type Response

type Response struct {
	Header ResponseHeader `json:"header"`
	Data   any            `json:"data,omitempty"`
}

Response is the default response of the API

type ResponseHeader

type ResponseHeader struct {
	Success   bool   `json:"success"`
	Message   string `json:"message,omitempty"`
	ErrorCode int    `json:"errorCode,omitempty"`
}

ResponseHeader is the header of the response

type RouterHandlerFn

type RouterHandlerFn = func(r *Request) (interface{}, error)

RouterHandlerFn is the function signature for adding handlers to the HTTProuter.

type Tool

type Tool struct {
	ID               int64            `json:"id"`
	Title            string           `json:"title"`
	Description      string           `json:"description"`
	MayBeFree        *bool            `json:"mayBeFree"`
	AskWithFee       *bool            `json:"askWithFee"`
	Cost             *uint64          `json:"cost"`
	Images           []types.HexBytes `json:"images"`
	TransportOptions []int            `json:"transportOptions"`
	Category         int              `json:"category"`
	Location         db.Location      `json:"location"`
	EstimatedValue   uint64           `json:"estimatedValue"`
	Height           uint32           `json:"height"`
	Weight           uint32           `json:"weight"`
}

Tool is the type of the tool

type ToolID

type ToolID struct {
	ID int64 `json:"id"`
}

type ToolSearch

type ToolSearch struct {
	Term          string  `json:"term"`
	Categories    []int   `json:"categories"`
	Distance      int     `json:"distance"`
	MaxCost       *uint64 `json:"maxCost"`
	MayBeFree     *bool   `json:"mayBeFree"`
	AvailableFrom int     `json:"availableFrom"`
}

ToolSearch is the type of the tool search

type ToolsWrapper

type ToolsWrapper struct {
	Tools []db.Tool `json:"tools"`
}

type UserProfile

type UserProfile struct {
	Name      string       `json:"name"`
	Community string       `json:"community"`
	Location  *db.Location `json:"location,omitempty"`
	Active    *bool        `json:"active,omitempty"`
	Avatar    []byte       `json:"avatar,omitempty"`
	Password  string       `json:"password,omitempty"`
}

type UsersWrapper

type UsersWrapper struct {
	Users []db.User `json:"users"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL