From 3ce4ab02cced12fc927d24aff9f02e76d53019db Mon Sep 17 00:00:00 2001
From: stz184 <ivanov.bg@gmail.com>
Date: Sat, 19 Dec 2020 00:50:29 +0200
Subject: [PATCH 1/3] Migrated the search to gorm

---
 api/graphql/resolvers/search.go | 104 ++++++++++++++++----------------
 1 file changed, 52 insertions(+), 52 deletions(-)

diff --git a/api/graphql/resolvers/search.go b/api/graphql/resolvers/search.go
index a63c27b..8fa04aa 100644
--- a/api/graphql/resolvers/search.go
+++ b/api/graphql/resolvers/search.go
@@ -2,71 +2,71 @@ package resolvers
 
 import (
 	"context"
+	"github.com/photoview/photoview/api/graphql/auth"
+	"github.com/pkg/errors"
+	"gorm.io/gorm/clause"
 
 	"github.com/photoview/photoview/api/graphql/models"
 )
 
 func (r *Resolver) Search(ctx context.Context, query string, _limitMedia *int, _limitAlbums *int) (*models.SearchResult, error) {
-	// user := auth.UserFromContext(ctx)
-	// if user == nil {
-	// 	return nil, auth.ErrUnauthorized
-	// }
+	user := auth.UserFromContext(ctx)
+	if user == nil {
+		return nil, auth.ErrUnauthorized
+	}
 
-	// limitMedia := 10
-	// limitAlbums := 10
+	limitMedia := 10
+	limitAlbums := 10
 
-	// if _limitMedia != nil {
-	// 	limitMedia = *_limitMedia
-	// }
+	if _limitMedia != nil {
+		limitMedia = *_limitMedia
+	}
 
-	// if _limitAlbums != nil {
-	// 	limitAlbums = *_limitAlbums
-	// }
+	if _limitAlbums != nil {
+		limitAlbums = *_limitAlbums
+	}
 
-	// wildQuery := "%" + query + "%"
+	wildQuery := "%" + query + "%"
 
-	// photoRows, err := r.Database.Query(`
-	// 	SELECT media.* FROM media JOIN album ON media.album_id = album.album_id
-	// 	WHERE album.owner_id = ? AND ( media.title LIKE ? OR media.path LIKE ? )
-	// 	ORDER BY (
-	// 		case when media.title LIKE ? then 2
-	// 		     when media.path LIKE ? then 1
-	// 		end ) DESC
-	// 	LIMIT ?
-	// `, user.UserID, wildQuery, wildQuery, wildQuery, wildQuery, limitMedia)
-	// if err != nil {
-	// 	return nil, errors.Wrapf(err, "searching media")
-	// }
+	var photos []*models.Media
 
-	// photos, err := models.NewMediaFromRows(photoRows)
-	// if err != nil {
-	// 	return nil, err
-	// }
+	err := r.Database.Joins("JOIN albums ON media.album_id = albums.id").
+		Where("albums.owner_id = ? AND ( media.title LIKE ? OR media.path LIKE ? )", user.ID, wildQuery, wildQuery).
+		Clauses(clause.OrderBy{
+			Expression: clause.Expr{
+				SQL:                "(CASE WHEN media.title LIKE ? THEN 2 WHEN media.path LIKE ? THEN 1 END) DESC",
+				Vars:               []interface{}{wildQuery, wildQuery},
+				WithoutParentheses: true},
+		}).
+		Limit(limitMedia).
+		Preload("Album").
+		Find(&photos).Error
 
-	// albumRows, err := r.Database.Query(`
-	// 	SELECT * FROM album
-	// 	WHERE owner_id = ? AND ( title LIKE ? OR path LIKE ? )
-	// 	ORDER BY (
-	// 		case when title LIKE ? then 2
-	// 		     when path LIKE ? then 1
-	// 		end ) DESC
-	// 	LIMIT ?
-	// `, user.UserID, wildQuery, wildQuery, wildQuery, wildQuery, limitAlbums)
-	// if err != nil {
-	// 	return nil, errors.Wrapf(err, "searching albums")
-	// }
+	if err != nil {
+		return nil, errors.Wrapf(err, "searching media")
+	}
 
-	// albums, err := models.NewAlbumsFromRows(albumRows)
-	// if err != nil {
-	// 	return nil, err
-	// }
+	var albums []*models.Album
 
-	// result := models.SearchResult{
-	// 	Query:  query,
-	// 	Media:  photos,
-	// 	Albums: albums,
-	// }
+	err = r.Database.Where("owner_id = ? AND (title LIKE ? OR path LIKE ?)", user.ID, wildQuery, wildQuery).
+		Clauses(clause.OrderBy{
+			Expression: clause.Expr{
+				SQL:                "(CASE WHEN title LIKE ? THEN 2 WHEN path LIKE ? THEN 1 END) DESC",
+				Vars:               []interface{}{wildQuery, wildQuery},
+				WithoutParentheses: true},
+		}).
+		Limit(limitAlbums).
+		Find(&albums).Error
 
-	// return &result, nil
-	panic("to be migrated")
+	if err != nil {
+		return nil, errors.Wrapf(err, "searching albums")
+	}
+
+	result := models.SearchResult{
+		Query:  query,
+		Media:  photos,
+		Albums: albums,
+	}
+
+	return &result, nil
 }

