Skip to content

Commit

Permalink
fix: various issues after ent refactor (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
mircearoata authored Jun 24, 2024
1 parent 8f4f74f commit 4a30196
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 38 deletions.
8 changes: 4 additions & 4 deletions dataloader/loaders.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func Middleware() func(handlerFunc echo.HandlerFunc) echo.HandlerFunc {
if u, ok := byID[id]; ok {
results[i] = &dataloader.Result[[]*ent.VersionDependency]{Data: u}
} else {
results[i] = &dataloader.Result[[]*ent.VersionDependency]{Error: errors.New("version not found")}
results[i] = &dataloader.Result[[]*ent.VersionDependency]{Data: []*ent.VersionDependency{}}
}
}

Expand All @@ -70,7 +70,7 @@ func Middleware() func(handlerFunc echo.HandlerFunc) echo.HandlerFunc {
if u, ok := byID[id]; ok {
results[i] = &dataloader.Result[[]*ent.UserMod]{Data: u}
} else {
results[i] = &dataloader.Result[[]*ent.UserMod]{Error: errors.New("version not found")}
results[i] = &dataloader.Result[[]*ent.UserMod]{Error: errors.New("mod not found")}
}
}

Expand All @@ -97,7 +97,7 @@ func Middleware() func(handlerFunc echo.HandlerFunc) echo.HandlerFunc {
if u, ok := byID[id]; ok {
results[i] = &dataloader.Result[[]*ent.Version]{Data: u}
} else {
results[i] = &dataloader.Result[[]*ent.Version]{Error: errors.New("version not found")}
results[i] = &dataloader.Result[[]*ent.Version]{Data: []*ent.Version{}}
}
}

Expand Down Expand Up @@ -145,7 +145,7 @@ func Middleware() func(handlerFunc echo.HandlerFunc) echo.HandlerFunc {
if u, ok := byID[id]; ok {
results[i] = &dataloader.Result[[]*ent.Version]{Data: u}
} else {
results[i] = &dataloader.Result[[]*ent.Version]{Error: errors.New("version not found")}
results[i] = &dataloader.Result[[]*ent.Version]{Data: []*ent.Version{}}
}
}

Expand Down
32 changes: 19 additions & 13 deletions db/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,31 @@ func ConvertModFilter(query *ent.ModQuery, filter *models.ModFilter, count bool,

if filter.OrderBy != nil && *filter.OrderBy != generated.ModFieldsSearch {
if string(*filter.OrderBy) == "last_version_date" {
query = query.Modify(func(s *sql.Selector) {
s.OrderExpr(sql.ExprP("case when last_version_date is null then 1 else 0 end, last_version_date"))
}).Clone()
} else {
query = query.Order(sql.OrderByField(
filter.OrderBy.String(),
OrderToOrder(filter.Order.String()),
).ToFunc())
query = query.Order(func(s *sql.Selector) {
s.OrderExpr(sql.ExprP("case when last_version_date is null then 1 else 0 end"))
})
}
query = query.Order(sql.OrderByField(
filter.OrderBy.String(),
OrderToOrder(filter.Order.String()),
).ToFunc())
}

if filter.Search != nil && *filter.Search != "" {
cleanSearch := strings.ReplaceAll(strings.TrimSpace(*filter.Search), " ", " & ")

query = query.Where(func(s *sql.Selector) {
join := sql.SelectExpr(sql.ExprP("id, (similarity(name, ?) * 2 + similarity(short_description, ?) + similarity(full_description, ?) * 0.5) as s", cleanSearch, cleanSearch, cleanSearch))
join := sql.Select("id")
join = join.AppendSelectExprAs(
sql.P(func(builder *sql.Builder) {
builder.WriteString("similarity(name, ").Arg(cleanSearch).WriteString(") * 2").
WriteString(" + ").
WriteString("similarity(short_description, ").Arg(cleanSearch).WriteString(")").
WriteString(" + ").
WriteString("similarity(full_description, ").Arg(cleanSearch).WriteString(") * 0.5")
}),
"s",
)
join.From(sql.Table(mod.Table)).As("t1")
s.Join(join).On(s.C(mod.FieldID), join.C("id"))
})
Expand All @@ -64,10 +73,7 @@ func ConvertModFilter(query *ent.ModQuery, filter *models.ModFilter, count bool,
}

if filter.TagIDs != nil && len(filter.TagIDs) > 0 {
query = query.Where(func(s *sql.Selector) {
t := sql.Table(modtag.Table)
s.Join(t).OnP(sql.ExprP("mod_tags.tag_id in ? AND mod_tags.mod_id = mods.id", filter.TagIDs))
})
query = query.Where(mod.HasModTagsWith(modtag.TagIDIn(filter.TagIDs...)))
}
}

Expand Down
11 changes: 7 additions & 4 deletions db/oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"

"github.com/satisfactorymodding/smr-api/generated/ent"
"github.com/satisfactorymodding/smr-api/generated/ent/predicate"
"github.com/satisfactorymodding/smr-api/generated/ent/user"
"github.com/satisfactorymodding/smr-api/oauth"
"github.com/satisfactorymodding/smr-api/storage"
Expand All @@ -15,16 +16,18 @@ func CompleteOAuthFlow(ctx context.Context, u *oauth.UserData, userAgent string)
avatarURL := u.Avatar
u.Avatar = ""

find := From(ctx).User.Query().Where(user.Email(u.Email))
var oauthPredicate predicate.User

if u.Site == oauth.SiteGithub {
find = find.Where(user.GithubID(u.ID))
oauthPredicate = user.GithubID(u.ID)
} else if u.Site == oauth.SiteGoogle {
find = find.Where(user.GoogleID(u.ID))
oauthPredicate = user.GoogleID(u.ID)
} else if u.Site == oauth.SiteFacebook {
find = find.Where(user.FacebookID(u.ID))
oauthPredicate = user.FacebookID(u.ID)
}

find := From(ctx).User.Query().Where(user.Or(user.Email(u.Email), oauthPredicate))

found, err := find.First(ctx)
if err != nil && !ent.IsNotFound(err) {
return nil, err
Expand Down
8 changes: 6 additions & 2 deletions gql/resolver_guides.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,13 @@ func convertGuideFilter(query *ent.GuideQuery, filter *models.GuideFilter) *ent.
).ToFunc())

if filter.Search != nil && *filter.Search != "" {
cleanedSearch := strings.ReplaceAll(*filter.Search, " ", " & ")

query = query.Modify(func(s *sql.Selector) {
s.Where(sql.ExprP("to_tsvector(name) @@ to_tsquery(?)", strings.ReplaceAll(*filter.Search, " ", " & ")))
}).Clone()
s.Where(sql.P(func(builder *sql.Builder) {
builder.WriteString("to_tsvector(name) @@ to_tsquery(").Arg(cleanedSearch).WriteString(")")
}))
}).GuideQuery
}

