Generalize updating entry and feed with update method

This commit is contained in:
Jason Streifling 2025-01-24 23:05:46 +01:00
parent 089c573aed
commit be79a13d48
2 changed files with 41 additions and 137 deletions

View File

@ -57,6 +57,15 @@ func (e *Entry) checkAuthors(authorInFeed bool) error {
}
// NewEntry creates a new Entry. It returns a *Entry.
// update sets the Updated time to time.Now.
func (e *Entry) update() {
if e.Updated == nil {
e.Updated = NewDate(time.Now())
} else {
e.Updated.DateTime = DateTime(time.Now())
}
}
func NewEntry(title string) *Entry {
return &Entry{
CommonAttributes: NewCommonAttributes(),
@ -69,13 +78,8 @@ func NewEntry(title string) *Entry {
// AddAuthor adds the Person as an author to the Entry. It returns its index as
// an int.
func (e *Entry) AddAuthor(p *Person) int {
if e.Updated == nil {
e.Updated = NewDate(time.Now())
} else {
e.Updated.DateTime = DateTime(time.Now())
}
return addToSlice(&e.Authors, p)
e.update()
}
// DeleteAuthor deletes the Person at index from the Entry. It return an error.
@ -84,23 +88,13 @@ func (e *Entry) DeleteAuthor(index int) error {
return fmt.Errorf("error deleting author %v from entry %v: %v", index, e.ID.URI, err)
}
if e.Updated == nil {
e.Updated = NewDate(time.Now())
} else {
e.Updated.DateTime = DateTime(time.Now())
}
e.update()
return nil
}
// AddCategory adds the Category to the Entry. It returns ts index as an int.
func (e *Entry) AddCategory(c *Category) int {
if e.Updated == nil {
e.Updated = NewDate(time.Now())
} else {
e.Updated.DateTime = DateTime(time.Now())
}
e.update()
return addToSlice(&e.Categories, c)
}
@ -111,24 +105,14 @@ func (e *Entry) DeleteCategory(index int) error {
return fmt.Errorf("error deleting category %v from entry %v: %v", index, e.ID.URI, err)
}
if e.Updated == nil {
e.Updated = NewDate(time.Now())
} else {
e.Updated.DateTime = DateTime(time.Now())
}
e.update()
return nil
}
// AddContributor adds the Person as a contributor to the Entry. It returns its
// index as an int.
func (e *Entry) AddContributor(c *Person) int {
if e.Updated == nil {
e.Updated = NewDate(time.Now())
} else {
e.Updated.DateTime = DateTime(time.Now())
}
e.update()
return addToSlice(&e.Contributors, c)
}
@ -139,23 +123,13 @@ func (e *Entry) DeleteContributor(index int) error {
return fmt.Errorf("error deleting contributor %v from entry %v: %v", index, e.ID.URI, err)
}
if e.Updated == nil {
e.Updated = NewDate(time.Now())
} else {
e.Updated.DateTime = DateTime(time.Now())
}
e.update()
return nil
}
// AddLink adds the Link to the Entry. It returns its index as an int.
func (e *Entry) AddLink(l *Link) int {
if e.Updated == nil {
e.Updated = NewDate(time.Now())
} else {
e.Updated.DateTime = DateTime(time.Now())
}
e.update()
return addToSlice(&e.Links, l)
}
@ -165,24 +139,14 @@ func (e *Entry) DeleteLink(index int) error {
return fmt.Errorf("error deleting link %v from entry %v: %v", index, e.ID.URI, err)
}
if e.Updated == nil {
e.Updated = NewDate(time.Now())
} else {
e.Updated.DateTime = DateTime(time.Now())
}
e.update()
return nil
}
// AddExtension adds the ExtensionElement to the Entry. It returns its index as
// an int.
func (e *Entry) AddExtension(x *ExtensionElement) int {
if e.Updated == nil {
e.Updated = NewDate(time.Now())
} else {
e.Updated.DateTime = DateTime(time.Now())
}
e.update()
return addToSlice(&e.Extensions, x)
}
@ -193,12 +157,7 @@ func (e *Entry) DeleteExtension(index int) error {
return fmt.Errorf("error deleting extension %v from entry %v: %v", index, e.ID.URI, err)
}
if e.Updated == nil {
e.Updated = NewDate(time.Now())
} else {
e.Updated.DateTime = DateTime(time.Now())
}
e.update()
return nil
}

99
feed.go
View File

