api

package
v0.0.0-...-0c3b333 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2023 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	StatusOK    = "OK"
	StatusError = "Error"
)

Variables

This section is empty.

Functions

func Run

func Run(log *slog.Logger, server *ServerAPI)

Types

type CsvReportRequest

type CsvReportRequest struct {
	storage.CsvReport
}

type CsvReportResponse

type CsvReportResponse struct {
	ResponseStatus
	CsvUrl string `json:"csv_url" validate:"required"`
}

type GetSegmentsResponse

type GetSegmentsResponse struct {
	ResponseStatus
	UserID      uint64   `json:"user_id" validate:"required"`
	SegmentSlug []string `json:"user_segments" validate:"required"`
}

type GetUsersResponse

type GetUsersResponse struct {
	ResponseStatus
	SegmentSlug string   `json:"user_segment" validate:"required"`
	UserIDs     []uint64 `json:"user_ids" validate:"required"`
}

type ResponseStatus

type ResponseStatus struct {
	Status string `json:"status"`
	Error  string `json:"error,omitempty"`
}

func Error

func Error(msg string) ResponseStatus

func OK

func OK() ResponseStatus

type SegmentRequest

type SegmentRequest struct {
	storage.Segment
}

type SegmentResponse

type SegmentResponse struct {
	ResponseStatus
	storage.Segment
}

type ServerAPI

type ServerAPI struct {
	ListenAddr string
	Store      Storage
	Log        *slog.Logger
}

func NewAPIServer

func NewAPIServer(listenAddr string, store Storage, log *slog.Logger) *ServerAPI

func (*ServerAPI) HandleAddSegment

func (s *ServerAPI) HandleAddSegment(w http.ResponseWriter, r *http.Request)

HandleAddSegment godoc @Summary Add a new segment @Description Add a new segment to the system @ID addSegment @Accept json @Produce json @Param segment body SegmentRequest true "Segment object to be added" @Success 201 {object} SegmentResponse "Successfully added segment" @Failure 400 {object} ResponseStatus "Invalid input data" @Failure 409 {object} ResponseStatus "Query execution failure" @Router /segment/new [post]

func (*ServerAPI) HandleAddUser

func (s *ServerAPI) HandleAddUser(w http.ResponseWriter, r *http.Request)

HandleAddUser godoc @Summary Add a new user @Description Add a new user to the system @ID addUser @Accept json @Produce json @Param user body UserRequest true "User object to be added" @Success 201 {object} UserResponse "Successfully added user" @Failure 400 {object} ResponseStatus "Invalid input data" @Failure 409 {object} ResponseStatus "Query execution failure" @Router /user/new [post]

func (*ServerAPI) HandleAddUserToSegment

func (s *ServerAPI) HandleAddUserToSegment(w http.ResponseWriter, r *http.Request)

HandleAddUserToSegment godoc @Summary Add user ti a segment @Description Link user to segments @ID addUserToSegment @Accept json @Produce json @Param segment body UserSegmentRequest true "Segment object to be added" @Success 201 {object} UserSegmentRequest "Successfully linked segment to a user" @Failure 400 {object} ResponseStatus "Invalid input data" @Failure 409 {object} ResponseStatus "Query execution failure" @Router /user/addSegment [post]

func (*ServerAPI) HandleCascadeDeleteSegment

func (s *ServerAPI) HandleCascadeDeleteSegment(w http.ResponseWriter, r *http.Request)

HandleCascadeDeleteSegment godoc @Summary Cascade delete a segment @Description Cascade delete a segment and remove associated users @ID cascadeDeleteSegment @Accept json @Produce json @Param segment body SegmentRequest true "Segment object to delete" @Success 200 {object} SegmentResponse "Successfully deleted segment" @Failure 400 {object} ResponseStatus "Invalid input data" @Failure 409 {object} ResponseStatus "Query execution failure" @Router /segment/remove [delete]

func (*ServerAPI) HandleCsvReport

func (s *ServerAPI) HandleCsvReport(w http.ResponseWriter, r *http.Request)

