service

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const MaxImportFileSize = 2 * 1024 * 1024

Variables

View Source
var (
	ErrPathAlreadyUsed       = errors.New("path is already used in this project")
	ErrContentSizeExceeded   = errors.New("content size exceeds the maximum allowed size")
	ErrTotalSizeLimitReached = errors.New("total content size limit for the project would be exceeded")
)
View Source
var (
	ErrRoleNotFound      = errors.New("role not found")
	ErrRoleAlreadyExists = errors.New("role already exists")
	ErrUserNotInRole     = errors.New("user is not in role")
	ErrUserAlreadyInRole = errors.New("user is already in role")
)
View Source
var (
	ErrTokenNotFound      = errors.New("token not found")
	ErrTokenAlreadyExists = errors.New("token with this name already exists")
	ErrTokenExpired       = errors.New("token has expired")
	ErrInvalidToken       = errors.New("invalid token")
	ErrTokenNameTooLong   = errors.New("token name is too long")
)
View Source
var (
	ErrUserNotFound       = errors.New("user not found")
	ErrUserAlreadyExists  = errors.New("user already exists")
	ErrInvalidCredentials = errors.New("invalid credentials")
	ErrUserInactive       = errors.New("user account is inactive")
)
View Source
var ErrPublishInProgress = errors.New("publish already in progress for this project")

ErrPublishInProgress is returned when a publish is already in progress for the project

View Source
var ErrSourceAlreadyUsed = errors.New("source is already used in this project")

Functions

This section is empty.

Types

type AgentService

type AgentService interface {
	GetTx(ctx context.Context) *gorm.DB
	GetQuery(ctx context.Context) *gorm.DB
	Upsert(ctx context.Context, agent *model.Agent) error
	GetByName(ctx context.Context, namespaceCode, projectCode, name string) (*model.Agent, error)
	FindByProject(ctx context.Context, namespaceCode, projectCode string) ([]model.Agent, error)
	SearchPaginate(ctx context.Context, pagination *commonTypes.PaginationInput, query *gorm.DB) (*model.AgentList, error)
	CountByProjectAndStatus(ctx context.Context, namespaceCode, projectCode string, status commonTypes.AgentStatus, lastHitAfter time.Time) (int64, error)
	UpdateLastHit(ctx context.Context, namespaceCode, projectCode, name string) error
	Delete(ctx context.Context, namespaceCode, projectCode, name string) error
}

type AuthService

type AuthService interface {
	Login(ctx context.Context, req *types.LoginRequest) (*model.User, *types.TokenPair, error)
	RefreshTokens(ctx context.Context, refreshToken string, claims *jwt.Claims) (*model.User, *types.TokenPair, error)
	Logout(ctx context.Context, userID int64) error
	ToUserResponse(user *model.User) *types.UserResponse
}

func NewAuthService

func NewAuthService(ctx *appContext.Context, userRepo repository.UserRepository, jwtService *jwt.ServiceJWT) AuthService

type ImportErrorReason

type ImportErrorReason string

ImportErrorReason represents the reason why a redirect import failed

const (
	ImportErrorInvalidFormat       ImportErrorReason = "INVALID_FORMAT"
	ImportErrorInvalidRedirect     ImportErrorReason = "INVALID_REDIRECT"
	ImportErrorInvalidType         ImportErrorReason = "INVALID_TYPE"
	ImportErrorInvalidStatus       ImportErrorReason = "INVALID_STATUS"
	ImportErrorEmptySource         ImportErrorReason = "EMPTY_SOURCE"
	ImportErrorEmptyTarget         ImportErrorReason = "EMPTY_TARGET"
	ImportErrorDuplicateInFile     ImportErrorReason = "DUPLICATE_SOURCE_IN_FILE"
	ImportErrorSourceAlreadyExists ImportErrorReason = "SOURCE_ALREADY_EXISTS"
	ImportErrorDatabaseError       ImportErrorReason = "DATABASE_ERROR"
)

type ImportRedirectError

type ImportRedirectError struct {
	Line    int
	Source  string
	Target  string
	Reason  ImportErrorReason
	Message string
}

ImportRedirectError represents a single import error

type ImportRedirectOptions

type ImportRedirectOptions struct {
	Overwrite bool
}

ImportRedirectOptions contains options for the import operation

type ImportRedirectResult

type ImportRedirectResult struct {
	Success       bool
	TotalLines    int
	ImportedCount int
	SkippedCount  int
	ErrorCount    int
	Errors        []ImportRedirectError
}

