database

package
v0.0.0-...-e6457d9 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2025 License: MIT Imports: 5 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 {
	// utilities
	GetName() (string, error)
	GetConversationsByUser(userID int) ([]Conversation, error)
	GetMessageUser(messageID int) (int, error)
	CheckUserParticipation(userID int, conversationID int) (bool, error)
	GetUserIDByUsername(username string) (int, error)
	GetGroupFromConversation(conversationID int) (Group, error)
	GetChatFromConversation(conversationID int) (Chat, error)
	GetOtherParticipant(conversationID int, userID int) (User, error)
	GetMessages(conversationID int, participants []string) ([]Message, error)
	CreateGroup(userID int, groupName string, photoURL string) (int, error)
	CreateChat(userID int, otherUserID int) (int, error)
	GetUsername(userID int) (string, error)
	GetMyReaction(userID int, messageID int) (string, error)
	// mains
	GetUserByReaction(reactionID int) (int, error)
	SetMyUserName(userID int, name string) error
	SetMyPhoto(userID int, profile_pic string) error
	AddToGroup(userID int, groupID int) error
	LeaveGroup(userID int, groupID int) error
	SetGroupName(groupID int, groupName string) error
	SetGroupPhoto(groupID int, photoURL string) error
	GetConversation(conversationID int) (Conversation, error)
	SendMessage(userID int, conversationID int, message string, picture string) error
	ForwardMessage(userID int, conversationID int, forwardedID int) error
	DeleteMessage(messageID int) error
	CommentMessage(userID int, messageID int, emoji string) error
	UncommentMessage(userID int, messageID int) (int64, error)
	CreateUsername(username string) (int, error)
	GetAllUsers() ([]string, error)
	GetGroupParticipants(groupID int) ([]string, error)
	DoLogin(userID int) (User, error)
	GetAllReactions(messageID int) ([]Reaction, error)
	ReceiveMessages(userID int) error
	ReadMessages(userID int, conversationID int) error
	GetMessageReceivedStatus(messageID int, userID int) (int, error)
	GetMessageReadStatus(messageID int, userID int) (int, error)
	// others
	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`.

type Chat

type Chat struct {
	ID      int `json:"id"`
	ConvoID int `json:"convo_id"`
}

type Conversation

type Conversation struct {
	ID   int    `json:"id"`
	Type string `json:"type"`
}

type Group

type Group struct {
	ID      int    `json:"id"`
	ConvoID int    `json:"convo_id"`
	Name    string `json:"name"`
	Photo   string `json:"photo"`
}

type Message

type Message struct {
	ID             int    `json:"id"`
	Username       string `json:"username"`
	ConvoID        int    `json:"convo_id"`
	Text           string `json:"text"`
	Pic            string `json:"pic"`
	Forwarded      bool   `json:"forwarded"`
	ReceivedStatus bool   `json:"received_status"`
	ReadStatus     bool   `json:"read_status"`
}

type Reaction

type Reaction struct {
	UserID int    `json:"user_id"`
	Emoji  string `json:"emoji"`
}

type User

type User struct {
	ID         int    `json:"id"`
	Username   string `json:"username"`
	ProfilePic string `json:"profile_pic"`
}

Jump to

Keyboard shortcuts

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