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. // Code generated by github.com/99designs/gqlgen version v0.17.40 import ( "context" "errors" "somepi.ddns.net/gitea/gilex-dev/YetAnotherToDoList/globals" "somepi.ddns.net/gitea/gilex-dev/YetAnotherToDoList/graph/model" "somepi.ddns.net/gitea/gilex-dev/YetAnotherToDoList/server/auth" ) // CreateUser is the resolver for the createUser field. func (r *mutationResolver) CreateUser(ctx context.Context, input model.NewUser) (*model.User, error) { // Access managed by directive todo, err := globals.DB.CreateUser(input) if err != nil { globals.Logger.Println("Failed to add new user:", err) return nil, errors.New("failed to add new user") } return todo, nil } // CreateTodo is the resolver for the createTodo field. func (r *mutationResolver) CreateTodo(ctx context.Context, input model.NewTodo) (*model.Todo, error) { if auth.ForContext(ctx).UserId != input.UserID { return nil, errors.New("only the owner can create a todo") } todo, err := globals.DB.CreateTodo(input) if err != nil { globals.Logger.Println("Failed to add new todo:", err) return nil, errors.New("failed to add new todo") } return todo, nil } // CreateRole is the resolver for the createRole field. func (r *mutationResolver) CreateRole(ctx context.Context, input model.NewRole) (*model.Role, error) { if !auth.ForContext(ctx).IsAdmin { return nil, errors.New("only an admin can create a role") } 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 userId := auth.ForContext(ctx).UserId refreshToken, tokenId, err := globals.DB.IssueRefreshToken(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: userId}, nil } // UpdateTodo is the resolver for the updateTodo field. func (r *mutationResolver) UpdateTodo(ctx context.Context, id string, changes model.UpdateTodo) (*model.Todo, error) { owner, err := globals.DB.GetTodoOwner(&model.Todo{ID: id}) if err != nil { return nil, err } if owner.ID != auth.ForContext(ctx).UserId && !auth.ForContext(ctx).IsAdmin { return nil, errors.New("only the owner can update a todo") } 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) { if auth.ForContext(ctx).UserId != id { return nil, errors.New("can only update yourself") } return globals.DB.UpdateUser(id, &changes) } // UpdateRole is the resolver for the updateRole field. func (r *mutationResolver) UpdateRole(ctx context.Context, id string, changes model.UpdateRole) (*model.Role, error) { // Access managed by directive 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) { ownerId, err := globals.DB.GetRefreshTokenOwner(id) if err != nil { return nil, err } if ownerId != auth.ForContext(ctx).UserId && !auth.ForContext(ctx).IsAdmin { return nil, errors.New("only the owner can update a refresh token") } return globals.DB.UpdateRefreshToken(id, &changes) } // DeleteUser is the resolver for the deleteUser field. func (r *mutationResolver) DeleteUser(ctx context.Context, id string) (*string, error) { // Access managed by directive return globals.DB.DeleteUser(id) } // DeleteTodo is the resolver for the deleteTodo field. func (r *mutationResolver) DeleteTodo(ctx context.Context, id string) (*string, error) { owner, err := globals.DB.GetTodoOwner(&model.Todo{ID: id}) if err != nil { return nil, err } if owner.ID != auth.ForContext(ctx).UserId && !auth.ForContext(ctx).IsAdmin { return nil, errors.New("only the owner can delete a todo") } return globals.DB.DeleteTodo(id) } // DeleteRole is the resolver for the deleteRole field. func (r *mutationResolver) DeleteRole(ctx context.Context, id string) (*string, error) { // Access managed by directive return globals.DB.DeleteRole(id) } // DeleteRefreshToken is the resolver for the deleteRefreshToken field. func (r *mutationResolver) DeleteRefreshToken(ctx context.Context, id string) (*string, error) { ownerId, err := globals.DB.GetRefreshTokenOwner(id) if err != nil { return nil, err } if ownerId != auth.ForContext(ctx).UserId && !auth.ForContext(ctx).IsAdmin { return nil, errors.New("only the owner can delete a refresh token") } return globals.DB.RevokeRefreshToken(id) } // AddUserRole is the resolver for the addUserRole field. func (r *mutationResolver) AddUserRole(ctx context.Context, userID string, roleID string, userIsRoleManager bool) ([]*model.RelationUserRole, error) { // Access managed by directive if _, err := globals.DB.AddUserRole(userID, roleID, userIsRoleManager); err != nil { return nil, err } return globals.DB.GetRolesFrom(userID) } // UpdateUserRole is the resolver for the UpdateUserRole field. func (r *mutationResolver) UpdateUserRole(ctx context.Context, userID string, roleID string, userIsRoleManager bool) ([]*model.RelationUserRole, error) { // Access managed by directive if _, err := globals.DB.UpdateUserRole(userID, roleID, userIsRoleManager); err != nil { return nil, err } return globals.DB.GetRolesFrom(userID) } // RemoveUserRole is the resolver for the RemoveUserRole field. func (r *mutationResolver) RemoveUserRole(ctx context.Context, userID string, roleID string) ([]*model.RelationUserRole, error) { // Access managed by directive if _, err := globals.DB.RemoveUserRole(userID, roleID); err != nil { return nil, err } return globals.DB.GetRolesFrom(userID) } // Todos is the resolver for the todos field. func (r *queryResolver) Todos(ctx context.Context) ([]*model.Todo, error) { // Access managed by directive 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() } // 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) { // Access managed by directive return globals.DB.GetAllRefreshTokens() } // 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) { owner, err := globals.DB.GetTodoOwner(&model.Todo{ID: id}) if err != nil { return nil, err } if owner.ID != auth.ForContext(ctx).UserId && !auth.ForContext(ctx).IsAdmin { return nil, errors.New("only the owner can view a todo") } return globals.DB.GetTodo(&model.Todo{ID: id}) } // 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) { ownerId, err := globals.DB.GetRefreshTokenOwner(id) if err != nil { return nil, err } if ownerId != auth.ForContext(ctx).UserId && !auth.ForContext(ctx).IsAdmin { return nil, errors.New("only the owner can view a refresh token") } return globals.DB.GetRefreshToken(&model.RefreshToken{ID: id, UserID: ownerId}) } // RoleMembers is the resolver for the roleMembers field. func (r *roleResolver) RoleMembers(ctx context.Context, obj *model.Role) ([]*model.RelationRoleUser, error) { return globals.DB.GetRoleMembers(obj.ID) } // User is the resolver for the user field. func (r *todoResolver) User(ctx context.Context, obj *model.Todo) (*model.User, error) { // 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) { if auth.ForContext(ctx).UserId != obj.ID && !auth.ForContext(ctx).IsAdmin { return nil, errors.New("only the owner can see this") } return globals.DB.GetTodosFrom(obj) } // Roles is the resolver for the roles field. func (r *userResolver) Roles(ctx context.Context, obj *model.User) ([]*model.RelationUserRole, error) { return globals.DB.GetRolesFrom(obj.ID) } // Mutation returns MutationResolver implementation. func (r *Resolver) Mutation() MutationResolver { return &mutationResolver{r} } // Query returns QueryResolver implementation. func (r *Resolver) Query() QueryResolver { return &queryResolver{r} } // Role returns RoleResolver implementation. func (r *Resolver) Role() RoleResolver { return &roleResolver{r} } // Todo returns TodoResolver implementation. func (r *Resolver) Todo() TodoResolver { return &todoResolver{r} } // User returns UserResolver implementation. func (r *Resolver) User() UserResolver { return &userResolver{r} } type mutationResolver struct{ *Resolver } type queryResolver struct{ *Resolver } type roleResolver struct{ *Resolver } type todoResolver struct{ *Resolver } type userResolver struct{ *Resolver }