Example #1
0
func (env Env) NewKnot(c *gin.Context) {
	var new models.Knot
	err := c.BindJSON(&new)

	if err == nil && new.Validate() {
		err := new.Insert(env.db)

		if err != nil {
			c.JSON(http.StatusInternalServerError, nil)
		} else {
			c.JSON(http.StatusOK, gin.H{
				"status": "posted",
				"title":  new.Title,
			})
		}
	} else {
		sendBadRequest(c, fmt.Errorf("invalid_data"))
	}
}
Example #2
0
func (env Env) UpdateKnot(c *gin.Context) {
	id := c.Param("id")
	if !bson.IsObjectIdHex(id) {
		sendBadRequest(c, fmt.Errorf("id_not_valid"))
	}
	mongoId := bson.ObjectIdHex(id)

	var new models.Knot
	var err error
	err = c.BindJSON(&new)

	if err != nil || !new.Validate() {
		sendBadRequest(c, fmt.Errorf("invalid_data"))
	} else {
		err = new.Update(env.db, mongoId)
		if err != nil {
			sendError(c, err)
		} else {
			c.JSON(http.StatusOK, gin.H{"status": true})
		}
	}
}