IDs should always reflect position in file
This commit is contained in:
parent
9ef6c61051
commit
13f20412ba
@ -41,23 +41,27 @@ func (projectlist *ProjectList) Filter(predicate func(*Project) bool) *ProjectLi
|
||||
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
|
||||
func (projectlist *ProjectList) AddProject(project *Project) {
|
||||
// The new project will be id 1
|
||||
project.Id = 1
|
||||
// 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
|
||||
project.Id = projectlist.GetNextId()
|
||||
projectlist.Projects = append(projectlist.Projects, project)
|
||||
}
|
||||
|
||||
// AddProjects adds all passed projects to the list
|
||||
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) }
|
||||
|
||||
@ -75,38 +79,38 @@ func (projectlist *ProjectList) GetProject(id int) (*Project, error) {
|
||||
// RemoveProjectById removes any Project with given Project 'id' from the ProjectList.
|
||||
// Returns an error if no Project was removed.
|
||||
func (projectlist *ProjectList) RemoveProjectById(id int) error {
|
||||
var newList ProjectList
|
||||
found := false
|
||||
for _, p := range projectlist.Projects {
|
||||
if p.Id != id {
|
||||
newList.AddProject(p)
|
||||
} else {
|
||||
var remIdx int
|
||||
var p *Project
|
||||
for remIdx, p = range projectlist.Projects {
|
||||
if p.Id == id {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
return errors.New("project not found")
|
||||
}
|
||||
*projectlist = newList
|
||||
projectlist.Projects = append(projectlist.Projects[:remIdx], projectlist.Projects[remIdx+1:]...)
|
||||
return nil
|
||||
}
|
||||
|
||||
// RemoveProject removes any Project from the ProjectList with the same String representation as the given Project.
|
||||
// Returns an error if no Project was removed.
|
||||
func (projectlist *ProjectList) RemoveProject(project Project) error {
|
||||
var newList ProjectList
|
||||
found := false
|
||||
for _, p := range projectlist.Projects {
|
||||
if p.String() != project.String() {
|
||||
newList.AddProject(p)
|
||||
} else {
|
||||
var remIdx int
|
||||
var p *Project
|
||||
for remIdx, p = range projectlist.Projects {
|
||||
if p.String() == project.String() {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
return errors.New("project not found")
|
||||
}
|
||||
*projectlist = newList
|
||||
projectlist.Projects = append(projectlist.Projects[:remIdx], projectlist.Projects[remIdx+1:]...)
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -126,6 +130,14 @@ func (projectlist *ProjectList) ArchiveProjectToFile(project Project, filename s
|
||||
return err
|
||||
}
|
||||
|
||||
func (projectlist *ProjectList) ResetIds() {
|
||||
projectId := 1
|
||||
for _, v := range projectlist.Projects {
|
||||
v.Id = projectId
|
||||
projectId++
|
||||
}
|
||||
}
|
||||
|
||||
// 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.
|
||||
func (projectlist *ProjectList) LoadFromFile(file *os.File) error {
|
||||
@ -143,8 +155,8 @@ func (projectlist *ProjectList) LoadFromFile(file *os.File) error {
|
||||
return err
|
||||
}
|
||||
project.Id = projectId
|
||||
projectlist.AddProject(project)
|
||||
projectId++
|
||||
projectlist.Projects = append(projectlist.Projects, project)
|
||||
}
|
||||
if err := scanner.Err(); err != nil {
|
||||
return err
|
||||
@ -157,8 +169,12 @@ func (projectlist *ProjectList) WriteToFile(file *os.File) error {
|
||||
writer := bufio.NewWriter(file)
|
||||
_, err := writer.WriteString(projectlist.String())
|
||||
writer.Flush()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
projectlist.ResetIds()
|
||||
return nil
|
||||
}
|
||||
|
||||
// LoadFromFilename loads a ProjectList from the filename.
|
||||
func (projectlist *ProjectList) LoadFromFilename(filename string) error {
|
||||
@ -172,7 +188,11 @@ func (projectlist *ProjectList) LoadFromFilename(filename string) error {
|
||||
|
||||
// WriteToFilename writes a ProjectList to the specified file (most likely called "project.txt").
|
||||
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.
|
||||
|
Loading…
Reference in New Issue
Block a user