-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete.go
51 lines (43 loc) · 1.2 KB
/
delete.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main
import (
"log"
"net/http"
"github.com/gorilla/mux"
)
func (b *Backstore) deleteFeed(writer http.ResponseWriter, request *http.Request) {
vars := mux.Vars(request)
feedID, ok := vars["id"]
if !ok {
writeHTTPResponse(http.StatusInternalServerError, "there was an error deleting the feed", writer)
return
}
feed, err := retrieveFeedByID(b.db, feedID)
if err != nil {
log.Println(err)
writeHTTPResponse(http.StatusNotFound, "unable to delete feed", writer)
return
}
// retrieve feed items first
items, err := retriveItemsByFeedID(b.db, feedID)
if err != nil {
log.Printf("unable to retrieve feed item, %v\n", err)
} else {
// deleting feed elements
for _, item := range items {
err = deleteItemFromDB(b.db, &item)
if err != nil {
// exit delete function if we're unable to delete an item
log.Printf("unable to delete feed item, %v\n", err)
writeHTTPResponse(http.StatusInternalServerError, "unable to delete feed", writer)
return
}
}
}
// now deleting feed
err = deleteFeedFromDB(b.db, &feed)
if err != nil {
log.Printf("unable to delete feed, %v\n", err)
writeHTTPResponse(http.StatusInternalServerError, "unable to delete", writer)
return
}
}