Skip to content

Commit

Permalink
Feat/25 edit schematics (#105)
Browse files Browse the repository at this point in the history
* various fixes, added explore page

* Edit schematics, delete schematics, delete account and more
  • Loading branch information
uberswe authored Feb 16, 2025
1 parent 27736a5 commit 9109b65
Show file tree
Hide file tree
Showing 48 changed files with 1,438 additions and 200 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@
pb_data
migration_data
main
dbdata
template
dbdata
File renamed without changes.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ docker compose up --build
You will need to run `npm install` and `npm run build` to generate the frontend files, you can do this with docker if needed using the following command.

```
docker compose run npm
docker compose run --remove-orphans npm
```

## Environmental Variables
Expand Down
54 changes: 54 additions & 0 deletions internal/cache/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ import (
"time"
)

// It's never the cache

const (
AllTagsWithCountKey = "AllTagsWithCount"
HighestRatedSchematicsKey = "HighestRatedSchematics"
TrendingSchematicsKey = "TrendingSchematics"
AllCategoriesKey = "AllCategories"
)

type Service struct {
c *cache.Cache
}
Expand Down Expand Up @@ -92,6 +101,10 @@ func (s *Service) SetSchematic(key string, value models.Schematic) {
s.c.Set(key, value, cache.DefaultExpiration)
}

func (s *Service) DeleteSchematic(key string) {
s.c.Delete(key)
}

func (s *Service) GetSchematic(key string) (models.Schematic, bool) {
v, found := s.Get(key)
if !found {
Expand All @@ -106,3 +119,44 @@ func (s *Service) GetSchematic(key string) (models.Schematic, bool) {
func (s *Service) SetSchematics(key string, value []models.Schematic) {
s.c.Set(key, value, cache.DefaultExpiration)
}

func (s *Service) GetSchematics(key string) ([]models.Schematic, bool) {
v, found := s.Get(key)
if !found {
return nil, found
}
if schem, ok := v.([]models.Schematic); ok {
return schem, found
}
return nil, false
}

func (s *Service) SetCategories(key string, value []models.SchematicCategory, duration time.Duration) {
s.c.Set(key, value, duration)
}

func (s *Service) GetCategories(key string) ([]models.SchematicCategory, bool) {
v, found := s.Get(key)
if !found {
return nil, found
}
if categories, ok := v.([]models.SchematicCategory); ok {
return categories, found
}
return nil, false
}

func (s *Service) SetTagWithCount(key string, tags []models.SchematicTagWithCount) {
s.c.Set(key, tags, cache.DefaultExpiration)
}

func (s *Service) GetTagWithCount(key string) ([]models.SchematicTagWithCount, bool) {
v, found := s.Get(key)
if !found {
return nil, found
}
if tags, ok := v.([]models.SchematicTagWithCount); ok {
return tags, found
}
return nil, false
}
8 changes: 8 additions & 0 deletions internal/models/image.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package models

type ImageData struct {
ID string
Name string
Title string
Image string
}
1 change: 1 addition & 0 deletions internal/models/schematic.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Schematic struct {
Dependencies string
HTMLDependencies template.HTML
Categories []SchematicCategory
CategoryId string
Tags []SchematicTag
CreatemodVersion string
MinecraftVersion string
Expand Down
5 changes: 3 additions & 2 deletions internal/pages/contact.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pages

import (
"createmod/internal/cache"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/tools/template"
Expand All @@ -13,15 +14,15 @@ type ContactData struct {
DefaultData
}

func ContactHandler(app *pocketbase.PocketBase, registry *template.Registry) func(e *core.RequestEvent) error {
func ContactHandler(app *pocketbase.PocketBase, registry *template.Registry, cacheService *cache.Service) func(e *core.RequestEvent) error {
return func(e *core.RequestEvent) error {
d := ContactData{}
d.Populate(e)
d.Title = "Contact"
d.Description = "Contact the CreateMod.com maintainers in case you have a question or suggestion."
d.Slug = "/contact"
d.Thumbnail = "https://createmod.com/assets/x/logo_sq_lg.png"
d.Categories = allCategories(app)
d.Categories = allCategories(app, cacheService)
html, err := registry.LoadFiles(contactTemplate).Render(d)
if err != nil {
return err
Expand Down
17 changes: 14 additions & 3 deletions internal/pages/default.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pages

import (
"createmod/internal/cache"
"createmod/internal/models"
"github.com/drexedam/gravatar"
"github.com/pocketbase/pocketbase"
Expand All @@ -9,11 +10,13 @@ import (
"golang.org/x/text/language"
"html/template"
"strings"
"time"
)

type DefaultData struct {
IsAuthenticated bool
Username string
UserID string
UsernameSlug string
Title string
Description string
Expand All @@ -31,6 +34,7 @@ func (d *DefaultData) Populate(e *core.RequestEvent) {
d.IsAuthenticated = true
caser := cases.Title(language.English)
d.Username = caser.String(user.GetString("username"))
d.UserID = user.Id
d.UsernameSlug = strings.ToLower(user.GetString("username"))
url := gravatar.New(user.GetString("email")).
Size(200).
Expand All @@ -42,14 +46,21 @@ func (d *DefaultData) Populate(e *core.RequestEvent) {
}
}

func allCategories(app *pocketbase.PocketBase) []models.SchematicCategory {
func allCategories(app *pocketbase.PocketBase, cacheService *cache.Service) []models.SchematicCategory {
categories, found := cacheService.GetCategories(cache.AllCategoriesKey)
if found {
return categories
}
categoriesCollection, err := app.FindCollectionByNameOrId("schematic_categories")
if err != nil {
return nil
}
records, err := app.FindRecordsByFilter(categoriesCollection.Id, "1=1", "+name", -1, 0)
records, err := app.FindRecordsByFilter(categoriesCollection.Id, "1=1", "+key", -1, 0)
if err != nil {
return nil
}
return mapResultToCategories(records)
categories = mapResultToCategories(records)
// 730 hours = 1 month
cacheService.SetCategories(cache.AllCategoriesKey, categories, time.Hour*730)
return categories
}
95 changes: 95 additions & 0 deletions internal/pages/editschematic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package pages

import (
"createmod/internal/cache"
"createmod/internal/models"
"createmod/internal/search"
"fmt"
"github.com/pocketbase/dbx"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/core"
template2 "github.com/pocketbase/pocketbase/tools/template"
"net/http"
)

var editSchematicTemplates = []string{
"./template/dist/editschematic.html",
}

type EditSchematicData struct {
DefaultData
Schematic models.Schematic
AuthorHasMore bool
// IsAuthor of the current schematic, for edit and delete actions
IsAuthor bool
MinecraftVersions []models.MinecraftVersion
CreatemodVersions []models.CreatemodVersion
Tags []models.SchematicTag
TagsWithSelected []SchematicTagWithSelected
CreateModVersionId string
}

type SchematicTagWithSelected struct {
models.SchematicTag
Selected bool
}

func EditSchematicHandler(app *pocketbase.PocketBase, searchService *search.Service, cacheService *cache.Service, registry *template2.Registry) func(e *core.RequestEvent) error {
return func(e *core.RequestEvent) error {
schematicsCollection, err := app.FindCollectionByNameOrId("schematics")
if err != nil {
return err
}
results, err := app.FindRecordsByFilter(
schematicsCollection.Id,
"name = {:name}",
"-created",
1,
0,
dbx.Params{"name": e.Request.PathValue("name")})

if len(results) != 1 {
html, err := registry.LoadFiles(fourOhFourTemplate).Render(nil)
if err != nil {
return err
}
return e.HTML(http.StatusNotFound, html)
}

d := EditSchematicData{
Schematic: mapResultToSchematic(app, results[0], cacheService),
}
d.Populate(e)
d.Title = fmt.Sprintf("Editing %s", d.Schematic.Title)
d.Slug = fmt.Sprintf("schematics/%s/edit", d.Schematic.Name)
d.Description = d.Schematic.Content
d.Thumbnail = fmt.Sprintf("https://createmod.com/api/files/schematics/%s/%s", d.Schematic.ID, d.Schematic.FeaturedImage)
d.SubCategory = "Schematic"
d.Categories = allCategories(app, cacheService)
d.Tags = allTags(app)
d.MinecraftVersions = allMinecraftVersions(app)
d.CreatemodVersions = allCreatemodVersions(app)
d.IsAuthor = d.Schematic.Author.ID == d.UserID
d.CreateModVersionId = results[0].GetString("createmod_version")

for _, t := range d.Tags {
selected := false
for _, t2 := range d.Schematic.Tags {
if t.Key == t2.Key {
selected = true
}
}
d.TagsWithSelected = append(d.TagsWithSelected, SchematicTagWithSelected{
SchematicTag: t,
Selected: selected,
})
}

countSchematicView(app, results[0])
html, err := registry.LoadFiles(editSchematicTemplates...).Render(d)
if err != nil {
return err
}
return e.HTML(http.StatusOK, html)
}
}
81 changes: 81 additions & 0 deletions internal/pages/explore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package pages

import (
"createmod/internal/cache"
"createmod/internal/models"
"fmt"
"github.com/pocketbase/dbx"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/tools/template"
"math/rand/v2"
"net/http"
)

var exploreTemplates = []string{
"./template/dist/explore.html",
}

type ExploreData struct {
DefaultData
Images []models.ImageData
}

func ExploreHandler(app *pocketbase.PocketBase, cacheService *cache.Service, registry *template.Registry) func(e *core.RequestEvent) error {
return func(e *core.RequestEvent) error {
schematicsCollection, err := app.FindCollectionByNameOrId("schematics")
if err != nil {
return err
}
var results []core.Record
err = app.RecordQuery(schematicsCollection).Select("id", "name", "title", "featured_image", "gallery").Where(dbx.NewExp("deleted = null")).All(&results)
if err != nil {
return err
}

images := make([]models.ImageData, 0)
for _, result := range results {
for _, g := range result.GetStringSlice("gallery") {
images = append(images, models.ImageData{
ID: result.Id,
Title: result.GetString("title"),
Name: result.GetString("name"),
Image: g,
})
}
images = append(images, models.ImageData{
ID: result.Id,
Title: result.GetString("title"),
Name: result.GetString("name"),
Image: result.GetString("featured_image"),
})
}

show := len(images)
if show > 1000 {
show = 1000
}
dest := make([]models.ImageData, show)
perm := rand.Perm(show)
for i, v := range perm {
dest[v] = images[i]
}

d := ExploreData{
Images: dest,
}
d.Populate(e)
d.Title = "Explore Create Mod Schematics"
d.Categories = allCategories(app, cacheService)
d.Description = "Explore a random gallery of Create Mod schematics"
d.Slug = "/explore"
if len(dest) > 0 {
d.Thumbnail = fmt.Sprintf("https://createmod.com/api/files/schematics/%s/%s", dest[0].ID, dest[0].Image)
}
html, err := registry.LoadFiles(exploreTemplates...).Render(d)
if err != nil {
return err
}
return e.HTML(http.StatusOK, html)
}
}
5 changes: 3 additions & 2 deletions internal/pages/guide.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pages

import (
"createmod/internal/cache"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/tools/template"
Expand All @@ -13,15 +14,15 @@ type GuideData struct {
DefaultData
}

func GuideHandler(app *pocketbase.PocketBase, registry *template.Registry) func(e *core.RequestEvent) error {
func GuideHandler(app *pocketbase.PocketBase, registry *template.Registry, cacheService *cache.Service) func(e *core.RequestEvent) error {
return func(e *core.RequestEvent) error {
d := GuideData{}
d.Populate(e)
d.Title = "Guide"
d.Description = "How do you use Create Mod schematic files? This page has a simple guide that should help!"
d.Slug = "/guide"
d.Thumbnail = "https://createmod.com/assets/x/logo_sq_lg.png"
d.Categories = allCategories(app)
d.Categories = allCategories(app, cacheService)
html, err := registry.LoadFiles(guideTemplate).Render(d)
if err != nil {
return err
Expand Down
Loading

0 comments on commit 9109b65

Please sign in to comment.