ImportRedirectResult represents the result of an import operation

type NamespaceService

type NamespaceService interface {
	GetTx(ctx context.Context) *gorm.DB
	GetQuery(ctx context.Context) *gorm.DB
	Create(ctx context.Context, input *model.Namespace) (*model.Namespace, error)
	Update(ctx context.Context, namespaceCode string, input model.Namespace) (*model.Namespace, error)
	Delete(ctx context.Context, namespaceCode string) (bool, error)
	GetByCode(ctx context.Context, namespaceCode string) (*model.Namespace, error)
	GetAll(ctx context.Context) ([]model.Namespace, error)
	Search(ctx context.Context, query *gorm.DB) ([]model.Namespace, error)
	SearchPaginate(ctx context.Context, pagination *types.PaginationInput, query *gorm.DB) (*model.NamespaceList, error)
}

type PageDraftService

type PageDraftService interface {
	GetTx(ctx context.Context) *gorm.DB
	GetQuery(ctx context.Context) *gorm.DB
	GetByID(ctx context.Context, id int64) (*model.PageDraft, error)
	GetByIDWithProject(ctx context.Context, namespaceCode, projectCode string, id int64) (*model.PageDraft, error)
	Create(ctx context.Context, namespaceCode, projectCode string, oldPageID *int64, newPage *commonTypes.Page) (*model.PageDraft, error)
	Update(ctx context.Context, id int64, newPage *commonTypes.Page) (*model.PageDraft, error)
	Delete(ctx context.Context, id int64) (bool, error)
	Rollback(ctx context.Context, namespaceCode, projectCode string) (bool, error)
	Search(ctx context.Context, query *gorm.DB) ([]model.PageDraft, error)
	SearchPaginate(ctx context.Context, pagination *commonTypes.PaginationInput, query *gorm.DB) (*model.PageDraftList, error)
}

type PageService

type PageService interface {
	GetTx(ctx context.Context) *gorm.DB
	GetQuery(ctx context.Context) *gorm.DB
	GetByID(ctx context.Context, namespaceCode, projectCode string, pageID int64) (*model.Page, error)
	FindByProject(ctx context.Context, namespaceCode, projectCode string) ([]model.Page, error)
	FindByProjectPublished(ctx context.Context, namespaceCode, projectCode string, pagination *commonTypes.PaginationInput) ([]model.Page, int64, error)
	Search(ctx context.Context, query *gorm.DB) ([]model.Page, error)
	SearchPaginate(ctx context.Context, pagination *commonTypes.PaginationInput, query *gorm.DB) (*model.PageList, error)
}

type ParsedRedirectRow

type ParsedRedirectRow struct {
	LineNum int
	Type    commonTypes.RedirectType
	Source  string
	Target  string
	Status  commonTypes.RedirectStatus
}

ParsedRedirectRow represents a parsed row from the import file

type ProjectDashboardService

type ProjectDashboardService interface {
	GetStats(ctx context.Context, namespaceCode, projectCode string) (*ProjectDashboardStats, error)
}

func NewProjectDashboardService

func NewProjectDashboardService(
	ctx *appContext.Context,
	projectService ProjectService,
	redirectService RedirectService,
	redirectDraftService RedirectDraftService,
	pageService PageService,
	pageDraftService PageDraftService,
	agentService AgentService,
) ProjectDashboardService

type ProjectDashboardStats

type ProjectDashboardStats struct {
	// Project info
	Version     int
	PublishedAt *time.Time

	// Redirect stats
	RedirectTotal          int64
	RedirectCountBasic     int64
	RedirectCountBasicHost int64
	RedirectCountRegex     int64
	RedirectCountRegexHost int64

	// Redirect draft stats
	RedirectDraftTotal       int64
	RedirectDraftCountCreate int64
	RedirectDraftCountUpdate int64
	RedirectDraftCountDelete int64

	// Page stats
	PageTotal          int64
	PageCountBasic     int64
	PageCountBasicHost int64

	// Page draft stats
	PageDraftTotal       int64
	PageDraftCountCreate int64
	PageDraftCountUpdate int64
	PageDraftCountDelete int64

	// Agent stats
	AgentTotalOnline int64
	AgentCountError  int64
}

type ProjectService

