Skip to content

Commit

Permalink
Merge pull request #75 from factly/feat/details_with_slug
Browse files Browse the repository at this point in the history
Fetch details with slug param
  • Loading branch information
deshetti authored Jun 14, 2021
2 parents 411fb98 + 84f7f60 commit afc561b
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 84 deletions.
101 changes: 73 additions & 28 deletions graph/generated/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 17 additions & 7 deletions graph/resolvers/category.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package resolvers

import (
"context"
"errors"
"fmt"

"gorm.io/gorm"
Expand Down Expand Up @@ -48,19 +49,28 @@ func (r *categoryResolver) MetaFields(ctx context.Context, obj *models.Category)
return obj.MetaFields, nil
}

func (r *queryResolver) Category(ctx context.Context, id int) (*models.Category, error) {
func (r *queryResolver) Category(ctx context.Context, id *int, slug *string) (*models.Category, error) {
sID, err := validator.GetSpace(ctx)
if err != nil {
return nil, err
}

result := &models.Category{}

err = config.DB.Model(&models.Category{}).Where(&models.Category{
ID: uint(id),
SpaceID: sID,
}).Preload("Medium").First(&result).Error
if id == nil && slug == nil {
return nil, errors.New("please provide either id or slug")
}

result := &models.Category{}
if id != nil {
err = config.DB.Model(&models.Category{}).Where(&models.Category{
ID: uint(*id),
SpaceID: sID,
}).Preload("Medium").First(&result).Error
} else {
err = config.DB.Model(&models.Category{}).Where(&models.Category{
Slug: *slug,
SpaceID: sID,
}).Preload("Medium").First(&result).Error
}
if err != nil {
return nil, nil
}
Expand Down
22 changes: 17 additions & 5 deletions graph/resolvers/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package resolvers

import (
"context"
"errors"

"github.com/factly/dega-api/config"
"github.com/factly/dega-api/graph/models"
Expand All @@ -10,18 +11,29 @@ import (
"github.com/factly/dega-api/util/cache"
)

func (r *queryResolver) Page(ctx context.Context, id int) (*models.Post, error) {
func (r *queryResolver) Page(ctx context.Context, id *int, slug *string) (*models.Post, error) {
sID, err := validator.GetSpace(ctx)
if err != nil {
return nil, err
}

if id == nil && slug == nil {
return nil, errors.New("please provide either id or slug")
}

result := &models.Post{}

err = config.DB.Model(&models.Post{}).Where(&models.Post{
ID: uint(id),
SpaceID: sID,
}).Where("is_page = ?", true).First(&result).Error
if id != nil {
err = config.DB.Model(&models.Post{}).Where(&models.Post{
ID: uint(*id),
SpaceID: sID,
}).Where("is_page = ?", true).First(&result).Error
} else {
err = config.DB.Model(&models.Post{}).Where(&models.Post{
Slug: *slug,
SpaceID: sID,
}).Where("is_page = ?", true).First(&result).Error
}

if err != nil {
return nil, nil
Expand Down
28 changes: 23 additions & 5 deletions graph/resolvers/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package resolvers
import (
"context"
"encoding/json"
"errors"
"fmt"
"strings"
"time"
Expand Down Expand Up @@ -222,18 +223,35 @@ type redisPost struct {
Users []*models.User `json:"users,omitempty"`
}

func (r *queryResolver) Post(ctx context.Context, id int) (*models.Post, error) {
func (r *queryResolver) Post(ctx context.Context, id *int, slug *string, include_page *bool) (*models.Post, error) {
sID, err := validator.GetSpace(ctx)
if err != nil {
return nil, err
}

if id == nil && slug == nil {
return nil, errors.New("please provide either id or slug")
}

result := &models.Post{}
tx := config.DB.Model(&models.Post{})
if id != nil {
tx.Where(&models.Post{
ID: uint(*id),
SpaceID: sID,
})
} else {
tx.Where(&models.Post{
Slug: *slug,
SpaceID: sID,
})
}

err = config.DB.Model(&models.Post{}).Where(&models.Post{
ID: uint(id),
SpaceID: sID,
}).Where("is_page = ?", false).First(&result).Error
if include_page == nil || !*include_page {
err = tx.Where("is_page = ?", false).First(&result).Error
} else {
err = tx.First(&result).Error
}

if err != nil {
return nil, nil
Expand Down
Loading

0 comments on commit afc561b

Please sign in to comment.