2020-02-05 14:51:46 +01:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2020-02-09 15:26:59 +01:00
|
|
|
"path"
|
2020-04-06 22:16:25 +02:00
|
|
|
|
|
|
|
"github.com/viktorstrate/photoview/api/utils"
|
2020-02-05 14:51:46 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type Photo struct {
|
2020-06-17 18:00:58 +02:00
|
|
|
PhotoID int
|
|
|
|
Title string
|
|
|
|
Path string
|
|
|
|
AlbumId int
|
|
|
|
ExifId *int
|
|
|
|
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"
|
2020-02-14 13:31:44 +01:00
|
|
|
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{}
|
|
|
|
|
2020-06-17 18:00:58 +02:00
|
|
|
if err := row.Scan(&photo.PhotoID, &photo.Title, &photo.Path, &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
|
2020-06-17 18:00:58 +02:00
|
|
|
if err := rows.Scan(&photo.PhotoID, &photo.Title, &photo.Path, &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 {
|
2020-04-05 18:54:13 +02:00
|
|
|
|
2020-04-06 22:16:25 +02:00
|
|
|
imageUrl := utils.ApiEndpointUrl()
|
|
|
|
imageUrl.Path = path.Join(imageUrl.Path, "photo", p.PhotoName)
|
2020-04-05 18:54:13 +02:00
|
|
|
|
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
|
|
|
|
}
|