Use generics for Add and Delete Methods
This commit is contained in:
84
feed.go
84
feed.go
@@ -37,144 +37,96 @@ func NewFeed(title string) *Feed {
|
||||
|
||||
// AddAuthor adds the Person as an author to the Feed.
|
||||
func (f *Feed) AddAuthor(p *Person) {
|
||||
if f.Authors == nil {
|
||||
f.Authors = make([]*Person, 1)
|
||||
f.Authors[0] = p
|
||||
} else {
|
||||
f.Authors = append(f.Authors, p)
|
||||
}
|
||||
|
||||
addToSlice(f.Authors, p)
|
||||
f.Updated = NewDate(time.Now())
|
||||
}
|
||||
|
||||
// DeleteAuthor deletes the Person from the Feed. It return an error.
|
||||
func (f *Feed) DeleteAuthor(id int) error {
|
||||
length := len(f.Authors)
|
||||
if id > length {
|
||||
return fmt.Errorf("error deleting author from feed %v: id %v out of range %v", f.ID.URI, id, length)
|
||||
if err := deleteFromSlice(f.Authors, id); err != nil {
|
||||
return fmt.Errorf("error deleting author %v from entry %v: %v", id, f.ID.URI, err)
|
||||
}
|
||||
|
||||
f.Authors = append(f.Authors[:id], f.Authors[id+1:]...)
|
||||
f.Updated = NewDate(time.Now())
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddCategory adds the Category to the Feed.
|
||||
func (f *Feed) AddCategory(c *Category) {
|
||||
if f.Categories == nil {
|
||||
f.Categories = make([]*Category, 1)
|
||||
f.Categories[0] = c
|
||||
} else {
|
||||
f.Categories = append(f.Categories, c)
|
||||
}
|
||||
|
||||
addToSlice(f.Categories, c)
|
||||
f.Updated = NewDate(time.Now())
|
||||
}
|
||||
|
||||
// DeleteCategory deletes the Category from the Feed. It return an error.
|
||||
func (f *Feed) DeleteCategory(id int) error {
|
||||
length := len(f.Categories)
|
||||
if id > length {
|
||||
return fmt.Errorf("error deleting category from feed %v: id %v out of range %v", f.ID.URI, id, length)
|
||||
if err := deleteFromSlice(f.Categories, id); err != nil {
|
||||
return fmt.Errorf("error deleting category %v from entry %v: %v", id, f.ID.URI, err)
|
||||
}
|
||||
|
||||
f.Categories = append(f.Categories[:id], f.Categories[id+1:]...)
|
||||
f.Updated = NewDate(time.Now())
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddContributor adds the Person as a contributor to the Feed.
|
||||
func (f *Feed) AddContributor(c *Person) {
|
||||
if f.Contributors == nil {
|
||||
f.Contributors = make([]*Person, 1)
|
||||
f.Contributors[0] = c
|
||||
} else {
|
||||
f.Contributors = append(f.Contributors, c)
|
||||
}
|
||||
|
||||
addToSlice(f.Contributors, c)
|
||||
f.Updated = NewDate(time.Now())
|
||||
}
|
||||
|
||||
// DeleteContributor deletes the Person from the Feed. It return an error.
|
||||
func (f *Feed) DeleteContributor(id int) error {
|
||||
length := len(f.Contributors)
|
||||
if id > length {
|
||||
return fmt.Errorf("error deleting contributor from feed %v: id %v out of range %v", f.ID.URI, id, length)
|
||||
if err := deleteFromSlice(f.Contributors, id); err != nil {
|
||||
return fmt.Errorf("error deleting contributor %v from entry %v: %v", id, f.ID.URI, err)
|
||||
}
|
||||
|
||||
f.Contributors = append(f.Contributors[:id], f.Contributors[id+1:]...)
|
||||
f.Updated = NewDate(time.Now())
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddLink adds the Link to the Feed. There should be one Link with Rel "self".
|
||||
func (f *Feed) AddLink(l *Link) {
|
||||
if f.Links == nil {
|
||||
f.Links = make([]*Link, 1)
|
||||
f.Links[0] = l
|
||||
} else {
|
||||
f.Links = append(f.Links, l)
|
||||
}
|
||||
|
||||
addToSlice(f.Links, l)
|
||||
f.Updated = NewDate(time.Now())
|
||||
}
|
||||
|
||||
// DeleteLink deletes the Link from the Feed. It return an error.
|
||||
func (f *Feed) DeleteLink(id int) error {
|
||||
length := len(f.Links)
|
||||
if id > length {
|
||||
return fmt.Errorf("error deleting link from feed %v: id %v out of range %v", f.ID.URI, id, length)
|
||||
if err := deleteFromSlice(f.Links, id); err != nil {
|
||||
return fmt.Errorf("error deleting link %v from entry %v: %v", id, f.ID.URI, err)
|
||||
}
|
||||
|
||||
f.Links = append(f.Links[:id], f.Links[id+1:]...)
|
||||
f.Updated = NewDate(time.Now())
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddExtension adds the Extension to the Feed.
|
||||
func (f *Feed) AddExtension(e *ExtensionElement) {
|
||||
if f.Extensions == nil {
|
||||
f.Extensions = make([]*ExtensionElement, 1)
|
||||
f.Extensions[0] = e
|
||||
} else {
|
||||
f.Extensions = append(f.Extensions, e)
|
||||
}
|
||||
|
||||
addToSlice(f.Extensions, e)
|
||||
f.Updated = NewDate(time.Now())
|
||||
}
|
||||
|
||||
// DeleteExtension deletes the Extension from the Feed. It return an error.
|
||||
func (f *Feed) DeleteExtension(id int) error {
|
||||
length := len(f.Extensions)
|
||||
if id > length {
|
||||
return fmt.Errorf("error deleting extension from feed %v: id %v out of range %v", f.ID.URI, id, length)
|
||||
if err := deleteFromSlice(f.Extensions, id); err != nil {
|
||||
return fmt.Errorf("error deleting extension %v from entry %v: %v", id, f.ID.URI, err)
|
||||
}
|
||||
|
||||
f.Extensions = append(f.Extensions[:id], f.Extensions[id+1:]...)
|
||||
f.Updated = NewDate(time.Now())
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddEntry adds the Entry to the Feed.
|
||||
func (f *Feed) AddEntry(e *Entry) {
|
||||
if f.Entries == nil {
|
||||
f.Entries = make([]*Entry, 1)
|
||||
f.Entries[0] = e
|
||||
} else {
|
||||
f.Entries = append(f.Entries, e)
|
||||
}
|
||||
|
||||
addToSlice(f.Entries, e)
|
||||
f.Updated = NewDate(time.Now())
|
||||
}
|
||||
|
||||
// DeleteEntry deletes the Entry from the Feed. It return an error.
|
||||
func (f *Feed) DeleteEntry(id int) error {
|
||||
length := len(f.Entries)
|
||||
if id > length {
|
||||
return fmt.Errorf("error deleting entry from feed %v: id %v out of range %v", f.ID.URI, id, length)
|
||||
if err := deleteFromSlice(f.Entries, id); err != nil {
|
||||
return fmt.Errorf("error deleting entry %v from entry %v: %v", id, f.ID.URI, err)
|
||||
}
|
||||
|
||||
f.Entries = append(f.Entries[:id], f.Entries[id+1:]...)
|
||||
f.Updated = NewDate(time.Now())
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user