tercul-backend/graph/schema.resolvers.go
Damir Mukimov fa336cacf3
wip
2025-09-01 00:43:59 +02:00

665 lines
23 KiB
Go

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.72
import (
"context"
"fmt"
"strconv"
"tercul/graph/model"
models2 "tercul/internal/models"
"tercul/services"
)
// Register is the resolver for the register field.
func (r *mutationResolver) Register(ctx context.Context, input model.RegisterInput) (*model.AuthPayload, error) {
// Convert GraphQL input to service input
registerInput := services.RegisterInput{
Username: input.Username,
Email: input.Email,
Password: input.Password,
FirstName: input.FirstName,
LastName: input.LastName,
}
// Call auth service
authResponse, err := r.AuthService.Register(ctx, registerInput)
if err != nil {
return nil, err
}
// Convert service response to GraphQL response
return &model.AuthPayload{
Token: authResponse.Token,
User: &model.User{
ID: fmt.Sprintf("%d", authResponse.User.ID),
Username: authResponse.User.Username,
Email: authResponse.User.Email,
FirstName: &authResponse.User.FirstName,
LastName: &authResponse.User.LastName,
DisplayName: &authResponse.User.DisplayName,
Role: model.UserRole(authResponse.User.Role),
Verified: authResponse.User.Verified,
Active: authResponse.User.Active,
},
}, nil
}
// Login is the resolver for the login field.
func (r *mutationResolver) Login(ctx context.Context, email string, password string) (*model.AuthPayload, error) {
// Convert GraphQL input to service input
loginInput := services.LoginInput{
Email: email,
Password: password,
}
// Call auth service
authResponse, err := r.AuthService.Login(ctx, loginInput)
if err != nil {
return nil, err
}
// Convert service response to GraphQL response
return &model.AuthPayload{
Token: authResponse.Token,
User: &model.User{
ID: fmt.Sprintf("%d", authResponse.User.ID),
Username: authResponse.User.Username,
Email: authResponse.User.Email,
FirstName: &authResponse.User.FirstName,
LastName: &authResponse.User.LastName,
DisplayName: &authResponse.User.DisplayName,
Role: model.UserRole(authResponse.User.Role),
Verified: authResponse.User.Verified,
Active: authResponse.User.Active,
},
}, nil
}
// CreateWork is the resolver for the createWork field.
func (r *mutationResolver) CreateWork(ctx context.Context, input model.WorkInput) (*model.Work, error) {
// Create work model
work := &models2.Work{
Title: input.Name,
}
work.Language = input.Language // Set language on the embedded TranslatableModel
// Create work using the work service
err := r.WorkService.CreateWork(ctx, work)
if err != nil {
return nil, err
}
// If content is provided and TranslationRepo is available, create a translation for it
if input.Content != nil && *input.Content != "" && r.TranslationRepo != nil {
translation := &models2.Translation{
Title: input.Name,
Content: *input.Content,
Language: input.Language,
TranslatableID: work.ID,
TranslatableType: "Work",
IsOriginalLanguage: true,
}
err = r.TranslationRepo.Create(ctx, translation)
if err != nil {
return nil, fmt.Errorf("failed to create translation: %v", err)
}
}
// Return work with resolved content using the localization service
var content *string
if r.Localization != nil {
if resolvedContent, err := r.Localization.GetWorkContent(ctx, work.ID, input.Language); err == nil && resolvedContent != "" {
content = &resolvedContent
}
}
return &model.Work{
ID: fmt.Sprintf("%d", work.ID),
Name: work.Title,
Language: input.Language,
Content: content,
}, nil
}
// UpdateWork is the resolver for the updateWork field.
func (r *mutationResolver) UpdateWork(ctx context.Context, id string, input model.WorkInput) (*model.Work, error) {
panic(fmt.Errorf("not implemented: UpdateWork - updateWork"))
}
// DeleteWork is the resolver for the deleteWork field.
func (r *mutationResolver) DeleteWork(ctx context.Context, id string) (bool, error) {
panic(fmt.Errorf("not implemented: DeleteWork - deleteWork"))
}
// CreateTranslation is the resolver for the createTranslation field.
func (r *mutationResolver) CreateTranslation(ctx context.Context, input model.TranslationInput) (*model.Translation, error) {
panic(fmt.Errorf("not implemented: CreateTranslation - createTranslation"))
}
// UpdateTranslation is the resolver for the updateTranslation field.
func (r *mutationResolver) UpdateTranslation(ctx context.Context, id string, input model.TranslationInput) (*model.Translation, error) {
panic(fmt.Errorf("not implemented: UpdateTranslation - updateTranslation"))
}
// DeleteTranslation is the resolver for the deleteTranslation field.
func (r *mutationResolver) DeleteTranslation(ctx context.Context, id string) (bool, error) {
panic(fmt.Errorf("not implemented: DeleteTranslation - deleteTranslation"))
}
// CreateAuthor is the resolver for the createAuthor field.
func (r *mutationResolver) CreateAuthor(ctx context.Context, input model.AuthorInput) (*model.Author, error) {
panic(fmt.Errorf("not implemented: CreateAuthor - createAuthor"))
}
// UpdateAuthor is the resolver for the updateAuthor field.
func (r *mutationResolver) UpdateAuthor(ctx context.Context, id string, input model.AuthorInput) (*model.Author, error) {
panic(fmt.Errorf("not implemented: UpdateAuthor - updateAuthor"))
}
// DeleteAuthor is the resolver for the deleteAuthor field.
func (r *mutationResolver) DeleteAuthor(ctx context.Context, id string) (bool, error) {
panic(fmt.Errorf("not implemented: DeleteAuthor - deleteAuthor"))
}
// UpdateUser is the resolver for the updateUser field.
func (r *mutationResolver) UpdateUser(ctx context.Context, id string, input model.UserInput) (*model.User, error) {
panic(fmt.Errorf("not implemented: UpdateUser - updateUser"))
}
// DeleteUser is the resolver for the deleteUser field.
func (r *mutationResolver) DeleteUser(ctx context.Context, id string) (bool, error) {
panic(fmt.Errorf("not implemented: DeleteUser - deleteUser"))
}
// CreateCollection is the resolver for the createCollection field.
func (r *mutationResolver) CreateCollection(ctx context.Context, input model.CollectionInput) (*model.Collection, error) {
panic(fmt.Errorf("not implemented: CreateCollection - createCollection"))
}
// UpdateCollection is the resolver for the updateCollection field.
func (r *mutationResolver) UpdateCollection(ctx context.Context, id string, input model.CollectionInput) (*model.Collection, error) {
panic(fmt.Errorf("not implemented: UpdateCollection - updateCollection"))
}
// DeleteCollection is the resolver for the deleteCollection field.
func (r *mutationResolver) DeleteCollection(ctx context.Context, id string) (bool, error) {
panic(fmt.Errorf("not implemented: DeleteCollection - deleteCollection"))
}
// AddWorkToCollection is the resolver for the addWorkToCollection field.
func (r *mutationResolver) AddWorkToCollection(ctx context.Context, collectionID string, workID string) (*model.Collection, error) {
panic(fmt.Errorf("not implemented: AddWorkToCollection - addWorkToCollection"))
}
// RemoveWorkFromCollection is the resolver for the removeWorkFromCollection field.
func (r *mutationResolver) RemoveWorkFromCollection(ctx context.Context, collectionID string, workID string) (*model.Collection, error) {
panic(fmt.Errorf("not implemented: RemoveWorkFromCollection - removeWorkFromCollection"))
}
// CreateComment is the resolver for the createComment field.
func (r *mutationResolver) CreateComment(ctx context.Context, input model.CommentInput) (*model.Comment, error) {
panic(fmt.Errorf("not implemented: CreateComment - createComment"))
}
// UpdateComment is the resolver for the updateComment field.
func (r *mutationResolver) UpdateComment(ctx context.Context, id string, input model.CommentInput) (*model.Comment, error) {
panic(fmt.Errorf("not implemented: UpdateComment - updateComment"))
}
// DeleteComment is the resolver for the deleteComment field.
func (r *mutationResolver) DeleteComment(ctx context.Context, id string) (bool, error) {
panic(fmt.Errorf("not implemented: DeleteComment - deleteComment"))
}
// CreateLike is the resolver for the createLike field.
func (r *mutationResolver) CreateLike(ctx context.Context, input model.LikeInput) (*model.Like, error) {
panic(fmt.Errorf("not implemented: CreateLike - createLike"))
}
// DeleteLike is the resolver for the deleteLike field.
func (r *mutationResolver) DeleteLike(ctx context.Context, id string) (bool, error) {
panic(fmt.Errorf("not implemented: DeleteLike - deleteLike"))
}
// CreateBookmark is the resolver for the createBookmark field.
func (r *mutationResolver) CreateBookmark(ctx context.Context, input model.BookmarkInput) (*model.Bookmark, error) {
panic(fmt.Errorf("not implemented: CreateBookmark - createBookmark"))
}
// DeleteBookmark is the resolver for the deleteBookmark field.
func (r *mutationResolver) DeleteBookmark(ctx context.Context, id string) (bool, error) {
panic(fmt.Errorf("not implemented: DeleteBookmark - deleteBookmark"))
}
// CreateContribution is the resolver for the createContribution field.
func (r *mutationResolver) CreateContribution(ctx context.Context, input model.ContributionInput) (*model.Contribution, error) {
panic(fmt.Errorf("not implemented: CreateContribution - createContribution"))
}
// UpdateContribution is the resolver for the updateContribution field.
func (r *mutationResolver) UpdateContribution(ctx context.Context, id string, input model.ContributionInput) (*model.Contribution, error) {
panic(fmt.Errorf("not implemented: UpdateContribution - updateContribution"))
}
// DeleteContribution is the resolver for the deleteContribution field.
func (r *mutationResolver) DeleteContribution(ctx context.Context, id string) (bool, error) {
panic(fmt.Errorf("not implemented: DeleteContribution - deleteContribution"))
}
// ReviewContribution is the resolver for the reviewContribution field.
func (r *mutationResolver) ReviewContribution(ctx context.Context, id string, status model.ContributionStatus, feedback *string) (*model.Contribution, error) {
panic(fmt.Errorf("not implemented: ReviewContribution - reviewContribution"))
}
// Logout is the resolver for the logout field.
func (r *mutationResolver) Logout(ctx context.Context) (bool, error) {
panic(fmt.Errorf("not implemented: Logout - logout"))
}
// RefreshToken is the resolver for the refreshToken field.
func (r *mutationResolver) RefreshToken(ctx context.Context) (*model.AuthPayload, error) {
panic(fmt.Errorf("not implemented: RefreshToken - refreshToken"))
}
// ForgotPassword is the resolver for the forgotPassword field.
func (r *mutationResolver) ForgotPassword(ctx context.Context, email string) (bool, error) {
panic(fmt.Errorf("not implemented: ForgotPassword - forgotPassword"))
}
// ResetPassword is the resolver for the resetPassword field.
func (r *mutationResolver) ResetPassword(ctx context.Context, token string, newPassword string) (bool, error) {
panic(fmt.Errorf("not implemented: ResetPassword - resetPassword"))
}
// VerifyEmail is the resolver for the verifyEmail field.
func (r *mutationResolver) VerifyEmail(ctx context.Context, token string) (bool, error) {
panic(fmt.Errorf("not implemented: VerifyEmail - verifyEmail"))
}
// ResendVerificationEmail is the resolver for the resendVerificationEmail field.
func (r *mutationResolver) ResendVerificationEmail(ctx context.Context, email string) (bool, error) {
panic(fmt.Errorf("not implemented: ResendVerificationEmail - resendVerificationEmail"))
}
// UpdateProfile is the resolver for the updateProfile field.
func (r *mutationResolver) UpdateProfile(ctx context.Context, input model.UserInput) (*model.User, error) {
panic(fmt.Errorf("not implemented: UpdateProfile - updateProfile"))
}
// ChangePassword is the resolver for the changePassword field.
func (r *mutationResolver) ChangePassword(ctx context.Context, currentPassword string, newPassword string) (bool, error) {
panic(fmt.Errorf("not implemented: ChangePassword - changePassword"))
}
// Work is the resolver for the work field.
func (r *queryResolver) Work(ctx context.Context, id string) (*model.Work, error) {
// Parse ID to uint
workID, err := strconv.ParseUint(id, 10, 32)
if err != nil {
return nil, fmt.Errorf("invalid work ID: %v", err)
}
// Get work by ID using repository
work, err := r.WorkRepo.GetByID(ctx, uint(workID))
if err != nil {
return nil, err
}
if work == nil {
return nil, nil
}
// Content resolved via Localization service when requested later
return &model.Work{
ID: id,
Name: work.Title,
Language: work.Language,
Content: r.resolveWorkContent(ctx, work.ID, work.Language),
}, nil
}
// Works is the resolver for the works field.
func (r *queryResolver) Works(ctx context.Context, limit *int32, offset *int32, language *string, authorID *string, categoryID *string, tagID *string, search *string) ([]*model.Work, error) {
var works []models2.Work
var err error
// Set default pagination
page := 1
pageSize := 20
if limit != nil {
pageSize = int(*limit)
}
if offset != nil {
page = int(*offset)/pageSize + 1
}
// Handle different query types
if language != nil {
// Query by language
result, err := r.WorkRepo.FindByLanguage(ctx, *language, page, pageSize)
if err != nil {
return nil, err
}
works = result.Items
} else if authorID != nil {
// Query by author
authorIDUint, err := strconv.ParseUint(*authorID, 10, 32)
if err != nil {
return nil, err
}
works, err = r.WorkRepo.FindByAuthor(ctx, uint(authorIDUint))
if err != nil {
return nil, err
}
} else if categoryID != nil {
// Query by category
categoryIDUint, err := strconv.ParseUint(*categoryID, 10, 32)
if err != nil {
return nil, err
}
works, err = r.WorkRepo.FindByCategory(ctx, uint(categoryIDUint))
if err != nil {
return nil, err
}
} else if search != nil {
// Search by title
works, err = r.WorkRepo.FindByTitle(ctx, *search)
if err != nil {
return nil, err
}
} else {
// Get all works with pagination
result, err := r.WorkRepo.List(ctx, page, pageSize)
if err != nil {
return nil, err
}
works = result.Items
}
// Convert to GraphQL model
var result []*model.Work
for _, w := range works {
// Resolve content lazily
result = append(result, &model.Work{
ID: fmt.Sprintf("%d", w.ID),
Name: w.Title,
Language: w.Language,
Content: r.resolveWorkContent(ctx, w.ID, w.Language),
})
}
return result, nil
}
// Translation is the resolver for the translation field.
func (r *queryResolver) Translation(ctx context.Context, id string) (*model.Translation, error) {
panic(fmt.Errorf("not implemented: Translation - translation"))
}
// Translations is the resolver for the translations field.
func (r *queryResolver) Translations(ctx context.Context, workID string, language *string, limit *int32, offset *int32) ([]*model.Translation, error) {
panic(fmt.Errorf("not implemented: Translations - translations"))
}
// Author is the resolver for the author field.
func (r *queryResolver) Author(ctx context.Context, id string) (*model.Author, error) {
panic(fmt.Errorf("not implemented: Author - author"))
}
// Authors is the resolver for the authors field.
func (r *queryResolver) Authors(ctx context.Context, limit *int32, offset *int32, search *string, countryID *string) ([]*model.Author, error) {
var authors []models2.Author
var err error
if countryID != nil {
countryIDUint, err := strconv.ParseUint(*countryID, 10, 32)
if err != nil {
return nil, err
}
authors, err = r.AuthorRepo.ListByCountryID(ctx, uint(countryIDUint))
} else {
result, err := r.AuthorRepo.List(ctx, 1, 1000) // Use pagination
if err != nil {
return nil, err
}
authors = result.Items
}
if err != nil {
return nil, err
}
// Convert to GraphQL model; resolve biography via Localization service
var result []*model.Author
for _, a := range authors {
var bio *string
if r.Localization != nil {
if b, err := r.Localization.GetAuthorBiography(ctx, a.ID, a.Language); err == nil && b != "" {
bio = &b
}
}
result = append(result, &model.Author{
ID: fmt.Sprintf("%d", a.ID),
Name: a.Name,
Language: a.Language,
Biography: bio,
})
}
return result, nil
}
// User is the resolver for the user field.
func (r *queryResolver) User(ctx context.Context, id string) (*model.User, error) {
panic(fmt.Errorf("not implemented: User - user"))
}
// UserByEmail is the resolver for the userByEmail field.
func (r *queryResolver) UserByEmail(ctx context.Context, email string) (*model.User, error) {
panic(fmt.Errorf("not implemented: UserByEmail - userByEmail"))
}
// UserByUsername is the resolver for the userByUsername field.
func (r *queryResolver) UserByUsername(ctx context.Context, username string) (*model.User, error) {
panic(fmt.Errorf("not implemented: UserByUsername - userByUsername"))
}
// Users is the resolver for the users field.
func (r *queryResolver) Users(ctx context.Context, limit *int32, offset *int32, role *model.UserRole) ([]*model.User, error) {
var users []models2.User
var err error
if role != nil {
// Convert GraphQL role to model role
var modelRole models2.UserRole
switch *role {
case model.UserRoleReader:
modelRole = models2.UserRoleReader
case model.UserRoleContributor:
modelRole = models2.UserRoleContributor
case model.UserRoleReviewer:
modelRole = models2.UserRoleReviewer
case model.UserRoleEditor:
modelRole = models2.UserRoleEditor
case model.UserRoleAdmin:
modelRole = models2.UserRoleAdmin
default:
return nil, fmt.Errorf("invalid user role: %s", *role)
}
users, err = r.UserRepo.ListByRole(ctx, modelRole)
} else {
result, err := r.UserRepo.List(ctx, 1, 1000) // Use pagination
if err != nil {
return nil, err
}
users = result.Items
}
if err != nil {
return nil, err
}
// Convert to GraphQL model
var result []*model.User
for _, u := range users {
// Convert model role to GraphQL role
var graphqlRole model.UserRole
switch u.Role {
case models2.UserRoleReader:
graphqlRole = model.UserRoleReader
case models2.UserRoleContributor:
graphqlRole = model.UserRoleContributor
case models2.UserRoleReviewer:
graphqlRole = model.UserRoleReviewer
case models2.UserRoleEditor:
graphqlRole = model.UserRoleEditor
case models2.UserRoleAdmin:
graphqlRole = model.UserRoleAdmin
default:
graphqlRole = model.UserRoleReader
}
result = append(result, &model.User{
ID: fmt.Sprintf("%d", u.ID),
Username: u.Username,
Email: u.Email,
Role: graphqlRole,
})
}
return result, nil
}
// Me is the resolver for the me field.
func (r *queryResolver) Me(ctx context.Context) (*model.User, error) {
panic(fmt.Errorf("not implemented: Me - me"))
}
// UserProfile is the resolver for the userProfile field.
func (r *queryResolver) UserProfile(ctx context.Context, userID string) (*model.UserProfile, error) {
panic(fmt.Errorf("not implemented: UserProfile - userProfile"))
}
// Collection is the resolver for the collection field.
func (r *queryResolver) Collection(ctx context.Context, id string) (*model.Collection, error) {
panic(fmt.Errorf("not implemented: Collection - collection"))
}
// Collections is the resolver for the collections field.
func (r *queryResolver) Collections(ctx context.Context, userID *string, limit *int32, offset *int32) ([]*model.Collection, error) {
panic(fmt.Errorf("not implemented: Collections - collections"))
}
// Tag is the resolver for the tag field.
func (r *queryResolver) Tag(ctx context.Context, id string) (*model.Tag, error) {
tagID, err := strconv.ParseUint(id, 10, 32)
if err != nil {
return nil, err
}
tag, err := r.TagRepo.GetByID(ctx, uint(tagID))
if err != nil {
return nil, err
}
return &model.Tag{
ID: fmt.Sprintf("%d", tag.ID),
Name: tag.Name,
}, nil
}
// Tags is the resolver for the tags field.
func (r *queryResolver) Tags(ctx context.Context, limit *int32, offset *int32) ([]*model.Tag, error) {
paginatedResult, err := r.TagRepo.List(ctx, 1, 1000) // Use pagination
if err != nil {
return nil, err
}
// Convert to GraphQL model
var result []*model.Tag
for _, t := range paginatedResult.Items {
result = append(result, &model.Tag{
ID: fmt.Sprintf("%d", t.ID),
Name: t.Name,
})
}
return result, nil
}
// Category is the resolver for the category field.
func (r *queryResolver) Category(ctx context.Context, id string) (*model.Category, error) {
categoryID, err := strconv.ParseUint(id, 10, 32)
if err != nil {
return nil, err
}
category, err := r.CategoryRepo.GetByID(ctx, uint(categoryID))
if err != nil {
return nil, err
}
return &model.Category{
ID: fmt.Sprintf("%d", category.ID),
Name: category.Name,
}, nil
}
// Categories is the resolver for the categories field.
func (r *queryResolver) Categories(ctx context.Context, limit *int32, offset *int32) ([]*model.Category, error) {
paginatedResult, err := r.CategoryRepo.List(ctx, 1, 1000)
if err != nil {
return nil, err
}
// Convert to GraphQL model
var result []*model.Category
for _, c := range paginatedResult.Items {
result = append(result, &model.Category{
ID: fmt.Sprintf("%d", c.ID),
Name: c.Name,
})
}
return result, nil
}
// Comment is the resolver for the comment field.
func (r *queryResolver) Comment(ctx context.Context, id string) (*model.Comment, error) {
panic(fmt.Errorf("not implemented: Comment - comment"))
}
// Comments is the resolver for the comments field.
func (r *queryResolver) Comments(ctx context.Context, workID *string, translationID *string, userID *string, limit *int32, offset *int32) ([]*model.Comment, error) {
panic(fmt.Errorf("not implemented: Comments - comments"))
}
// Search is the resolver for the search field.
func (r *queryResolver) Search(ctx context.Context, query string, limit *int32, offset *int32, filters *model.SearchFilters) (*model.SearchResults, error) {
panic(fmt.Errorf("not implemented: Search - search"))
}
// Mutation returns MutationResolver implementation.
func (r *Resolver) Mutation() MutationResolver { return &mutationResolver{r} }
// Query returns QueryResolver implementation.
func (r *Resolver) Query() QueryResolver { return &queryResolver{r} }
type mutationResolver struct{ *Resolver }
type queryResolver struct{ *Resolver }
// resolveWorkContent uses Localization service to fetch preferred content
func (r *queryResolver) resolveWorkContent(ctx context.Context, workID uint, preferredLanguage string) *string {
if r.Localization == nil {
return nil
}
content, err := r.Localization.GetWorkContent(ctx, workID, preferredLanguage)
if err != nil || content == "" {
return nil
}
return &content
}