1
Fork 0

Improve myAlbums query

This commit is contained in:
viktorstrate 2020-02-20 17:14:11 +01:00
parent 8f0489c706
commit ce11d5201b
5 changed files with 60 additions and 18 deletions

View File

@ -120,7 +120,7 @@ type ComplexityRoot struct {
Query struct { Query struct {
Album func(childComplexity int, id int) int Album func(childComplexity int, id int) int
MyAlbums func(childComplexity int, filter *models.Filter) int MyAlbums func(childComplexity int, filter *models.Filter, onlyRoot *bool, showEmpty *bool) int
MyPhotos func(childComplexity int, filter *models.Filter) int MyPhotos func(childComplexity int, filter *models.Filter) int
MyUser func(childComplexity int) int MyUser func(childComplexity int) int
Photo func(childComplexity int, id int) int Photo func(childComplexity int, id int) int
@ -191,7 +191,7 @@ type QueryResolver interface {
SiteInfo(ctx context.Context) (*models.SiteInfo, error) SiteInfo(ctx context.Context) (*models.SiteInfo, error)
User(ctx context.Context, filter *models.Filter) ([]*models.User, error) User(ctx context.Context, filter *models.Filter) ([]*models.User, error)
MyUser(ctx context.Context) (*models.User, error) MyUser(ctx context.Context) (*models.User, error)
MyAlbums(ctx context.Context, filter *models.Filter) ([]*models.Album, error) MyAlbums(ctx context.Context, filter *models.Filter, onlyRoot *bool, showEmpty *bool) ([]*models.Album, error)
Album(ctx context.Context, id int) (*models.Album, error) Album(ctx context.Context, id int) (*models.Album, error)
MyPhotos(ctx context.Context, filter *models.Filter) ([]*models.Photo, error) MyPhotos(ctx context.Context, filter *models.Filter) ([]*models.Photo, error)
Photo(ctx context.Context, id int) (*models.Photo, error) Photo(ctx context.Context, id int) (*models.Photo, error)
@ -637,7 +637,7 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in
return 0, false return 0, false
} }
return e.complexity.Query.MyAlbums(childComplexity, args["filter"].(*models.Filter)), true return e.complexity.Query.MyAlbums(childComplexity, args["filter"].(*models.Filter), args["onlyRoot"].(*bool), args["showEmpty"].(*bool)), true
case "Query.myPhotos": case "Query.myPhotos":
if e.complexity.Query.MyPhotos == nil { if e.complexity.Query.MyPhotos == nil {
@ -892,8 +892,14 @@ type Query {
"Information about the currently logged in user" "Information about the currently logged in user"
myUser: User! myUser: User!
"List of albums owned by the logged in user" "List of albums owned by the logged in user."
myAlbums(filter: Filter): [Album!]! myAlbums(
filter: Filter
"Return only albums from the root directory of the user"
onlyRoot: Boolean
"Return also albums with no photos directly in them"
showEmpty: Boolean
): [Album!]!
"Get album by id, user must own the album or be admin" "Get album by id, user must own the album or be admin"
album(id: Int!): Album! album(id: Int!): Album!
@ -1399,6 +1405,22 @@ func (ec *executionContext) field_Query_myAlbums_args(ctx context.Context, rawAr
} }
} }
args["filter"] = arg0 args["filter"] = arg0
var arg1 *bool
if tmp, ok := rawArgs["onlyRoot"]; ok {
arg1, err = ec.unmarshalOBoolean2ᚖbool(ctx, tmp)
if err != nil {
return nil, err
}
}
args["onlyRoot"] = arg1
var arg2 *bool
if tmp, ok := rawArgs["showEmpty"]; ok {
arg2, err = ec.unmarshalOBoolean2ᚖbool(ctx, tmp)
if err != nil {
return nil, err
}
}
args["showEmpty"] = arg2
return args, nil return args, nil
} }
@ -3537,7 +3559,7 @@ func (ec *executionContext) _Query_myAlbums(ctx context.Context, field graphql.C
ctx = ec.Tracer.StartFieldResolverExecution(ctx, rctx) ctx = ec.Tracer.StartFieldResolverExecution(ctx, rctx)
resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) {
ctx = rctx // use context from middleware stack in children ctx = rctx // use context from middleware stack in children
return ec.resolvers.Query().MyAlbums(rctx, args["filter"].(*models.Filter)) return ec.resolvers.Query().MyAlbums(rctx, args["filter"].(*models.Filter), args["onlyRoot"].(*bool), args["showEmpty"].(*bool))
}) })
if err != nil { if err != nil {
ec.Error(ctx, err) ec.Error(ctx, err)

View File

@ -9,7 +9,7 @@ import (
"github.com/viktorstrate/photoview/api/graphql/models" "github.com/viktorstrate/photoview/api/graphql/models"
) )
func (r *queryResolver) MyAlbums(ctx context.Context, filter *models.Filter) ([]*models.Album, error) { func (r *queryResolver) MyAlbums(ctx context.Context, filter *models.Filter, onlyRoot *bool, showEmpty *bool) ([]*models.Album, error) {
user := auth.UserFromContext(ctx) user := auth.UserFromContext(ctx)
if user == nil { if user == nil {
return nil, auth.ErrUnauthorized return nil, auth.ErrUnauthorized
@ -20,14 +20,28 @@ func (r *queryResolver) MyAlbums(ctx context.Context, filter *models.Filter) ([]
return nil, err return nil, err
} }
rows, err := r.Database.Query(` var rows *sql.Rows
filterEmpty := " AND EXISTS (SELECT * FROM photo WHERE album_id = album.album_id) "
if showEmpty != nil && *showEmpty == true {
filterEmpty = ""
}
if onlyRoot == nil || *onlyRoot == false {
rows, err = r.Database.Query("SELECT * FROM album WHERE owner_id = ?"+filterEmpty+filterSQL, user.UserID)
if err != nil {
return nil, err
}
} else {
rows, err = r.Database.Query(`
SELECT * FROM album WHERE owner_id = ? AND parent_album = ( SELECT * FROM album WHERE owner_id = ? AND parent_album = (
SELECT album_id FROM album WHERE parent_album IS NULL SELECT album_id FROM album WHERE parent_album IS NULL
) )
`+filterSQL, user.UserID) `+filterEmpty+filterSQL, user.UserID)
if err != nil { if err != nil {
return nil, err return nil, err
} }
}
albums, err := models.NewAlbumsFromRows(rows) albums, err := models.NewAlbumsFromRows(rows)
if err != nil { if err != nil {

View File

@ -22,8 +22,14 @@ type Query {
"Information about the currently logged in user" "Information about the currently logged in user"
myUser: User! myUser: User!
"List of albums owned by the logged in user" "List of albums owned by the logged in user."
myAlbums(filter: Filter): [Album!]! myAlbums(
filter: Filter
"Return only albums from the root directory of the user"
onlyRoot: Boolean
"Return also albums with no photos directly in them"
showEmpty: Boolean
): [Album!]!
"Get album by id, user must own the album or be admin" "Get album by id, user must own the album or be admin"
album(id: Int!): Album! album(id: Int!): Album!

View File

@ -20,7 +20,7 @@
"parcel-bundler": "^1.12.4", "parcel-bundler": "^1.12.4",
"prettier": "^1.19.1", "prettier": "^1.19.1",
"prop-types": "^15.7.2", "prop-types": "^15.7.2",
"react": "^16.9.0", "react": "^16.12.0",
"react-apollo": "^3.0.1", "react-apollo": "^3.0.1",
"react-dom": "^16.9.0", "react-dom": "^16.9.0",
"react-lazyload": "^2.6.5", "react-lazyload": "^2.6.5",

View File

@ -6,7 +6,7 @@ import { Query } from 'react-apollo'
const getAlbumsQuery = gql` const getAlbumsQuery = gql`
query getMyAlbums { query getMyAlbums {
myAlbums(filter: { order_by: "title" }) { myAlbums(filter: { order_by: "title" }, onlyRoot: true, showEmpty: true) {
id id
title title
thumbnail { thumbnail {