Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "encoding/json"
- "fmt"
- "net/http"
- )
- type Note struct {
- Id string `json:"id"`
- Title string `json:"title"`
- Text string `json:"text"`
- DateCreate uint `json:"date_create"`
- DateUpdate uint `json:"date_update"`
- }
- var preparedNotes = []Note{
- Note{"10dcdccb-8876-4245-ac53-92900c6509bd", "Первая заметка", "Вторая заметка", 1513759529, 1517259529},
- Note{"10dcdccb-8876-4245-ac53-92900c6509bd", "Первая заметка", "Вторая заметка", 1513759529, 1517259529},
- }
- func addNote(w http.ResponseWriter, r *http.Request) {
- var note Note
- err := json.NewDecoder(r.Body).Decode(¬e)
- if err != nil || note.Text == "" || note.Title == "" {
- w.WriteHeader(400)
- return
- }
- //add
- w.WriteHeader(201)
- }
- func updateNote(w http.ResponseWriter, r *http.Request) {
- var note Note
- err := json.NewDecoder(r.Body).Decode(¬e)
- if err != nil || note.Id == "" || note.Text == "" || note.Title == "" {
- w.WriteHeader(400)
- return
- }
- //update
- w.WriteHeader(201)
- }
- func deleteNote(w http.ResponseWriter, r *http.Request) {
- var note Note
- err := json.NewDecoder(r.Body).Decode(¬e)
- if err != nil || note.Id == "" {
- w.WriteHeader(400)
- return
- }
- //delete
- w.WriteHeader(204)
- }
- func getNotes(w http.ResponseWriter, r *http.Request) {
- w.Header().Set("Content-type", "application/json; charset=utf-8")
- if err := json.NewEncoder(w).Encode(preparedNotes); err != nil {
- w.WriteHeader(500)
- }
- }
- func updateRecord(w http.ResponseWriter, r *http.Request) {
- }
- func main() {
- //http.HandleFunc("")
- http.MethodDelete("/", deleteNote)
- http.HandleFunc("/", getNotes)
- http.HandleFunc("/", addNote)
- fmt.Println("Starting server at 8080")
- http.ListenAndServe(":8080", nil)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement