database

package
v0.0.0-...-d40b927 Latest Latest
Warning

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

Go to latest
Published: May 16, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package database is the middleware between the app database and the code. All data (de)serialization (save/load) from a persistent database are handled here. Database specific logic should never escape this package.

To use this package you need to apply migrations to the database if needed/wanted, connect to it (using the database data source name from config), and then initialize an instance of AppDatabase from the DB connection.

For example, this code adds a parameter in `webapi` executable for the database data source name (add it to the main.WebAPIConfiguration structure):

DB struct {
	Filename string `conf:""`
}

This is an example on how to migrate the DB and connect to it:

// Start Database
logger.Println("initializing database support")
db, err := sql.Open("sqlite3", "./foo.db")
if err != nil {
	logger.WithError(err).Error("error opening SQLite DB")
	return fmt.Errorf("opening SQLite: %w", err)
}
defer func() {
	logger.Debug("database stopping")
	_ = db.Close()
}()

Then you can initialize the AppDatabase and pass it to the api package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AppDatabase

type AppDatabase interface {
	CreateUser(user User) (User, error)
	GetAllUsers(page int, pageSize int) ([]User, error)
	GetUserProfile(user User) (User, error)
	GetMyStream(user User) ([]Photo, error)
	SetMyUsername(user User, currentUsername string) (User, error)

	GetBansList(userId uint) ([]User, error)
	GetBansCount(userId uint) (uint, error)
	GetBanStatus(userId uint, targetUserId uint) (bool, error)
	BanUser(userId uint, targetUserId uint) error
	UnbanUser(userId uint, targetUserId uint) error
	DeleteCommentsByBannedUser(userId uint, bannedUserId uint) error
	DeleteLikesByBannedUser(userId uint, bannedUserId uint) error

	GetFollowersList(userId uint) ([]User, error)
	GetFollowersCount(userId uint) (uint, error)
	GetFollowingList(userId uint) ([]User, error)
	GetFollowingCount(userId uint) (uint, error)
	GetFollowStatus(userId uint, targetUserId uint) (bool, error)
	FollowUser(userId uint, targetUserId uint) error
	UnfollowUser(userId uint, targetUserId uint) error

	GetPhoto(photoId uint) (Photo, error)
	GetPhotoList(userId uint) ([]Photo, error)
	GetPhotoCount(userId uint) (uint, error)
	UploadPhoto(photo Photo) (Photo, error)
	DeletePhoto(photo Photo) error

	LikePhoto(userId uint, photoId uint) error
	UnlikePhoto(userId uint, photoId uint) error
	GetLikeStatus(userId uint, photoId uint) (bool, error)

	GetPhotoComments(photoId uint) ([]Comment, error)
	CommentPhoto(photoId uint, comment Comment) (Comment, error)
	UncommentPhoto(photoId uint, comment Comment) error

	Ping() error
}

AppDatabase is the high level interface for the DB

func New

func New(db *sql.DB) (AppDatabase, error)

New returns a new instance of AppDatabase based on the SQLite connection `db`. `db` is required - an error will be returned if `db` is `nil`.

Jump to

Keyboard shortcuts

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