Skip to content

Multiapplication Support #359

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

Merged
merged 12 commits into from
Sep 4, 2023
Merged
26 changes: 6 additions & 20 deletions backend/database/repositories/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ import (

// Implements IRepositoryInterface
type filesystemRepository struct {
frontEndID uuid.UUID
frontendRoot uuid.UUID
logicalName string
URL string
embeddedContext
}

// The ID for root, set this as the ID in a specified request
var FilesystemRootID uuid.UUID = uuid.Nil

// We really should use an ORM jesus this is ugly
func (rep filesystemRepository) query(query string, input ...interface{}) (FilesystemEntry, error) {
entity := FilesystemEntry{}
Expand Down Expand Up @@ -51,16 +52,6 @@ func (rep filesystemRepository) query(query string, input ...interface{}) (Files

// Returns: entry struct containing the entity that was just created
func (rep filesystemRepository) CreateEntry(file FilesystemEntry) (FilesystemEntry, error) {
if file.ParentFileID == FilesystemRootID {
// determine root ID
root, err := rep.GetRoot()
if err != nil {
return FilesystemEntry{}, errors.New("failed to get root")
}

file.ParentFileID = root.EntityID
}

var newID uuid.UUID
err := rep.ctx.Query("SELECT new_entity($1, $2, $3, $4)", []interface{}{file.ParentFileID, file.LogicalName, file.OwnerUserId, file.IsDocument}, &newID)
if err != nil {
Expand All @@ -70,17 +61,12 @@ func (rep filesystemRepository) CreateEntry(file FilesystemEntry) (FilesystemEnt
}

func (rep filesystemRepository) GetEntryWithID(ID uuid.UUID) (FilesystemEntry, error) {
if ID == FilesystemRootID {
return rep.GetRoot()
}

result, err := rep.query("SELECT * FROM filesystem WHERE EntityID = $1", ID)
return result, err
}

func (rep filesystemRepository) GetRoot() (FilesystemEntry, error) {
// Root is currently set to ID 1
return rep.query("SELECT * FROM filesystem WHERE EntityID = $1", FilesystemRootID)
return rep.query("SELECT * FROM filesystem WHERE EntityID = $1", rep.frontendRoot)
}

func (rep filesystemRepository) GetEntryWithParentID(ID uuid.UUID) (FilesystemEntry, error) {
Expand All @@ -95,7 +81,7 @@ func (rep filesystemRepository) GetIDWithPath(path string) (uuid.UUID, error) {
}

// Determine main parent
parent, err := rep.query("SELECT * FROM filesystem WHERE LogicalName = $1", parentNames[1])
parent, err := rep.query("SELECT * FROM filesystem WHERE LogicalName = $1 AND Parent = $2", parentNames[1], rep.frontendRoot)
if err != nil {
return uuid.Nil, err
}
Expand Down
35 changes: 34 additions & 1 deletion backend/database/repositories/frontends.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,48 @@
package repositories

import (
"fmt"

"github.com/google/uuid"
)

type frontendsRepository struct {
embeddedContext
}

const InvalidFrontend = -1

func NewFrontendRepo(logicalName string, URL string, embeddedContext embeddedContext) (filesystemRepository, error) {
rows, err := embeddedContext.ctx.QueryRow("SELECT * from new_frontend($1, $2)", []interface{}{logicalName, URL})
if err != nil {
return filesystemRepository{}, fmt.Errorf("Error setting up frontend in Postgres (new_frontend): %w", err)
}

defer rows.Close()

var frontendID uuid.UUID
var frontendRoot uuid.UUID

if rows.Next() {
err := rows.Scan(&frontendID, &frontendRoot)
if err != nil {
return filesystemRepository{}, fmt.Errorf("Error scanning columns within new_frontend: %w", err)
}
}

return filesystemRepository{
frontendID,
frontendRoot,
logicalName,
URL,
embeddedContext,
}, nil
}

// GetFrontendFromURL is the implementation of the frontend repository for frontendRepository
func (rep frontendsRepository) GetFrontendFromURL(url string) int {
var frontendId int
err := rep.ctx.Query("SELECT FrontendId from frontend where FrontendUrl = $1;", []interface{}{url}, &frontendId)
err := rep.ctx.Query("SELECT ID from frontend where URL = $1;", []interface{}{url}, &frontendId)
if err != nil {
return InvalidFrontend
}
Expand Down
9 changes: 4 additions & 5 deletions backend/database/repositories/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"sync"

"cms.csesoc.unsw.edu.au/database/contexts"
"github.com/google/uuid"
)

// Start up a database connection with a provided context
Expand All @@ -15,10 +16,8 @@ var context contexts.DatabaseContext = nil
// Open constructors available for everyone

// NewFilesystemRepo instantiates a new file system repository with the current embedded context
func NewFilesystemRepo(context contexts.DatabaseContext) FilesystemRepository {
return filesystemRepository{
embeddedContext{context},
}
func NewFilesystemRepo(logicalName string, URL string, context contexts.DatabaseContext) (FilesystemRepository, error) {
return NewFrontendRepo(logicalName, URL, embeddedContext{context})
}

// NewGroupsRepo instantiates a new groups repository
Expand All @@ -36,7 +35,7 @@ func NewFrontendsRepo(context contexts.DatabaseContext) FrontendsRepository {
}

// NewPersonRepo instantiates a new person repository
func NewPersonRepo(frontendId int) PersonRepository {
func NewPersonRepo(frontendId uuid.UUID) PersonRepository {
return personRepository{
frontendId,
embeddedContext{getContext()},
Expand Down
Loading