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

82 lines
1.5 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
"net/url"
"os"
"path"
2020-02-05 14:51:46 +01:00
)
type Photo struct {
2020-02-09 12:53:21 +01:00
PhotoID int
Title string
Path string
AlbumId int
ExifId *int
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-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{}
2020-02-09 12:53:21 +01:00
if err := row.Scan(&photo.PhotoID, &photo.Title, &photo.Path, &photo.AlbumId, &photo.ExifId); 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
2020-02-09 12:53:21 +01:00
if err := rows.Scan(&photo.PhotoID, &photo.Title, &photo.Path, &photo.AlbumId, &photo.ExifId); err != nil {
2020-02-05 16:14:21 +01:00
return nil, err
}
photos = append(photos, &photo)
}
return photos, nil
}
2020-02-05 14:51:46 +01:00
func (p *PhotoURL) URL() string {
2020-02-09 15:26:59 +01:00
imageUrl, err := url.Parse(os.Getenv("API_ENDPOINT"))
if err != nil {
return path.Join("/photo", p.PhotoName)
}
imageUrl.Path = path.Join(imageUrl.Path, "photo", p.PhotoName)
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
}