db

package
v0.0.0-...-9a0b025 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2021 License: AGPL-3.0 Imports: 9 Imported by: 0

Documentation

Overview

Package db defines the requirements for our database, via the Store interface, and implements it for postgres.

Index

Constants

This section is empty.

Variables

View Source
var (

	// ErrAlbumExists is returned when the unique requirement of an
	// album name is violated
	ErrAlbumExists = errors.New("DB: Album exists")
	// ErrAlbumNameInvalid is returned when an album name is not valid.
	// Most likely to fire when a slug is blank. (jphotos#60)
	ErrAlbumNameInvalid = errors.New("DB: Album name invalid")
)
View Source
var (
	// ErrNotFound is returned when the requested value isn't found
	ErrNotFound = errors.New("DB: Not Found")
)
View Source
var (
	// ErrUsernameExists is returned when the unique requirement of a username is violated
	ErrUsernameExists = errors.New("DB: Username exists")
)

Functions

This section is empty.

Types

type Album

type Album struct {
	Name, Slug, ID string
}

An Album is a view into an album

type Group

type Group struct {
	Name, UUID string
}

A Group holds information necessary for roles

type GroupMember

type GroupMember struct {
	Username string
	Admin    bool
}

A GroupMember is a member of a group

type PGStore

type PGStore struct {
	// contains filtered or unexported fields
}

A PGStore implements storage against PostGres

func NewPGStore

func NewPGStore(u, p, d string) (*PGStore, error)

NewPGStore connects to a postgres database

func (*PGStore) AddAlbum

func (pg *PGStore) AddAlbum(name string) error

AddAlbum adds an album to the albums table of the database

func (*PGStore) AddGroup

func (pg *PGStore) AddGroup(username string, groupname string) error

AddGroup creates a group and adds the creating user to it as an admin

func (*PGStore) AddPhoto

func (pg *PGStore) AddPhoto(p Photo, albumID string) error

AddPhoto adds a photo to the database

func (*PGStore) AddUser

func (pg *PGStore) AddUser(username string, hash []byte) error

AddUser adds a user to the users table of the database, hashing the password with bcrypt

func (*PGStore) DeleteAlbumBySlug

func (pg *PGStore) DeleteAlbumBySlug(slug string) error

DeleteAlbumBySlug deletes the album, and all photos in it, matching the slug

func (*PGStore) DeletePhotoByID

func (pg *PGStore) DeletePhotoByID(id string) error

DeletePhotoByID deletes the photo at the provided ID

func (*PGStore) Exec

func (pg *PGStore) Exec(query string, args ...interface{}) error

Exec executes a raw query against the DB, returns the result, and closes the connection

func (*PGStore) ExecuteSchema

func (pg *PGStore) ExecuteSchema(filename string) error

ExecuteSchema runs all the sql commands int he given file to initialize the database

func (*PGStore) GetAlbumBySlug

func (pg *PGStore) GetAlbumBySlug(slug string) (*Album, error)

GetAlbum returns an album, if it exists and matches the provided id

func (*PGStore) GetAlbumIDByPhotoID

func (pg *PGStore) GetAlbumIDByPhotoID(photoID string) (string, error)

GetAlbumIDByPhotoID returns the album slug a photo belongs to

func (*PGStore) GetAlbumPhotosByID

func (pg *PGStore) GetAlbumPhotosByID(id string) ([]Photo, error)

GetAlbumPhotos returns a list of all photos in an album

func (*PGStore) GetAlbumSlugByID

func (pg *PGStore) GetAlbumSlugByID(id string) (string, error)

GetAlbumSlugByID returns the slug matching the provided ID

func (*PGStore) GetAllAlbums

func (pg *PGStore) GetAllAlbums() ([]Album, error)

GetAlbums returns a list of all Albums

func (*PGStore) GetAllUsers

func (pg *PGStore) GetAllUsers() ([]User, error)

GetAllUsers returns a slice of Users

func (*PGStore) GetGroupByID

func (pg *PGStore) GetGroupByID(id string) (Group, []GroupMember, error)

