Example #1
0
// @title 删除文章
// @description 删除文章及 postMate中的信息
// @router /del/:id [get]
func (c *PostController) Del() {
	res := &common.Response{}
	defer func() {
		c.Data["json"] = res
		c.ServeJSON()
	}()
	id, err := strconv.ParseInt(c.Ctx.Input.Param(":id"), 10, 64)
	if err != nil {
		res.Code = 1
		res.Msg = "del post id not valid"
		return
	}
	var post models.Post
	post.Id = id
	if post.Read() != nil {
		res.Code = 2
		res.Msg = "post not exist"
		return
	}
	if post.Del() != nil {
		res.Code = 3
		res.Msg = "del post failed"
		return
	}
	// sync postMate
	o := orm.NewOrm()
	var tagList []orm.Params
	num, err := o.QueryTable("postmeta").Filter("postid", id).Values(&tagList)
	if err != nil {
		fmt.Printf("del post id %v in postmeta failed!", id)
		for _, tag := range tagList {
			//metaId, _ := strconv.ParseInt(tag["MetaId"].(string), 10, 64)
			meta := models.Meta{Id: tag["MetaId"].(int64)}
			if meta.Read("Id") == nil {
				meta.Count -= 1
				meta.Update("Count")
			}
			//postMetaId, _ := strconv.ParseInt(tag["Id"].(string), 10, 64)
			postMeta := models.Postmeta{Id: tag["Id"].(int64)}
			if err := postMeta.Del(); err == nil {
				fmt.Println("delete post meta success")
			} else {
				fmt.Println("delete post meta error", err)
			}
		}
	}
	fmt.Printf("del post id %v in postmeta affected num is %v", id, num)
	return
}
Example #2
0
// @router /create [post]
func (c *PostController) Create() {
	var (
		id       int64
		title    = strings.TrimSpace(c.GetString("title"))
		subTitle = c.GetString("subTitle")
		content  = c.GetString("content")
		desc     = strings.TrimSpace(c.GetString("description"))
		tags     = strings.TrimSpace(c.GetString("tags"))
		types    int64
		status   int64
		coverId  int64
		post     models.Post
	)
	res := &common.PostCreateResponse{}
	defer func() {
		c.Data["json"] = res
		c.ServeJSON()
	}()

	// filter
	if title == "" {
		res.Code = 1
		res.Msg = "no title"
		return
	}
	o := orm.NewOrm()
	id, _ = c.GetInt64("id")
	status, _ = c.GetInt64("status")
	coverId, _ = c.GetInt64("coverId")
	types, _ = c.GetInt64("type")
	// deal tags
	var addtags []string
	if tags != "" {
		tagarr := strings.Split(tags, ",")
		for _, v := range tagarr {
			if tag := strings.TrimSpace(v); tag != "" {
				exists := false
				for _, vv := range addtags {
					if vv == tag {
						exists = true
						break
					}
				}
				if !exists {
					addtags = append(addtags, tag)
				}
			}
		}
	}

	// TODO check post is exits
	if id > 0 {
		post.Id = id
		if post.Read() != nil {
			fmt.Println("post not exist, will create one")
			post.Id = 0
		}
	}
	post.UserId = c.userid
	post.Status = status
	post.Title = title
	post.SubTitle = subTitle
	post.CoverId = coverId
	post.Description = desc
	post.Content = content
	post.Type = types
	if id < 1 { // add
		if _, err := post.Add(); err == nil {
			res.Code = 0
			res.Msg = "create post success"
			res.PostId = post.Id
		} else {
			res.Code = 2
			res.Msg = "create post error"
			return
		}
	} else { // update
		if post.Update("createdate", "updatedate", "title",
			"content", "subtitle", "description",
			"status", "userid", "type", "coverId") == nil {
			res.Code = 0
			res.Msg = "update post success"
			res.PostId = post.Id
		} else {
			res.Code = 2
			res.Msg = "update post error"
			return
		}
	}

	// update or create type meta
	if created, id, err := o.ReadOrCreate(&models.Postmeta{MetaId: types, PostId: post.Id}, "MetaId", "PostId"); err == nil {
		if created {
			fmt.Println("New Insert an object. Id:", id)
		} else {
			fmt.Println("Get an object. Id:", id)
		}
	}

	// TODO update post meta of tags
	if len(addtags) > 0 {
		for _, v := range addtags {
			meta := models.Meta{Value: v, Type: "tag"}
			if meta.Read("Value") != nil {
				meta.Count = 1
				meta.Add()
			} else {
				meta.Count++
				meta.Update("Count")
			}
			postMeta := models.Postmeta{MetaId: meta.Id, PostId: post.Id}
			// created-> is new
			// id-> the id
			// err-> error
			if created, id, err := o.ReadOrCreate(&postMeta, "MetaId", "PostId"); err == nil {
				if created {
					fmt.Println("New Insert an object. Id:", id)
				} else {
					fmt.Println("Get an object. Id:", id)
				}
			}
		}
	}
	return
}
Example #3
0
// @router /create [post]
func (c *MetaController) Create() {
	var (
		types = strings.TrimSpace(c.GetString("type")) // tag:标签  category:分类 keyword:关键字  vote: 赞
		value = strings.TrimSpace(c.GetString("value"))
		meta  models.Meta
	)

	res := &common.MetaCreateResponse{}
	defer func() {
		c.Data["json"] = res
		c.ServeJSON()
	}()

	if types == "" || value == "" {
		res.Code = 1
		res.Msg = "type or value not empty"
		return
	}

	meta.Value = value
	meta.Type = types
	id, _ := c.GetInt64("id")
	if id > 0 {
		meta.Id = id
		if meta.Read() == nil {
			meta.Update("Type", "Value")
		} else {
			meta.Add()
		}
	} else {
		meta.Add()
	}
	res.Code = 0
	res.Msg = "success"
	res.MetaId = meta.Id
	return
}