package queries

import (
	"github.com/fiskerinc/cloud-services/pkg/common"

	"github.com/go-pg/pg/v10/orm"
	"github.com/pkg/errors"
)

type IssueImagesInterface interface {
	Insert(issueImage *[]common.IssueImage) (orm.Result, error)
	SearchByIssueID(issueID string) ([]common.IssueImage, error)
}

type IssueImages struct {
	QueryBase
}

func (c *IssueImages) Insert(issueImages *[]common.IssueImage) (orm.Result, error) {

	res, err := c.GetDBConn().Model(issueImages).Insert()
	if err != nil {
		return nil, errors.WithStack(err)
	}
	return res, nil
}

func (c *IssueImages) SearchByIssueID(issueID string) ([]common.IssueImage, error) {
	issueImages := []common.IssueImage{}
	query := c.GetDBConn().Model(&issueImages)

	err := query.Where("issue_id = ?", issueID).Select()

	return issueImages, errors.WithStack(err)
}
