first commit
This commit is contained in:
parent
0773d38c8c
commit
b54a862e23
18
api/api.go
Normal file
18
api/api.go
Normal file
@ -0,0 +1,18 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"dacremont.xyz/learnrest/api/messages"
|
||||
)
|
||||
|
||||
type Api struct {}
|
||||
|
||||
func NewApi () *http.ServeMux {
|
||||
mux := http.NewServeMux()
|
||||
|
||||
mux.Handle("/messages", messages.NewMessagesAPI())
|
||||
mux.HandleFunc("GET /hello", func(w http.ResponseWriter, r *http.Request) {w.Write([]byte("World!"))})
|
||||
|
||||
return mux
|
||||
}
|
44
api/messages/handler.go
Normal file
44
api/messages/handler.go
Normal file
@ -0,0 +1,44 @@
|
||||
package messages
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func getMessages(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
err := json.NewEncoder(w).Encode(getAllMessages())
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
func postMessages(w http.ResponseWriter, r *http.Request) {
|
||||
var payload Message
|
||||
err := json.NewDecoder(r.Body).Decode(&payload)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
msg := Message{
|
||||
Author_id: payload.Author_id,
|
||||
Text: payload.Text,
|
||||
}
|
||||
|
||||
err = insertMessage(msg)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
}
|
||||
}
|
||||
|
||||
func NewMessagesAPI() *http.ServeMux {
|
||||
initMessages()
|
||||
mux := http.NewServeMux()
|
||||
|
||||
mux.HandleFunc("GET /", getMessages)
|
||||
mux.HandleFunc("POST /", postMessages)
|
||||
|
||||
return mux
|
||||
}
|
45
api/messages/model.go
Normal file
45
api/messages/model.go
Normal file
@ -0,0 +1,45 @@
|
||||
package messages
|
||||
|
||||
import "errors"
|
||||
|
||||
type Message struct {
|
||||
MessageId uint64 `json:"message_id"`
|
||||
Author_id uint64 `json:"author_id"`
|
||||
Text string `json:"text"`
|
||||
}
|
||||
|
||||
var messages []Message
|
||||
|
||||
func initMessages() {
|
||||
messages = make([]Message, 0)
|
||||
}
|
||||
|
||||
func insertMessage(msg Message) error {
|
||||
if msg.Author_id == 0 {
|
||||
return errors.New("An author_id must be specified")
|
||||
}
|
||||
|
||||
if msg.Text == "" {
|
||||
return errors.New("The field text must not be empty")
|
||||
}
|
||||
|
||||
messages = append(messages, Message{
|
||||
MessageId: uint64(len(messages)),
|
||||
Text: msg.Text,
|
||||
Author_id: msg.Author_id,
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func getAllMessages() []Message {
|
||||
return messages
|
||||
}
|
||||
|
||||
func getMessageById(id uint64) (Message, error) {
|
||||
if int(id) >= len(messages) {
|
||||
return Message{}, errors.New("Message doesn't exists")
|
||||
}
|
||||
|
||||
return messages[id], nil
|
||||
}
|
1
api/users/handler.go
Normal file
1
api/users/handler.go
Normal file
@ -0,0 +1 @@
|
||||
package users
|
7
api/users/model.go
Normal file
7
api/users/model.go
Normal file
@ -0,0 +1,7 @@
|
||||
package users
|
||||
|
||||
type User struct {
|
||||
Id uint64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user