IDs should always reflect position in file

This commit is contained in:
Brian Buller 2023-09-07 13:06:01 -05:00
parent 9ef6c61051
commit 13f20412ba

View File

@ -41,23 +41,27 @@ func (projectlist *ProjectList) Filter(predicate func(*Project) bool) *ProjectLi
return &newList return &newList
} }
func (projectlist *ProjectList) GetNextId() int {
nextId := 0
for _, p := range projectlist.Projects {
if p.Id > nextId {
nextId = p.Id
}
}
return nextId + 1
}
// AddProject prepends a Project to the current ProjectList and takes care to set the ProjectId correctly // AddProject prepends a Project to the current ProjectList and takes care to set the ProjectId correctly
func (projectlist *ProjectList) AddProject(project *Project) { func (projectlist *ProjectList) AddProject(project *Project) {
// The new project will be id 1 project.Id = projectlist.GetNextId()
project.Id = 1 projectlist.Projects = append(projectlist.Projects, project)
// All other projects get their id incremented
for _, p := range projectlist.Projects {
p.Id++
}
// Now prepend the project to the slice
projectlist.Projects = append(projectlist.Projects, &Project{})
copy(projectlist.Projects[1:], projectlist.Projects[0:])
projectlist.Projects[0] = project
} }
// AddProjects adds all passed projects to the list // AddProjects adds all passed projects to the list
func (projectlist *ProjectList) AddProjects(projects []*Project) { func (projectlist *ProjectList) AddProjects(projects []*Project) {
projectlist.Projects = append(projectlist.Projects, projects...) for _, v := range projects {
projectlist.AddProject(v)
}
} }
func (projectlist *ProjectList) Combine(other *ProjectList) { projectlist.AddProjects(other.Projects) } func (projectlist *ProjectList) Combine(other *ProjectList) { projectlist.AddProjects(other.Projects) }
@ -75,38 +79,38 @@ func (projectlist *ProjectList) GetProject(id int) (*Project, error) {
// RemoveProjectById removes any Project with given Project 'id' from the ProjectList. // RemoveProjectById removes any Project with given Project 'id' from the ProjectList.
// Returns an error if no Project was removed. // Returns an error if no Project was removed.
func (projectlist *ProjectList) RemoveProjectById(id int) error { func (projectlist *ProjectList) RemoveProjectById(id int) error {
var newList ProjectList
found := false found := false
for _, p := range projectlist.Projects { var remIdx int
if p.Id != id { var p *Project
newList.AddProject(p) for remIdx, p = range projectlist.Projects {
} else { if p.Id == id {
found = true found = true
break
} }
} }
if !found { if !found {
return errors.New("project not found") return errors.New("project not found")
} }
*projectlist = newList projectlist.Projects = append(projectlist.Projects[:remIdx], projectlist.Projects[remIdx+1:]...)
return nil return nil
} }
// RemoveProject removes any Project from the ProjectList with the same String representation as the given Project. // RemoveProject removes any Project from the ProjectList with the same String representation as the given Project.
// Returns an error if no Project was removed. // Returns an error if no Project was removed.
func (projectlist *ProjectList) RemoveProject(project Project) error { func (projectlist *ProjectList) RemoveProject(project Project) error {
var newList ProjectList
found := false found := false
for _, p := range projectlist.Projects { var remIdx int
if p.String() != project.String() { var p *Project
newList.AddProject(p) for remIdx, p = range projectlist.Projects {
} else { if p.String() == project.String() {
found = true found = true
break
} }
} }
if !found { if !found {
return errors.New("project not found") return errors.New("project not found")
} }
*projectlist = newList projectlist.Projects = append(projectlist.Projects[:remIdx], projectlist.Projects[remIdx+1:]...)
return nil return nil
} }
@ -126,6 +130,14 @@ func (projectlist *ProjectList) ArchiveProjectToFile(project Project, filename s
return err return err
} }
func (projectlist *ProjectList) ResetIds() {
projectId := 1
for _, v := range projectlist.Projects {
v.Id = projectId
projectId++
}
}
// LoadFromFile loads a ProjectList from *os.File. // LoadFromFile loads a ProjectList from *os.File.
// Note: This will clear the current TimerList and overwrite it's contents with whatever is in *os.File. // Note: This will clear the current TimerList and overwrite it's contents with whatever is in *os.File.
func (projectlist *ProjectList) LoadFromFile(file *os.File) error { func (projectlist *ProjectList) LoadFromFile(file *os.File) error {
@ -143,8 +155,8 @@ func (projectlist *ProjectList) LoadFromFile(file *os.File) error {
return err return err
} }
project.Id = projectId project.Id = projectId
projectlist.AddProject(project)
projectId++ projectId++
projectlist.Projects = append(projectlist.Projects, project)
} }
if err := scanner.Err(); err != nil { if err := scanner.Err(); err != nil {
return err return err
@ -157,7 +169,11 @@ func (projectlist *ProjectList) WriteToFile(file *os.File) error {
writer := bufio.NewWriter(file) writer := bufio.NewWriter(file)
_, err := writer.WriteString(projectlist.String()) _, err := writer.WriteString(projectlist.String())
writer.Flush() writer.Flush()
return err if err != nil {
return err
}
projectlist.ResetIds()
return nil
} }
// LoadFromFilename loads a ProjectList from the filename. // LoadFromFilename loads a ProjectList from the filename.
@ -172,7 +188,11 @@ func (projectlist *ProjectList) LoadFromFilename(filename string) error {
// WriteToFilename writes a ProjectList to the specified file (most likely called "project.txt"). // WriteToFilename writes a ProjectList to the specified file (most likely called "project.txt").
func (projectlist *ProjectList) WriteToFilename(filename string) error { func (projectlist *ProjectList) WriteToFilename(filename string) error {
return ioutil.WriteFile(filename, []byte(projectlist.String()), 0640) if err := ioutil.WriteFile(filename, []byte(projectlist.String()), 0640); err != nil {
return err
}
projectlist.ResetIds()
return nil
} }
// String returns a complete list of projects in project.txt format. // String returns a complete list of projects in project.txt format.