HandleCsvReport godoc @Summary Generate CSV report @Description Generate a CSV report for a specific month and year @ID generateCsvReport @Accept json @Produce json @Param csvReport body CsvReportRequest true "CSV report request" @Success 200 {object} CsvReportResponse "Successfully generated CSV report" @Failure 400 {object} ResponseStatus "Invalid input data" @Failure 409 {object} ResponseStatus "Query execution failure" @Router /report [post]

func (*ServerAPI) HandleDeleteUserFromSegment

func (s *ServerAPI) HandleDeleteUserFromSegment(w http.ResponseWriter, r *http.Request)

HandleDeleteUserFromSegment godoc @Summary Remove a user from one or more segments @Description Remove a user from one or more segments @ID deleteUserFromSegment @Accept json @Produce json @Param userSegments body UserSegmentRequest true "User and segment association" @Success 200 {object} UserSegmentResponse "Successfully removed user from segment" @Failure 400 {object} ResponseStatus "Invalid input data" @Failure 409 {object} ResponseStatus "Query execution failure" @Router /user/segments [delete]

func (*ServerAPI) HandleDownloadCsv

func (s *ServerAPI) HandleDownloadCsv(w http.ResponseWriter, r *http.Request)

HandleDownloadCsv godoc @Summary Download CSV report @Description Download a previously generated CSV report @ID downloadCsvReport @Produce text/csv @Param fileName path string true "CSV file name to download" @Success 200 {file} file "CSV file for download" @Failure 400 {object} ResponseStatus "Invalid file name" @Router /report/{fileName} [get]

func (*ServerAPI) HandleGetSegmentUsersInfo

func (s *ServerAPI) HandleGetSegmentUsersInfo(w http.ResponseWriter, r *http.Request)

HandleGetSegmentUsersInfo godoc @Summary Get users of a segment @Description Get a list of users belonging to a specific segment @ID getSegmentUsersInfo @Produce json @Param segmentName path string true "segment Name to get list of its users" @Success 200 {object} GetUsersResponse "Successfully retrieved segment users" @Failure 400 {object} ResponseStatus "Invalid input data" @Failure 409 {object} ResponseStatus "Query execution failure" @Router /segment/users/{segmentName} [get]

func (*ServerAPI) HandleGetUserSegmentsInfo

func (s *ServerAPI) HandleGetUserSegmentsInfo(w http.ResponseWriter, r *http.Request)

HandleGetUserSegmentsInfo godoc @Summary Get user's segments information @Description Get information about the segments a user belongs to @ID getUserSegmentsInfo @Produce json @Param userID path string true "user ID to get list of segments for" @Success 200 {object} GetSegmentsResponse "Successfully retrieved user segments" @Failure 400 {object} ResponseStatus "Invalid input data" @Failure 409 {object} ResponseStatus "Query execution failure" @Router /user/segments/{userID} [get]

type Storage

type Storage interface {
	CascadeDeleteSegment(context.Context, storage.Segment, *slog.Logger) error
	DeleteUserFromSegments(context.Context, storage.UserSegments, *slog.Logger) error
	GetUserSegmentsInfo(context.Context, storage.User, *slog.Logger) ([]string, error)
	GetSegmentUsersInfo(context.Context, storage.Segment, *slog.Logger) ([]uint64, error)
	AddUserToSegments(context.Context, storage.UserSegments, *slog.Logger) error
	AddUser(context.Context, storage.User, *slog.Logger) (uint64, error)
	AddSegment(context.Context, storage.Segment, *slog.Logger) (uint64, error)
	CsvHistoryReport(context.Context, storage.CsvReport, *slog.Logger) error
}

type UserRequest

type UserRequest struct {
	storage.User
}

type UserResponse

type UserResponse struct {
	ResponseStatus
	storage.User
}

type UserSegmentRequest

type UserSegmentRequest struct {
	storage.UserSegments
}

type UserSegmentResponse

type UserSegmentResponse struct {
	ResponseStatus
	storage.UserSegments
}

Jump to

Keyboard shortcuts

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