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.95 import ( "context" "fmt" "strings" "adam-french.co.uk/backend/graph/model" "adam-french.co.uk/backend/models" "adam-french.co.uk/backend/services" ) // ID is the resolver for the id field. func (r *jobApplicationResolver) ID(ctx context.Context, obj *models.JobApplication) (int, error) { return int(obj.ID), nil } // CreateJobApplication is the resolver for the createJobApplication field. func (r *mutationResolver) CreateJobApplication(ctx context.Context, input model.CreateJobApplicationInput) (*models.JobApplication, error) { if !IsAdminFromCtx(ctx) { return nil, fmt.Errorf("admin access required") } // String! is non-null but not non-empty, and the columns are only // `not null`, so "" stores happily and then never matches the email // pipeline's LOWER(company) lookup — an orphan row no sync can advance. jobTitle := strings.TrimSpace(input.JobTitle) company := strings.TrimSpace(input.Company) if jobTitle == "" || company == "" { return nil, fmt.Errorf("jobTitle and company are required") } app := models.JobApplication{ JobTitle: jobTitle, Company: company, Location: input.Location, URL: input.URL, Status: services.NormalizeStatus(input.Status), Notes: input.Notes, AppliedAt: input.AppliedAt, } if err := r.Store.DB.Create(&app).Error; err != nil { return nil, err } return &app, nil } // UpdateJobApplication is the resolver for the updateJobApplication field. func (r *mutationResolver) UpdateJobApplication(ctx context.Context, id int, input model.UpdateJobApplicationInput) (*models.JobApplication, error) { if !IsAdminFromCtx(ctx) { return nil, fmt.Errorf("admin access required") } // Partial update: every input field is a pointer so that "not supplied" // is distinguishable from "set to empty". A nil field is left untouched. // An explicit "" on a nullable column does clear it — that is how the UI // empties a wrong URL or stale notes — but the same value on a required // column is rejected rather than blanking a live row. var app models.JobApplication if err := r.Store.DB.First(&app, id).Error; err != nil { return nil, err } if input.JobTitle != nil { if strings.TrimSpace(*input.JobTitle) == "" { return nil, fmt.Errorf("jobTitle cannot be empty") } app.JobTitle = strings.TrimSpace(*input.JobTitle) } if input.Company != nil { if strings.TrimSpace(*input.Company) == "" { return nil, fmt.Errorf("company cannot be empty") } app.Company = strings.TrimSpace(*input.Company) } if input.Location != nil { app.Location = input.Location } if input.URL != nil { app.URL = input.URL } if input.Status != nil { app.Status = services.NormalizeStatus(*input.Status) } if input.Notes != nil { app.Notes = input.Notes } if input.AppliedAt != nil { app.AppliedAt = input.AppliedAt } if err := r.Store.DB.Save(&app).Error; err != nil { return nil, err } return &app, nil } // DeleteJobApplication is the resolver for the deleteJobApplication field. func (r *mutationResolver) DeleteJobApplication(ctx context.Context, id int) (bool, error) { if !IsAdminFromCtx(ctx) { return false, fmt.Errorf("admin access required") } // Soft delete straight by id, without loading first — so deleting an id // that does not exist still returns true. if err := r.Store.DB.Delete(&models.JobApplication{}, id).Error; err != nil { return false, err } return true, nil } // JobApplications is the resolver for the jobApplications field. func (r *queryResolver) JobApplications(ctx context.Context) ([]*models.JobApplication, error) { // Admin-only even though it is a query: job applications are private. if !IsAdminFromCtx(ctx) { return nil, fmt.Errorf("admin access required") } // Find can populate a slice of pointers directly, avoiding the // copy-to-pointers loop the other list resolvers need. var apps []*models.JobApplication // id is the tie-break: one sync loop inserts several rows inside the same // timestamp, and Postgres gives no stable order for equal sort keys, so // without it the list can reshuffle between two identical page loads. if err := r.Store.DB.Order("created_at desc, id desc").Find(&apps).Error; err != nil { return nil, err } return apps, nil } // JobApplication is the resolver for the jobApplication field. func (r *queryResolver) JobApplication(ctx context.Context, id int) (*models.JobApplication, error) { if !IsAdminFromCtx(ctx) { return nil, fmt.Errorf("admin access required") } var app models.JobApplication if err := r.Store.DB.First(&app, id).Error; err != nil { return nil, err } return &app, nil } // JobApplication returns JobApplicationResolver implementation. func (r *Resolver) JobApplication() JobApplicationResolver { return &jobApplicationResolver{r} } type jobApplicationResolver struct{ *Resolver }