type ProjectService interface {
	GetTx(ctx context.Context) *gorm.DB
	GetQuery(ctx context.Context) *gorm.DB
	Create(ctx context.Context, input *model.Project) (*model.Project, error)
	Update(ctx context.Context, namespaceCode, projectCode string, input model.Project) (*model.Project, error)
	Delete(ctx context.Context, namespaceCode, projectCode string) (bool, error)
	GetByCode(ctx context.Context, namespaceCode, projectCode string) (*model.Project, error)
	GetByCodeWithNamespace(ctx context.Context, namespaceCode, projectCode string) (*model.Project, error)
	GetByNamespace(ctx context.Context, namespaceCode string) ([]model.Project, error)
	GetAll(ctx context.Context) ([]model.Project, error)
	Search(ctx context.Context, query *gorm.DB) ([]model.Project, error)
	SearchPaginate(ctx context.Context, pagination *commonTypes.PaginationInput, query *gorm.DB) (*model.ProjectList, error)
	CountRedirects(ctx context.Context, namespaceCode, projectCode string) (int64, error)
	CountRedirectDrafts(ctx context.Context, namespaceCode, projectCode string) (int64, error)
	CountPages(ctx context.Context, namespaceCode, projectCode string) (int64, error)
	CountPageDrafts(ctx context.Context, namespaceCode, projectCode string) (int64, error)
	TotalPageContentSize(ctx context.Context, namespaceCode, projectCode string) (int64, error)
	TotalPageContentSizeLimit() int64
	Publish(ctx context.Context, namespaceCode, projectCode string) (*model.Project, error)
}

type RedirectDraftService

type RedirectDraftService interface {
	GetTx(ctx context.Context) *gorm.DB
	GetQuery(ctx context.Context) *gorm.DB
	GetByID(ctx context.Context, id int64) (*model.RedirectDraft, error)
	GetByIDWithProject(ctx context.Context, namespaceCode, projectCode string, id int64) (*model.RedirectDraft, error)
	Create(ctx context.Context, namespaceCode, projectCode string, oldRedirectID *int64, newRedirect *commonTypes.Redirect) (*model.RedirectDraft, error)
	Update(ctx context.Context, id int64, newRedirect *commonTypes.Redirect) (*model.RedirectDraft, error)
	Delete(ctx context.Context, id int64) (bool, error)
	Rollback(ctx context.Context, namespaceCode, projectCode string) (bool, error)
	Search(ctx context.Context, query *gorm.DB) ([]model.RedirectDraft, error)
	SearchPaginate(ctx context.Context, pagination *commonTypes.PaginationInput, query *gorm.DB) (*model.RedirectDraftList, error)
}

type RedirectImportService

type RedirectImportService interface {
	GetTx(ctx context.Context) *gorm.DB
	GetQuery(ctx context.Context) *gorm.DB
	ValidateFile(filename string, contentType string, size int64) error
	ParseFile(reader io.Reader) ([]ParsedRedirectRow, []ImportRedirectError, error)
	Import(ctx context.Context, namespaceCode, projectCode string, rows []ParsedRedirectRow, opts ImportRedirectOptions) (*ImportRedirectResult, error)
}

RedirectImportService handles redirect import operations

func NewRedirectImportService

func NewRedirectImportService(ctx *appContext.Context, redirectDraftRepo repository.RedirectDraftRepository) RedirectImportService

NewRedirectImportService creates a new RedirectImportService

type RedirectService

type RedirectService interface {
	GetTx(ctx context.Context) *gorm.DB
	GetQuery(ctx context.Context) *gorm.DB
	GetByID(ctx context.Context, namespaceCode, projectCode string, redirectID int64) (*model.Redirect, error)
	FindByProject(ctx context.Context, namespaceCode, projectCode string) ([]model.Redirect, error)
	FindByProjectPublished(ctx context.Context, namespaceCode, projectCode string, pagination *commonTypes.PaginationInput) ([]model.Redirect, int64, error)
	Search(ctx context.Context, query *gorm.DB) ([]model.Redirect, error)
	SearchPaginate(ctx context.Context, pagination *commonTypes.PaginationInput, query *gorm.DB) (*model.RedirectList, error)
	SearchBatch(ctx context.Context, query *gorm.DB, batchSize int, fn func([]model.Redirect) error) error
}

type RoleService

