Files
ersteller/authentication/google/googleauth-repository_gen.go
T

298 lines
6.9 KiB
Go

package google
// GENERATED FILE
// DO NOT EDIT
import (
"context"
"encoding/json"
"errors"
"fmt"
"git.gorlug.de/code/golang/ersteller-lib/ersteller"
"github.com/doug-martin/goqu/v9"
_ "github.com/doug-martin/goqu/v9/dialect/postgres"
"github.com/doug-martin/goqu/v9/exp"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"strings"
"time"
)
type GoogleAuthRepository struct {
connPool *pgxpool.Pool
dialect goqu.DialectWrapper
}
func NewGoogleAuthRepository(connPool *pgxpool.Pool) *GoogleAuthRepository {
return &GoogleAuthRepository{
connPool: connPool,
dialect: goqu.Dialect("postgres"),
}
}
func (r *GoogleAuthRepository) Create(googleAuth GoogleAuth) (int, error) {
sql, args, err := r.dialect.Insert("googleAuth").
Prepared(true).
Rows(goqu.Record{
"updated_at": time.Now(),
"credentials": r.jsonToString(googleAuth.Credentials),
"user_id": googleAuth.UserId,
}).
Returning("id").
ToSQL()
if err != nil {
ersteller.LogError("error creating create GoogleAuth sql: %v", err)
return -1, err
}
rows, err := r.connPool.Query(context.Background(), sql, args...)
if err != nil {
ersteller.LogError("error creating GoogleAuth: %v", err)
return -1, err
}
defer rows.Close()
var id int
if rows.Next() {
err = rows.Scan(&id)
if err != nil {
ersteller.LogError("error scanning User: %v", err)
return -1, err
}
} else {
ersteller.Error("GoogleAuth already exists")
return -1, GoogleAuthAlreadyExistsError{GoogleAuth: googleAuth}
}
return id, nil
}
type GoogleAuthAlreadyExistsError struct {
GoogleAuth GoogleAuth
}
func (e GoogleAuthAlreadyExistsError) Error() string {
return fmt.Sprint("GoogleAuth ", e.GoogleAuth, " already exists")
}
func (r *GoogleAuthRepository) getSelectColumns() []any {
return []any{"id", "created_at", "updated_at",
"credentials", "user_id",
}
}
func (r *GoogleAuthRepository) Read(userId int, id int) (GoogleAuth, error) {
ersteller.Debug("Getting GoogleAuth by id ", id)
sql, args, _ := r.dialect.From("googleAuth").
Prepared(true).
Select(r.getSelectColumns()...).
Where(goqu.Ex{
"id": id,
"user_id": userId,
}).
ToSQL()
rows, err := r.connPool.Query(context.Background(), sql, args...)
if err != nil {
ersteller.Error("Failed to get GoogleAuth: ", err)
}
defer rows.Close()
if rows.Next() {
item, _, err := r.rowToItem(rows, false)
return item, err
}
return GoogleAuth{}, errors.New("no rows found")
}
type GoogleAuthItemScan struct {
GoogleAuth
RowId int
Count int
}
func (r *GoogleAuthRepository) rowToItem(rows pgx.Rows, rowId bool) (GoogleAuth, int, error) {
var item GoogleAuthItemScan
if rowId {
err := rows.Scan(
&item.RowId,
&item.Count,
&item.Id,
&item.CreatedAt,
&item.UpdatedAt,
&item.Credentials,
&item.UserId,
)
if err != nil {
return GoogleAuth{}, -1, err
}
} else {
err := rows.Scan(
&item.Id,
&item.CreatedAt,
&item.UpdatedAt,
&item.Credentials,
&item.UserId,
)
if err != nil {
return GoogleAuth{}, -1, err
}
}
return GoogleAuth{
Id: item.Id,
CreatedAt: item.CreatedAt,
UpdatedAt: item.UpdatedAt,
Credentials: item.Credentials,
UserId: item.UserId,
}, item.Count, nil
}
func (r *GoogleAuthRepository) Update(userId int, googleAuth GoogleAuth) error {
sql, args, err := r.dialect.Update("googleAuth").
Prepared(true).
Set(goqu.Record{
"updated_at": time.Now(),
"credentials": r.jsonToString(googleAuth.Credentials),
"user_id": googleAuth.UserId,
}).
Where(goqu.Ex{
"id": googleAuth.Id,
"user_id": userId,
}).
ToSQL()
if err != nil {
ersteller.LogError("error creating update GoogleAuth sql: %v", err)
return err
}
_, err = r.connPool.Exec(context.Background(), sql, args...)
if err != nil {
ersteller.LogError("error updating GoogleAuth: %v", err)
return err
}
return nil
}
func (r *GoogleAuthRepository) Delete(userId int, id int) error {
sql, args, err := r.dialect.Delete("googleAuth").
Prepared(true).
Where(goqu.Ex{
"id": id,
"user_id": userId,
}).
ToSQL()
if err != nil {
ersteller.LogError("error creating delete GoogleAuth sql: %v", err)
return err
}
_, err = r.connPool.Exec(context.Background(), sql, args...)
if err != nil {
ersteller.LogError("error deleting GoogleAuth: %v", err)
return err
}
return nil
}
type GoogleAuthField string
const (
GoogleAuthFieldCredentials GoogleAuthField = "credentials"
)
type GoogleAuthOrderDirection string
const (
GoogleAuthOrderDirectionAsc GoogleAuthOrderDirection = "asc"
GoogleAuthOrderDirectionDesc GoogleAuthOrderDirection = "desc"
)
type GoogleAuthReferences struct {
UserId int
}
type GoogleAuthPaginationParams struct {
RowId int
PageSize int
OrderBy GoogleAuthField
OrderDirection GoogleAuthOrderDirection
References GoogleAuthReferences
}
func (r *GoogleAuthRepository) GetPage(params GoogleAuthPaginationParams) ([]GoogleAuth, int, error) {
var orderByWindow exp.WindowExpression
if params.OrderDirection == GoogleAuthOrderDirectionAsc {
orderByWindow = goqu.W().OrderBy(goqu.C(string(params.OrderBy)).Asc())
} else {
orderByWindow = goqu.W().OrderBy(goqu.C(string(params.OrderBy)).Desc())
}
selectColumns := []any{
goqu.ROW_NUMBER().Over(orderByWindow).As("row_id"),
goqu.COUNT("*"),
}
selectColumns = append(selectColumns, r.getSelectColumns()...)
whereExpressions := []goqu.Expression{
goqu.Ex{
"user_id": params.References.UserId,
},
}
whereExpressions = r.addPageFilters(params, whereExpressions)
var colOrder exp.OrderedExpression
if params.OrderDirection == GoogleAuthOrderDirectionAsc {
colOrder = goqu.C(string(params.OrderBy)).Asc()
} else {
colOrder = goqu.C(string(params.OrderBy)).Desc()
}
dialect := goqu.Dialect("postgres")
innerFrom := dialect.From("googleAuth").
Prepared(true).
Select(selectColumns...).
Where(whereExpressions...).
Order(colOrder)
outerFrom := dialect.From(innerFrom).
Prepared(true).
Where(goqu.Ex{"row_id": goqu.Op{"gt": params.RowId}})
if params.PageSize > 0 {
outerFrom = outerFrom.Limit(uint(params.PageSize))
}
sql, args, _ := outerFrom.ToSQL()
sql = strings.Replace(sql, "COUNT(*)", "COUNT(*) over()", 1)
rows, err := r.connPool.Query(context.Background(), sql, args...)
if err != nil {
ersteller.LogError("failed to run sql query: %v", err)
return nil, -1, err
}
defer rows.Close()
results := make([]GoogleAuth, 0)
totalCount := 0
for rows.Next() {
parsed, count, err := r.rowToItem(rows, true)
if err != nil {
return nil, -1, err
}
totalCount = count
results = append(results, parsed)
}
return results, totalCount, nil
}
func (r *GoogleAuthRepository) addPageFilters(params GoogleAuthPaginationParams, whereExpressions []goqu.Expression) []goqu.Expression {
return whereExpressions
}
func (r *GoogleAuthRepository) jsonToString(jsonData any) string {
bytes, err := json.Marshal(jsonData)
if err != nil {
return "{}"
}
return string(bytes)
}