database

package
v0.0.0-...-13cec93 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2023 License: MIT Imports: 6 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

View Source
var (
	// ErrDatabaseNotInitialized is returned when the database is not initialized.
	ErrUserNotExists          = errors.New("user does not exist")
	ErrUserExists             = errors.New("user already exists")
	ErrAuthenticationFailed   = errors.New("authentication failed: user is not authenticated")
	ErrPhotoNotExists         = errors.New("photo does not exist")
	ErrFollowerNotPresent     = errors.New("user B is not following user A")
	ErrFollowerAlreadyPresent = errors.New("user B is already following user A")
	ErrUserCantFollowHimself  = errors.New("user can't follow himself")
	ErrLikeAlreadyPut         = errors.New("like already exists")
	ErrLikeNotPresent         = errors.New("like not present")
	ErrUserCantLikeHimself    = errors.New("user can't like its own photo")
	ErrCommentNotExists       = errors.New("comment not exists")
	ErrBanNotPresent          = errors.New("user B didn't ban user A")
	ErrBanAlreadyPresent      = errors.New("user B already banned user A")
)

Functions

This section is empty.

Types

type AppDatabase

type AppDatabase interface {

	// PROFILE
	// if the user does not exist, it will be created and an identifier will be returned.
	// If it does exist, the user identifier will be returned.
	DoLogin(username string) (string, error)
	// Get the user's profile by username
	GetUserProfileByUsername(username string) (Profile_db, error)
	// Update profile of the user
	UpdateUserProfile(updateUsername bool, p Profile_db) (Profile_db, error)
	// Delete user profile
	DeleteUserProfile(userID string) error

	// MEDIA
	// Upload a photo on the profile of a specific user
	UploadPhoto(userId string, p Photo_db) (Photo_db, error)
	// Get a single photo from the profile of a user
	GetUserPhoto(photoId string) (Photo_db, error)
	// Retrieve collection of photos resources of a certain user
	GetListUserPhotos(userId string) ([]Photo_db, error)

	// Delete photo from the profile of a specific user. It also removes likes and comments
	DeletePhoto(photoId string) error

	// COMMENTS
	// Comment a photo
	CommentPhoto(photoId string, c Comment_db) (Comment_db, error)
	// Get list of comments
	GetComments(photoId string) ([]Comment_db, error)
	// Get single comment
	GetSingleComment(commentId string) (Comment_db, error)
	// Modify a comment
	ModifyComment(c Comment_db) (Comment_db, error)
	// Uncomment a photo
	UncommentPhoto(commentId string) error

	// LIKES
	// Put a like to a photo
	LikePhoto(photoId string, userId string) error
	// Delete a like to a photo
	UnlikePhoto(photoId string, userId string) error
	// Get likes to a photo
	GetLikes(photoId string) ([]Short_profile_db, error)

	// FOLLOWERS
	// Follow a user
	FollowUser(userId string, followerId string) error
	// Unfollow a user
	UnfollowUser(userId string, followerId string) error
	// Get a list of followers of a specific user
	GetFollowers(userId string) ([]Short_profile_db, error)
	// Get a list of the users the user is following
	GetFollowing(userId string) ([]Short_profile_db, error)

	// BANS
	// Ban a user
	BanUser(userId string, followerId string) error
	// Unfollow a user
	UnbanUser(userId string, followerId string) error
	// Get the list of users banned by a specific user
	GetBannedUsers(banner_id string) ([]Short_profile_db, error)

	// STREAM
	// Get the stream of photos of the users we are following in reverse chronological order
	GetMyStream(user_id string, offset string) ([]Photo_db, error)

	// UTILS
	// Get owner of a profile
	GetProfileOwner(photo_id string) (string, error)

	// Count the number of occurencies of a verb
	CountStuffs(filter string, table_name string, filterValue string) uint32

	// Convert id and name
	GetNameById(userId string) (string, error)
	GetIdByName(username string) (string, error)
	GetPhotoIdFromCommentId(comment_id string) (string, error)
	GetProfilePic(user_id string) (string, error)

	// check availability
	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 Comment_db

type Comment_db struct {
	// Id of the author of the comment
	UserId string
	// Id of the comment
	CommentId string
	// Date and time of creation of the comment following RFC3339
	Created_in string
	// Content of the comment
	Body string
	// Id of the photo under which the comments are being written
	PhotoId string
	// Date and time of when the comment was modified following RFC3339
	// If it wasn't modified, it coincides with the created_in date
	Modified_in string
	// States if a comment is a reply to another comment or not
	IsReplyComment bool
	// Id of the parent comment
	ParentId string
}

Attributes of a comment

type Photo_db

type Photo_db struct {
	PhotoId string
	// Date and time of creation following RFC3339
	Timestamp string
	// URL of the image just uploaded. | Accepting only http/https URLs and .png/.jpg/.jpeg extensions.
	Image string
	// A written description or explanation about a photo to provide more context
	Caption string
	// Id of the user that uploaded the photo
	UserId string
}

Attributes of a photo

type Profile_db

type Profile_db struct {

	// ID of the user
	ID string
	// Name of the user
	Username string
	// URL of the profile picture. Accepting only http/https URLs and .png/.jpg/.jpeg extensions.
	ProfilePictureUrl string
	// Biography of the profile. Just allowing alphanumeric characters and basic punctuation.
	Bio string
}

Represents the information seen in the Profile Page of a user

type Short_profile_db

type Short_profile_db struct {

	// Name of the user
	Username string
	// URL of the profile picture. Accepting only http/https URLs and .png/.jpg/.jpeg extensions.
	ProfilePictureUrl string
}

It's shown when viewing list of followers, likers...

Jump to

Keyboard shortcuts

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