Esempio n. 1
0
func (i *Impl) JsonGetPostThreadComments(w rest.ResponseWriter, r *rest.Request) {

	// Get starttime for measuring how long this functions takes
	timeStart := time.Now()

	i.DumpRequestHeader(r)

	// Sleep for debugging delay . DEBUG
	time.Sleep(500 * time.Millisecond)

	i.SetResponseContentType("application/json", &w)

	postid := r.PathParam("postid")
	pid, err := strconv.ParseUint(postid, 10, 0)
	if err != nil {
		rest.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	const PARAM_OFFSET = "offset"
	const PARAM_LIMIT = "limit"
	var Offset int64
	var Limit int64

	Offset = -1
	Limit = -1

	// set all map query strings to lowercase
	m, err := url.ParseQuery(strings.ToLower(r.URL.RawQuery))
	if err != nil {
		rest.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	// Get the query params for limit and offset (if exists)
	if m.Get(PARAM_OFFSET) != "" {
		Offset, err = strconv.ParseInt(m.Get(PARAM_OFFSET), 10, 64)
		if err != nil {
			rest.Error(w, err.Error(), http.StatusBadRequest)
			return
		}
	}
	if m.Get(PARAM_LIMIT) != "" {
		Limit, err = strconv.ParseInt(m.Get(PARAM_LIMIT), 10, 64)
		if err != nil {
			rest.Error(w, err.Error(), http.StatusBadRequest)
			return
		}
	}
	lock.RLock()
	defer lock.RUnlock()

	log.Printf("Limit %d, Offset %d\n", Limit, Offset)
	res, err := i.Dbmap.GetWithChilds(post.Post{}, Limit, Offset, pid)
	if err != nil {
		rest.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	if res == nil {
		err = errors.New(fmt.Sprintf("Get post id %d not found", pid))
		rest.Error(w, err.Error(), http.StatusNotFound)
		return
	}
	postlist := post.Posts{}
	postlist.JsonApiVersion = post.API_VERSION
	dbpost := res.(*post.Post)

	for _, comment := range dbpost.Comments {
		markedDown := blackfriday.MarkdownCommon([]byte(comment.Body))
		comment.Body = string(bluemonday.UGCPolicy().SanitizeBytes(markedDown))
	}
	postlist.Posts = append(postlist.Posts, dbpost)

	// Get and set the execution time in milliseconds
	postlist.RequestDuration = (time.Since(timeStart).Nanoseconds() / int64(time.Millisecond))

	w.WriteJson(&postlist)
}
Esempio n. 2
0
func (i *Impl) JsonGetPosts(w rest.ResponseWriter, r *rest.Request) {

	// Get starttime for measuring how long this functions takes
	timeStart := time.Now()

	i.DumpRequestHeader(r)

	// Sleep for debugging delay . DEBUG
	time.Sleep(500 * time.Millisecond)

	i.SetResponseContentType("application/json", &w)

	sort := "desc"
	orderby := r.PathParam("orderby")
	if (orderby != "postdate") && (orderby != "commentcount") && (orderby != "score") {
		rest.Error(w, "Invalid Endpoint", http.StatusBadRequest)
		return
	}

	filterby := r.PathParam("filterby")
	if filterby != "" {
		filterby = strings.ToLower(filterby)
		filterby = strings.Replace(filterby, "=", ") like '", 1)
		//filterby = strings.Replace(filterby, "delete", "", 99)
		//filterby = strings.Replace(filterby, "insert", "", 99)
		//filterby = strings.Replace(filterby, "update", "", 99)
		filterby = "lower(" + filterby + string('%') + "'"
		log.Printf("************* JsonGetPosts: filter :%s:\n", filterby)

	}

	const PARAM_OFFSET = "offset"
	const PARAM_LIMIT = "limit"
	const PARAM_FILTER_BY_POSTER = "fbp"
	var Offset int64
	var Limit int64

	Offset = -1
	Limit = -1

	// set all map query strings to lowercase
	m, err := url.ParseQuery(strings.ToLower(r.URL.RawQuery))
	if err != nil {
		rest.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	// Get the query params for limit and offset (if exists)
	if m.Get(PARAM_OFFSET) != "" {
		Offset, err = strconv.ParseInt(m.Get(PARAM_OFFSET), 10, 64)
		if err != nil {
			rest.Error(w, err.Error(), http.StatusBadRequest)
			return
		}
	}
	if m.Get(PARAM_LIMIT) != "" {
		Limit, err = strconv.ParseInt(m.Get(PARAM_LIMIT), 10, 64)
		if err != nil {
			rest.Error(w, err.Error(), http.StatusBadRequest)
			return
		}
	}
	lock.RLock()
	defer lock.RUnlock()
	postlist := post.Posts{}
	postlist.JsonApiVersion = post.API_VERSION

	//	postlist.Posts := make([]post.Post, 0)
	getpostsql := "select * from " + i.Dbmap.Dialect.QuotedTableForQuery("", table_posts)

	if filterby != "" {
		println("----------")
		println(filterby)
		println(getpostsql)
		filterby = " user like \"a%\""
		getpostsql = getpostsql + " where" + filterby
		println(getpostsql)
		println("----------")
	}
	if orderby != "" {
		getpostsql = getpostsql + " order by " + orderby + " " + sort
	}

	if (Limit > -1) && (Offset > -1) {
		getpostsql = fmt.Sprintf(getpostsql+" limit %d offset %d", Limit, Offset)
	} else {
		if (Limit < 0) && (Offset > 0) {
			Limit = 999999999999999999
			getpostsql = fmt.Sprintf(getpostsql+" limit %d offset %d", Limit, Offset)
		}
	}

	//getpostsql = "select * from `posts_index_test`"
	getpostsql = "select * from " + i.Dbmap.Dialect.QuotedTableForQuery("", table_posts)

	//getpostsql = getpostsql + " where user like \"a%\""
	filterby = " user like \"a%\""
	getpostsql = getpostsql + " where " + filterby + " order by commentcount desc limit 2 offset 0"
	//getpostsql = "select * from `posts_index_test`" + " where user like \"a%\""
	println(getpostsql)

	_, err = i.Dbmap.Select(&postlist.Posts, getpostsql)
	if err != nil {
		err = errors.New(fmt.Sprintf("Getting posts from DB failed: %s", err.Error()))
		postlist.Posts = append(postlist.Posts, &post.Post{Err: err})

	}
	if i.DebugLevel > 2 {
		log.Printf("Postlist len=%d\n", len(postlist.Posts))
		log.Printf("JsonGetPosts: '%s'\n", getpostsql)
	}

	// Get and set the execution time in milliseconds
	postlist.RequestDuration = (time.Since(timeStart).Nanoseconds() / int64(time.Millisecond))

	w.WriteJson(&postlist)
}