-
-
Notifications
You must be signed in to change notification settings - Fork 0
[feature] Get Posts/Post #59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
796070f
start working on query
gocanto 72f529d
fix query
gocanto a2c4ecb
create transformer
gocanto 2b47a17
map response
gocanto 025b73b
map posts
gocanto b2ae80d
extract filter logic
gocanto 6ae6566
format
gocanto 15c1a3b
extract Pagination
gocanto 1b5af9e
use URL query
gocanto 879d719
extract pagination attr
gocanto af6a663
split extraction
gocanto 971b2e1
work on filters
gocanto 7569139
format
gocanto 59f5245
tweaks
gocanto 14f18ea
fix query + naming
gocanto 3cfe3d7
format
gocanto f7c7e7d
add show endpoint
gocanto ffa800e
implement filtering
gocanto dd68891
validations + format
gocanto 9a63dae
defear closer
gocanto 021dc82
close with logs instead
gocanto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package pagination | ||
|
|
||
| type Paginate struct { | ||
| Page int | ||
| Limit int | ||
| NumItems int64 | ||
| } | ||
|
|
||
| func (a *Paginate) SetNumItems(number int64) { | ||
| a.NumItems = number | ||
| } | ||
|
|
||
| func (a *Paginate) GetNumItemsAsInt() int64 { | ||
| return a.NumItems | ||
| } | ||
|
|
||
| func (a *Paginate) GetNumItemsAsFloat() float64 { | ||
| return float64(a.NumItems) | ||
| } | ||
|
|
||
| func (a *Paginate) GetLimit() int { | ||
| return a.Limit | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| package pagination | ||
|
|
||
| import "math" | ||
|
|
||
| const MinPage = 1 | ||
| const MaxLimit = 100 | ||
|
|
||
| // Pagination holds the data for a single page along with all pagination metadata. | ||
| // It's generic and can be used for any data type. | ||
| // | ||
| // NextPage and PreviousPage are pointers (*int) so they can be nil (and omitted from JSON output) | ||
| // when there isn't a next or previous page. | ||
| type Pagination[T any] struct { | ||
| Data []T `json:"data"` | ||
| Page int `json:"page"` | ||
| Total int64 `json:"total"` | ||
| PageSize int `json:"page_size"` | ||
| TotalPages int `json:"total_pages"` | ||
| NextPage *int `json:"next_page,omitempty"` | ||
| PreviousPage *int `json:"previous_page,omitempty"` | ||
| } | ||
|
|
||
| func MakePagination[T any](data []T, paginate Paginate) *Pagination[T] { | ||
| pSize := float64(paginate.Limit) | ||
| if pSize <= 0 { | ||
| pSize = 10 | ||
| } | ||
|
|
||
| totalPages := int( | ||
| math.Ceil(paginate.GetNumItemsAsFloat() / pSize), | ||
| ) | ||
|
|
||
| pagination := Pagination[T]{ | ||
| Data: data, | ||
| Page: paginate.Page, | ||
| Total: paginate.GetNumItemsAsInt(), | ||
| PageSize: paginate.Limit, | ||
| TotalPages: totalPages, | ||
| NextPage: nil, | ||
| PreviousPage: nil, | ||
| } | ||
|
|
||
| var nextPage *int | ||
| if pagination.Page < pagination.TotalPages { | ||
| p := pagination.Page + 1 | ||
| nextPage = &p | ||
| } | ||
|
|
||
| var prevPage *int | ||
| if pagination.Page > 1 && pagination.Page <= pagination.TotalPages { | ||
| p := pagination.Page - 1 | ||
| prevPage = &p | ||
| } | ||
|
|
||
| pagination.NextPage = nextPage | ||
| pagination.PreviousPage = prevPage | ||
|
|
||
| return &pagination | ||
| } | ||
|
|
||
| // HydratePagination transforms a paginated result containing items of a source type (S) | ||
| // into a new result containing items of a destination type (D). | ||
| // | ||
| // It takes a source Pagination and a mapper function that defines the conversion | ||
| // logic from an item of type S to an item of type D. | ||
| // | ||
| // Type Parameters: | ||
| // - S: The source type (e.g., a database model like database.Post). | ||
| // - D: The destination type (e.g., an API response DTO like PostResponse). | ||
| // | ||
| // The function returns a new Pagination with the transformed data, while preserving | ||
| // all original pagination metadata (Total, CurrentPage, etc.). | ||
| func HydratePagination[S any, D any](source *Pagination[S], mapper func(S) D) *Pagination[D] { | ||
| mappedData := make([]D, len(source.Data)) | ||
|
|
||
| // Iterate over the source data and apply the mapper function | ||
| for i, item := range source.Data { | ||
| mappedData[i] = mapper(item) | ||
| } | ||
|
|
||
| return &Pagination[D]{ | ||
| Data: mappedData, | ||
| Total: source.Total, | ||
| Page: source.Page, | ||
| PageSize: source.PageSize, | ||
| TotalPages: source.TotalPages, | ||
| NextPage: source.NextPage, | ||
| PreviousPage: source.PreviousPage, | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| package queries | ||
|
|
||
| import ( | ||
| "gorm.io/gorm" | ||
| ) | ||
|
|
||
| // ApplyPostsFilters The given query master table is "posts" | ||
| func ApplyPostsFilters(filters *PostFilters, query *gorm.DB) { | ||
| if filters == nil { | ||
| return | ||
| } | ||
|
|
||
| if filters.GetTitle() != "" { | ||
| query.Where("LOWER(posts.title) ILIKE ?", "%"+filters.GetTitle()+"%") | ||
| } | ||
|
|
||
| if filters.GetText() != "" { | ||
| query. | ||
| Where("LOWER(posts.slug) ILIKE ? OR LOWER(posts.excerpt) ILIKE ? OR LOWER(posts.content) ILIKE ?", | ||
| "%"+filters.GetText()+"%", | ||
| "%"+filters.GetText()+"%", | ||
| "%"+filters.GetText()+"%", | ||
| ) | ||
| } | ||
|
|
||
| if filters.GetAuthor() != "" { | ||
| query. | ||
| Joins("JOIN users ON posts.author_id = users.id"). | ||
| Where("users.deleted_at IS NULL"). | ||
| Where("("+ | ||
| "LOWER(users.bio) ILIKE ? OR LOWER(users.first_name) LIKE ? OR LOWER(users.last_name) LIKE ? OR LOWER(users.display_name) ILIKE ?"+ | ||
| ")", | ||
gocanto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "%"+filters.GetAuthor()+"%", | ||
| "%"+filters.GetAuthor()+"%", | ||
| "%"+filters.GetAuthor()+"%", | ||
| "%"+filters.GetAuthor()+"%", | ||
| ) | ||
| } | ||
|
|
||
| if filters.GetCategory() != "" { | ||
| query. | ||
| Joins("JOIN post_categories ON post_categories.post_id = posts.id"). | ||
| Joins("JOIN categories ON categories.id = post_categories.category_id"). | ||
| Where("categories.deleted_at IS NULL"). | ||
| Where("("+ | ||
| "LOWER(categories.slug) ILIKE ? OR LOWER(categories.name) ILIKE ? OR LOWER(categories.description) ILIKE ?"+ | ||
| ")", | ||
| "%"+filters.GetCategory()+"%", | ||
| "%"+filters.GetCategory()+"%", | ||
| "%"+filters.GetCategory()+"%", | ||
| ) | ||
| } | ||
|
|
||
| if filters.GetTag() != "" { | ||
| query. | ||
| Joins("JOIN post_tags ON post_tags.post_id = posts.id"). | ||
| Joins("JOIN tags ON tags.id = post_tags.tag_id"). | ||
| Where("tags.deleted_at IS NULL"). | ||
| Where("("+ | ||
| "LOWER(tags.slug) ILIKE ? OR LOWER(tags.name) ILIKE ? OR LOWER(tags.description) ILIKE ?"+ | ||
| ")", | ||
| "%"+filters.GetTag()+"%", | ||
| "%"+filters.GetTag()+"%", | ||
| "%"+filters.GetTag()+"%", | ||
| ) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package queries | ||
|
|
||
| import ( | ||
| "github.com/oullin/pkg" | ||
| "strings" | ||
| ) | ||
|
|
||
| type PostFilters struct { | ||
| Text string | ||
| Title string // Will perform a case-insensitive partial match | ||
| Author string | ||
| Category string | ||
| Tag string | ||
| } | ||
|
|
||
| func (f PostFilters) GetText() string { | ||
| return f.sanitiseString(f.Text) | ||
| } | ||
|
|
||
| func (f PostFilters) GetTitle() string { | ||
| return f.sanitiseString(f.Title) | ||
| } | ||
|
|
||
| func (f PostFilters) GetAuthor() string { | ||
| return f.sanitiseString(f.Author) | ||
| } | ||
|
|
||
| func (f PostFilters) GetCategory() string { | ||
| return f.sanitiseString(f.Category) | ||
| } | ||
|
|
||
| func (f PostFilters) GetTag() string { | ||
| return f.sanitiseString(f.Tag) | ||
| } | ||
|
|
||
| func (f PostFilters) sanitiseString(seed string) string { | ||
| str := pkg.MakeStringable(seed) | ||
|
|
||
| return strings.TrimSpace(str.ToLower()) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.