Skip to content

feat: modpacks #125

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

Open
wants to merge 2 commits into
base: staging
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ func Setup(ctx context.Context) *echo.Echo {
nodes.RegisterModRoutes(v1.Group("/mod"))
nodes.RegisterModsRoutes(v1.Group("/mods"))
nodes.RegisterVersionRoutes(v1.Group("/version"))
nodes.RegisterModpackRoutes(v1.Group("/modpack"))
nodes.RegisterModpacksRoutes(v1.Group("/modpacks"))

v2 := e.Group("/v2")

Expand Down
1 change: 1 addition & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ coverage:

ignore:
- "generated"
- "db/schema"
29 changes: 29 additions & 0 deletions conversion/ent_to_graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,35 @@ type VirustotalResult interface {
ConvertSlice(source []*ent.VirustotalResult) []*generated.VirustotalResult
}

// goverter:converter
// goverter:output:file ../generated/conv/modpack.go
// goverter:output:package conv
// goverter:extend TimeToString UIntToInt Int64ToInt EntModpackTargetToString
type Modpack interface {
// goverter:map Edges.Targets Targets
// goverter:map Edges.Tags Tags
// goverter:map Edges.ModpackMods Mods
// goverter:map Edges.Parent Parent
// goverter:map Edges.Releases Releases
// goverter:map Edges.Children Children
// goverter:ignore Creator
Convert(source *ent.Modpack) *generated.Modpack
ConvertSlice(source []*ent.Modpack) []*generated.Modpack
}

func EntModpackTargetToString(t *ent.ModpackTarget) string {
return t.TargetName
}

// goverter:converter
// goverter:output:file ../generated/conv/modpack_release.go
// goverter:output:package conv
// goverter:extend TimeToString
type ModpackRelease interface {
Convert(source *ent.ModpackRelease) *generated.ModpackRelease
ConvertSlice(source []*ent.ModpackRelease) []*generated.ModpackRelease
}

func TimeToString(i time.Time) string {
return i.Format(time.RFC3339)
}
Expand Down
71 changes: 71 additions & 0 deletions db/modpack.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package db

import (
"strings"

"entgo.io/ent/dialect/sql"

"github.com/satisfactorymodding/smr-api/generated"
"github.com/satisfactorymodding/smr-api/generated/ent"
"github.com/satisfactorymodding/smr-api/generated/ent/modpack"
"github.com/satisfactorymodding/smr-api/generated/ent/tag"
"github.com/satisfactorymodding/smr-api/models"
)

func ConvertModpackFilter(query *ent.ModpackQuery, filter *models.ModpackFilter, count bool) *ent.ModpackQuery {
if len(filter.IDs) > 0 {
query = query.Where(modpack.IDIn(filter.IDs...))

Check warning on line 17 in db/modpack.go

View check run for this annotation

Codecov / codecov/patch

db/modpack.go#L17

Added line #L17 was not covered by tests
} else if filter != nil {
if !count {
query = query.
Limit(*filter.Limit).
Offset(*filter.Offset)
}

if filter.OrderBy != nil && *filter.OrderBy != generated.ModpackFieldsSearch {
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.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(modpack.Table)).As("t1")
s.Join(join).On(s.C(modpack.FieldID), join.C("id"))
})

query = query.Where(func(s *sql.Selector) {
s.Where(sql.ExprP(`"t1"."s" > 0.2`))
})

if !count && *filter.OrderBy == generated.ModpackFieldsSearch {
query = query.Order(func(s *sql.Selector) {
s.OrderExpr(sql.ExprP(`"t1"."s" DESC`))
})

Check warning on line 58 in db/modpack.go

View check run for this annotation

Codecov / codecov/patch

db/modpack.go#L56-L58

Added lines #L56 - L58 were not covered by tests
}
}

if filter.Hidden == nil || !(*filter.Hidden) {
query = query.Where(modpack.Hidden(false))
}

if len(filter.TagIDs) > 0 {
query = query.Where(modpack.HasTagsWith(tag.IDIn(filter.TagIDs...)))
}

Check warning on line 68 in db/modpack.go

View check run for this annotation

Codecov / codecov/patch

db/modpack.go#L67-L68

Added lines #L67 - L68 were not covered by tests
}
return query
}
3 changes: 3 additions & 0 deletions db/schema/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,8 @@ func (Mod) Edges() []ent.Edge {
edge.From("dependents", Version.Type).
Ref("dependencies").
Through("version_dependencies", VersionDependency.Type),
edge.From("modpacks", Modpack.Type).
Ref("mods").
Through("modpack_mods", ModpackMod.Type),
}
}
59 changes: 59 additions & 0 deletions db/schema/modpack.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package schema

import (
"entgo.io/ent"
"entgo.io/ent/dialect/entsql"
"entgo.io/ent/schema/edge"
"entgo.io/ent/schema/field"
)

type Modpack struct {
ent.Schema
}

func (Modpack) Mixin() []ent.Mixin {
return []ent.Mixin{
IDMixin{},
TimeMixin{},
}
}

func (Modpack) Fields() []ent.Field {
return []ent.Field{
field.String("name"),
field.String("short_description").MaxLen(128),
field.String("full_description"),
field.String("logo").Optional(),
field.String("logo_thumbhash").Optional(),
field.String("creator_id"),
field.Uint("views").Default(0),
field.Uint("hotness").Default(0),
field.Uint("installs").Default(0),
field.Uint("popularity").Default(0),
field.Bool("hidden").Default(false),
field.String("parent_id").Optional().Immutable(),
}
}

