Skip to content

Instantly share code, notes, and snippets.

@percybolmer
Created September 11, 2021 12:34
Show Gist options
  • Save percybolmer/7fac293ca55faa4fd24ed23daf5108c2 to your computer and use it in GitHub Desktop.
Save percybolmer/7fac293ca55faa4fd24ed23daf5108c2 to your computer and use it in GitHub Desktop.
graphql
// GetJob will return a job based on the ID
func (imr *InMemoryRepository) GetJob(employeeID, jobID string) (Job, error) {
if jobs, ok := imr.jobs[employeeID]; ok {
for _, job := range jobs {
// If Company Is Empty accept it, If Company matches filter accept it
if job.ID == jobID {
return job, nil
}
}
return Job{}, errors.New("no such job exists for that employee")
}
return Job{}, errors.New("no such employee exist")
}
// Update will update a job and return the new state of it
func (imr *InMemoryRepository) Update(j Job) (Job, error) {
imr.Lock()
defer imr.Unlock()
// Grab the employees jobs and locate the job and change the value
if jobs, ok := imr.jobs[j.EmployeeID]; ok {
// Find correct job
for i, job := range jobs {
if job.ID == j.ID {
// Replace the whole instance by index
imr.jobs[j.EmployeeID][i] = j
// Return Job, we can Image changes from input Job, like CreateJob which will generate an ID etc etc.
return j, nil
}
}
}
return Job{}, errors.New("no such employee exist")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment