Skip to content

Commit 5545f74

Browse files
Multiapplication Support (#359)
* cleaned sql files and added multi-application & permissions schemas * fixed some tests * updated sql files * added context argument to root retrieval test * fixed up most tests * fixed last test * added PR suggestions * fixed faulty schema and test * removed confusing global testing variable frontendID * adding multiapplications test case
1 parent e9acc3a commit 5545f74

24 files changed

+891
-178
lines changed

backend/database/repositories/filesystem.go

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ import (
1010

1111
// Implements IRepositoryInterface
1212
type filesystemRepository struct {
13+
frontEndID uuid.UUID
14+
frontendRoot uuid.UUID
15+
logicalName string
16+
URL string
1317
embeddedContext
1418
}
1519

16-
// The ID for root, set this as the ID in a specified request
17-
var FilesystemRootID uuid.UUID = uuid.Nil
18-
1920
// We really should use an ORM jesus this is ugly
2021
func (rep filesystemRepository) query(query string, input ...interface{}) (FilesystemEntry, error) {
2122
entity := FilesystemEntry{}
@@ -51,16 +52,6 @@ func (rep filesystemRepository) query(query string, input ...interface{}) (Files
5152

5253
// Returns: entry struct containing the entity that was just created
5354
func (rep filesystemRepository) CreateEntry(file FilesystemEntry) (FilesystemEntry, error) {
54-
if file.ParentFileID == FilesystemRootID {
55-
// determine root ID
56-
root, err := rep.GetRoot()
57-
if err != nil {
58-
return FilesystemEntry{}, errors.New("failed to get root")
59-
}
60-
61-
file.ParentFileID = root.EntityID
62-
}
63-
6455
var newID uuid.UUID
6556
err := rep.ctx.Query("SELECT new_entity($1, $2, $3, $4)", []interface{}{file.ParentFileID, file.LogicalName, file.OwnerUserId, file.IsDocument}, &newID)
6657
if err != nil {
@@ -70,17 +61,12 @@ func (rep filesystemRepository) CreateEntry(file FilesystemEntry) (FilesystemEnt
7061
}
7162

7263
func (rep filesystemRepository) GetEntryWithID(ID uuid.UUID) (FilesystemEntry, error) {
73-
if ID == FilesystemRootID {
74-
return rep.GetRoot()
75-
}
76-
7764
result, err := rep.query("SELECT * FROM filesystem WHERE EntityID = $1", ID)
7865
return result, err
7966
}
8067

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

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

9783
// Determine main parent
98-
parent, err := rep.query("SELECT * FROM filesystem WHERE LogicalName = $1", parentNames[1])
84+
parent, err := rep.query("SELECT * FROM filesystem WHERE LogicalName = $1 AND Parent = $2", parentNames[1], rep.frontendRoot)
9985
if err != nil {
10086
return uuid.Nil, err
10187
}

backend/database/repositories/frontends.go

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,48 @@
11
package repositories
22

3+
import (
4+
"fmt"
5+
6+
"github.com/google/uuid"
7+
)
8+
39
type frontendsRepository struct {
410
embeddedContext
511
}
612

713
const InvalidFrontend = -1
814

15+
func NewFrontendRepo(logicalName string, URL string, embeddedContext embeddedContext) (filesystemRepository, error) {
16+
rows, err := embeddedContext.ctx.QueryRow("SELECT * from new_frontend($1, $2)", []interface{}{logicalName, URL})
17+
if err != nil {
18+
return filesystemRepository{}, fmt.Errorf("Error setting up frontend in Postgres (new_frontend): %w", err)
19+
}
20+
21+
defer rows.Close()
22+
23+
var frontendID uuid.UUID
24+
var frontendRoot uuid.UUID
25+
26+
if rows.Next() {
27+
err := rows.Scan(&frontendID, &frontendRoot)
28+
if err != nil {
29+
return filesystemRepository{}, fmt.Errorf("Error scanning columns within new_frontend: %w", err)
30+
}
31+
}
32+
33+
return filesystemRepository{
34+
frontendID,
35+
frontendRoot,
36+
logicalName,
37+
URL,
38+
embeddedContext,
39+
}, nil
40+
}
41+
942
// GetFrontendFromURL is the implementation of the frontend repository for frontendRepository
1043
func (rep frontendsRepository) GetFrontendFromURL(url string) int {
1144
var frontendId int
12-
err := rep.ctx.Query("SELECT FrontendId from frontend where FrontendUrl = $1;", []interface{}{url}, &frontendId)
45+
err := rep.ctx.Query("SELECT ID from frontend where URL = $1;", []interface{}{url}, &frontendId)
1346
if err != nil {
1447
return InvalidFrontend
1548
}

backend/database/repositories/main.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"sync"
55

66
"cms.csesoc.unsw.edu.au/database/contexts"
7+
"github.com/google/uuid"
78
)
89

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

1718
// NewFilesystemRepo instantiates a new file system repository with the current embedded context
18-
func NewFilesystemRepo(context contexts.DatabaseContext) FilesystemRepository {
19-
return filesystemRepository{
20-
embeddedContext{context},
21-
}
19+
func NewFilesystemRepo(logicalName string, URL string, context contexts.DatabaseContext) (FilesystemRepository, error) {
20+
return NewFrontendRepo(logicalName, URL, embeddedContext{context})
2221
}
2322

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

3837
// NewPersonRepo instantiates a new person repository
39-
func NewPersonRepo(frontendId int) PersonRepository {
38+
func NewPersonRepo(frontendId uuid.UUID) PersonRepository {
4039
return personRepository{
4140
frontendId,
4241
embeddedContext{getContext()},

0 commit comments

Comments
 (0)