Ejemplo n.º 1
0
/*
tid: 文章id
content:评论内容
*/
func AddComment(tid, content string) error {
	titleId, err := strconv.ParseInt(tid, 10, 64)
	if err != nil {
		return err
	}
	orm := orm.NewOrm()
	comment := &Comment{
		Tid:       titleId,
		Content:   content,
		ReplyTime: CurrentTime(),
	}
	_, err = orm.Insert(comment)
	if err != nil {
		return err
	}
	// 文章评论数增一
	topic := &Topic{
		Id: titleId,
	}
	if orm.Read(topic) == nil {
		topic.ReplyCount++
		topic.LastReplyTime = CurrentTime()
		// TODO set current user id
		// topic.ReplyLastUserId = current_user_id
		orm.Update(topic)
	}
	return err
}
Ejemplo n.º 2
0
func DeleteComment(cid, tid string) error {
	commentId, err := strconv.ParseInt(cid, 10, 64)
	if err != nil {
		return err
	}
	orm := orm.NewOrm()
	comment := &Comment{Id: commentId}
	_, err = orm.Delete(comment)
	if err != nil {
		return err
	}
	topicId, err := strconv.ParseInt(tid, 10, 64)
	if err != nil {
		return err
	}
	comments := make([]*Comment, 0)
	_, err = orm.QueryTable("comment").Filter("tid", topicId).OrderBy("-replyTime").All(&comments)
	if err != nil {
		return err
	}
	topic := &Topic{Id: topicId}
	if orm.Read(topic) == nil && len(comments) > 0 {
		topic.ReplyCount--
		topic.LastReplyTime = comments[0].ReplyTime
		// TODO set last user id
		// topic.ReplyLastUserId = comments[0].UserId
		orm.Update(topic)
	}
	return err
}
Ejemplo n.º 3
0
func UpdateTopic(id, title, content, tags, cid, attachName string, attached bool) error {
	tid, err := strconv.ParseInt(id, 10, 64)
	if err != nil {
		return err
	}
	categoryId, err := strconv.ParseInt(cid, 10, 64)
	if err != nil {
		return err
	}

	orm := orm.NewOrm()
	category := &Category{Id: categoryId}
	err = orm.QueryTable("category").Filter("id", categoryId).One(category)
	if err != nil {
		return err
	}
	topic := &Topic{Id: tid}
	tagArray := strings.Split(strings.Trim(tags, " "), " ")
	tags = "$" + strings.Join(tagArray, "#$") + "#"
	var oldTags string
	var oldTitle string
	var oldAttachName string
	// valida exist and modify
	err = orm.Read(topic)
	if err != nil {
		return err
	} else {
		oldTitle = topic.Category
		oldTags = topic.Tags
		oldAttachName = topic.Attachment

		topic.Title = title
		topic.Content = content
		topic.Tags = tags
		if attached {
			topic.Attachment = attachName
		}
		topic.UpdateTime = CurrentTime()
		topic.Category = category.Title
		_, err = orm.Update(topic)
		if err != nil {
			return err
		}
	}

	// delete old file
	if attached {
		oldAttachName = path.Join("attachment", oldAttachName)
		if _, err := os.Stat(oldAttachName); err == nil {
			os.Remove(oldAttachName)
		}
	}
	// update category's topicCount
	if oldTitle != category.Title {
		// new category plus one
		category.TopicCount++
		_, err = orm.Update(category)
		if err != nil {
			return err
		}
		// TODO 如果旧的分类被删除,则报错,错误信息是:<QuerySeter> no row found
		count, err := orm.QueryTable("category").Filter("title", oldTitle).Count()
		if err != nil {
			return err
		}
		// 分类存在,则分类数目减一
		if count == 1 {
			oldCategory := &Category{Title: oldTitle}
			err = orm.QueryTable("category").Filter("title", oldTitle).One(oldCategory)
			if err != nil {
				return err
			}
			oldCategory.TopicCount--
			if oldCategory.TopicCount >= 0 {
				_, err = orm.Update(oldCategory)
				if err != nil {
					return err
				}
			}
		}
	}
	oldTags = strings.Replace(strings.Replace(oldTags, "#", " ", -1), "$", "", -1)
	oldTagArray := strings.Split(strings.Trim(oldTags, " "), " ")
	return ModifyTags(commons.Diff(tagArray, oldTagArray))
}