Example #1
0
// RetrieveLocations retrieves locations.
func RetrieveLocations(c *gin.Context) ([]model.Location, bool, int, bool, bool, int, error) {
	var locations []model.Location
	var locationCount, locationPerPage int
	filterQuery := c.Request.URL.Query().Get("filter")
	locationPerPage = config.LocationPerPage
	filter := &LocationFilter{}
	whereBuffer := new(bytes.Buffer)
	whereValues := []interface{}{}
	if len(filterQuery) > 0 {
		log.Debugf("retrieve Locations filter : %s\n", filterQuery)
		json.Unmarshal([]byte(filterQuery), &filter)
		if filter.UserId > 0 {
			stringHelper.Concat(whereBuffer, "user_id = ?")
			whereValues = append(whereValues, filter.UserId)
			log.Debugf("userId : %d\n", filter.UserId)
		}
		if filter.LocationPerPage > 0 {
			locationPerPage = filter.LocationPerPage
			log.Debugf("locationPerPage : %d\n", filter.LocationPerPage)
		}
	} else {
		log.Debug("no filters found.\n")
	}
	log.Debugf("filterQuery %v.\n", filterQuery)
	log.Debugf("filter %v.\n", filter)
	whereStr := whereBuffer.String()
	db.ORM.Model(model.Location{}).Where(whereStr, whereValues...).Count(&locationCount)
	offset, currentPage, hasPrev, hasNext := pagination.Paginate(filter.CurrentPage, locationPerPage, locationCount)
	db.ORM.Limit(locationPerPage).Offset(offset).Order(config.LocationOrder).Where(whereStr, whereValues...).Find(&locations)

	return locations, canUserWrite(c), currentPage, hasPrev, hasNext, http.StatusOK, nil
}
Example #2
0
// RetrieveComments retrieves comments.
func RetrieveComments(item interface{}, currentPages ...int) ([]model.Comment, int, bool, bool, int) {
	var comments []model.Comment
	var currentPage int
	if len(currentPages) > 0 {
		currentPage = currentPages[0]
	} else {
		currentPage = 1
	}
	count := db.ORM.Model(item).Association("Comments").Count()
	offset, currentPage, hasPrev, hasNext := pagination.Paginate(currentPage, config.CommentPerPage, count)
	db.ORM.Limit(config.CommentPerPage).Order(config.CommentOrder).Offset(offset).Model(item).Association("Comments").Find(&comments)
	AssignRelatedUser(comments)
	log.Debugf("comments : %v", comments)
	return comments, currentPage, hasPrev, hasNext, count
}
Example #3
0
// RetrieveArticles retrieves articles.
func RetrieveArticles(c *gin.Context) ([]model.Article, bool, int, int, bool, bool, int, error) {
	var articles []model.Article
	var category int
	var articleCount, articlePerPage int
	filterQuery := c.Request.URL.Query().Get("filter")
	articlePerPage = config.ArticlePerPage
	filter := &ArticleFilter{}
	whereBuffer := new(bytes.Buffer)
	whereValues := []interface{}{}
	if len(filterQuery) > 0 {
		log.Debugf("retrieve Articles filter : %s\n", filterQuery)
		json.Unmarshal([]byte(filterQuery), &filter)
		if filter.UserId > 0 {
			stringHelper.Concat(whereBuffer, "user_id = ?")
			whereValues = append(whereValues, filter.UserId)
			log.Debugf("userId : %d\n", filter.UserId)
		}
		if len(filter.Categories) > 0 {
			if len(whereValues) == 1 {
				stringHelper.Concat(whereBuffer, " and ")
			}
			stringHelper.Concat(whereBuffer, "category_id = ?")
			whereValues = append(whereValues, filter.Categories[0])
			log.Debugf("categories : %d\n", filter.Categories[0])
			category = filter.Categories[0]
		}
		if filter.ArticlePerPage > 0 {
			articlePerPage = filter.ArticlePerPage
			log.Debugf("articlePerPage : %d\n", filter.ArticlePerPage)
		}
	} else {
		log.Debug("no filters found.\n")
	}
	log.Debugf("filterQuery %v.\n", filterQuery)
	log.Debugf("filter %v.\n", filter)
	whereStr := whereBuffer.String()
	log.Debugf("whereStr %s.\n", whereStr)
	log.Debugf("whereValues %v.\n", whereValues)
	db.ORM.Model(model.Article{}).Where(whereStr, whereValues...).Count(&articleCount)
	offset, currentPage, hasPrev, hasNext := pagination.Paginate(filter.CurrentPage, articlePerPage, articleCount)
	log.Debugf("currentPage, perPage, total : %d, %d, %d", filter.CurrentPage, articlePerPage, articleCount)
	log.Debugf("offset, currentPage, hasPrev, hasNext : %d, %d, %t, %t", offset, currentPage, hasPrev, hasNext)
	db.ORM.Limit(articlePerPage).Offset(offset).Order(config.ArticleOrder).Where(whereStr, whereValues...).Find(&articles)
	return articles, canUserWrite(c, category), category, currentPage, hasPrev, hasNext, http.StatusOK, nil
}
Example #4
0
// RetrieveLikings retrieves likings.
func RetrieveLikings(item interface{}, currentPages ...int) ([]model.User, int, bool, bool, int) {
	var users []model.User
	var currentPage int
	if len(currentPages) > 0 {
		currentPage = currentPages[0]
	} else {
		currentPage = 1
	}
	count := db.ORM.Model(item).Association("Likings").Count()
	offset, currentPage, hasPrev, hasNext := pagination.Paginate(currentPage, config.LikingPerPage, count)
	db.ORM.Limit(config.LikingPerPage).Order(config.LikingOrder).Offset(offset).Select(config.UserPublicFields).Model(item).Association("Likings").Find(&users)
	// var likingArr []model.PublicUser
	// for _, user := range users {
	// 	likingArr = append(likingArr, model.PublicUser{User: user})
	// }

	// log.Debugf("likings : %v", likingArr)
	return users, currentPage, hasPrev, hasNext, count
}
Example #5
0
// RetrieveLikedOnUser retrieve liked on a user.
func RetrieveLikedOnUser(c *gin.Context) ([]model.User, int, bool, bool, int, error) {
	var user model.User
	var liked []model.User
	var retrieveListForm form.RetrieveListForm
	var hasPrev, hasNext bool
	var offset, currentPage int
	userId := c.Params.ByName("id")
	// userId, err := strconv.Atoi(c.Params.ByName("id"))
	log.Debugf("Liking params : %v", c.Params)
	c.BindWith(&retrieveListForm, binding.Form)
	log.Debugf("retrieveListForm %+v\n", retrieveListForm)
	log.Debugf("offset %+d\n", offset)
	// if hasUserId := log.CheckError(err); hasUserId {
	// db.ORM.First(&user, userId)
	if db.ORM.First(&user, userId).RecordNotFound() {
		return liked, currentPage, hasPrev, hasNext, http.StatusNotFound, errors.New("User is not found.")
	}
	offset, currentPage, hasPrev, hasNext = pagination.Paginate(retrieveListForm.CurrentPage, config.LikedPerPage, user.LikedCount)
	db.ORM.Limit(config.LikingPerPage).Offset(offset).Model(&user).Association("Liked").Find(&liked)
	// }
	return liked, currentPage, hasPrev, hasNext, http.StatusOK, nil
}