From e5cb91532a730b58e29afa3a341fbde5bd354acb Mon Sep 17 00:00:00 2001
From: stz184 <ivanov.bg@gmail.com>
Date: Sat, 19 Dec 2020 01:07:43 +0200
Subject: [PATCH 2/3] implicitly join on the Album table

---
 api/graphql/resolvers/search.go | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/api/graphql/resolvers/search.go b/api/graphql/resolvers/search.go
index 8fa04aa..973d1d3 100644
--- a/api/graphql/resolvers/search.go
+++ b/api/graphql/resolvers/search.go
@@ -39,7 +39,7 @@ func (r *Resolver) Search(ctx context.Context, query string, _limitMedia *int, _
 				WithoutParentheses: true},
 		}).
 		Limit(limitMedia).
-		Preload("Album").
+		Joins("Album").
 		Find(&photos).Error
 
 	if err != nil {

From d815472458e22816504655f785fd65ba12b69866 Mon Sep 17 00:00:00 2001
From: viktorstrate <viktorstrate@gmail.com>
Date: Sat, 19 Dec 2020 09:45:43 +0100
Subject: [PATCH 3/3] Simplify search query a little

---
 api/graphql/resolvers/search.go | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/api/graphql/resolvers/search.go b/api/graphql/resolvers/search.go
index 973d1d3..c9c4ac8 100644
--- a/api/graphql/resolvers/search.go
+++ b/api/graphql/resolvers/search.go
@@ -2,6 +2,7 @@ package resolvers
 
 import (
 	"context"
+
 	"github.com/photoview/photoview/api/graphql/auth"
 	"github.com/pkg/errors"
 	"gorm.io/gorm/clause"
@@ -28,10 +29,10 @@ func (r *Resolver) Search(ctx context.Context, query string, _limitMedia *int, _
 
 	wildQuery := "%" + query + "%"
 
-	var photos []*models.Media
+	var media []*models.Media
 
-	err := r.Database.Joins("JOIN albums ON media.album_id = albums.id").
-		Where("albums.owner_id = ? AND ( media.title LIKE ? OR media.path LIKE ? )", user.ID, wildQuery, wildQuery).
+	err := r.Database.Joins("Album").
+		Where("Album.owner_id = ? AND ( media.title LIKE ? OR media.path LIKE ? )", user.ID, wildQuery, wildQuery).
 		Clauses(clause.OrderBy{
 			Expression: clause.Expr{
 				SQL:                "(CASE WHEN media.title LIKE ? THEN 2 WHEN media.path LIKE ? THEN 1 END) DESC",
@@ -39,8 +40,7 @@ func (r *Resolver) Search(ctx context.Context, query string, _limitMedia *int, _
 				WithoutParentheses: true},
 		}).
 		Limit(limitMedia).
-		Joins("Album").
-		Find(&photos).Error
+		Find(&media).Error
 
 	if err != nil {
 		return nil, errors.Wrapf(err, "searching media")
@@ -64,7 +64,7 @@ func (r *Resolver) Search(ctx context.Context, query string, _limitMedia *int, _
 
 	result := models.SearchResult{
 		Query:  query,
-		Media:  photos,
+		Media:  media,
 		Albums: albums,
 	}
 