type RoleService interface {
	GetTx(ctx context.Context) *gorm.DB
	GetQuery(ctx context.Context) *gorm.DB
	Create(ctx context.Context, input *model.Role) (*model.Role, error)
	Update(ctx context.Context, id int64, input model.Role) (*model.Role, error)
	Delete(ctx context.Context, id int64) (bool, error)
	GetByID(ctx context.Context, id int64) (*model.Role, error)
	GetByCode(ctx context.Context, code string, roleType model.RoleType) (*model.Role, error)
	GetAll(ctx context.Context) ([]model.Role, error)
	GetAllByType(ctx context.Context, roleType model.RoleType) ([]model.Role, error)
	SearchPaginate(ctx context.Context, pagination *commonTypes.PaginationInput, query *gorm.DB) (*model.RoleList, error)

	// User-Role management
	AddUserToRole(ctx context.Context, userID, roleID int64) error
	RemoveUserFromRole(ctx context.Context, userID, roleID int64) error
	GetUserRoles(ctx context.Context, userID int64) ([]model.Role, error)
	GetUserRolesByType(ctx context.Context, userID int64, roleType model.RoleType) ([]model.Role, error)
	GetRoleUsers(ctx context.Context, roleID int64) ([]model.User, error)
	GetRoleUsersPaginate(ctx context.Context, roleCode string, pagination *commonTypes.PaginationInput, search string) (*model.UserList, error)
	GetUsersNotInRole(ctx context.Context, roleCode string, search string, limit int) ([]model.User, error)

	// Permissions
	GetPermissionsByRoleCode(ctx context.Context, code string) (*model.SubjectPermissions, error)
	GetPermissionsByUsername(ctx context.Context, username string) (*model.SubjectPermissions, error)
	GetPermissionsByTokenName(ctx context.Context, tokenName string) (*model.SubjectPermissions, error)
	UpdateRolePermissions(ctx context.Context, roleID int64, permissions *model.SubjectPermissions) error
	UpdateUserRoles(ctx context.Context, userID int64, roleCodes []string) error
}

type Services

type Services struct {
	Namespace        NamespaceService
	Project          ProjectService
	User             UserService
	Auth             AuthService
	Role             RoleService
	Token            TokenService
	Redirect         RedirectService
	RedirectDraft    RedirectDraftService
	RedirectImport   RedirectImportService
	Page             PageService
	PageDraft        PageDraftService
	Agent            AgentService
	ProjectDashboard ProjectDashboardService
}

func NewServices

func NewServices(ctx *appContext.Context, repos *repository.Repositories, jwtService *jwt.ServiceJWT) *Services

type TokenService

type TokenService interface {
	GetTx(ctx context.Context) *gorm.DB
	GetQuery(ctx context.Context) *gorm.DB
	Create(ctx context.Context, name string, expiresAt *string, permissions *model.SubjectPermissions) (*model.Token, string, error)
	Delete(ctx context.Context, id int64) (bool, error)
	GetByID(ctx context.Context, id int64) (*model.Token, error)
	GetByName(ctx context.Context, name string) (*model.Token, error)
	ValidateToken(ctx context.Context, plainToken string) (*model.Token, *model.SubjectPermissions, error)
	GetAll(ctx context.Context) ([]model.Token, error)
	SearchPaginate(ctx context.Context, pagination *commonTypes.PaginationInput, query *gorm.DB) (*model.TokenList, error)
	GetRole(ctx context.Context, tokenID int64) (*model.Role, error)
}

type UserService

type UserService interface {
	GetTx(ctx context.Context) *gorm.DB
	GetQuery(ctx context.Context) *gorm.DB
	Create(ctx context.Context, input *model.User) (*model.User, error)
	Update(ctx context.Context, id int64, input model.User) (*model.User, error)
	Delete(ctx context.Context, id int64) (bool, error)
	GetByID(ctx context.Context, id int64) (*model.User, error)
	GetByUsername(ctx context.Context, username string) (*model.User, error)
	GetAll(ctx context.Context) ([]model.User, error)
	Search(ctx context.Context, query *gorm.DB) ([]model.User, error)
	SearchPaginate(ctx context.Context, pagination *types.PaginationInput, query *gorm.DB) (*model.UserList, error)
	UpdatePassword(ctx context.Context, id int64, newPassword string) error
	UpdateStatus(ctx context.Context, id int64, active bool) (*model.User, error)
	SetPassword(ctx context.Context, id int64, newPassword string) error
	UpdateRefreshToken(ctx context.Context, id int64, refreshTokenHash string) error
	FindOrCreate(ctx context.Context, input *model.User) (*model.User, error)
}

Jump to

Keyboard shortcuts

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