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

Multifrontend Support #253

Open
wants to merge 6 commits into
base: main
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
31 changes: 13 additions & 18 deletions backend/database/repositories/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ type filesystemRepository struct {
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,15 +48,16 @@ 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")
}
// TODO: I feel like this is useless?
// 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
}
// 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)
Expand All @@ -69,18 +67,15 @@ func (rep filesystemRepository) CreateEntry(file FilesystemEntry) (FilesystemEnt
return rep.GetEntryWithID(newID)
}

// TODO: Change this
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)
// TODO: Is this even necessary anymore? frontend table's GetIDWithName does same thing
func (rep filesystemRepository) GetRoot(frontend string) (FilesystemEntry, error) {
return rep.query("SELECT * FROM frontend WHERE FrontendLogicalname = $1", frontend)
}

func (rep filesystemRepository) GetEntryWithParentID(ID uuid.UUID) (FilesystemEntry, error) {
Expand Down
24 changes: 18 additions & 6 deletions backend/database/repositories/frontends.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
package repositories

import "github.com/google/uuid"

type frontendsRepository struct {
embeddedContext
}

const InvalidFrontend = -1
var InvalidFrontend = uuid.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)
func (rep frontendsRepository) GetFrontendFromURL(url string) (uuid.UUID, error) {
var frontendId uuid.UUID
err := rep.ctx.Query("SELECT FrontendID from frontend where FrontendUrl = $1;", []interface{}{url}, &frontendId)
if err != nil {
return InvalidFrontend
return InvalidFrontend, err
}

return frontendId
return frontendId, nil
}

// Get FrontendID (uuid) with logical name
func (rep frontendsRepository) GetIDWithName(name string) (uuid.UUID, error) {
var frontendId uuid.UUID
err := rep.ctx.Query("SELECT FrontendID from frontend where FrontendLogicalName = $1;", []interface{}{name}, &frontendId)
if err != nil {
return InvalidFrontend, err
}
return frontendId, nil
}
3 changes: 2 additions & 1 deletion 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 Down Expand Up @@ -36,7 +37,7 @@ func NewFrontendsRepo() 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
4 changes: 3 additions & 1 deletion backend/database/repositories/person.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ package repositories

import (
"log"

"github.com/google/uuid"
)

// Implements IPersonRepository
type personRepository struct {
frontEndID int
frontEndID uuid.UUID
embeddedContext
}

Expand Down
5 changes: 3 additions & 2 deletions backend/database/repositories/repository_interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type (
// mocked/real should implement
FilesystemRepository interface {
GetEntryWithID(ID uuid.UUID) (FilesystemEntry, error)
GetRoot() (FilesystemEntry, error)
GetRoot(frontend string) (FilesystemEntry, error)
GetEntryWithParentID(ID uuid.UUID) (FilesystemEntry, error)
GetIDWithPath(path string) (uuid.UUID, error)

Expand Down Expand Up @@ -72,7 +72,8 @@ type (

// repository interface for getting information from the frontend table
FrontendsRepository interface {
GetFrontendFromURL(url string) int
GetIDWithName(name string) (uuid.UUID, error)
GetFrontendFromURL(url string) (uuid.UUID, error)
}
)

Expand Down
Loading