Skip to content

Instantly share code, notes, and snippets.

@percybolmer
Last active September 10, 2021 09:44
Show Gist options
  • Save percybolmer/715490e74b8a98d512e30633c26de59d to your computer and use it in GitHub Desktop.
Save percybolmer/715490e74b8a98d512e30633c26de59d to your computer and use it in GitHub Desktop.
graphql
// memory is a in memory data storage solution for Job
package job
import (
"errors"
"sync"
)
// InMemoryRepository is a storage for jobs that uses a map to store them
type InMemoryRepository struct {
// jobs is used to store jobs
jobs map[string][]Job
sync.Mutex
}
// NewMemoryRepository initializes a memory with mock data
func NewMemoryRepository() *InMemoryRepository {
jobs := make(map[string][]Job)
jobs["1"] = []Job{
{
ID: "123-123",
EmployeeID: "1",
Company: "Google",
Title: "Logo",
Start: "2021-01-01",
End: "",
},
}
jobs["2"] = []Job{
{
ID: "124-124",
EmployeeID: "2",
Company: "Google",
Title: "Janitor",
Start: "2021-05-03",
End: "",
}, {
ID: "125-125",
EmployeeID: "2",
Company: "Microsoft",
Title: "Janitor",
Start: "1980-03-04",
End: "2021-05-02",
},
}
return &InMemoryRepository{
jobs: jobs,
}
}
// GetJobs returns all jobs for a certain Employee
func (imr *InMemoryRepository) GetJobs(employeeID string) ([]Job, error) {
if jobs, ok := imr.jobs[employeeID]; ok {
return jobs, nil
}
return nil, errors.New("no such employee exist")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment