// GetBoardPostsByID returns the filtered posts for a board by the ID of the board func (ps *PostService) GetBoardPostsByID(boardID, page int) (posts []*model.Post, err error) { var rows *sqlx.Rows rows, err = ps.db.Queryx(queries.Get("get_board_posts_by_id"), boardID, page*50) if err != nil { return } for rows.Next() { var post model.Post err = rows.StructScan(&post) if err != nil { return } posts = append(posts, &post) } return }
// GetComment returns a comment along with its full tree func (cs *CommentService) GetComment(commentID int) (c *model.Comment, err error) { row := cs.db.QueryRowx(queries.Get("get_comment_by_id"), commentID) var scanC model.Comment err = row.StructScan(&scanC) if err != nil { return } c = &scanC return }
// GetUserByUsername returns a user by their username func (us *UserService) GetUserByUsername(username string) (u *model.User, err error) { row := us.db.QueryRowx(queries.Get("get_user_by_username"), username) var scanUser model.User err = row.StructScan(&scanUser) if err != nil { return } u = &scanUser return }
// GetUserByID returns a user by their id func (us *UserService) GetUserByID(id int) (u *model.User, err error) { row := us.db.QueryRowx(queries.Get("get_user_by_id"), id) var scanUser model.User err = row.StructScan(&scanUser) if err != nil { return } u = &scanUser return }
// GetPost returns a single post, without comments, by ID func (ps *PostService) GetPost(postID int) (post *model.Post, err error) { row := ps.db.QueryRowx(queries.Get("get_post_by_id"), postID) var scanPost model.Post err = row.StructScan(&scanPost) if err != nil { return } post = &scanPost return }
// GetUserPosts returns all posts owned by a certain user func (ps *PostService) GetUserPosts(userID, page int) (posts []*model.Post, err error) { var rows *sqlx.Rows rows, err = ps.db.Queryx(queries.Get("get_user_posts"), userID, page*50) if err != nil { return } for rows.Next() { var post model.Post err = rows.StructScan(&post) if err != nil { return } posts = append(posts, &post) } return }
// GetPostComments returns all root level comments along with their children for a Post` func (cs *CommentService) GetPostComments(postID int) (comments []*model.Comment, err error) { var rows *sqlx.Rows rows, err = cs.db.Queryx(queries.Get("get_post_comments"), postID) if err != nil { return } var pgcr []types.PGComment for rows.Next() { var pgc types.PGComment err = rows.StructScan(&pgc) if err != nil { return } pgcr = append(pgcr, pgc) } comments = types.AssembleCommentTree(pgcr) return }
// GetUserComments returns all of a users past comments func (cs *CommentService) GetUserComments(userID int) (comments []*model.Comment, err error) { var rows *sqlx.Rows rows, err = cs.db.Queryx(queries.Get("get_user_comments"), userID) if err != nil { return } var pgcr []types.PGComment for rows.Next() { var pgc types.PGComment err = rows.StructScan(&pgc) if err != nil { return } log.Printf("%+v", pgc) pgcr = append(pgcr, pgc) } comments = types.AssembleCommentTree(pgcr) return }
// CreateComment creates a comment on another comment func (cs *CommentService) CreateComment(c *model.Comment) (err error) { _, err = cs.db.NamedQuery(queries.Get("insert_comment"), c) return }
// UpdateUser updates a database user by ID func (us *UserService) UpdateUser(u *model.User) (err error) { _, err = us.db.NamedQuery(queries.Get("update_user"), u) return }
// CreatePost adds a new post to the database func (ps *PostService) CreatePost(p *model.Post) (err error) { _, err = ps.db.NamedQuery(queries.Get("insert_post"), p) return }