2023-08-19 23:47:36 +02:00
|
|
|
package graph
|
|
|
|
|
|
|
|
// 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.
|
2023-10-10 22:26:07 +02:00
|
|
|
// Code generated by github.com/99designs/gqlgen version v0.17.37
|
2023-08-19 23:47:36 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-10-10 22:26:07 +02:00
|
|
|
"errors"
|
2023-08-19 23:47:36 +02:00
|
|
|
|
2023-10-10 22:26:07 +02:00
|
|
|
"somepi.ddns.net/gitea/gilex-dev/YetAnotherToDoList/globals"
|
2023-08-19 23:47:36 +02:00
|
|
|
"somepi.ddns.net/gitea/gilex-dev/YetAnotherToDoList/graph/model"
|
2023-11-05 17:42:14 +01:00
|
|
|
"somepi.ddns.net/gitea/gilex-dev/YetAnotherToDoList/server/auth"
|
2023-08-19 23:47:36 +02:00
|
|
|
)
|
|
|
|
|
2023-10-10 22:26:07 +02:00
|
|
|
// CreateUser is the resolver for the createUser field.
|
|
|
|
func (r *mutationResolver) CreateUser(ctx context.Context, input model.NewUser) (*model.User, error) {
|
2023-11-05 17:42:14 +01:00
|
|
|
todo, err := globals.DB.CreateUser(input)
|
2023-10-10 22:26:07 +02:00
|
|
|
if err != nil {
|
|
|
|
globals.Logger.Println("Failed to add new user:", err)
|
|
|
|
return nil, errors.New("failed to add new user")
|
|
|
|
}
|
|
|
|
return todo, nil
|
|
|
|
}
|
|
|
|
|
2023-08-19 23:47:36 +02:00
|
|
|
// CreateTodo is the resolver for the createTodo field.
|
|
|
|
func (r *mutationResolver) CreateTodo(ctx context.Context, input model.NewTodo) (*model.Todo, error) {
|
2023-11-17 19:09:01 +01:00
|
|
|
todo, err := globals.DB.CreateTodo(input)
|
2023-10-10 22:26:07 +02:00
|
|
|
if err != nil {
|
|
|
|
globals.Logger.Println("Failed to add new todo:", err)
|
|
|
|
return nil, errors.New("failed to add new todo")
|
2023-09-01 21:33:26 +02:00
|
|
|
}
|
|
|
|
return todo, nil
|
2023-08-19 23:47:36 +02:00
|
|
|
}
|
|
|
|
|
2023-11-05 17:42:14 +01:00
|
|
|
// CreateRole is the resolver for the createRole field.
|
|
|
|
func (r *mutationResolver) CreateRole(ctx context.Context, input model.NewRole) (*model.Role, error) {
|
|
|
|
role, err := globals.DB.CreateRole(&input)
|
|
|
|
if err != nil {
|
|
|
|
globals.Logger.Println("Failed to add new role:", err)
|
|
|
|
return nil, errors.New("failed to add new role")
|
|
|
|
}
|
|
|
|
return role, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateRefreshToken is the resolver for the createRefreshToken field.
|
|
|
|
func (r *mutationResolver) CreateRefreshToken(ctx context.Context, input model.NewRefreshToken) (*model.RefreshToken, error) {
|
|
|
|
// TODO: unify model.RefreshToken & auth.RefreshToken
|
|
|
|
userToken := auth.ForContext(ctx)
|
|
|
|
refreshToken, tokenId, err := globals.DB.IssueRefreshToken(userToken.UserId, input.TokenName)
|
|
|
|
if err != nil {
|
|
|
|
globals.Logger.Println("Failed to create refresh token:", err)
|
|
|
|
return nil, errors.New("failed to create refresh token")
|
|
|
|
}
|
|
|
|
return &model.RefreshToken{ID: tokenId, ExpiryDate: refreshToken.ExpiryDate, TokenName: input.TokenName, Selector: &refreshToken.Selector, Token: &refreshToken.Token, UserID: userToken.UserId}, nil
|
|
|
|
}
|
|
|
|
|
2023-10-10 22:26:07 +02:00
|
|
|
// UpdateTodo is the resolver for the updateTodo field.
|
|
|
|
func (r *mutationResolver) UpdateTodo(ctx context.Context, id string, changes model.UpdateTodo) (*model.Todo, error) {
|
|
|
|
return globals.DB.UpdateTodo(id, &changes)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateUser is the resolver for the updateUser field.
|
|
|
|
func (r *mutationResolver) UpdateUser(ctx context.Context, id string, changes model.UpdateUser) (*model.User, error) {
|
|
|
|
return globals.DB.UpdateUser(id, &changes)
|
|
|
|
}
|
|
|
|
|
2023-11-05 17:42:14 +01:00
|
|
|
// UpdateRole is the resolver for the updateRole field.
|
|
|
|
func (r *mutationResolver) UpdateRole(ctx context.Context, id string, changes model.UpdateRole) (*model.Role, error) {
|
|
|
|
return globals.DB.UpdateRole(id, &changes)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateRefreshToken is the resolver for the updateRefreshToken field.
|
|
|
|
func (r *mutationResolver) UpdateRefreshToken(ctx context.Context, id string, changes model.UpdateRefreshToken) (*model.RefreshToken, error) {
|
|
|
|
return globals.DB.UpdateRefreshToken(id, &changes)
|
|
|
|
}
|
|
|
|
|
2023-10-10 22:26:07 +02:00
|
|
|
// DeleteUser is the resolver for the deleteUser field.
|
|
|
|
func (r *mutationResolver) DeleteUser(ctx context.Context, id string) (*string, error) {
|
|
|
|
return globals.DB.DeleteUser(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteTodo is the resolver for the deleteTodo field.
|
|
|
|
func (r *mutationResolver) DeleteTodo(ctx context.Context, id string) (*string, error) {
|
|
|
|
return globals.DB.DeleteTodo(id)
|
|
|
|
}
|
|
|
|
|
2023-11-05 17:42:14 +01:00
|
|
|
// DeleteRole is the resolver for the deleteRole field.
|
|
|
|
func (r *mutationResolver) DeleteRole(ctx context.Context, id string) (*string, error) {
|
|
|
|
return globals.DB.DeleteRole(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteRefreshToken is the resolver for the deleteRefreshToken field.
|
|
|
|
func (r *mutationResolver) DeleteRefreshToken(ctx context.Context, id string) (*string, error) {
|
|
|
|
return globals.DB.RevokeRefreshToken(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddRole is the resolver for the addRole field.
|
|
|
|
func (r *mutationResolver) AddRole(ctx context.Context, userID string, roleID string) ([]*model.Role, error) {
|
|
|
|
if _, err := globals.DB.AddRole(userID, roleID); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return globals.DB.GetRolesFrom(userID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveRole is the resolver for the removeRole field.
|
|
|
|
func (r *mutationResolver) RemoveRole(ctx context.Context, userID string, roleID string) ([]*model.Role, error) {
|
|
|
|
if _, err := globals.DB.RemoveRole(userID, roleID); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return globals.DB.GetRolesFrom(userID)
|
|
|
|
}
|
|
|
|
|
2023-08-19 23:47:36 +02:00
|
|
|
// Todos is the resolver for the todos field.
|
|
|
|
func (r *queryResolver) Todos(ctx context.Context) ([]*model.Todo, error) {
|
2023-10-10 22:26:07 +02:00
|
|
|
return globals.DB.GetAllTodos()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Users is the resolver for the users field.
|
|
|
|
func (r *queryResolver) Users(ctx context.Context) ([]*model.User, error) {
|
|
|
|
return globals.DB.GetAllUsers()
|
|
|
|
}
|
|
|
|
|
2023-11-05 17:42:14 +01:00
|
|
|
// Roles is the resolver for the roles field.
|
|
|
|
func (r *queryResolver) Roles(ctx context.Context) ([]*model.Role, error) {
|
|
|
|
return globals.DB.GetAllRoles()
|
|
|
|
}
|
|
|
|
|
|
|
|
// RefreshTokens is the resolver for the refreshTokens field.
|
|
|
|
func (r *queryResolver) RefreshTokens(ctx context.Context) ([]*model.RefreshToken, error) {
|
|
|
|
return globals.DB.GetAllRefreshTokens()
|
|
|
|
}
|
|
|
|
|
2023-10-10 22:26:07 +02:00
|
|
|
// User is the resolver for the user field.
|
|
|
|
func (r *queryResolver) User(ctx context.Context, id string) (*model.User, error) {
|
|
|
|
return globals.DB.GetUser(&model.User{ID: id})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Todo is the resolver for the todo field.
|
|
|
|
func (r *queryResolver) Todo(ctx context.Context, id string) (*model.Todo, error) {
|
2023-11-17 23:49:50 +01:00
|
|
|
return globals.DB.GetTodo(&model.Todo{ID: id, User: &model.User{ID: auth.ForContext(ctx).UserId}})
|
2023-09-01 21:33:26 +02:00
|
|
|
}
|
|
|
|
|
2023-11-05 17:42:14 +01:00
|
|
|
// Role is the resolver for the role field.
|
|
|
|
func (r *queryResolver) Role(ctx context.Context, id string) (*model.Role, error) {
|
|
|
|
return globals.DB.GetRole(&model.Role{ID: id})
|
|
|
|
}
|
|
|
|
|
|
|
|
// RefreshToken is the resolver for the refreshToken field.
|
|
|
|
func (r *queryResolver) RefreshToken(ctx context.Context, id string) (*model.RefreshToken, error) {
|
|
|
|
return globals.DB.GetRefreshToken(&model.RefreshToken{ID: id})
|
|
|
|
}
|
|
|
|
|
2023-09-01 21:33:26 +02:00
|
|
|
// User is the resolver for the user field.
|
|
|
|
func (r *todoResolver) User(ctx context.Context, obj *model.Todo) (*model.User, error) {
|
2023-10-10 22:26:07 +02:00
|
|
|
// TODO: implement dataloader
|
|
|
|
return globals.DB.GetUser(obj.User)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Todos is the resolver for the todos field.
|
|
|
|
func (r *userResolver) Todos(ctx context.Context, obj *model.User) ([]*model.Todo, error) {
|
|
|
|
return globals.DB.GetTodosFrom(obj)
|
2023-08-19 23:47:36 +02:00
|
|
|
}
|
|
|
|
|
2023-11-05 17:42:14 +01:00
|
|
|
// Roles is the resolver for the roles field.
|
|
|
|
func (r *userResolver) Roles(ctx context.Context, obj *model.User) ([]*model.Role, error) {
|
|
|
|
return globals.DB.GetRolesFrom(obj.ID)
|
|
|
|
}
|
|
|
|
|
2023-08-19 23:47:36 +02:00
|
|
|
// Mutation returns MutationResolver implementation.
|
|
|
|
func (r *Resolver) Mutation() MutationResolver { return &mutationResolver{r} }
|
|
|
|
|
|
|
|
// Query returns QueryResolver implementation.
|
|
|
|
func (r *Resolver) Query() QueryResolver { return &queryResolver{r} }
|
|
|
|
|
2023-09-01 21:33:26 +02:00
|
|
|
// Todo returns TodoResolver implementation.
|
|
|
|
func (r *Resolver) Todo() TodoResolver { return &todoResolver{r} }
|
|
|
|
|
2023-10-10 22:26:07 +02:00
|
|
|
// User returns UserResolver implementation.
|
|
|
|
func (r *Resolver) User() UserResolver { return &userResolver{r} }
|
|
|
|
|
2023-08-19 23:47:36 +02:00
|
|
|
type mutationResolver struct{ *Resolver }
|
|
|
|
type queryResolver struct{ *Resolver }
|
2023-09-01 21:33:26 +02:00
|
|
|
type todoResolver struct{ *Resolver }
|
2023-10-10 22:26:07 +02:00
|
|
|
type userResolver struct{ *Resolver }
|