service

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Jul 21, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUsernameExists      = errors.New("username already exists")
	ErrInvalidCredentials  = errors.New("invalid credentials")
	ErrCannotChangeOwnRole = errors.New("cannot change own role")
	ErrCannotDeleteSelf    = errors.New("cannot delete self")
	ErrInvalidRole         = errors.New("invalid role")
)

Functions

This section is empty.

Types

type AuthUser

type AuthUser struct {
	JWTSecret    string
	TokenVersion int
}

AuthUser holds minimal user data for token validation.

func GetUserForAuth

func GetUserForAuth(db *gorm.DB, userID uint) (*AuthUser, error)

GetUserForAuth returns user auth fields for token validation.

type LoginInput

type LoginInput struct {
	Username string
	Password string
}

LoginInput is the input for Login.

type LoginResponse

type LoginResponse struct {
	Token string       `json:"token"`
	User  *UserPayload `json:"user"`
}

LoginResponse is the response for login/register.

type RegisterInput

type RegisterInput struct {
	Username string
	Password string
}

RegisterInput is the input for Register.

type UserPayload

type UserPayload struct {
	ID       uint   `json:"id"`
	Username string `json:"username"`
	Role     string `json:"role"`
}

UserPayload is the user data returned in API responses.

type UserService

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

UserService handles user business logic.

func NewUserService

func NewUserService(db *gorm.DB, cfg *config.Config) *UserService

NewUserService creates a UserService.

func (*UserService) DeleteUser

func (s *UserService) DeleteUser(id uint, currentUserID uint) error

DeleteUser deletes a user (admin only). Cannot delete self.

func (*UserService) HasUsers

func (s *UserService) HasUsers() (bool, error)

HasUsers returns true if any users exist.

func (*UserService) ListUsers

func (s *UserService) ListUsers() ([]UserPayload, error)

ListUsers returns all users (admin only).

func (*UserService) Login

func (s *UserService) Login(in LoginInput) (*LoginResponse, error)

Login authenticates a user and returns a token.

func (*UserService) Register

func (s *UserService) Register(in RegisterInput) (*LoginResponse, error)

Register creates a new user. First user becomes admin.

func (*UserService) UpdateLastActivity

func (s *UserService) UpdateLastActivity(userID uint) error

UpdateLastActivity sets LastActivityAt for the user.

func (*UserService) UpdateUserRole

func (s *UserService) UpdateUserRole(id uint, newRole string, currentUserID uint) error

UpdateUserRole updates a user's role (admin only). Cannot change own role.

Jump to

Keyboard shortcuts

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