|
|
- package Database
-
- import (
- "fmt"
-
- "git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Models"
-
- "gorm.io/gorm"
- "gorm.io/gorm/clause"
- )
-
- func GetFrontPagePosts() ([]Models.Post, error) {
- var (
- posts []Models.Post
- err error
- )
-
- err = DB.Where("front_page = ?", true).
- Where("published_at IS NOT NULL").
- Order("order asc").
- Find(&posts).
- Error
-
- return posts, err
- }
-
- func GetPostsPublic(page, pageSize int, search string) ([]Models.Post, error) {
- var (
- posts []Models.Post
- query *gorm.DB
- offset int
- err error
- )
-
- switch {
- case pageSize > 100:
- pageSize = 100
- case pageSize <= 0:
- pageSize = 10
- }
-
- offset = page * pageSize
-
- search = fmt.Sprintf("%%%s%%", search)
-
- query = DB.Model(Models.Post{}).
- Preload(clause.Associations).
- Where("published_at IS NOT NULL").
- Offset(offset).
- Limit(pageSize).
- Order("created_at desc")
-
- if search != "%%" {
- query = query.
- Where("title LIKE ?", search).
- Or("content LIKE ?", search)
- }
-
- err = query.
- Find(&posts).
- Error
-
- return posts, err
- }
-
- func GetPostByIdPublic(id string) (Models.Post, error) {
- var (
- postData Models.Post
- err error
- )
-
- err = DB.Preload(clause.Associations).
- Where("published_at IS NOT NULL").
- First(&postData, "id = ?", id).
- Error
-
- return postData, err
- }
|