// HandlerDelete represents a API handler to delete an article func HandlerDelete(req *router.Request) { //appCtx := app.GetContext() //doc := appCtx.DB.C("article") //article := Article{ // ID: bson.NewObjectId(), // CreatedAt: time.Now(), //} // //if err := gin.Bind(&article); err != nil { // req.BadRequest(err) // return //} // //if err := doc.Insert(article); err != nil { // if mgo.IsDup(err) { // req.Conflict("The slug %s already exists in the database", article.Slug) // return // } // // req.ServerError(err) // return //} //httpResponse.Ok(gin, httpResponse.Resource{article}) req.NoContent() }
// HandlerList represents a API handler to get a list of articles func HandlerList(req *router.Request) { arts := []*Article{} if err := Query().Find(defaultSearch).Sort("-createdAt").All(&arts); err != nil { req.Error(err) return } req.Ok(NewPayloadFromModels(arts)) }
// HandlerGet represents a API handler to get a single article func HandlerGet(req *router.Request) { // appCtx := app.GetContext() // doc := appCtx.DB.C("article") // article := article.Article{} // id := bson.ObjectIdHex(mux.Vars(req.Request)["id"]) // if err := doc.Find(bson.M{"_id": id}).One(&article); err != nil { // if err == mgo.ErrNotFound { // req.NotFound("Article %s not found", id) // return // } // req.Error(NewSer) // return // } //httpResponse.Ok(gin, httpResponse.Resource{article}) req.NoContent() }
// HandlerAdd represents an API handler to add a new article func HandlerAdd(req *router.Request) { params, ok := req.Params.(*HandlerAddParams) if !ok { req.Error(apierror.NewServerError("Couldn't cast params")) } a := &Article{ Title: params.Title, Subtitle: params.Subtitle, Content: params.Content, Description: params.Description, IsDeleted: false, IsPublished: false, } if err := a.Save(); err != nil { req.Error(err) } req.Created(a) }