api

package
v0.0.0-...-87a19a3 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2023 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const TokenHeader = "X-Api-Token"

Variables

This section is empty.

Functions

func AdminAuth

func AdminAuth(c *fiber.Ctx) error

func CreateTokenFromUser

func CreateTokenFromUser(user *types.User) string

CreateTokenFromUser does the following: - Create a token that is valid for 4 hours - Sign it with the environment variable JWT_SECRET

func ErrorHandler

func ErrorHandler(c *fiber.Ctx, err error) error

func JWTAuthentication

func JWTAuthentication(userStore db.UserStore) fiber.Handler

JWTAuthentication will:

  • Expect that the HTTP request has a header of name `TokenHeader`
  • Get the user claims (email, expiration date and user id)
  • Verify that the token is valid
  • Verify that the token hasn't expired
  • Pass the user as types.User struct to the fiber context so that it may be used in the room handler.

Types

type AuthHandler

type AuthHandler struct {
	// contains filtered or unexported fields
}

func NewAuthHandler

func NewAuthHandler(userStore db.UserStore) *AuthHandler

func (*AuthHandler) HandleAuthenticate

func (h *AuthHandler) HandleAuthenticate(c *fiber.Ctx) error

HandleAuthenticate will do the following: - Parse the body and expect it to be a JSON formated AuthParams - Get the user information based on the passed email - Verify that their password is correct - Return a authentication response with a token

type AuthParams

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

AuthParams are used when the user is trying to authenticate, these fields are mandatory.

type AuthResponse

type AuthResponse struct {
	User  *types.User `json:"user"`
	Token string      `json:"token"`
}

type BookRoomParams

type BookRoomParams struct {
	FromDate   time.Time `json:"fromDate"`
	UntilDate  time.Time `json:"untilDate"`
	NumPersons int       `json:"numPersons"`
}

The User ID and Room ID isn't specified here because the User ID is already in the Context

type BookingHandler

type BookingHandler struct {
	// contains filtered or unexported fields
}

func NewBookingHandler

func NewBookingHandler(store *db.Store) *BookingHandler

func (*BookingHandler) HandleCancelBooking

func (h *BookingHandler) HandleCancelBooking(c *fiber.Ctx) error

HandleCancelBooking will: - get the id from the HTTP URL - get the booking based on the id - get the user from the context (sent via JWT middelware) - check that the user owns it or the user is an admin - update the canceled field

func (*BookingHandler) HandleGetBooking

func (h *BookingHandler) HandleGetBooking(c *fiber.Ctx) error

func (*BookingHandler) HandleGetBookings

func (h *BookingHandler) HandleGetBookings(c *fiber.Ctx) error

HandleGetBookings will return all bookings with no filter

type Error

type Error struct {
	Response
	Code int `json:"error"`
}

func ErrorBadRequest

func ErrorBadRequest() Error

func ErrorInvalidID

func ErrorInvalidID() Error

func ErrorNotFound

func ErrorNotFound(resource string) Error

func ErrorTokenExpired

func ErrorTokenExpired() Error

func ErrorUnauthorized

func ErrorUnauthorized() Error

func NewError

func NewError(code int, message string) *Error

func (Error) Error

func (e Error) Error() string

type HotelHandler

type HotelHandler struct {
	// contains filtered or unexported fields
}

func NewHotelHandler

func NewHotelHandler(store *db.Store) *HotelHandler

func (*HotelHandler) HandleGetHotel

func (h *HotelHandler) HandleGetHotel(c *fiber.Ctx) error

func (*HotelHandler) HandleGetHotels

func (h *HotelHandler) HandleGetHotels(c *fiber.Ctx) error

func (*HotelHandler) HandleGetRooms

func (h *HotelHandler) HandleGetRooms(c *fiber.Ctx) error

type HotelQueryParams

type HotelQueryParams struct {
	db.Pagination
	Rating int
}

type Response

type Response struct {
	Success bool   `json:"success"`
	Message string `json:"message"`
}

type RoomHandler

type RoomHandler struct {
	// contains filtered or unexported fields
}

func NewRoomHandler

func NewRoomHandler(store *db.Store) *RoomHandler

func (*RoomHandler) HandleBookRoom

func (h *RoomHandler) HandleBookRoom(c *fiber.Ctx) error

HandleBookRoom will do the following: - Expect that the body is a JSON marshaled `BookRoomParams` - Validate the parameters - Convert the room ID from the HTTP URL to a primitive MongoDB id - Get the user data from the fiber context (this is being passed from the middleware) - Check that the room is available - Generate and insert a booking

func (*RoomHandler) HandleGetRoom

func (h *RoomHandler) HandleGetRoom(c *fiber.Ctx) error

HandleGetRoom will return th rooms that matches id

func (*RoomHandler) HandleGetRooms

func (h *RoomHandler) HandleGetRooms(c *fiber.Ctx) error

HandleGetRooms will return all the rooms with no filter

type UserHandler

type UserHandler struct {
	// contains filtered or unexported fields
}

func NewUserHandler

func NewUserHandler(userStore db.UserStore) *UserHandler

func (*UserHandler) HandleDeleteUser

func (h *UserHandler) HandleDeleteUser(c *fiber.Ctx) error

func (*UserHandler) HandleGetUser

func (h *UserHandler) HandleGetUser(c *fiber.Ctx) error

func (*UserHandler) HandleGetUsers

func (h *UserHandler) HandleGetUsers(c *fiber.Ctx) error

func (*UserHandler) HandlePostUser

func (h *UserHandler) HandlePostUser(c *fiber.Ctx) error

func (*UserHandler) HandlePutUser

func (h *UserHandler) HandlePutUser(c *fiber.Ctx) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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