forked from GoesToEleven/GolangTraining
-
Notifications
You must be signed in to change notification settings - Fork 0
/
newmovie.go
executable file
·46 lines (37 loc) · 817 Bytes
/
newmovie.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
package movieinfo
import (
"net/http"
"google.golang.org/appengine"
"google.golang.org/appengine/search"
)
type newMovieModel struct {
CreatedID string
}
func handleNewMovie(res http.ResponseWriter, req *http.Request) {
ctx := appengine.NewContext(req)
model := &newMovieModel{}
if req.Method == "POST" {
title := req.FormValue("title")
summary := req.FormValue("summary")
index, err := search.Open("movies")
if err != nil {
http.Error(res, err.Error(), 500)
return
}
movie := &Movie{
Title: title,
Summary: summary,
}
id, err := index.Put(ctx, "", movie)
if err != nil {
http.Error(res, err.Error(), 500)
return
}
model.CreatedID = id
}
err := tpl.ExecuteTemplate(res, "new-movie", model)
if err != nil {
http.Error(res, err.Error(), 500)
return
}
}