Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: various issues after ent refactor #75

Merged
merged 9 commits into from
Jun 24, 2024
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 @@
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{}}

Check warning on line 50 in dataloader/loaders.go

View check run for this annotation

Codecov / codecov/patch

dataloader/loaders.go#L50

Added line #L50 was not covered by tests
}
}

Expand All @@ -70,7 +70,7 @@
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")}

Check warning on line 73 in dataloader/loaders.go

View check run for this annotation

Codecov / codecov/patch

dataloader/loaders.go#L73

Added line #L73 was not covered by tests
}
}

Expand All @@ -97,7 +97,7 @@
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{}}

Check warning on line 100 in dataloader/loaders.go

View check run for this annotation

Codecov / codecov/patch

dataloader/loaders.go#L100

Added line #L100 was not covered by tests
}
}

Expand Down Expand Up @@ -145,7 +145,7 @@
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{}}

Check warning on line 148 in dataloader/loaders.go

View check run for this annotation

Codecov / codecov/patch

dataloader/loaders.go#L148

Added line #L148 was not covered by tests
}
}

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 @@

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"))
})

Check warning on line 33 in db/mod.go

View check run for this annotation

Codecov / codecov/patch

db/mod.go#L31-L33

Added lines #L31 - L33 were not covered by tests
}
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")
}),

Check warning on line 53 in db/mod.go

View check run for this annotation

Codecov / codecov/patch

db/mod.go#L45-L53

Added lines #L45 - L53 were not covered by tests
"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 @@
}

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...)))

Check warning on line 76 in db/mod.go

View check run for this annotation

Codecov / codecov/patch

db/mod.go#L76

Added line #L76 was not covered by tests
}
}

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 @@
"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 @@
avatarURL := u.Avatar
u.Avatar = ""

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

Check warning on line 19 in db/oauth.go

View check run for this annotation

Codecov / codecov/patch

db/oauth.go#L19

Added line #L19 was not covered by tests

if u.Site == oauth.SiteGithub {
find = find.Where(user.GithubID(u.ID))
oauthPredicate = user.GithubID(u.ID)

Check warning on line 22 in db/oauth.go

View check run for this annotation

Codecov / codecov/patch

db/oauth.go#L22

Added line #L22 was not covered by tests
} else if u.Site == oauth.SiteGoogle {
find = find.Where(user.GoogleID(u.ID))
oauthPredicate = user.GoogleID(u.ID)

Check warning on line 24 in db/oauth.go

View check run for this annotation

Codecov / codecov/patch

db/oauth.go#L24

Added line #L24 was not covered by tests
} else if u.Site == oauth.SiteFacebook {
find = find.Where(user.FacebookID(u.ID))
oauthPredicate = user.FacebookID(u.ID)

Check warning on line 26 in db/oauth.go

View check run for this annotation

Codecov / codecov/patch

db/oauth.go#L26

Added line #L26 was not covered by tests
}

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

Check warning on line 29 in db/oauth.go

View check run for this annotation

Codecov / codecov/patch

db/oauth.go#L29

Added line #L29 was not covered by tests

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 @@
).ToFunc())

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

Check warning on line 210 in gql/resolver_guides.go

View check run for this annotation

Codecov / codecov/patch

gql/resolver_guides.go#L210

Added line #L210 was not covered by tests

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(")")
}))

Check warning on line 215 in gql/resolver_guides.go

View check run for this annotation

Codecov / codecov/patch

gql/resolver_guides.go#L213-L215

Added lines #L213 - L215 were not covered by tests
}).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 @@
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

Check warning on line 251 in gql/resolver_mods.go

View check run for this annotation

Codecov / codecov/patch

gql/resolver_mods.go#L247-L251

Added lines #L247 - L251 were not covered by tests
}
}

if existing != nil {
if err := db.From(ctx).UserMod.UpdateOne(existing).
SetRole(role).
Exec(ctx); err != nil {
return nil, err

Check warning on line 259 in gql/resolver_mods.go

View check run for this annotation

Codecov / codecov/patch

gql/resolver_mods.go#L255-L259

Added lines #L255 - L259 were not covered by tests
}
} else {
if err := db.From(ctx).UserMod.Create().
SetUserID(userMod.UserID).
SetModID(modID).
SetRole(role).
Exec(ctx); err != nil {
return nil, err

Check warning on line 267 in gql/resolver_mods.go

View check run for this annotation

Codecov / codecov/patch

gql/resolver_mods.go#L261-L267

Added lines #L261 - L267 were not covered by tests
}
}
}
}
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 @@
).ToFunc())

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

Check warning on line 235 in gql/resolver_sml_versions.go

View check run for this annotation

Codecov / codecov/patch

gql/resolver_sml_versions.go#L235

Added line #L235 was not covered by tests

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(")")
}))

Check warning on line 240 in gql/resolver_sml_versions.go

View check run for this annotation

Codecov / codecov/patch

gql/resolver_sml_versions.go#L238-L240

Added lines #L238 - L240 were not covered by tests
}).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 @@
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")
}))

Check warning on line 100 in gql/resolver_tags.go

View check run for this annotation

Codecov / codecov/patch

gql/resolver_tags.go#L98-L100

Added lines #L98 - L100 were not covered by tests
}).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) 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)

Check warning on line 303 in gql/resolver_users.go

View check run for this annotation

Codecov / codecov/patch

gql/resolver_users.go#L303

Added line #L303 was not covered by tests
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 @@
).ToFunc())

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

Check warning on line 475 in gql/resolver_versions.go

View check run for this annotation

Codecov / codecov/patch

gql/resolver_versions.go#L475

Added line #L475 was not covered by tests

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(")")
}))

Check warning on line 480 in gql/resolver_versions.go

View check run for this annotation

Codecov / codecov/patch

gql/resolver_versions.go#L478-L480

Added lines #L478 - L480 were not covered by tests
}).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
Loading