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 ¶
var ( ErrUserNotFound = errors.New("error: user does not exist") ErrAlreadyBlocked = errors.New("error: user is already blocked") ErrPostNotFound = errors.New("error: post does not exist") ErrUserIsBlocked = errors.New("error: user is blocked") ErrUserNotBlocked = errors.New("error: user is not blocked") ErrCommentNotFound = errors.New("error: comment not found") ErrUserAlreadyExists = errors.New("error: user already exists") ErrNotFollowing = errors.New("cannot unfollow user: is not followed") ErrAlreadyFollowing = errors.New("error: already following") ErrDidNotLike = errors.New("error: user did not like post/comment") ErrBadImage = errors.New("error: bad image") ErrUserIsNotAuthor = errors.New("error: you cannot delete somebody else's post") ErrAlreadyLiked = errors.New("already liked") ErrBadCharset = errors.New("bad charset") ErrUsernameAlreadyTaken = errors.New("username already taken") )
Custom errors
Functions ¶
This section is empty.
Types ¶
type Account ¶
type Account struct { UserID int64 `json:"userID"` Username string `json:"username"` ProPicB64 string `json:"proPicB64"` Followers uint `json:"followers"` Following uint `json:"following"` Posts []int64 `json:"posts"` }
Data model
type AppDatabase ¶
type AppDatabase interface { UserExists(userID int64) (bool, error) UsersExist(user1 int64, user2 int64) (bool, error) UsernameTaken(login string) (bool, error) RegisterUser(username string) (int64, error) SetProPic(userID int64, imgB64 string) error SetUsername(userID int64, username string) error Follows(follower int64, following int64) (bool, error) Follow(follower int64, toFollow int64) error Unfollow(follower int64, toUnfollow int64) error GetFollowers(id int64) ([]int64, error) GetFollowing(id int64) ([]int64, error) GetBlocked(id int64) ([]int64, error) RmFollower(user int64, follower int64) error Block(user int64, toBlock int64) error Unblock(user int64, toUnblock int64) error IsBlockedBy(blocked int64, blocker int64) (bool, error) NewPost(op int64, imgpath string, caption string) (int64, error) RmPost(op int64, postid int64) error PostExists(postID int64) (bool, error) GetPost(userID int64, postid int64) (Post, error) GetAccount(id int64, userID int64) (Account, error) CommentExists(commentID int64) (bool, error) GetComment(commentID int64) (Comment, error) IsLiked(user int64, post int64) (bool, error) IsCommentLiked(user int64, comment int64) (bool, error) LikePost(user int64, postID int64) error UnlikePost(user int64, postID int64) error CommentPost(user int64, postID int64, comment string) (int64, error) LikeComment(user int64, commentID int64) error UnlikeComment(user int64, commentID int64) error DeleteComment(user int64, commentID int64) error GetFeed(user int64) ([]int64, error) SearchUser(query string) ([]int64, error) Ping() error }
AppDatabase is the high level interface for the DB
Source Files ¶
- block.go
- comment-exists.go
- comment-post.go
- database.go
- delete-comment.go
- follow.go
- follows.go
- get-account.go
- get-blocked.go
- get-comment.go
- get-feed.go
- get-followers.go
- get-following.go
- get-post.go
- is-blocked-by.go
- is-comment-liked.go
- is-liked.go
- like-comment.go
- like-post.go
- new-post.go
- post-exists.go
- register-user.go
- rm-follower.go
- rm-post.go
- search-user.go
- set-propic.go
- set-username.go
- unblock.go
- unfollow.go
- unlike-comment.go
- unlike-post.go
- user-exists.go
- users-exist.go