Browse Source
File model and api is almost complete
File model and api is almost complete
- Added "Kind" field to File model - Added file query - Added editFile mutation - Added removeFile mutation1.0
Gisle Aune
7 years ago
4 changed files with 232 additions and 0 deletions
@ -0,0 +1,129 @@ |
|||
package resolver |
|||
|
|||
import ( |
|||
"context" |
|||
"time" |
|||
|
|||
"git.aiterp.net/rpdata/api/internal/session" |
|||
"git.aiterp.net/rpdata/api/model/file" |
|||
) |
|||
|
|||
// FileResolver for the File graphql type
|
|||
type FileResolver struct{ F file.File } |
|||
|
|||
// FileArgs is an arg
|
|||
type FileArgs struct { |
|||
ID string |
|||
} |
|||
|
|||
// File implements the file query
|
|||
func (r *QueryResolver) File(ctx context.Context, args *FileArgs) (*FileResolver, error) { |
|||
file, err := file.FindID(args.ID) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
return &FileResolver{F: file}, nil |
|||
} |
|||
|
|||
// FileEditInput is an input for the editFile mutation
|
|||
type FileEditInput struct { |
|||
ID string |
|||
Name *string |
|||
Public *bool |
|||
} |
|||
|
|||
// EditFile resolves the editFile mutation
|
|||
func (r *MutationResolver) EditFile(ctx context.Context, args *struct{ Input FileEditInput }) (*FileResolver, error) { |
|||
user := session.FromContext(ctx).User() |
|||
if user == nil || !user.Permitted("member") { |
|||
return nil, ErrUnauthorized |
|||
} |
|||
|
|||
file, err := file.FindID(args.Input.ID) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
if file.Author != user.ID && !user.Permitted("file.edit") { |
|||
return nil, ErrUnauthorized |
|||
} |
|||
|
|||
err = file.Edit(args.Input.Name, args.Input.Public) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
return &FileResolver{F: file}, nil |
|||
} |
|||
|
|||
// RemoveFile resolves the removeFIle mutation
|
|||
func (r *MutationResolver) RemoveFile(ctx context.Context, args *FileArgs) (*FileResolver, error) { |
|||
user := session.FromContext(ctx).User() |
|||
if user == nil || !user.Permitted("member") { |
|||
return nil, ErrUnauthorized |
|||
} |
|||
|
|||
file, err := file.FindID(args.ID) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
if file.Author != user.ID && !user.Permitted("file.remove") { |
|||
return nil, ErrUnauthorized |
|||
} |
|||
|
|||
err = file.Delete() |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
return &FileResolver{F: file}, nil |
|||
} |
|||
|
|||
// ID resolves File.id
|
|||
func (f *FileResolver) ID() string { |
|||
return f.F.ID |
|||
} |
|||
|
|||
// Author resolves File.author
|
|||
func (f *FileResolver) Author() string { |
|||
return f.F.Author |
|||
} |
|||
|
|||
// Kind resolves File.kind
|
|||
func (f *FileResolver) Kind() string { |
|||
return f.F.Kind |
|||
} |
|||
|
|||
// Time resolves File.time
|
|||
func (f *FileResolver) Time() string { |
|||
return f.F.Time.Format(time.RFC3339Nano) |
|||
} |
|||
|
|||
// Public resolves File.public
|
|||
func (f *FileResolver) Public() bool { |
|||
return f.F.Public |
|||
} |
|||
|
|||
// Name resolves File.name
|
|||
func (f *FileResolver) Name() string { |
|||
return f.F.Name |
|||
} |
|||
|
|||
// MimeType resolves File.mimeType
|
|||
func (f *FileResolver) MimeType() string { |
|||
return f.F.MimeType |
|||
} |
|||
|
|||
// Size resolves File.size
|
|||
func (f *FileResolver) Size() int32 { |
|||
return int32(f.F.Size) |
|||
} |
|||
|
|||
// URL resolves File.url
|
|||
func (f *FileResolver) URL() *string { |
|||
if f.F.URL == "" { |
|||
return nil |
|||
} |
|||
|
|||
return &f.F.URL |
|||
} |
@ -0,0 +1,43 @@ |
|||
# A File contains information about a file and where to download it. |
|||
type File { |
|||
# The file's unique ID |
|||
id: String! |
|||
|
|||
# The kind of file. Most will be "upload", but some that are ported from the wiki will have other values for this. |
|||
kind: String! |
|||
|
|||
# The time of uploading |
|||
time: String! |
|||
|
|||
# Whether the file is publicly listable. Someone with knowledge of the ID |
|||
# will still be able to view it, however. |
|||
public: Boolean! |
|||
|
|||
# The file's name |
|||
name: String! |
|||
|
|||
# The MIME type of the file |
|||
mimeType: String! |
|||
|
|||
# The file's size in bytes |
|||
size: Int! |
|||
|
|||
# The uploader |
|||
author: String! |
|||
|
|||
# The URL where the file is hosted |
|||
url: String |
|||
} |
|||
|
|||
# Input for editFile mutation |
|||
input EditFileInput { |
|||
# The file's unique ID |
|||
id: String! |
|||
|
|||
# Whether the file is publicly listable. Someone with knowledge of the ID |
|||
# will still be able to view it, however. |
|||
public: Boolean |
|||
|
|||
# The file's name |
|||
name: String |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue