This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
func main() { | |
fmt.Println("Hello World!") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"net/http" | |
"github.com/gorilla/mux" | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type event struct { | |
ID string `json:"ID"` | |
Title string `json:"Title"` | |
Description string `json:"Description"` | |
} | |
type allEvents []event | |
var events = allEvents{ | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func createEvent(w http.ResponseWriter, r *http.Request) { | |
var newEvent event | |
reqBody, err := ioutil.ReadAll(r.Body) | |
if err != nil { | |
fmt.Fprintf(w, "Kindly enter data with the event title and description only in order to update") | |
} | |
json.Unmarshal(reqBody, &newEvent) | |
events = append(events, newEvent) | |
w.WriteHeader(http.StatusCreated) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func getOneEvent(w http.ResponseWriter, r *http.Request) { | |
eventID := mux.Vars(r)["id"] | |
for _, singleEvent := range events { | |
if singleEvent.ID == eventID { | |
json.NewEncoder(w).Encode(singleEvent) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func getAllEvents(w http.ResponseWriter, r *http.Request) { | |
json.NewEncoder(w).Encode(events) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func deleteEvent(w http.ResponseWriter, r *http.Request) { | |
eventID := mux.Vars(r)["id"] | |
for i, singleEvent := range events { | |
if singleEvent.ID == eventID { | |
events = append(events[:i], events[i+1:]...) | |
fmt.Fprintf(w, "The event with ID %v has been deleted successfully", eventID) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func updateEvent(w http.ResponseWriter, r *http.Request) { | |
eventID := mux.Vars(r)["id"] | |
var updatedEvent event | |
reqBody, err := ioutil.ReadAll(r.Body) | |
if err != nil { | |
fmt.Fprintf(w, "Kindly enter data with the event title and description only in order to update") | |
} | |
json.Unmarshal(reqBody, &updatedEvent) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func main() { | |
initEvents() | |
router := mux.NewRouter().StrictSlash(true) | |
router.HandleFunc("/", homeLink) | |
router.HandleFunc("/event", createEvent).Methods("POST") | |
router.HandleFunc("/events", getAllEvents).Methods("GET") | |
router.HandleFunc("/events/{id}", getOneEvent).Methods("GET") | |
router.HandleFunc("/events/{id}", updateEvent).Methods("PATCH") | |
router.HandleFunc("/events/{id}", deleteEvent).Methods("DELETE") | |
log.Fatal(http.ListenAndServe(":8080", router)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"io/ioutil" | |
"log" | |
"net/http" | |
"github.com/gorilla/mux" |