if filter.TagIDs != nil && len(filter.TagIDs) > 0 {
Expand Down
28 changes: 22 additions & 6 deletions gql/resolver_mods.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,28 @@ func (r *mutationResolver) UpdateMod(ctx context.Context, modID string, updateMo
role = "editor"
}

if err := db.From(ctx).UserMod.Create().
SetUserID(userMod.UserID).
SetModID(modID).
SetRole(role).
Exec(ctx); err != nil {
return nil, err
var existing *ent.UserMod
for _, author := range authors {
if author.UserID == userMod.UserID {
existing = author
break
}
}

if existing != nil {
if err := db.From(ctx).UserMod.UpdateOne(existing).
SetRole(role).
Exec(ctx); err != nil {
return nil, err
}
} else {
if err := db.From(ctx).UserMod.Create().
SetUserID(userMod.UserID).
SetModID(modID).
SetRole(role).
Exec(ctx); err != nil {
return nil, err
}
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions gql/resolver_sml_versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,13 @@ func convertSMLVersionFilter(query *ent.SmlVersionQuery, filter *models.SMLVersi
).ToFunc())

if filter.Search != nil && *filter.Search != "" {
cleanedSearch := strings.ReplaceAll(*filter.Search, " ", " & ")

query = query.Modify(func(s *sql.Selector) {
s.Where(sql.ExprP("to_tsvector(name) @@ to_tsquery(?)", strings.ReplaceAll(*filter.Search, " ", " & ")))
}).Clone()
s.Where(sql.P(func(builder *sql.Builder) {
builder.WriteString("to_tsvector(version) @@ to_tsquery(").Arg(cleanedSearch).WriteString(")")
}))
}).SmlVersionQuery
}
}
return query
Expand Down
7 changes: 4 additions & 3 deletions gql/resolver_tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,10 @@ func (r *queryResolver) GetTags(ctx context.Context, filter *generated.TagFilter
cleanSearch := strings.ReplaceAll(strings.TrimSpace(*filter.Search), " ", " & ")

query = query.Modify(func(s *sql.Selector) {
s.AppendSelectExpr(sql.Expr("similarity(name, ?) as s", cleanSearch))
s.Where(sql.ExprP("s > 0.2"))
}).Clone()
s.Where(sql.P(func(builder *sql.Builder) {
builder.WriteString("similarity(name, ").Arg(cleanSearch).WriteString(") > 0.2")
}))
}).TagQuery
}

if filter.Ids != nil && len(filter.Ids) > 0 {
Expand Down
2 changes: 1 addition & 1 deletion gql/resolver_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ func (r *userModResolver) User(ctx context.Context, obj *generated.UserMod) (*ge
}

func (r *userModResolver) Mod(ctx context.Context, obj *generated.UserMod) (*generated.Mod, error) {
result, err := db.From(ctx).Mod.Get(ctx, obj.ModID)
result, err := db.From(ctx).Mod.Query().WithTags().Where(mod.ID(obj.ModID)).Only(ctx)
if err != nil {
return nil, err
}
Expand Down
8 changes: 6 additions & 2 deletions gql/resolver_versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,9 +472,13 @@ func convertVersionFilter(query *ent.VersionQuery, filter *models.VersionFilter,
).ToFunc())

if filter.Search != nil && *filter.Search != "" {
cleanedSearch := strings.ReplaceAll(*filter.Search, " ", " & ")

query = query.Modify(func(s *sql.Selector) {
s.Where(sql.ExprP("to_tsvector(name) @@ to_tsquery(?)", strings.ReplaceAll(*filter.Search, " ", " & ")))
}).Clone()
s.Where(sql.P(func(builder *sql.Builder) {
builder.WriteString("to_tsvector(version) @@ to_tsquery(").Arg(cleanedSearch).WriteString(")")
}))
}).VersionQuery
}
}

Expand Down
2 changes: 1 addition & 1 deletion migrations/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func ReindexAllModFiles(ctx context.Context, withMetadata bool, modFilter func(*
offset := 0

for {
mods, err := db.From(ctx).Mod.Query().Limit(100).Offset(100).Order(mod.ByCreatedAt(sql.OrderDesc())).All(ctx)
mods, err := db.From(ctx).Mod.Query().Limit(100).Offset(offset).Order(mod.ByCreatedAt(sql.OrderDesc())).All(ctx)
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions util/converter/converter_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/Vilsol/slox"
"github.com/chai2010/webp"

// GIF Support
_ "image/gif"
// JPEG Support
Expand Down

0 comments on commit 4a30196

Please sign in to comment.