1
Fork 0
photoview/api/graphql/models/photo.go

101 lines
2.0 KiB
Go
Raw Normal View History

2020-02-05 14:51:46 +01:00
package models
import (
"database/sql"
2020-02-09 15:26:59 +01:00
"path"
"github.com/viktorstrate/photoview/api/utils"
2020-02-05 14:51:46 +01:00
)
type Photo struct {
PhotoID int
Title string
Path string
PathHash string
AlbumId int
ExifId *int
2020-06-17 18:00:58 +02:00
Favorite bool
2020-02-05 14:51:46 +01:00
}
2020-02-09 21:25:33 +01:00
func (p *Photo) ID() int {
return p.PhotoID
}
2020-02-09 12:53:21 +01:00
type PhotoPurpose string
const (
PhotoThumbnail PhotoPurpose = "thumbnail"
PhotoHighRes PhotoPurpose = "high-res"
PhotoOriginal PhotoPurpose = "original"
2020-02-09 12:53:21 +01:00
)
2020-02-05 14:51:46 +01:00
type PhotoURL struct {
2020-02-09 14:21:53 +01:00
UrlID int
PhotoId int
PhotoName string
Width int
Height int
Purpose PhotoPurpose
ContentType string
2020-02-05 14:51:46 +01:00
}
func NewPhotoFromRow(row *sql.Row) (*Photo, error) {
photo := Photo{}
if err := row.Scan(&photo.PhotoID, &photo.Title, &photo.Path, &photo.PathHash, &photo.AlbumId, &photo.ExifId, &photo.Favorite); err != nil {
2020-02-05 14:51:46 +01:00
return nil, err
}
return &photo, nil
}
2020-02-05 16:14:21 +01:00
func NewPhotosFromRows(rows *sql.Rows) ([]*Photo, error) {
photos := make([]*Photo, 0)
for rows.Next() {
var photo Photo
if err := rows.Scan(&photo.PhotoID, &photo.Title, &photo.Path, &photo.PathHash, &photo.AlbumId, &photo.ExifId, &photo.Favorite); err != nil {
2020-02-05 16:14:21 +01:00
return nil, err
}
photos = append(photos, &photo)
}
2020-02-28 20:57:46 +01:00
rows.Close()
2020-02-05 16:14:21 +01:00
return photos, nil
}
2020-02-05 14:51:46 +01:00
func (p *PhotoURL) URL() string {
imageUrl := utils.ApiEndpointUrl()
imageUrl.Path = path.Join(imageUrl.Path, "photo", p.PhotoName)
2020-02-09 15:26:59 +01:00
return imageUrl.String()
2020-02-09 14:21:53 +01:00
}
func NewPhotoURLFromRow(row *sql.Row) (*PhotoURL, error) {
url := PhotoURL{}
if err := row.Scan(&url.UrlID, &url.PhotoId, &url.PhotoName, &url.Width, &url.Height, &url.Purpose, &url.ContentType); err != nil {
return nil, err
}
return &url, nil
2020-02-05 14:51:46 +01:00
}
2020-02-21 22:42:39 +01:00
func NewPhotoURLFromRows(rows *sql.Rows) ([]*PhotoURL, error) {
urls := make([]*PhotoURL, 0)
for rows.Next() {
var url PhotoURL
if err := rows.Scan(&url.UrlID, &url.PhotoId, &url.PhotoName, &url.Width, &url.Height, &url.Purpose, &url.ContentType); err != nil {
return nil, err
}
urls = append(urls, &url)
}
2020-02-28 20:57:46 +01:00
rows.Close()
2020-02-21 22:42:39 +01:00
return urls, nil
}