GetGroupByID returns a group and a list of all its members

func (*PGStore) GetGroupsForUser

func (pg *PGStore) GetGroupsForUser(u User) ([]Group, error)

GetGroupsForUser returns all the groups a user can access

func (*PGStore) GetPhotoByID

func (pg *PGStore) GetPhotoByID(id string) (*Photo, error)

GetPhotoByID returns a photo object for that ID.

func (*PGStore) GetUserByUsername

func (pg *PGStore) GetUserByUsername(username string) (*User, error)

GetUserByUsername returns the DB user information for a user if that user exists

func (*PGStore) Query

func (pg *PGStore) Query(query string, args ...interface{}) (*sql.Rows, error)

Query executes a raw query against the DB and returns the result

func (*PGStore) RemoveUser

func (pg *PGStore) RemoveUser(username string) error

RemoveUser removes a user from the database.

func (*PGStore) RenameAlbumByID

func (pg *PGStore) RenameAlbumByID(id, newName string) error

RenameAlbum renames an album

func (*PGStore) RevokeSession

func (pg *PGStore) RevokeSession(session string) error

RevokeSession removes a user's session from the sessions table

func (*PGStore) SessionGet

func (pg *PGStore) SessionGet(session string, newExpiration time.Time) (*Session, error)

SessionGet checks the database for a session and returns it if found If the session is absent, an error is returned. SessionGet will not return an expired session.

func (*PGStore) UpdatePhotoAlbum

func (pg *PGStore) UpdatePhotoAlbum(photoID, albumID string) error

UpdatePhotoAlbum changes the album a photo belongs to

func (*PGStore) UpdatePhotoCaptionByID

func (pg *PGStore) UpdatePhotoCaptionByID(id, newCaption string) error

UpdatePhotoCaptionByID updates the photo's caption

func (*PGStore) UserAddSession

func (pg *PGStore) UserAddSession(user User, session string, expires time.Time) error

UserAddSession stores a session in the database

type Photo

type Photo struct {
	ID, Caption, Location string
	Added                 time.Time
}

A Photo is a view into a photo

type Session

type Session struct {
	User    User
	Expires time.Time
}

A Session is a view into a session

func (Session) IsExpired

func (s Session) IsExpired() bool

IsExpired returns true is a session has expired

type Store

type Store interface {
	ExecuteSchema(filename string) error
	AddUser(username string, hash []byte) error
	RemoveUser(username string) error
	GetAllUsers() ([]User, error)
	GetUserByUsername(username string) (*User, error)
	UserAddSession(user User, session string, expires time.Time) error

	//
	// Album Methods
	// These are methods used to primarily access the albums tabl
	GetAllAlbums() ([]Album, error)
	GetAlbumBySlug(slug string) (*Album, error)
	GetAlbumPhotosByID(id string) ([]Photo, error)
	GetAlbumSlugByID(id string) (string, error)

	AddAlbum(name string) error

	RenameAlbumByID(id, newName string) error

	DeleteAlbumBySlug(slug string) error

	//
	// Photo Methods
	// These are methods used to primarily access the photos table
	AddPhoto(p Photo, albumID string) error
	GetPhotoByID(id string) (*Photo, error)
	GetAlbumIDByPhotoID(id string) (string, error)

	UpdatePhotoCaptionByID(id, newCaption string) error
	UpdatePhotoAlbum(photoID, albumID string) error

	DeletePhotoByID(id string) error

	//
	// Group Methods
	GetGroupsForUser(u User) ([]Group, error)
	GetGroupByID(id string) (Group, []GroupMember, error)

	// SessionGet returns a valid session if one exists.
	// Guranteed to not return expired sessinos.
	// If a valid session is found, extend it! I don't recommend passing
	// in a time that's past, though.
	SessionGet(session string, newExpiration time.Time) (*Session, error)
	RevokeSession(session string) error
}

A Store provides the methods required to access the database.

type User

type User struct {
	Username, Created string
	Hash              []byte
	// contains filtered or unexported fields
}

A User is a view into the details for a given user

Jump to

Keyboard shortcuts

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