stufflog graphql server
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

159 lines
3.8 KiB

package resolvers
// This file will be automatically regenerated based on the schema, any resolver implementations
// will be copied through when generating and any unknown code will be moved to the end.
import (
"context"
"errors"
"git.aiterp.net/stufflog/server/graph/graphcore"
"git.aiterp.net/stufflog/server/internal/xlerrors"
"git.aiterp.net/stufflog/server/models"
)
func (r *queryResolver) Issue(ctx context.Context, id string) (*models.Issue, error) {
user := r.Auth.UserFromContext(ctx)
if user == nil {
return nil, xlerrors.PermissionDenied
}
issue, err := r.Database.Issues().Find(ctx, id)
if err != nil {
return nil, err
}
_, err = r.Auth.IssuePermission(ctx, *issue)
if err != nil {
return nil, err
}
return issue, nil
}
func (r *queryResolver) Issues(ctx context.Context, filter *models.IssueFilter) ([]*models.Issue, error) {
user := r.Auth.UserFromContext(ctx)
if user == nil {
return nil, xlerrors.PermissionDenied
}
if filter == nil {
filter = &models.IssueFilter{}
}
issues, err := r.Database.Issues().List(ctx, *filter)
if err != nil {
return nil, err
}
deleteList := make([]int, 0, len(issues))
for i, issue := range issues {
_, err := r.Auth.IssuePermission(ctx, *issue)
if err != nil {
deleteList = append(deleteList, i-len(deleteList))
}
}
for _, index := range deleteList {
issues = append(issues[:index], issues[index+1:]...)
}
return issues, nil
}
func (r *queryResolver) Item(ctx context.Context, id string) (*models.Item, error) {
user := r.Auth.UserFromContext(ctx)
if user == nil {
return nil, xlerrors.PermissionDenied
}
return r.Database.Items().Find(ctx, id)
}
func (r *queryResolver) Items(ctx context.Context, filter *models.ItemFilter) ([]*models.Item, error) {
user := r.Auth.UserFromContext(ctx)
if user == nil {
return nil, xlerrors.PermissionDenied
}
if filter == nil {
filter = &models.ItemFilter{}
}
return r.Database.Items().List(ctx, *filter)
}
func (r *queryResolver) ItemTags(ctx context.Context) ([]string, error) {
user := r.Auth.UserFromContext(ctx)
if user == nil {
return nil, xlerrors.PermissionDenied
}
return r.Database.Items().GetTags(ctx)
}
func (r *queryResolver) Project(ctx context.Context, id string) (*models.Project, error) {
user := r.Auth.UserFromContext(ctx)
if user == nil {
return nil, xlerrors.PermissionDenied
}
project, err := r.Database.Projects().Find(ctx, id)
if err != nil {
return nil, err
}
_, err = r.Auth.ProjectPermission(ctx, *project)
if err != nil {
return nil, err
}
return project, nil
}
func (r *queryResolver) Projects(ctx context.Context, filter *models.ProjectFilter) ([]*models.Project, error) {
user := r.Auth.UserFromContext(ctx)
if user == nil {
return nil, xlerrors.PermissionDenied
}
skipCheck := false
if filter == nil {
filter = &models.ProjectFilter{}
}
if filter.Permission == nil {
skipCheck = true
filter.Permission = &models.ProjectFilterPermission{
UserID: user.ID,
MinLevel: models.ProjectPermissionLevelObserver,
}
}
projects, err := r.Database.Projects().List(ctx, *filter)
if err != nil {
return nil, err
}
if !skipCheck && len(projects) > 0 {
deleteList := make([]int, 0, 4)
for i, project := range projects {
if _, err := r.Auth.ProjectPermission(ctx, *project); err != nil {
deleteList = append(deleteList, i-len(deleteList))
}
}
for _, di := range deleteList {
projects = append(projects[:di], projects[di:]...)
}
}
return projects, nil
}
func (r *queryResolver) Session(ctx context.Context) (*models.User, error) {
user := r.Auth.UserFromContext(ctx)
if user == nil {
return nil, errors.New("not logged in")
}
return user, nil
}
// Query returns graphcore.QueryResolver implementation.
func (r *Resolver) Query() graphcore.QueryResolver { return &queryResolver{r} }
type queryResolver struct{ *Resolver }