1
Fork 0

Add sql tables for photos and albums

This commit is contained in:
viktorstrate 2020-02-01 14:52:27 +01:00
parent 9892366143
commit e2049bd6e5
10 changed files with 1440 additions and 17 deletions

View File

@ -1,2 +0,0 @@
DROP TABLE IF EXISTS users;
DROP TABLE IF NOT EXISTS access_tokens;

View File

@ -0,0 +1,2 @@
DROP TABLE IF EXISTS user;
DROP TABLE IF NOT EXISTS access_token;

View File

@ -1,4 +1,4 @@
CREATE TABLE IF NOT EXISTS users (
CREATE TABLE IF NOT EXISTS user (
user_id int NOT NULL AUTO_INCREMENT,
username varchar(255) NOT NULL UNIQUE,
password varchar(255) NOT NULL,
@ -8,12 +8,12 @@ CREATE TABLE IF NOT EXISTS users (
PRIMARY KEY (user_id)
);
CREATE TABLE IF NOT EXISTS access_tokens (
CREATE TABLE IF NOT EXISTS access_token (
token_id int NOT NULL AUTO_INCREMENT,
user_id int NOT NULL,
value char(24) NOT NULL UNIQUE,
expire timestamp NOT NULL,
PRIMARY KEY (token_id),
FOREIGN KEY (user_id) REFERENCES users(user_id)
FOREIGN KEY (user_id) REFERENCES user(user_id)
);

View File

@ -0,0 +1,3 @@
DROP TABLE IF EXISTS photo;
DROP TABLE IF EXISTS album;
DROP TABLE IF EXISTS photo_url;

View File

@ -0,0 +1,33 @@
CREATE TABLE IF NOT EXISTS photo_url (
url_id int NOT NULL AUTO_INCREMENT,
width int NOT NULL,
height int NOT NULL,
PRIMARY KEY (url_id)
);
CREATE TABLE IF NOT EXISTS album (
album_id int NOT NULL AUTO_INCREMENT,
title varchar(256) NOT NULL,
parent_album int,
owner_id int NOT NULL,
path varchar(512) NOT NULL,
PRIMARY KEY (album_id),
FOREIGN KEY (parent_album) REFERENCES album(album_id),
FOREIGN KEY (owner_id) REFERENCES user(user_id)
);
CREATE TABLE IF NOT EXISTS photo (
photo_id int NOT NULL AUTO_INCREMENT,
title varchar(256) NOT NULL,
path varchar(512) NOT NULL,
original_url int NOT NULL,
thumbnail_url int NOT NULL,
album_id int NOT NULL,
-- exif_id int NOT NULL,
PRIMARY KEY (photo_id),
FOREIGN KEY (original_url) REFERENCES photo_url(url_id),
FOREIGN KEY (thumbnail_url) REFERENCES photo_url(url_id)
);

File diff suppressed because it is too large Load Diff

View File

@ -2,8 +2,40 @@
package models
type Album struct {
ID string `json:"id"`
Title *string `json:"title"`
Photos []*Photo `json:"photos"`
SubAlbums []*Album `json:"subAlbums"`
ParentAlbum *Album `json:"parentAlbum"`
Owner *User `json:"owner"`
Path *string `json:"path"`
}
type AuthorizeResult struct {
Success bool `json:"success"`
Status string `json:"status"`
Token *string `json:"token"`
}
type Photo struct {
ID string `json:"id"`
Title *string `json:"title"`
// Local filepath for the photo
Path *string `json:"path"`
// URL to display the photo in full resolution
Original *PhotoURL `json:"original"`
// URL to display the photo in a smaller resolution
Thumbnail *PhotoURL `json:"thumbnail"`
// The album that holds the photo
Album *Album `json:"album"`
}
type PhotoURL struct {
// URL for previewing the image
URL *string `json:"url"`
// Width of the image in pixels
Width *int `json:"width"`
// Height of the image in pixels
Height *int `json:"height"`
}

View File

@ -60,7 +60,7 @@ func NewUsersFromRows(rows *sql.Rows) ([]*User, error) {
}
func AuthorizeUser(database *sql.DB, username string, password string) (*User, error) {
row := database.QueryRow("SELECT * FROM users WHERE username = ?", username)
row := database.QueryRow("SELECT * FROM user WHERE username = ?", username)
user, err := NewUserFromRow(row)
if err != nil {
@ -85,11 +85,11 @@ func RegisterUser(database *sql.DB, username string, password string, rootPath s
}
hashedPass := string(hashedPassBytes)
if _, err := database.Exec("INSERT INTO users (username, password, root_path) VALUES (?, ?, ?)", username, hashedPass, rootPath); err != nil {
if _, err := database.Exec("INSERT INTO user (username, password, root_path) VALUES (?, ?, ?)", username, hashedPass, rootPath); err != nil {
return nil, err
}
row := database.QueryRow("SELECT * FROM users WHERE username = ?", username)
row := database.QueryRow("SELECT * FROM user WHERE username = ?", username)
if row == nil {
return nil, ErrorInvalidUserCredentials
}
@ -116,7 +116,7 @@ func (user *User) GenerateAccessToken(database *sql.DB) (*AccessToken, error) {
expire := time.Now().Add(14 * 24 * time.Hour)
expireString := expire.UTC().Format("2006-01-02 15:04:05")
if _, err := database.Exec("INSERT INTO access_tokens (value, expire, user_id) VALUES (?, ?, ?)", token_value, expireString, user.UserID); err != nil {
if _, err := database.Exec("INSERT INTO access_token (value, expire, user_id) VALUES (?, ?, ?)", token_value, expireString, user.UserID); err != nil {
return nil, err
}
@ -132,7 +132,7 @@ func VerifyTokenAndGetUser(database *sql.DB, token string) (*User, error) {
now := time.Now().UTC().Format("2006-01-02 15:04:05")
row := database.QueryRow("SELECT (user_id) FROM access_tokens WHERE expire > ? AND value = ?", now, token)
row := database.QueryRow("SELECT (user_id) FROM access_token WHERE expire > ? AND value = ?", now, token)
var userId string
@ -141,7 +141,7 @@ func VerifyTokenAndGetUser(database *sql.DB, token string) (*User, error) {
return nil, err
}
row = database.QueryRow("SELECT * FROM users WHERE user_id = ?", userId)
row = database.QueryRow("SELECT * FROM user WHERE user_id = ?", userId)
user, err := NewUserFromRow(row)
if err != nil {
return nil, err

View File

@ -40,3 +40,16 @@ func (r *queryResolver) Users(ctx context.Context) ([]*models.User, error) {
return users, nil
}
func (r *queryResolver) MyAlbums(ctx context.Context) ([]*models.Album, error) {
panic("Not implemented")
}
func (r *queryResolver) Album(ctx context.Context, id *string) (*models.Album, error) {
panic("Not implemented")
}
func (r *queryResolver) MyPhotos(ctx context.Context) ([]*models.Photo, error) {
panic("Not implemented")
}
func (r *queryResolver) Photo(ctx context.Context, id string) (*models.Photo, error) {
panic("Not implemented")
}

View File

@ -3,9 +3,20 @@ directive @isAdmin on FIELD_DEFINITION
scalar Time
type Query {
"List of registered users, must be admin to call"
users: [User!]! @isAdmin
"Information about the currently logged in user"
myUser: User
"List of albums owned by the logged in user"
myAlbums: [Album]
"Get album by id, user must own the album or be admin"
album(id: ID): Album
"List of photos owned by the logged in user"
myPhotos: [Photo]
"Get photo by id, user must own the photo or be admin"
photo(id: ID!): Photo
}
type Mutation {
@ -28,8 +39,46 @@ type User {
id: ID!
username: String!
#albums: [Album]
# Local filepath for the user's photos
"Local filepath for the user's photos"
rootPath: String! @isAdmin
admin: Boolean!
#shareTokens: [ShareToken]
}
type Album {
id: ID!
title: String
photos: [Photo]
subAlbums: [Album]
parentAlbum: Album
owner: User!
path: String
# shares: [ShareToken]
}
type PhotoURL {
"URL for previewing the image"
url: String
"Width of the image in pixels"
width: Int
"Height of the image in pixels"
height: Int
}
type Photo {
id: ID!
title: String
"Local filepath for the photo"
path: String
"URL to display the photo in full resolution"
original: PhotoURL
"URL to display the photo in a smaller resolution"
thumbnail: PhotoURL
"The album that holds the photo"
album: Album!
# exif: PhotoEXIF
# shares: [ShareToken]
# downloads: [PhotoDownload]
}