Gisle Aune
5 years ago
36 changed files with 1450 additions and 80 deletions
-
2.gitignore
-
2database/database.go
-
31database/drivers/mysqldriver/db.go
-
3database/drivers/mysqldriver/db_test.go
-
10database/drivers/mysqldriver/projects.go
-
70database/drivers/mysqldriver/projectstatuses.go
-
165database/drivers/mysqldriver/projectstatuses_test.go
-
1database/repositories/projectrepository.go
-
13database/repositories/projectstatusrepository.go
-
1go.mod
-
1go.sum
-
19graph/graph.go
-
2graph/graphcore/package.go
-
19graph/graphutil/fields.go
-
2graph/loaders/package.go
-
224graph/loaders/projectpermissionloader_gen.go
-
57graph/loaders/userloader.go
-
215graph/loaders/userloader_gen.go
-
62graph/resolvers/issue.resolvers.go
-
96graph/resolvers/mutation.resolvers.go
-
47graph/resolvers/project.resolvers.go
-
104graph/resolvers/query.resolvers.go
-
8graph/resolvers/resolver.go
-
25graph/schema/issue.gql
-
17graph/schema/mutation.gql
-
30graph/schema/project.gql
-
20graph/schema/user.gql
-
5internal/xlerrors/permission.go
-
101main.go
-
1migrations/mysql/20200406110301_create_table_item.sql
-
17migrations/mysql/20200503195913_create_table_project_status.sql
-
11models/item.go
-
4models/project.go
-
11models/projectstatus.go
-
120services/auth.go
-
14services/bundle.go
@ -1,2 +1,2 @@ |
|||||
/.idea |
/.idea |
||||
/graph/graphcore/* |
|
||||
|
/graph/graphcore/*_gen.go |
@ -0,0 +1,70 @@ |
|||||
|
package mysqldriver |
||||
|
|
||||
|
import ( |
||||
|
"context" |
||||
|
"database/sql" |
||||
|
"git.aiterp.net/stufflog/server/internal/xlerrors" |
||||
|
"git.aiterp.net/stufflog/server/models" |
||||
|
sq "github.com/Masterminds/squirrel" |
||||
|
"github.com/jmoiron/sqlx" |
||||
|
) |
||||
|
|
||||
|
type projectStatusRepository struct { |
||||
|
db *sqlx.DB |
||||
|
} |
||||
|
|
||||
|
func (r *projectStatusRepository) Find(ctx context.Context, projectID string, name string) (*models.ProjectStatus, error) { |
||||
|
projectStatus := models.ProjectStatus{} |
||||
|
err := r.db.GetContext(ctx, &projectStatus, "SELECT * FROM project_status WHERE project_id=? AND name=?", projectID, name) |
||||
|
if err != nil { |
||||
|
if err == sql.ErrNoRows { |
||||
|
return nil, xlerrors.NotFound("Project status") |
||||
|
} |
||||
|
|
||||
|
return nil, err |
||||
|
} |
||||
|
|
||||
|
return &projectStatus, nil |
||||
|
} |
||||
|
|
||||
|
func (r *projectStatusRepository) List(ctx context.Context, filter models.ProjectStatusFilter) ([]*models.ProjectStatus, error) { |
||||
|
q := sq.Select("*").From("project_status") |
||||
|
if filter.ProjectID != nil { |
||||
|
q = q.Where(sq.Eq{"project_id": *filter.ProjectID}) |
||||
|
} |
||||
|
if filter.MinStage != nil { |
||||
|
q = q.Where(sq.GtOrEq{"stage": *filter.MinStage}) |
||||
|
} |
||||
|
if filter.MaxStage != nil { |
||||
|
q = q.Where(sq.LtOrEq{"stage": *filter.MaxStage}) |
||||
|
} |
||||
|
q = q.OrderBy("project_id", "stage", "name") |
||||
|
|
||||
|
query, args, err := q.ToSql() |
||||
|
if err != nil { |
||||
|
return nil, err |
||||
|
} |
||||
|
|
||||
|
statuses := make([]*models.ProjectStatus, 0, 16) |
||||
|
err = r.db.SelectContext(ctx, &statuses, query, args...) |
||||
|
if err != nil { |
||||
|
return nil, err |
||||
|
} |
||||
|
|
||||
|
return statuses, nil |
||||
|
} |
||||
|
|
||||
|
func (r *projectStatusRepository) Save(ctx context.Context, status models.ProjectStatus) error { |
||||
|
_, err := r.db.NamedExecContext(ctx, ` |
||||
|
REPLACE INTO project_status (project_id, name, stage, description) |
||||
|
VALUES (:project_id, :name, :stage, :description) |
||||
|
`, status) |
||||
|
|
||||
|
return err |
||||
|
} |
||||
|
|
||||
|
func (r *projectStatusRepository) Delete(ctx context.Context, status models.ProjectStatus) error { |
||||
|
_, err := r.db.ExecContext(ctx, "DELETE FROM project_status WHERE project_id=? AND name=?", status.ProjectID, status.Name) |
||||
|
|
||||
|
return err |
||||
|
} |
@ -0,0 +1,165 @@ |
|||||
|
package mysqldriver |
||||
|
|
||||
|
import ( |
||||
|
"context" |
||||
|
"git.aiterp.net/stufflog/server/internal/xlerrors" |
||||
|
"git.aiterp.net/stufflog/server/models" |
||||
|
"github.com/stretchr/testify/assert" |
||||
|
"testing" |
||||
|
"time" |
||||
|
) |
||||
|
|
||||
|
var projectStatus1 = models.ProjectStatus{ |
||||
|
ProjectID: project1.ID, |
||||
|
Stage: models.IssueStagePending, |
||||
|
Name: "Ready", |
||||
|
Description: "This task is ready to be done.", |
||||
|
} |
||||
|
var projectStatus2 = models.ProjectStatus{ |
||||
|
ProjectID: project1.ID, |
||||
|
Stage: models.IssueStageInactive, |
||||
|
Name: "Idea", |
||||
|
Description: "Maybe do this?", |
||||
|
} |
||||
|
var projectStatus3 = models.ProjectStatus{ |
||||
|
ProjectID: project2.ID, |
||||
|
Stage: models.IssueStageActive, |
||||
|
Name: "In Progress", |
||||
|
Description: "It is being done.", |
||||
|
} |
||||
|
var projectStatus4 = models.ProjectStatus{ |
||||
|
ProjectID: project2.ID, |
||||
|
Stage: models.IssueStageReview, |
||||
|
Name: "Rendered", |
||||
|
Description: "It is rendered and awaiting feedback.", |
||||
|
} |
||||
|
var projectStatus5 = models.ProjectStatus{ |
||||
|
ProjectID: project3.ID, |
||||
|
Stage: models.IssueStageCompleted, |
||||
|
Name: "Om Nom Nom", |
||||
|
Description: "Sustenance intake complete.", |
||||
|
} |
||||
|
var projectStatus6 = models.ProjectStatus{ |
||||
|
ProjectID: project1.ID, |
||||
|
Stage: models.IssueStageFailed, |
||||
|
Name: "Bad Stuff", |
||||
|
Description: "Stuff could not be done.", |
||||
|
} |
||||
|
var projectStatus7 = models.ProjectStatus{ |
||||
|
ProjectID: project2.ID, |
||||
|
Stage: models.IssueStagePostponed, |
||||
|
Name: "Too Hard", |
||||
|
Description: "Do it later.", |
||||
|
} |
||||
|
var projectStatus7Updated = models.ProjectStatus{ |
||||
|
ProjectID: project2.ID, |
||||
|
Stage: models.IssueStageInactive, |
||||
|
Name: "Too Hard", |
||||
|
Description: "Do it later, when you're less of newbie.", |
||||
|
} |
||||
|
|
||||
|
func TestProjectStatusRepository(t *testing.T) { |
||||
|
statuses := testDB.projectStatuses |
||||
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) |
||||
|
defer cancel() |
||||
|
|
||||
|
assert.NoError(t, clearTable("project_status")) |
||||
|
|
||||
|
// INSERT
|
||||
|
assert.NoError(t, statuses.Save(ctx, projectStatus1)) |
||||
|
assert.NoError(t, statuses.Save(ctx, projectStatus2)) |
||||
|
assert.NoError(t, statuses.Save(ctx, projectStatus3)) |
||||
|
assert.NoError(t, statuses.Save(ctx, projectStatus4)) |
||||
|
assert.NoError(t, statuses.Save(ctx, projectStatus5)) |
||||
|
assert.NoError(t, statuses.Save(ctx, projectStatus6)) |
||||
|
assert.NoError(t, statuses.Save(ctx, projectStatus7)) |
||||
|
|
||||
|
// FIND
|
||||
|
result, err := statuses.Find(ctx, project2.ID, projectStatus3.Name) |
||||
|
assert.NoError(t, err) |
||||
|
assert.Equal(t, &projectStatus3, result) |
||||
|
result, err = statuses.Find(ctx, project1.ID, projectStatus6.Name) |
||||
|
assert.NoError(t, err) |
||||
|
assert.Equal(t, &projectStatus6, result) |
||||
|
|
||||
|
// FINDn't
|
||||
|
result, err = statuses.Find(ctx, project3.ID, projectStatus6.Name) |
||||
|
assert.Error(t, err) |
||||
|
assert.True(t, xlerrors.IsNotFound(err)) |
||||
|
assert.Nil(t, result) |
||||
|
result, err = statuses.Find(ctx, project2.ID, "Non-existent Name") |
||||
|
assert.Error(t, err) |
||||
|
assert.True(t, xlerrors.IsNotFound(err)) |
||||
|
assert.Nil(t, result) |
||||
|
|
||||
|
// LIST
|
||||
|
results, err := statuses.List(ctx, models.ProjectStatusFilter{}) |
||||
|
assert.NoError(t, err) |
||||
|
assert.Equal(t, []*models.ProjectStatus{ |
||||
|
&projectStatus5, |
||||
|
&projectStatus3, |
||||
|
&projectStatus4, |
||||
|
&projectStatus7, |
||||
|
&projectStatus2, |
||||
|
&projectStatus1, |
||||
|
&projectStatus6, |
||||
|
}, results) |
||||
|
results, err = statuses.List(ctx, models.ProjectStatusFilter{ProjectID: &project1.ID}) |
||||
|
assert.NoError(t, err) |
||||
|
assert.Equal(t, []*models.ProjectStatus{ |
||||
|
&projectStatus2, |
||||
|
&projectStatus1, |
||||
|
&projectStatus6, |
||||
|
}, results) |
||||
|
results, err = statuses.List(ctx, models.ProjectStatusFilter{ProjectID: &project2.ID, MinStage: ptrInt(models.IssueStageReview)}) |
||||
|
assert.NoError(t, err) |
||||
|
assert.Equal(t, []*models.ProjectStatus{ |
||||
|
&projectStatus4, |
||||
|
&projectStatus7, |
||||
|
}, results) |
||||
|
results, err = statuses.List(ctx, models.ProjectStatusFilter{ProjectID: &project2.ID, MaxStage: ptrInt(models.IssueStageReview)}) |
||||
|
assert.NoError(t, err) |
||||
|
assert.Equal(t, []*models.ProjectStatus{ |
||||
|
&projectStatus3, |
||||
|
&projectStatus4, |
||||
|
}, results) |
||||
|
results, err = statuses.List(ctx, models.ProjectStatusFilter{ProjectID: &project3.ID, MaxStage: ptrInt(models.IssueStagePending)}) |
||||
|
assert.NoError(t, err) |
||||
|
assert.Equal(t, []*models.ProjectStatus{}, results) |
||||
|
|
||||
|
// UPDATE
|
||||
|
assert.NoError(t, statuses.Save(ctx, projectStatus7Updated)) |
||||
|
|
||||
|
// FIND after UPDATE
|
||||
|
result, err = statuses.Find(ctx, project2.ID, projectStatus7.Name) |
||||
|
assert.NoError(t, err) |
||||
|
assert.Equal(t, &projectStatus7Updated, result) |
||||
|
|
||||
|
// LIST after UPDATE
|
||||
|
results, err = statuses.List(ctx, models.ProjectStatusFilter{}) |
||||
|
assert.NoError(t, err) |
||||
|
assert.Equal(t, []*models.ProjectStatus{ |
||||
|
&projectStatus5, |
||||
|
&projectStatus7Updated, |
||||
|
&projectStatus3, |
||||
|
&projectStatus4, |
||||
|
&projectStatus2, |
||||
|
&projectStatus1, |
||||
|
&projectStatus6, |
||||
|
}, results) |
||||
|
|
||||
|
// DELETE
|
||||
|
assert.NoError(t, statuses.Delete(ctx, projectStatus7Updated)) |
||||
|
|
||||
|
// LIST after DELETE
|
||||
|
results, err = statuses.List(ctx, models.ProjectStatusFilter{}) |
||||
|
assert.NoError(t, err) |
||||
|
assert.Equal(t, []*models.ProjectStatus{ |
||||
|
&projectStatus5, |
||||
|
&projectStatus3, |
||||
|
&projectStatus4, |
||||
|
&projectStatus2, |
||||
|
&projectStatus1, |
||||
|
&projectStatus6, |
||||
|
}, results) |
||||
|
} |
@ -0,0 +1,13 @@ |
|||||
|
package repositories |
||||
|
|
||||
|
import ( |
||||
|
"context" |
||||
|
"git.aiterp.net/stufflog/server/models" |
||||
|
) |
||||
|
|
||||
|
type ProjectStatusRepository interface { |
||||
|
Find(ctx context.Context, projectID string, id string) (*models.ProjectStatus, error) |
||||
|
List(ctx context.Context, filter models.ProjectStatusFilter) ([]*models.ProjectStatus, error) |
||||
|
Save(ctx context.Context, status models.ProjectStatus) error |
||||
|
Delete(ctx context.Context, status models.ProjectStatus) error |
||||
|
} |
@ -0,0 +1,2 @@ |
|||||
|
// Package graphcore contains the generated code.
|
||||
|
package graphcore |
@ -0,0 +1,19 @@ |
|||||
|
package graphutil |
||||
|
|
||||
|
import ( |
||||
|
"context" |
||||
|
"github.com/99designs/gqlgen/graphql" |
||||
|
) |
||||
|
|
||||
|
func SelectsAnyField(ctx context.Context, fields ...string) bool { |
||||
|
collectedFields := graphql.CollectFieldsCtx(ctx, []string{"description"}) |
||||
|
for _, collectedField := range collectedFields { |
||||
|
for _, field := range fields { |
||||
|
if collectedField.Name == field { |
||||
|
return true |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return false |
||||
|
} |
@ -0,0 +1,2 @@ |
|||||
|
// Package loaders is for the generated data loaders.
|
||||
|
package loaders |
@ -0,0 +1,224 @@ |
|||||
|
// Code generated by github.com/vektah/dataloaden, DO NOT EDIT.
|
||||
|
|
||||
|
package loaders |
||||
|
|
||||
|
import ( |
||||
|
"sync" |
||||
|
"time" |
||||
|
|
||||
|
"git.aiterp.net/stufflog/server/models" |
||||
|
) |
||||
|
|
||||
|
// ProjectPermissionLoaderConfig captures the config to create a new ProjectPermissionLoader
|
||||
|
type ProjectPermissionLoaderConfig struct { |
||||
|
// Fetch is a method that provides the data for the loader
|
||||
|
Fetch func(keys []string) ([]*models.ProjectPermission, []error) |
||||
|
|
||||
|
// Wait is how long wait before sending a batch
|
||||
|
Wait time.Duration |
||||
|
|
||||
|
// MaxBatch will limit the maximum number of keys to send in one batch, 0 = not limit
|
||||
|
MaxBatch int |
||||
|
} |
||||
|
|
||||
|
// NewProjectPermissionLoader creates a new ProjectPermissionLoader given a fetch, wait, and maxBatch
|
||||
|
func NewProjectPermissionLoader(config ProjectPermissionLoaderConfig) *ProjectPermissionLoader { |
||||
|
return &ProjectPermissionLoader{ |
||||
|
fetch: config.Fetch, |
||||
|
wait: config.Wait, |
||||
|
maxBatch: config.MaxBatch, |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// ProjectPermissionLoader batches and caches requests
|
||||
|
type ProjectPermissionLoader struct { |
||||
|
// this method provides the data for the loader
|
||||
|
fetch func(keys []string) ([]*models.ProjectPermission, []error) |
||||
|
|
||||
|
// how long to done before sending a batch
|
||||
|
wait time.Duration |
||||
|
|
||||
|
// this will limit the maximum number of keys to send in one batch, 0 = no limit
|
||||
|
maxBatch int |
||||
|
|
||||
|
// INTERNAL
|
||||
|
|
||||
|
// lazily created cache
|
||||
|
cache map[string]*models.ProjectPermission |
||||
|
|
||||
|
// the current batch. keys will continue to be collected until timeout is hit,
|
||||
|
// then everything will be sent to the fetch method and out to the listeners
|
||||
|
batch *projectPermissionLoaderBatch |
||||
|
|
||||
|
// mutex to prevent races
|
||||
|
mu sync.Mutex |
||||
|
} |
||||
|
|
||||
|
type projectPermissionLoaderBatch struct { |
||||
|
keys []string |
||||
|
data []*models.ProjectPermission |
||||
|
error []error |
||||
|
closing bool |
||||
|
done chan struct{} |
||||
|
} |
||||
|
|
||||
|
// Load a ProjectPermission by key, batching and caching will be applied automatically
|
||||
|
func (l *ProjectPermissionLoader) Load(key string) (*models.ProjectPermission, error) { |
||||
|
return l.LoadThunk(key)() |
||||
|
} |
||||
|
|
||||
|
// LoadThunk returns a function that when called will block waiting for a ProjectPermission.
|
||||
|
// This method should be used if you want one goroutine to make requests to many
|
||||
|
// different data loaders without blocking until the thunk is called.
|
||||
|
func (l *ProjectPermissionLoader) LoadThunk(key string) func() (*models.ProjectPermission, error) { |
||||
|
l.mu.Lock() |
||||
|
if it, ok := l.cache[key]; ok { |
||||
|
l.mu.Unlock() |
||||
|
return func() (*models.ProjectPermission, error) { |
||||
|
return it, nil |
||||
|
} |
||||
|
} |
||||
|
if l.batch == nil { |
||||
|
l.batch = &projectPermissionLoaderBatch{done: make(chan struct{})} |
||||
|
} |
||||
|
batch := l.batch |
||||
|
pos := batch.keyIndex(l, key) |
||||
|
l.mu.Unlock() |
||||
|
|
||||
|
return func() (*models.ProjectPermission, error) { |
||||
|
<-batch.done |
||||
|
|
||||
|
var data *models.ProjectPermission |
||||
|
if pos < len(batch.data) { |
||||
|
data = batch.data[pos] |
||||
|
} |
||||
|
|
||||
|
var err error |
||||
|
// its convenient to be able to return a single error for everything
|
||||
|
if len(batch.error) == 1 { |
||||
|
err = batch.error[0] |
||||
|
} else if batch.error != nil { |
||||
|
err = batch.error[pos] |
||||
|
} |
||||
|
|
||||
|
if err == nil { |
||||
|
l.mu.Lock() |
||||
|
l.unsafeSet(key, data) |
||||
|
l.mu.Unlock() |
||||
|
} |
||||
|
|
||||
|
return data, err |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// LoadAll fetches many keys at once. It will be broken into appropriate sized
|
||||
|
// sub batches depending on how the loader is configured
|
||||
|
func (l *ProjectPermissionLoader) LoadAll(keys []string) ([]*models.ProjectPermission, []error) { |
||||
|
results := make([]func() (*models.ProjectPermission, error), len(keys)) |
||||
|
|
||||
|
for i, key := range keys { |
||||
|
results[i] = l.LoadThunk(key) |
||||
|
} |
||||
|
|
||||
|
projectPermissions := make([]*models.ProjectPermission, len(keys)) |
||||
|
errors := make([]error, len(keys)) |
||||
|
for i, thunk := range results { |
||||
|
projectPermissions[i], errors[i] = thunk() |
||||
|
} |
||||
|
return projectPermissions, errors |
||||
|
} |
||||
|
|
||||
|
// LoadAllThunk returns a function that when called will block waiting for a ProjectPermissions.
|
||||
|
// This method should be used if you want one goroutine to make requests to many
|
||||
|
// different data loaders without blocking until the thunk is called.
|
||||
|
func (l *ProjectPermissionLoader) LoadAllThunk(keys []string) func() ([]*models.ProjectPermission, []error) { |
||||
|
results := make([]func() (*models.ProjectPermission, error), len(keys)) |
||||
|
for i, key := range keys { |
||||
|
results[i] = l.LoadThunk(key) |
||||
|
} |
||||
|
return func() ([]*models.ProjectPermission, []error) { |
||||
|
projectPermissions := make([]*models.ProjectPermission, len(keys)) |
||||
|
errors := make([]error, len(keys)) |
||||
|
for i, thunk := range results { |
||||
|
projectPermissions[i], errors[i] = thunk() |
||||
|
} |
||||
|
return projectPermissions, errors |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// Prime the cache with the provided key and value. If the key already exists, no change is made
|
||||
|
// and false is returned.
|
||||
|
// (To forcefully prime the cache, clear the key first with loader.clear(key).prime(key, value).)
|
||||
|
func (l *ProjectPermissionLoader) Prime(key string, value *models.ProjectPermission) bool { |
||||
|
l.mu.Lock() |
||||
|
var found bool |
||||
|
if _, found = l.cache[key]; !found { |
||||
|
// make a copy when writing to the cache, its easy to pass a pointer in from a loop var
|
||||
|
// and end up with the whole cache pointing to the same value.
|
||||
|
cpy := *value |
||||
|
l.unsafeSet(key, &cpy) |
||||
|
} |
||||
|
l.mu.Unlock() |
||||
|
return !found |
||||
|
} |
||||
|
|
||||
|
// Clear the value at key from the cache, if it exists
|
||||
|
func (l *ProjectPermissionLoader) Clear(key string) { |
||||
|
l.mu.Lock() |
||||
|
delete(l.cache, key) |
||||
|
l.mu.Unlock() |
||||
|
} |
||||
|
|
||||
|
func (l *ProjectPermissionLoader) unsafeSet(key string, value *models.ProjectPermission) { |
||||
|
if l.cache == nil { |
||||
|
l.cache = map[string]*models.ProjectPermission{} |
||||
|
} |
||||
|
l.cache[key] = value |
||||
|
} |
||||
|
|
||||
|
// keyIndex will return the location of the key in the batch, if its not found
|
||||
|
// it will add the key to the batch
|
||||
|
func (b *projectPermissionLoaderBatch) keyIndex(l *ProjectPermissionLoader, key string) int { |
||||
|
for i, existingKey := range b.keys { |
||||
|
if key == existingKey { |
||||
|
return i |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
pos := len(b.keys) |
||||
|
b.keys = append(b.keys, key) |
||||
|
if pos == 0 { |
||||
|
go b.startTimer(l) |
||||
|
} |
||||
|
|
||||
|
if l.maxBatch != 0 && pos >= l.maxBatch-1 { |
||||
|
if !b.closing { |
||||
|
b.closing = true |
||||
|
l.batch = nil |
||||
|
go b.end(l) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return pos |
||||
|
} |
||||
|
|
||||
|
func (b *projectPermissionLoaderBatch) startTimer(l *ProjectPermissionLoader) { |
||||
|
time.Sleep(l.wait) |
||||
|
l.mu.Lock() |
||||
|
|
||||
|
// we must have hit a batch limit and are already finalizing this batch
|
||||
|
if b.closing { |
||||
|
l.mu.Unlock() |
||||
|
return |
||||
|
} |
||||
|
|
||||
|
l.batch = nil |
||||
|
l.mu.Unlock() |
||||
|
|
||||
|
b.end(l) |
||||
|
} |
||||
|
|
||||
|
func (b *projectPermissionLoaderBatch) end(l *ProjectPermissionLoader) { |
||||
|
b.data, b.error = l.fetch(b.keys) |
||||
|
close(b.done) |
||||
|
} |
@ -0,0 +1,57 @@ |
|||||
|
package loaders |
||||
|
|
||||
|
import ( |
||||
|
"context" |
||||
|
"git.aiterp.net/stufflog/server/database/repositories" |
||||
|
"git.aiterp.net/stufflog/server/internal/xlerrors" |
||||
|
"git.aiterp.net/stufflog/server/models" |
||||
|
"time" |
||||
|
) |
||||
|
|
||||
|
// go run github.com/vektah/dataloaden UserLoader string *git.aiterp.net/stufflog/server/models.User
|
||||
|
|
||||
|
var userLoaderCtxKey = "ctx.stufflog.UserLoader" |
||||
|
|
||||
|
func NewUserLoaderContext(ctx context.Context, userRepo repositories.UserRepository) context.Context { |
||||
|
return context.WithValue(ctx, userLoaderCtxKey, NewUserLoader(ctx, userRepo)) |
||||
|
} |
||||
|
|
||||
|
func UserLoaderFromContext(ctx context.Context) *UserLoader { |
||||
|
return ctx.Value(userLoaderCtxKey).(*UserLoader) |
||||
|
} |
||||
|
|
||||
|
func NewUserLoader(ctx context.Context, userRepo repositories.UserRepository) *UserLoader { |
||||
|
return &UserLoader{ |
||||
|
wait: 2 * time.Millisecond, |
||||
|
maxBatch: 100, |
||||
|
fetch: func(keys []string) ([]*models.User, []error) { |
||||
|
results := make([]*models.User, len(keys)) |
||||
|
errors := make([]error, len(keys)) |
||||
|
|
||||
|
users, err := userRepo.List(ctx, models.UserFilter{UserIDs: keys}) |
||||
|
if err != nil { |
||||
|
for i := range errors { |
||||
|
errors[i] = err |
||||
|
} |
||||
|
|
||||
|
return results, errors |
||||
|
} |
||||
|
|
||||
|
userMap := make(map[string]*models.User, len(users)) |
||||
|
for _, user := range users { |
||||
|
userMap[user.ID] = user |
||||
|
} |
||||
|
|
||||
|
for i, id := range keys { |
||||
|
user := userMap[id] |
||||
|
if user != nil { |
||||
|
results[i] = user |
||||
|
} else { |
||||
|
errors[i] = xlerrors.NotFound("User") |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return results, errors |
||||
|
}, |
||||
|
} |
||||
|
} |
@ -0,0 +1,215 @@ |
|||||
|
// Code generated by github.com/vektah/dataloaden, DO NOT EDIT.
|
||||
|
|
||||
|
package loaders |
||||
|
|
||||
|
import ( |
||||
|
"sync" |
||||
|
"time" |
||||
|
|
||||
|
"git.aiterp.net/stufflog/server/models" |
||||
|
) |
||||
|
|
||||
|
// UserLoaderConfig captures the config to create a new UserLoader
|
||||
|
type UserLoaderConfig struct { |
||||
|
// Fetch is a method that provides the data for the loader
|
||||
|
Fetch func(keys []string) ([]*models.User, []error) |
||||
|
|
||||
|
// Wait is how long wait before sending a batch
|
||||
|
Wait time.Duration |
||||
|
|
||||
|
// MaxBatch will limit the maximum number of keys to send in one batch, 0 = not limit
|
||||
|
MaxBatch int |
||||
|
} |
||||
|
|
||||
|
// UserLoader batches and caches requests
|
||||
|
type UserLoader struct { |
||||
|
// this method provides the data for the loader
|
||||
|
fetch func(keys []string) ([]*models.User, []error) |
||||
|
|
||||
|
// how long to done before sending a batch
|
||||
|
wait time.Duration |
||||
|
|
||||
|
// this will limit the maximum number of keys to send in one batch, 0 = no limit
|
||||
|
maxBatch int |
||||
|
|
||||
|
// INTERNAL
|
||||
|
|
||||
|
// lazily created cache
|
||||
|
cache map[string]*models.User |
||||
|
|
||||
|
// the current batch. keys will continue to be collected until timeout is hit,
|
||||
|
// then everything will be sent to the fetch method and out to the listeners
|
||||
|
batch *userLoaderBatch |
||||
|
|
||||
|
// mutex to prevent races
|
||||
|
mu sync.Mutex |
||||
|
} |
||||
|
|
||||
|
type userLoaderBatch struct { |
||||
|
keys []string |
||||
|
data []*models.User |
||||
|
error []error |
||||
|
closing bool |
||||
|
done chan struct{} |
||||
|
} |
||||
|
|
||||
|
// Load a User by key, batching and caching will be applied automatically
|
||||
|
func (l *UserLoader) Load(key string) (*models.User, error) { |
||||
|
return l.LoadThunk(key)() |
||||
|
} |
||||
|
|
||||
|
// LoadThunk returns a function that when called will block waiting for a User.
|
||||
|
// This method should be used if you want one goroutine to make requests to many
|
||||
|
// different data loaders without blocking until the thunk is called.
|
||||
|
func (l *UserLoader) LoadThunk(key string) func() (*models.User, error) { |
||||
|
l.mu.Lock() |
||||
|
if it, ok := l.cache[key]; ok { |
||||
|
l.mu.Unlock() |
||||
|
return func() (*models.User, error) { |
||||
|
return it, nil |
||||
|
} |
||||
|
} |
||||
|
if l.batch == nil { |
||||
|
l.batch = &userLoaderBatch{done: make(chan struct{})} |
||||
|
} |
||||
|
batch := l.batch |
||||
|
pos := batch.keyIndex(l, key) |
||||
|
l.mu.Unlock() |
||||
|
|
||||
|
return func() (*models.User, error) { |
||||
|
<-batch.done |
||||
|
|
||||
|
var data *models.User |
||||
|
if pos < len(batch.data) { |
||||
|
data = batch.data[pos] |
||||
|
} |
||||
|
|
||||
|
var err error |
||||
|
// its convenient to be able to return a single error for everything
|
||||
|
if len(batch.error) == 1 { |
||||
|
err = batch.error[0] |
||||
|
} else if batch.error != nil { |
||||
|
err = batch.error[pos] |
||||
|
} |
||||
|
|
||||
|
if err == nil { |
||||
|
l.mu.Lock() |
||||
|
l.unsafeSet(key, data) |
||||
|
l.mu.Unlock() |
||||
|
} |
||||
|
|
||||
|
return data, err |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// LoadAll fetches many keys at once. It will be broken into appropriate sized
|
||||
|
// sub batches depending on how the loader is configured
|
||||
|
func (l *UserLoader) LoadAll(keys []string) ([]*models.User, []error) { |
||||
|
results := make([]func() (*models.User, error), len(keys)) |
||||
|
|
||||
|
for i, key := range keys { |
||||
|
results[i] = l.LoadThunk(key) |
||||
|
} |
||||
|
|
||||
|
users := make([]*models.User, len(keys)) |
||||
|
errors := make([]error, len(keys)) |
||||
|
for i, thunk := range results { |
||||
|
users[i], errors[i] = thunk() |
||||
|
} |
||||
|
return users, errors |
||||
|
} |
||||
|
|
||||
|
// LoadAllThunk returns a function that when called will block waiting for a Users.
|
||||
|
// This method should be used if you want one goroutine to make requests to many
|
||||
|
// different data loaders without blocking until the thunk is called.
|
||||
|
func (l *UserLoader) LoadAllThunk(keys []string) func() ([]*models.User, []error) { |
||||
|
results := make([]func() (*models.User, error), len(keys)) |
||||
|
for i, key := range keys { |
||||
|
results[i] = l.LoadThunk(key) |
||||
|
} |
||||
|
return func() ([]*models.User, []error) { |
||||
|
users := make([]*models.User, len(keys)) |
||||
|
errors := make([]error, len(keys)) |
||||
|
for i, thunk := range results { |
||||
|
users[i], errors[i] = thunk() |
||||
|
} |
||||
|
return users, errors |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// Prime the cache with the provided key and value. If the key already exists, no change is made
|
||||
|
// and false is returned.
|
||||
|
// (To forcefully prime the cache, clear the key first with loader.clear(key).prime(key, value).)
|
||||
|
func (l *UserLoader) Prime(key string, value *models.User) bool { |
||||
|
l.mu.Lock() |
||||
|
var found bool |
||||
|
if _, found = l.cache[key]; !found { |
||||
|
// make a copy when writing to the cache, its easy to pass a pointer in from a loop var
|
||||
|
// and end up with the whole cache pointing to the same value.
|
||||
|
cpy := *value |
||||
|
l.unsafeSet(key, &cpy) |
||||
|
} |
||||
|
l.mu.Unlock() |
||||
|
return !found |
||||
|
} |
||||
|
|
||||
|
// Clear the value at key from the cache, if it exists
|
||||
|
func (l *UserLoader) Clear(key string) { |
||||
|
l.mu.Lock() |
||||
|
delete(l.cache, key) |
||||
|
l.mu.Unlock() |
||||
|
} |
||||
|
|
||||
|
func (l *UserLoader) unsafeSet(key string, value *models.User) { |
||||
|
if l.cache == nil { |
||||
|
l.cache = map[string]*models.User{} |
||||
|
} |
||||
|
l.cache[key] = value |
||||
|
} |
||||
|
|
||||
|
// keyIndex will return the location of the key in the batch, if its not found
|
||||
|
// it will add the key to the batch
|
||||
|
func (b *userLoaderBatch) keyIndex(l *UserLoader, key string) int { |
||||
|
for i, existingKey := range b.keys { |
||||
|
if key == existingKey { |
||||
|
return i |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
pos := len(b.keys) |
||||
|
b.keys = append(b.keys, key) |
||||
|
if pos == 0 { |
||||
|
go b.startTimer(l) |
||||
|
} |
||||
|
|
||||
|
if l.maxBatch != 0 && pos >= l.maxBatch-1 { |
||||
|
if !b.closing { |
||||
|
b.closing = true |
||||
|
l.batch = nil |
||||
|
go b.end(l) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return pos |
||||
|
} |
||||
|
|
||||
|
func (b *userLoaderBatch) startTimer(l *UserLoader) { |
||||
|
time.Sleep(l.wait) |
||||
|
l.mu.Lock() |
||||
|
|
||||
|
// we must have hit a batch limit and are already finalizing this batch
|
||||
|
if b.closing { |
||||
|
l.mu.Unlock() |
||||
|
return |
||||
|
} |
||||
|
|
||||
|
l.batch = nil |
||||
|
l.mu.Unlock() |
||||
|
|
||||
|
b.end(l) |
||||
|
} |
||||
|
|
||||
|
func (b *userLoaderBatch) end(l *UserLoader) { |
||||
|
b.data, b.error = l.fetch(b.keys) |
||||
|
close(b.done) |
||||
|
} |
@ -1,11 +1,15 @@ |
|||||
package resolvers |
package resolvers |
||||
|
|
||||
import "git.aiterp.net/stufflog/server/services" |
|
||||
|
import ( |
||||
|
"git.aiterp.net/stufflog/server/database" |
||||
|
"git.aiterp.net/stufflog/server/services" |
||||
|
) |
||||
|
|
||||
// This file will not be regenerated automatically.
|
// This file will not be regenerated automatically.
|
||||
//
|
//
|
||||
// It serves as dependency injection for your app, add any dependencies you require here.
|
// It serves as dependency injection for your app, add any dependencies you require here.
|
||||
|
|
||||
type Resolver struct { |
type Resolver struct { |
||||
S services.Bundle |
|
||||
|
services.Bundle |
||||
|
Database database.Database |
||||
} |
} |
@ -1,4 +1,19 @@ |
|||||
type Mutation { |
type Mutation { |
||||
loginUser(input: UserLoginInput): User! |
|
||||
|
# PROJECT |
||||
|
"Create a new project" |
||||
|
createProject(input: ProjectCreateInput!): Project! |
||||
|
|
||||
|
# ISSUE |
||||
|
"Create a new issue" |
||||
|
createIssue(input: IssueCreateInput!): Issue! |
||||
|
|
||||
|
# USER |
||||
|
"Log in" |
||||
|
loginUser(input: UserLoginInput!): User! |
||||
|
"Log out" |
||||
logoutUser: User! |
logoutUser: User! |
||||
|
"Create a new user. This can only be done by administrators." |
||||
|
createUser(input: UserCreateInput!): User! |
||||
|
"Edit an existing user. This can only be done by administrators" |
||||
|
editUser(input: UserEditInput!): User! |
||||
} |
} |
@ -0,0 +1,5 @@ |
|||||
|
package xlerrors |
||||
|
|
||||
|
import "errors" |
||||
|
|
||||
|
var PermissionDenied = errors.New("permission denied") |
@ -0,0 +1,17 @@ |
|||||
|
-- +goose Up |
||||
|
-- +goose StatementBegin |
||||
|
CREATE TABLE project_status ( |
||||
|
project_id CHAR(16) NOT NULL, |
||||
|
name VARCHAR(255) NOT NULL, |
||||
|
stage INT NOT NULL, |
||||
|
description VARCHAR(255) NOT NULL, |
||||
|
|
||||
|
PRIMARY KEY (project_id, name), |
||||
|
INDEX (project_id, stage, name) # for sorting |
||||
|
); |
||||
|
-- +goose StatementEnd |
||||
|
|
||||
|
-- +goose Down |
||||
|
-- +goose StatementBegin |
||||
|
DROP TABLE project_status; |
||||
|
-- +goose StatementEnd |
@ -1,5 +1,19 @@ |
|||||
package services |
package services |
||||
|
|
||||
|
import "git.aiterp.net/stufflog/server/database" |
||||
|
|
||||
type Bundle struct { |
type Bundle struct { |
||||
Auth *Auth |
Auth *Auth |
||||
} |
} |
||||
|
|
||||
|
func NewBundle(db database.Database) Bundle { |
||||
|
auth := &Auth{ |
||||
|
users: db.Users(), |
||||
|
session: db.Session(), |
||||
|
projects: db.Projects(), |
||||
|
} |
||||
|
|
||||
|
return Bundle{ |
||||
|
Auth: auth, |
||||
|
} |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue