database

package
v0.0.0-...-cbf586e Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2025 License: MIT Imports: 4 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 {
	GetName() (string, error)
	SetName(name string) error
	Ping() error

	Login(userName string) (int, error)
	SetUsername(userId int, newName string) error
	GetUser(userId int) (User, bool, error)
	SetUserPhoto(userId int, newPhoto []byte) error
	GetAllUsers() ([]User, error)
	CheckUserName(userName string) (bool, error)

	GetChat(chatId int, flagSingle bool, userId int) (Chat, error)
	CreateChat(chatName string, chatPhoto []byte, chatType string) (int, error)
	GetAllChats(userId int) ([]Chat, error)
	AddParticipant(chatId int, participantId int) error
	SetChatName(chatId int, newName string) error
	SetChatPhoto(chatId int, newPhoto []byte) error
	LeaveChat(chatId int, userId int) error
	MessageSeen(chatId int, userId int) ([]time.Time, error)

	SendMessage(messageContent string, messagePhoto []byte, messageSender int, messageReceiver int, messageForwarded int, messageReply int) (int, error)
	ForwardMessage(messageId int, messageReceiver int, messageForwarded int) error
	DeleteMessage(messageId int) error
	AddReaction(userId int, messageId int, reaction string) error
	DeleteReaction(userId int, messageId int) 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`.

type Chat

type Chat struct {
	Id           int       `json:"id"`
	ChatType     string    `json:"chatType"`
	Name         string    `json:"chatName"`
	Photo        []byte    `json:"chatPhoto"`
	Participants []User    `json:"chatParticipants"`
	Messages     []Message `json:"chatMessages"`
}

type Message

type Message struct {
	Id        int        `json:"id"`
	Content   string     `json:"content"`
	Photo     []byte     `json:"photo"`
	Sender    int        `json:"sender"`
	Receiver  int        `json:"receiver"`
	Forwarded int        `json:"forwarded"`
	TimeStamp time.Time  `json:"timestamp"`
	Reactions []Reaction `json:"reactions"`
	Reply     int        `json:"reply"`
}

type Reaction

type Reaction struct {
	UserId    int    `json:"userId"`
	MessageId int    `json:"messageId"`
	Emoji     string `json:"reaction"`
}

type User

type User struct {
	Id       int    `json:"userId"`
	Username string `json:"userName"`
	Photo    []byte `json:"userPhoto"`
}

Jump to

Keyboard shortcuts

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