@ -26,6 +26,15 @@ type Feed struct {
}
// NewFeed creates a new Feed. It returns a *Feed.
// update sets the Updated time to time.Now.
func (f *Feed) update() {
if f.Updated == nil {
f.Updated = NewDate(time.Now())
} else {
f.Updated.DateTime = DateTime(time.Now())
}
}
func NewFeed(title string) *Feed {
return &Feed{
CommonAttributes: NewCommonAttributes(),
@ -38,13 +47,8 @@ func NewFeed(title string) *Feed {
// AddAuthor adds the Person as an author to the Feed. It returns its index as
// an int.
func (f *Feed) AddAuthor(p *Person) int {
if f.Updated == nil {
f.Updated = NewDate(time.Now())
} else {
f.Updated.DateTime = DateTime(time.Now())
}
return addToSlice(&f.Authors, p)
f.update()
}
// DeleteAuthor deletes the Person at index from the Feed. It return an error.
@ -53,23 +57,13 @@ func (f *Feed) DeleteAuthor(index int) error {
return fmt.Errorf("error deleting author %v from entry %v: %v", index, f.ID.URI, err)
}
if f.Updated == nil {
f.Updated = NewDate(time.Now())
} else {
f.Updated.DateTime = DateTime(time.Now())
}
f.update()
return nil
}
// AddCategory adds the Category to the Feed. It returns its index as an int.
func (f *Feed) AddCategory(c *Category) int {
if f.Updated == nil {
f.Updated = NewDate(time.Now())
} else {
f.Updated.DateTime = DateTime(time.Now())
}
f.update()
return addToSlice(&f.Categories, c)
}
@ -80,24 +74,14 @@ func (f *Feed) DeleteCategory(index int) error {
return fmt.Errorf("error deleting category %v from entry %v: %v", index, f.ID.URI, err)
}
if f.Updated == nil {
f.Updated = NewDate(time.Now())
} else {
f.Updated.DateTime = DateTime(time.Now())
}
f.update()
return nil
}
// AddContributor adds the Person as a contributor to the Feed. It returns its
// index as an int.
func (f *Feed) AddContributor(c *Person) int {
if f.Updated == nil {
f.Updated = NewDate(time.Now())
} else {
f.Updated.DateTime = DateTime(time.Now())
}
f.update()
return addToSlice(&f.Contributors, c)
}
@ -108,24 +92,14 @@ func (f *Feed) DeleteContributor(index int) error {
return fmt.Errorf("error deleting contributor %v from entry %v: %v", index, f.ID.URI, err)
}
if f.Updated == nil {
f.Updated = NewDate(time.Now())
} else {
f.Updated.DateTime = DateTime(time.Now())
}
f.update()
return nil
}
// AddLink adds the Link to the Feed. There should be one Link with Rel "self".
// It returns its index as an int.
func (f *Feed) AddLink(l *Link) int {
if f.Updated == nil {
f.Updated = NewDate(time.Now())
} else {
f.Updated.DateTime = DateTime(time.Now())
}
f.update()
return addToSlice(&f.Links, l)
}
@ -135,23 +109,13 @@ func (f *Feed) DeleteLink(index int) error {
return fmt.Errorf("error deleting link %v from entry %v: %v", index, f.ID.URI, err)
}
if f.Updated == nil {
f.Updated = NewDate(time.Now())
} else {
f.Updated.DateTime = DateTime(time.Now())
}
f.update()
return nil
}
// AddExtension adds the Extension to the Feed. It returns its index as an int.
func (f *Feed) AddExtension(e *ExtensionElement) int {
if f.Updated == nil {
f.Updated = NewDate(time.Now())
} else {
f.Updated.DateTime = DateTime(time.Now())
}
f.update()
return addToSlice(&f.Extensions, e)
}
@ -162,23 +126,13 @@ func (f *Feed) DeleteExtension(index int) error {
return fmt.Errorf("error deleting extension %v from entry %v: %v", index, f.ID.URI, err)
}
if f.Updated == nil {
f.Updated = NewDate(time.Now())
} else {
f.Updated.DateTime = DateTime(time.Now())
}
f.update()
return nil
}
// AddEntry adds the Entry to the Feed. It returns its index as an int.
func (f *Feed) AddEntry(e *Entry) int {
if f.Updated == nil {
f.Updated = NewDate(time.Now())
} else {
f.Updated.DateTime = DateTime(time.Now())
}
f.update()
return addToSlice(&f.Entries, e)
}
@ -188,12 +142,7 @@ func (f *Feed) DeleteEntry(index int) error {
return fmt.Errorf("error deleting entry %v from entry %v: %v", index, f.ID.URI, err)
}
if f.Updated == nil {
f.Updated = NewDate(time.Now())
} else {
f.Updated.DateTime = DateTime(time.Now())
}
f.update()
return nil
}
@ -215,11 +164,7 @@ func (f *Feed) DeleteEntryByURI(uri string) error {
}
f.Entries = append(f.Entries[:index], f.Entries[index+1:]...)
if f.Updated == nil {
f.Updated = NewDate(time.Now())
} else {
f.Updated.DateTime = DateTime(time.Now())
}
f.update()
return nil
}