func (cr *PeopleController) Create(cx *goweb.Context) { // create a new appengine context gaerecords.CreateAppEngineContext(cx.Request) // create a new person person := People.New() // get the fields from the HTTP request var name string = cx.Request.FormValue("name") age, _ := strconv.Atoi64(cx.Request.FormValue("age")) // set the fields person. SetString("name", name). SetInt64("age", age) // save it err := person.Put() if err == nil { // success - redirect to see this person cx.RespondWithLocation(fmt.Sprint("/people/", person.ID())) } else { // failed - write the error cx.ResponseWriter.Write([]byte(fmt.Sprintf("Error: %v", err))) } }
/** * Retourneer hoofd categorieen * * @author A. Glansbeek en P. Kompier * @version 1.0 * @date 2011-12-18 */ func (cr *CategoryController) ReadMany(cx *goweb.Context) { var m structs.WordpressHeadCategory models.CallWordpressApi(cx, &m, "get_category_index", nil) var headCategories []structs.HeadCategory for _, category := range m.Categories { // Alleen parents 0 (dat is hoofd category) if category.Parent == 0 { headCategories = append(headCategories, structs.HeadCategory{category.Id, category.Title}) } } cx.RespondWithData(headCategories) }
/** * GET artikel met id [1..n], retourneer een artikel * * @author A. Glansbeek en P. Kompier * @version 1.0 * @date 2011-12-18 */ func (cr *ArticleController) Read(id string, cx *goweb.Context) { params := map[string]string{"post_id": id} var m structs.WordpressPost models.CallWordpressApi(cx, &m, "get_post", params) if m.Post.Id == 0 { var str []string str = append(str, "Het opgevraagde id bestaat niet") cx.Respond(nil, 202, str, cx) } else { cx.RespondWithData(createArticle(m.Post)) } }
/** * Creert een nieuwe comment (tip) * * @author A. Glansbeek en P. Kompier * @version 1.0 * @date 2011-12-18 */ func (cr *CommentController) Create(cx *goweb.Context) { reqValues := cx.GetReqData() params := map[string]string{ "post_id": reqValues.Get("post_id"), "name": reqValues.Get("name"), "email": reqValues.Get("email"), "content": reqValues.Get("content"), } var m structs.WordpressCommentResponse models.CallWordpressApi(cx, &m, "submit_comment", params) cx.RespondWithData(structs.TipResponse{m.Status}) }
/** * Zoek functie! * * @author A. Glansbeek en P. Kompier * @version 1.0 * @date 2012-01-06 */ func (cr *SearchController) Read(input string, cx *goweb.Context) { params := map[string]string{"search": input} var m structs.WordpressPostMany models.CallWordpressApi(cx, &m, "get_search_results", params) var articles []structs.Article for _, post := range m.Posts { // Nieuw struct artikel article := createArticle(post) articles = append(articles, article) } cx.RespondWithData(articles) }
func (cr *PeopleController) Delete(id string, cx *goweb.Context) { // create a new appengine context gaerecords.CreateAppEngineContext(cx.Request) // get the person ID from the URL personID, _ := strconv.Atoi64(id) // load the person person, _ := People.Find(personID) // delete the person person.Delete() // send them on their way cx.RespondWithLocation("/people") }
/** * Roep de wordpress API aan * * @author A. Glansbeek en P. Kompier * @params *goweb.Context Context van goweb * @params interface De struct waarin json opgeslagen wordt * @params string functie naam van API wordpress * @params map[string]string map waarin params staan */ func CallWordpressApi(cx *goweb.Context, v interface{}, callfunction string, params map[string]string) { var contents []uint8 url := createURL(callfunction, params) c := appengine.NewContext(cx.GetRequest()) client := urlfetch.Client(c) response, err := client.Get(url) u := user.Current(c) var name string if u == nil { name = "Gast" } else { name = u.String() } data.SaveLog(url, name, cx.GetRequest()) // Error check if err != nil { cx.RespondWithErrorMessage("Kan geen verbinding maken met de wordpress webservice", http.StatusBadRequest) return } else { // Aan het einde van deze functie sluit response defer response.Body.Close() // Maak van de content bytes contents, err = ioutil.ReadAll(response.Body) // Error check if err != nil { cx.RespondWithErrorMessage("Kan response niet lezen", http.StatusBadRequest) return } } err = json.Unmarshal(contents, v) // Error check if err != nil { cx.RespondWithErrorMessage("Kan json niet parsen err: "+err.String(), http.StatusBadRequest) return } }
/** * Retourneer alle artikelen * * @author A. Glansbeek en P. Kompier * @version 1.0 * @date 2011-12-18 */ func (cr *ArticleController) ReadMany(cx *goweb.Context) { var m structs.WordpressPostMany // Als category_id is, pak dan alleen article van die category if cx.PathParams["category_id"] != "" { params := map[string]string{ "category_id": cx.PathParams["category_id"], } models.CallWordpressApi(cx, &m, "get_category_posts", params) } else { models.CallWordpressApi(cx, &m, "get_recent_posts", nil) } var mapper = make(map[string][]structs.Article) for _, post := range m.Posts { // Nieuw struct artikel article := createArticle(post) // Zet het artikel in de goede category _, present := mapper[article.Category] if present { mapper[article.Category] = append(mapper[article.Category], article) } else { var articles []structs.Article articles = append(articles, article) mapper[article.Category] = articles } } cx.RespondWithData(mapper) }
func (cr *MyAPIController) Read(id string, cx *goweb.Context) { if id == "1" { cx.RespondWithData(TestEntity{id, "Mat", 28}) } else if id == "2" { cx.RespondWithData(TestEntity{id, "Laurie", 27}) } else { cx.RespondWithNotFound() } }
func (cr *CommentController) Read(id string, cx *goweb.Context) { cx.RespondWithStatus(http.StatusForbidden) }
func (cr *CommentController) UpdateMany(cx *goweb.Context) { cx.RespondWithStatus(http.StatusForbidden) }
func (cr *CategoryController) Update(id string, cx *goweb.Context) { cx.RespondWithStatus(http.StatusForbidden) }
func (cr *PeopleController) DeleteMany(cx *goweb.Context) { cx.RespondWithNotImplemented() }
func (cr *SearchController) Create(cx *goweb.Context) { cx.RespondWithStatus(http.StatusForbidden) }
func (cr *CategoryController) DeleteMany(cx *goweb.Context) { cx.RespondWithStatus(http.StatusForbidden) }
func (cr *MyAPIController) UpdateMany(cx *goweb.Context) { cx.RespondWithData(TestEntity{"1", "Mat", 28}) }
func (cr *MyAPIController) Update(id string, cx *goweb.Context) { cx.RespondWithData(TestEntity{id, "Mat", 28}) }
func (cr *MyAPIController) ReadMany(cx *goweb.Context) { cx.RespondWithData([]TestEntity{TestEntity{"1", "Mat", 28}, TestEntity{"2", "Laurie", 27}}) }
func (cr *SearchController) Delete(id string, cx *goweb.Context) { cx.RespondWithStatus(http.StatusForbidden) }
func (cr *MyAPIController) Delete(id string, cx *goweb.Context) { cx.RespondWithOK() }