feat:#16 added uuid for all modules in back-/frontend

This commit is contained in:
masterelmar
2023-10-25 16:54:48 +02:00
parent 3cd9f1fdbd
commit 5c438424a2
12 changed files with 70 additions and 205 deletions

27
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,27 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch file",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "backend/main.go",
"args": ["serve"],
"showLog": true
},
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "backend",
"env": {},
"args": ["serve"],
"showLog": true
}
]
}

View File

@ -1,8 +1,13 @@
package model
import "github.com/pocketbase/pocketbase/models"
type Feed struct {
Id string `db:"id" json:"id"`
Modules string `db:"modules" json:"modules"`
Created string `db:"created" json:"created"`
Updated string `db:"updated" json:"updated"`
models.BaseModel
}
// SetModules set modules field
func (f *Feed) SetModules(modules string) {
f.Modules = modules
}

View File

@ -23,12 +23,6 @@ type Entry struct {
type Entries []*Entry
type FeedCollection struct {
Name string `db:"Name" json:"name"`
Course string `db:"course" json:"course"`
UserDefinedName string `db:"userDefinedName" json:"userDefinedName"`
}
type UUIDFeedCollection struct {
UUID string `db:"uuid" json:"uuid"`
Name string `db:"Name" json:"name"`
Course string `db:"course" json:"course"`

View File

@ -1,6 +1,7 @@
package model
type Module struct {
UUID string `json:"uuid"`
Name string `json:"name"`
Prof string `json:"prof"`
Course string `json:"course"`

View File

@ -1,172 +1,4 @@
[
{
"id": "_pb_users_auth_",
"name": "users",
"type": "auth",
"system": false,
"schema": [
{
"id": "users_name",
"name": "name",
"type": "text",
"system": false,
"required": false,
"options": {
"min": null,
"max": null,
"pattern": ""
}
},
{
"id": "users_avatar",
"name": "avatar",
"type": "file",
"system": false,
"required": false,
"options": {
"maxSelect": 1,
"maxSize": 5242880,
"mimeTypes": [
"image/jpeg",
"image/png",
"image/svg+xml",
"image/gif",
"image/webp"
],
"thumbs": null,
"protected": false
}
}
],
"indexes": [],
"listRule": "id = @request.auth.id",
"viewRule": "id = @request.auth.id",
"createRule": "",
"updateRule": "id = @request.auth.id",
"deleteRule": "id = @request.auth.id",
"options": {
"allowEmailAuth": true,
"allowOAuth2Auth": true,
"allowUsernameAuth": true,
"exceptEmailDomains": null,
"manageRule": null,
"minPasswordLength": 8,
"onlyEmailDomains": null,
"requireEmail": false
}
},
{
"id": "cfq9mqlmd97v8z5",
"name": "groups",
"type": "base",
"system": false,
"schema": [
{
"id": "85msl21p",
"name": "university",
"type": "text",
"system": false,
"required": false,
"options": {
"min": null,
"max": null,
"pattern": ""
}
},
{
"id": "2sii4dtp",
"name": "shortcut",
"type": "text",
"system": false,
"required": false,
"options": {
"min": null,
"max": null,
"pattern": ""
}
},
{
"id": "uiwgo28f",
"name": "groupId",
"type": "text",
"system": false,
"required": false,
"options": {
"min": null,
"max": null,
"pattern": ""
}
},
{
"id": "y0l1lrzs",
"name": "course",
"type": "text",
"system": false,
"required": false,
"options": {
"min": null,
"max": null,
"pattern": ""
}
},
{
"id": "kr62mhbz",
"name": "faculty",
"type": "text",
"system": false,
"required": false,
"options": {
"min": null,
"max": null,
"pattern": ""
}
},
{
"id": "ya6znpez",
"name": "facultyId",
"type": "text",
"system": false,
"required": false,
"options": {
"min": null,
"max": null,
"pattern": ""
}
}
],
"indexes": [
"CREATE UNIQUE INDEX `idx_rcaN2Oq` ON `groups` (`course`)"
],
"listRule": null,
"viewRule": null,
"createRule": null,
"updateRule": null,
"deleteRule": null,
"options": {}
},
{
"id": "d65h4wh7zk13gxp",
"name": "feeds",
"type": "base",
"system": false,
"schema": [
{
"id": "cowxjfmc",
"name": "modules",
"type": "json",
"system": false,
"required": true,
"options": {}
}
],
"indexes": [],
"listRule": null,
"viewRule": null,
"createRule": null,
"updateRule": null,
"deleteRule": null,
"options": {}
},
{
"id": "7her4515qsmrxe8",
"name": "events",

View File

@ -12,16 +12,10 @@ import (
"io"
"net/http"
"net/url"
"os"
)
func AddRoutes(app *pocketbase.PocketBase) {
app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
e.Router.GET("/*", apis.StaticDirectoryHandler(os.DirFS("./pb_public"), false))
return nil
})
app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
_, err := e.Router.AddRoute(echo.Route{
Method: http.MethodGet,
@ -249,8 +243,13 @@ func AddRoutes(app *pocketbase.PocketBase) {
Method: http.MethodGet,
Path: "/api/feed/migrate",
Handler: func(c echo.Context) error {
ical.MigrateFeedJson(app)
return c.JSON(200, "Migrated")
err := ical.MigrateFeedJson(app)
if err != nil {
return c.JSON(500, err)
} else {
return c.JSON(200, "Migrated")
}
},
Middlewares: []echo.MiddlewareFunc{
apis.ActivityLogger(app),

View File

@ -53,6 +53,7 @@ func GetModuleByName(app *pocketbase.PocketBase, name string) (model.Module, err
return model.Module{}, err
} else {
return model.Module{
UUID: events[0].UUID,
Name: name,
Events: events,
Prof: events[0].Prof,

View File

@ -16,8 +16,6 @@ import (
const expirationTime = 5 * time.Minute
func Feed(c echo.Context, app *pocketbase.PocketBase, token string) error {
layout := "2006-01-02 15:04:05 -0700 MST"
var result string
var responseWriter = c.Response().Writer
feed, err := db.FindFeedByToken(token, app)
@ -25,11 +23,11 @@ func Feed(c echo.Context, app *pocketbase.PocketBase, token string) error {
return c.JSON(http.StatusNotFound, err)
}
created, _ := time.Parse(layout, feed.Created)
created := feed.Created
var modules []model.FeedCollection
_ = json.Unmarshal([]byte(feed.Modules), &modules)
if created.Add(time.Hour * 265).Before(time.Now()) {
if created.Time().Add(time.Hour * 265).Before(time.Now()) {
newFeed, err := createFeedForToken(app, modules)
if err != nil {
return c.JSON(http.StatusInternalServerError, err)

View File

@ -11,44 +11,48 @@ import (
//add uuid field
//remove module name field
func MigrateFeedJson(app *pocketbase.PocketBase) {
func MigrateFeedJson(app *pocketbase.PocketBase) error {
var feeds []model.Feed
err := app.Dao().DB().Select("*").From("feed").All(&feeds)
records, err := app.Dao().FindRecordsByFilter("feeds", "1=1", "-created", 0, 0)
if err != nil {
return
return err
}
for _, feed := range feeds {
for _, feed := range records {
var modules []model.FeedCollection
err := json.Unmarshal([]byte(feed.Modules), &modules)
err := json.Unmarshal([]byte(feed.GetString("modules")), &modules)
if err != nil {
return
return err
}
var uuidFeedCollection model.UUIDFeedCollection
var uuidFeedCollections []model.FeedCollection
for _, module := range modules {
uuid := searchUUIDForModule(app, module.Name)
if uuid != "" {
uuidFeedCollection = model.UUIDFeedCollection{UUID: uuid, Name: module.Name, Course: module.Course, UserDefinedName: module.UserDefinedName}
app.Dao().DB().Update("feed", dbx.Params{"modules": uuidFeedCollection}, dbx.NewExp("id = {:id}", dbx.Params{"id": feed.Id}))
uuidFeedCollections = append(uuidFeedCollections, model.FeedCollection{UUID: uuid, Name: module.Name, Course: module.Course, UserDefinedName: module.UserDefinedName})
}
}
}
jsonModules, _ := json.Marshal(uuidFeedCollections)
feed.Set("modules", string(jsonModules))
err = app.Dao().SaveRecord(feed)
if err != nil {
return err
}
}
return nil
}
func searchUUIDForModule(app *pocketbase.PocketBase, module string) string {
var uuid string
err := app.Dao().DB().Select("uuid").From("module").Where(dbx.NewExp("name = {:name}", dbx.Params{"name": module})).One(&uuid)
func searchUUIDForModule(app *pocketbase.PocketBase, moduleName string) string {
var event model.Event
err := app.Dao().DB().Select("*").From("events").Where(dbx.NewExp("Name = {:name}", dbx.Params{"name": moduleName})).One(&event)
if err != nil {
return ""
}
return uuid
return event.UUID
}

View File

@ -27,6 +27,7 @@ export async function fetchModulesByCourseAndSemester(
modulesResponse.forEach((module: Module) =>
modules.push(
new Module(
module.uuid,
module.name,
course,
module.name,
@ -50,6 +51,7 @@ export async function fetchAllModules(): Promise<Module[]> {
responseModules.forEach((module: Module) => {
modules.push(
new Module(
module.uuid,
module.name,
module.course,
module.name,

View File

@ -16,6 +16,7 @@ export async function fetchModule(name: string): Promise<Module> {
.then(
(module: Module) =>
new Module(
module.uuid,
module.name,
module.course,
module.name,

View File

@ -2,6 +2,7 @@ import { Event } from "./event";
export class Module {
constructor(
public uuid: string,
public name: string,
public course: string,
public userDefinedName: string,