package models

import (
	"time"

	"git.trj.tw/golang/utils/dbtool"
)

// Photo model
type Photo struct {
	ID        string    `sql:"id" cc:"id"`
	Album     string    `sql:"album" cc:"album"`
	Name      string    `sql:"name" cc:"name"`
	Thumbnail string    `sql:"thumbnail" cc:"thumbnail"`
	Ctime     time.Time `sql:"ctime" cc:"ctime"`
	Mtime     time.Time `sql:"mtime" cc:"mtime"`
}

// GetPhotos -
func GetPhotos(album string) (photo []Photo, err error) {
	rows, err := x.Query(`select "id", "album", "name", "thumbnail", "ctime", "mtime" from "storage"."photo" where "album" = $1`, album)
	if err != nil {
		return nil, err
	}

	err = dbtool.ScanToStructAll(rows, &photo)
	if err != nil {
		return nil, err
	}

	return
}

// GetPhoto -
func GetPhoto(id string) (photo *Photo, err error) {
	rows, err := x.Query(`select "id", "album", "name", "thumbnail", "ctime", "mtime" from "storage"."photo" where "id" = $1`, id)
	if err != nil {
		return nil, err
	}

	photo = &Photo{}
	ok, err := dbtool.ScanToStruct(rows, photo)
	if err != nil {
		return nil, err
	}

	if !ok {
		return nil, nil
	}

	return
}