func (Modpack) Edges() []ent.Edge {
return []ent.Edge{
edge.To("children", Modpack.Type).
Annotations(entsql.OnDelete(entsql.Restrict)),
edge.From("parent", Modpack.Type).
Ref("children").
Field("parent_id").
Unique().
Immutable().
Annotations(entsql.OnDelete(entsql.Restrict)),
edge.To("targets", ModpackTarget.Type).
Annotations(entsql.OnDelete(entsql.Cascade)),
edge.To("releases", ModpackRelease.Type).
Annotations(entsql.OnDelete(entsql.Cascade)),
edge.To("mods", Mod.Type).
Through("modpack_mods", ModpackMod.Type).
Annotations(entsql.OnDelete(entsql.Cascade)),
edge.To("tags", Tag.Type).
Through("modpack_tags", ModpackTag.Type).
Annotations(entsql.OnDelete(entsql.Cascade)),
}
}
42 changes: 42 additions & 0 deletions db/schema/modpack_mods.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package schema

import (
"entgo.io/ent"
"entgo.io/ent/dialect/entsql"
"entgo.io/ent/schema"
"entgo.io/ent/schema/edge"
"entgo.io/ent/schema/field"
)

type ModpackMod struct {
ent.Schema
}

func (ModpackMod) Annotations() []schema.Annotation {
return []schema.Annotation{
field.ID("modpack_id", "mod_id"),
}
}

func (ModpackMod) Fields() []ent.Field {
return []ent.Field{
field.String("modpack_id"),
field.String("mod_id"),
field.String("version_constraint"),
}
}

func (ModpackMod) Edges() []ent.Edge {
return []ent.Edge{
edge.To("modpack", Modpack.Type).
Unique().
Required().
Field("modpack_id").
Annotations(entsql.OnDelete(entsql.Cascade)),
edge.To("mod", Mod.Type).
Unique().
Required().
Field("mod_id").
Annotations(entsql.OnDelete(entsql.Cascade)),
}
}
46 changes: 46 additions & 0 deletions db/schema/modpack_release.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package schema

import (
"entgo.io/ent"
"entgo.io/ent/dialect/entsql"
"entgo.io/ent/schema/edge"
"entgo.io/ent/schema/field"
"entgo.io/ent/schema/index"
)

type ModpackRelease struct {
ent.Schema
}

func (ModpackRelease) Mixin() []ent.Mixin {
return []ent.Mixin{
IDMixin{},
TimeMixin{},
}
}

func (ModpackRelease) Fields() []ent.Field {
return []ent.Field{
field.String("modpack_id"),
field.String("version"),
field.String("changelog"),
field.String("lockfile"),
}
}

func (ModpackRelease) Edges() []ent.Edge {
return []ent.Edge{
edge.From("modpack", Modpack.Type).
Ref("releases").
Field("modpack_id").
Unique().
Required().
Annotations(entsql.OnDelete(entsql.Cascade)),
}
}

func (ModpackRelease) Indexes() []ent.Index {
return []ent.Index{
index.Fields("modpack_id", "version").Unique(),
}
}
41 changes: 41 additions & 0 deletions db/schema/modpack_tags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package schema

import (
"entgo.io/ent"
"entgo.io/ent/dialect/entsql"
"entgo.io/ent/schema"
"entgo.io/ent/schema/edge"
"entgo.io/ent/schema/field"
)

type ModpackTag struct {
ent.Schema
}

func (ModpackTag) Annotations() []schema.Annotation {
return []schema.Annotation{
field.ID("modpack_id", "tag_id"),
}
}

func (ModpackTag) Fields() []ent.Field {
return []ent.Field{
field.String("modpack_id"),
field.String("tag_id"),
}
}

func (ModpackTag) Edges() []ent.Edge {
return []ent.Edge{
edge.To("modpack", Modpack.Type).
Unique().
Required().
Field("modpack_id").
Annotations(entsql.OnDelete(entsql.Cascade)),
edge.To("tag", Tag.Type).
Unique().
Required().
Field("tag_id").
Annotations(entsql.OnDelete(entsql.Cascade)),
}
}
43 changes: 43 additions & 0 deletions db/schema/modpack_target.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package schema

import (
"entgo.io/ent"
"entgo.io/ent/dialect/entsql"
"entgo.io/ent/schema/edge"
"entgo.io/ent/schema/field"
"entgo.io/ent/schema/index"
)

type ModpackTarget struct {
ent.Schema
}

func (ModpackTarget) Mixin() []ent.Mixin {
return []ent.Mixin{
IDMixin{},
}
}

func (ModpackTarget) Fields() []ent.Field {
return []ent.Field{
field.String("modpack_id"),
field.String("target_name"),
}
}

func (ModpackTarget) Edges() []ent.Edge {
return []ent.Edge{
edge.From("modpack", Modpack.Type).
Ref("targets").
Field("modpack_id").
Unique().
Required().
Annotations(entsql.OnDelete(entsql.Cascade)),
}
}

func (ModpackTarget) Indexes() []ent.Index {
return []ent.Index{
index.Fields("modpack_id", "target_name").Unique(),
}
}
3 changes: 3 additions & 0 deletions db/schema/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,8 @@ func (Tag) Edges() []ent.Edge {
edge.From("guides", Guide.Type).
Ref("tags").
Through("guide_tags", GuideTag.Type),
edge.From("modpacks", Modpack.Type).
Ref("tags").
Through("modpack_tags", ModpackTag.Type),
}
}
Loading