Skip to content

Commit

Permalink
update public api
Browse files Browse the repository at this point in the history
  • Loading branch information
shreeharsha-factly committed Jul 8, 2024
1 parent 70c6a88 commit e664029
Show file tree
Hide file tree
Showing 23 changed files with 52 additions and 47 deletions.
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ services:
- 7789:8000
- 7788:8001
- 7787:8002
- 7786:8004
build:
context: ./server
dockerfile: Dockerfile.dev
Expand Down
5 changes: 5 additions & 0 deletions server/cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ var serveCmd = &cobra.Command{
}()
}

go func() {
r := service.RegisterPublicRoutes()
log.Fatal(http.ListenAndServe(":8004", r))
}()

if err := http.ListenAndServe(":8000", r); err != nil {
log.Fatal(err)
}
Expand Down
1 change: 0 additions & 1 deletion server/config/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ type requestBody struct {

func CachingMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

body := requestBody{}
spaceId := r.Header.Get("x-space")
bodyBytes, _ := io.ReadAll(r.Body)
Expand Down
2 changes: 1 addition & 1 deletion server/service/core/action/page/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func create(w http.ResponseWriter, r *http.Request) {
tx.Model(&model.Post{}).Preload("Medium").Preload("Format").Preload("Tags").Preload("Categories").First(&result.Post)

// fetch all authors
authors, err := util.GetAuthors(authCtx.OrganisationID, page.AuthorIDs)
authors, err := util.GetAuthors(r.Header.Get("Authorization"), authCtx.OrganisationID, page.AuthorIDs)

if err != nil {
loggerx.Error(err)
Expand Down
2 changes: 1 addition & 1 deletion server/service/core/action/page/details.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func details(w http.ResponseWriter, r *http.Request) {
}

// Adding author
authors, err := util.GetAuthors(authCtx.OrganisationID, authorIds)
authors, err := util.GetAuthors(r.Header.Get("Authorization"), authCtx.OrganisationID, authorIds)

if err != nil {
loggerx.Error(err)
Expand Down
2 changes: 1 addition & 1 deletion server/service/core/action/page/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func update(w http.ResponseWriter, r *http.Request) {
}

// fetch all authors
authors, err := util.GetAuthors(authCtx.OrganisationID, page.AuthorIDs)
authors, err := util.GetAuthors(r.Header.Get("Authorization"), authCtx.OrganisationID, page.AuthorIDs)
if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.InternalServerError()))
Expand Down
3 changes: 2 additions & 1 deletion server/service/core/action/post/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ func createPost(ctx context.Context, post post, status string, r *http.Request)
DescriptionAMP: post.DescriptionAMP,
MigratedHTML: post.MigratedHTML,
Language: post.Language,
CustomFormat: post.CustomFormat,
}

if post.MigrationID != nil {
Expand Down Expand Up @@ -234,7 +235,7 @@ func createPost(ctx context.Context, post post, status string, r *http.Request)
}
}

authors, err := util.GetAuthors(authCtx.OrganisationID, post.AuthorIDs)
authors, err := util.GetAuthors(r.Header.Get("Authorization"), authCtx.OrganisationID, post.AuthorIDs)
if err != nil {
loggerx.Error(err)
return nil, errorx.InternalServerError()
Expand Down
2 changes: 1 addition & 1 deletion server/service/core/action/post/details.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func details(w http.ResponseWriter, r *http.Request) {
}

// Adding author
authors, err := util.GetAuthors(authCtx.OrganisationID, authorIDs)
authors, err := util.GetAuthors(r.Header.Get("Authorization"), authCtx.OrganisationID, authorIDs)
if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.InternalServerError()))
Expand Down
3 changes: 1 addition & 2 deletions server/service/core/action/post/feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/factly/dega-server/config"
"github.com/factly/dega-server/service/core/model"
"github.com/factly/dega-server/util"
"github.com/factly/x/errorx"
"github.com/factly/x/loggerx"
"github.com/factly/x/paginationx"
Expand Down Expand Up @@ -97,7 +96,7 @@ func GetItemsList(postList []model.Post, space model.Space) []*feeds.Item {
authorIDs = append(authorIDs, po.AuthorID)
}
}
authorMap, _ := util.GetAuthors("", authorIDs)
authorMap := make(map[string]model.Author)

// generate post author map
postAuthorMap := make(map[uuid.UUID][]string)
Expand Down
28 changes: 14 additions & 14 deletions server/service/core/action/post/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func list(w http.ResponseWriter, r *http.Request) {
}

// fetch all authors
authors, err := util.GetAuthors(authCtx.OrganisationID, authorIDs)
authors, err := util.GetAuthors(r.Header.Get("Authorization"), authCtx.OrganisationID, authorIDs)

if err != nil {
loggerx.Error(err)
Expand Down Expand Up @@ -333,26 +333,26 @@ func PublicList(w http.ResponseWriter, r *http.Request) {

posts := make([]model.Post, 0)

order := "posts." + pageSortBy + " " + pageSortOrder
order := "de_post." + pageSortBy + " " + pageSortOrder

tx := config.DB.Model(&model.Post{})

tx.Preload("Tags").
Preload("Categories").
Preload("Medium").
Preload("Format").
Joins("JOIN post_tag ON post_tag.post_id = posts.id").
Joins("JOIN post_category ON post_category.post_id = posts.id").
Joins("JOIN post_author ON post_author.post_id = posts.id").
Where("posts.space_id = ?", authCtx.SpaceID).
Where("post_tag.tag_id IN ?", tagUUIDs).
Where("post_category.category_id IN ?", categoryUUIDs).
Where("post_author.author_id IN ?", authorIds).
Where("posts.format_id IN ?", formatUUIDs).
Where("posts.id IN ?", uuids).
Where("posts.is_page = ?", isPageBool).
Where("posts.status = ?", status).
Where("posts.is_featured = ?", isFeaturedBool).
Joins("JOIN de_post_tags ON de_post_tags.post_id = de_post.id").
Joins("JOIN de_post_categories ON de_post_categories.post_id = de_post.id").
Joins("JOIN de_post_author ON de_post_author.post_id = de_post.id").
Where("de_post.space_id = ?", authCtx.SpaceID).
Where("de_post_tags.tag_id IN ?", tagUUIDs).
Where("de_post_categories.category_id IN ?", categoryUUIDs).
Where("de_post_author.author_id IN ?", authorIds).
Where("de_post.format_id IN ?", formatUUIDs).
Where("de_post.id IN ?", uuids).
Where("de_post.is_page = ?", isPageBool).
Where("de_post.status = ?", status).
Where("de_post.is_featured = ?", isFeaturedBool).
Count(&result.Total).
Limit(limit).
Offset(offset).
Expand Down
1 change: 1 addition & 0 deletions server/service/core/action/post/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type post struct {
TagIDs []uuid.UUID `json:"tag_ids"`
ClaimIDs []uuid.UUID `json:"claim_ids"`
AuthorIDs []string `json:"author_ids"`
CustomFormat string `json:"custom_format"`
}

type postData struct {
Expand Down
3 changes: 2 additions & 1 deletion server/service/core/action/post/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func update(w http.ResponseWriter, r *http.Request) {
result.Claims = make([]factCheckModel.Claim, 0)

// fetch all authors
authors, err := util.GetAuthors(authCtx.OrganisationID, post.AuthorIDs)
authors, err := util.GetAuthors(r.Header.Get("Authorization"), authCtx.OrganisationID, post.AuthorIDs)

if err != nil {
loggerx.Error(err)
Expand Down Expand Up @@ -189,6 +189,7 @@ func update(w http.ResponseWriter, r *http.Request) {
"description_amp": post.DescriptionAMP,
"migrated_html": post.MigratedHTML,
"language": post.Language,
"custom_format": post.CustomFormat,
}

if post.MigrationID != nil {
Expand Down
1 change: 1 addition & 0 deletions server/service/core/model/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type Post struct {
MetaFields postgres.Jsonb `gorm:"column:meta_fields" json:"meta_fields" swaggertype:"primitive,string"`
Tags []Tag `gorm:"many2many:post_tags;" json:"tags"`
Categories []Category `gorm:"many2many:post_categories;" json:"categories"`
CustomFormat string `gorm:"column:custom_format" json:"custom_format"`
}

// PostAuthor model
Expand Down
1 change: 1 addition & 0 deletions server/service/core/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func PublicRouter() http.Handler {
r.Get("/tags", tag.PublicList)
r.Get("/formats", format.List)
r.Get("/authors", author.PublicList)
r.Get("/posts", post.PublicList)

return r
}
4 changes: 1 addition & 3 deletions server/service/core/service/category.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,10 @@ func (cs CategoryService) PublicList(sID uuid.UUID, offset, limit int, searchQue

order := pageSortBy + " " + pageSortOrder

var tx *gorm.DB
tx := config.DB.Model(&model.Category{})

if len(ids) > 0 {
tx = tx.Model(&model.Category{}).Where(ids)
} else {
tx = tx.Model(&model.Category{})
}

if searchQuery != "" {
Expand Down
2 changes: 1 addition & 1 deletion server/service/core/service/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func (ts TagService) PublicList(sID uuid.UUID, offset, limit int, searchQuery, s

order := pageSortBy + " " + pageSortOrder

var tx *gorm.DB
tx := config.DB.Model(&model.Tag{})

if len(ids) > 0 {
tx = tx.Model(&model.Tag{}).Where(ids)
Expand Down
2 changes: 1 addition & 1 deletion server/service/podcast/action/episode/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func create(w http.ResponseWriter, r *http.Request) {

episodeService := service.GetEpisodeService()

result, serviceErr := episodeService.Create(r.Context(), authCtx.SpaceID, authCtx.UserID, authCtx.OrganisationID, episode)
result, serviceErr := episodeService.Create(r.Context(), authCtx.SpaceID, authCtx.UserID, authCtx.OrganisationID, r.Header.Get("Authorization"), episode)
if serviceErr != nil {
errorx.Render(w, serviceErr)
return
Expand Down
2 changes: 1 addition & 1 deletion server/service/podcast/action/episode/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func delete(w http.ResponseWriter, r *http.Request) {

episodeService := service.GetEpisodeService()

result, serviceErr := episodeService.GetById(r.Context(), authCtx.SpaceID, id, authCtx.OrganisationID)
result, serviceErr := episodeService.GetById(r.Context(), authCtx.SpaceID, id, authCtx.OrganisationID, r.Header.Get("Authorization"))

if serviceErr != nil {
errorx.Render(w, serviceErr)
Expand Down
2 changes: 1 addition & 1 deletion server/service/podcast/action/episode/details.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func details(w http.ResponseWriter, r *http.Request) {

episodeService := service.GetEpisodeService()

result, serviceErr := episodeService.GetById(r.Context(), authCtx.SpaceID, id, authCtx.OrganisationID)
result, serviceErr := episodeService.GetById(r.Context(), authCtx.SpaceID, id, authCtx.OrganisationID, r.Header.Get("Authorization"))
if serviceErr != nil {
errorx.Render(w, serviceErr)
return
Expand Down
2 changes: 1 addition & 1 deletion server/service/podcast/action/episode/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func update(w http.ResponseWriter, r *http.Request) {
}
episodeService := service.GetEpisodeService()

result, serviceErr := episodeService.Update(r.Context(), authCtx.SpaceID, id, authCtx.UserID, authCtx.OrganisationID, episode)
result, serviceErr := episodeService.Update(r.Context(), authCtx.SpaceID, id, authCtx.UserID, authCtx.OrganisationID, r.Header.Get("Authorization"), episode)
if serviceErr != nil {
errorx.Render(w, serviceErr)
return
Expand Down
18 changes: 9 additions & 9 deletions server/service/podcast/service/episode.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ type episodePaging struct {
}

type IEpisodeService interface {
GetById(ctx context.Context, sID, id uuid.UUID, oID string) (model.Episode, []errorx.Message)
GetById(ctx context.Context, sID, id uuid.UUID, oID, token string) (model.Episode, []errorx.Message)
List(ctx context.Context, sID uuid.UUID, offset, limit int, searchQuery, sort string, queryMap url.Values) (episodePaging, []errorx.Message)
Create(ctx context.Context, sID uuid.UUID, uID, oID string, episode *Episode) (EpisodeData, []errorx.Message)
Update(ctx context.Context, sID, id uuid.UUID, uID, oID string, episode *Episode) (EpisodeData, []errorx.Message)
Create(ctx context.Context, sID uuid.UUID, uID, oID, token string, episode *Episode) (EpisodeData, []errorx.Message)
Update(ctx context.Context, sID, id uuid.UUID, uID, oID, token string, episode *Episode) (EpisodeData, []errorx.Message)
Delete(sID, id uuid.UUID) []errorx.Message
}

Expand All @@ -65,7 +65,7 @@ type episodeService struct {
}

// Create implements IEpisodeService
func (es *episodeService) Create(ctx context.Context, sID uuid.UUID, uID, oID string, episode *Episode) (EpisodeData, []errorx.Message) {
func (es *episodeService) Create(ctx context.Context, sID uuid.UUID, uID, oID, token string, episode *Episode) (EpisodeData, []errorx.Message) {

validationError := validationx.Check(episode)

Expand Down Expand Up @@ -141,7 +141,7 @@ func (es *episodeService) Create(ctx context.Context, sID uuid.UUID, uID, oID st
}

if len(episode.AuthorIDs) > 0 {
authorMap, err := util.GetAuthors(oID, episode.AuthorIDs)
authorMap, err := util.GetAuthors(token, oID, episode.AuthorIDs)
if err != nil {
tx.Rollback()
loggerx.Error(err)
Expand Down Expand Up @@ -195,7 +195,7 @@ func (es *episodeService) Delete(sID, id uuid.UUID) []errorx.Message {
}

// GetById implements IEpisodeService
func (es *episodeService) GetById(ctx context.Context, sID, id uuid.UUID, oId string) (model.Episode, []errorx.Message) {
func (es *episodeService) GetById(ctx context.Context, sID, id uuid.UUID, oId, token string) (model.Episode, []errorx.Message) {

result := EpisodeData{}

Expand All @@ -222,7 +222,7 @@ func (es *episodeService) GetById(ctx context.Context, sID, id uuid.UUID, oId st
}

// Adding authors in response
authorMap, err := util.GetAuthors(oId, authorIds)
authorMap, err := util.GetAuthors(token, oId, authorIds)
if err != nil {
loggerx.Error(err)
return result.Episode, errorx.Parser(errorx.DBError())
Expand Down Expand Up @@ -325,7 +325,7 @@ func (es *episodeService) List(ctx context.Context, sID uuid.UUID, offset, limit
}

// Update implements IEpisodeService
func (es *episodeService) Update(ctx context.Context, sID, id uuid.UUID, uID, oID string, episode *Episode) (EpisodeData, []errorx.Message) {
func (es *episodeService) Update(ctx context.Context, sID, id uuid.UUID, uID, oID, token string, episode *Episode) (EpisodeData, []errorx.Message) {

validationError := validationx.Check(episode)

Expand Down Expand Up @@ -444,7 +444,7 @@ func (es *episodeService) Update(ctx context.Context, sID, id uuid.UUID, uID, oI
}

// Fetch current authors
authorMap, err := util.GetAuthors(oID, episode.AuthorIDs)
authorMap, err := util.GetAuthors(token, oID, episode.AuthorIDs)
if err != nil {
loggerx.Error(err)
return EpisodeData{}, errorx.Parser(errorx.DBError())
Expand Down
10 changes: 4 additions & 6 deletions server/service/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,14 @@ func RegisterPublicRoutes() http.Handler {
"meilisearch": util.MeiliChecker,
})

r.Use(util.CheckAPIAcess)

if config.CacheEnabled() {
config.SetupCache()
r.Use(config.CachingMiddleware, config.RespMiddleware)
}

r.Mount("/core", core.PublicRouter())
r.Mount("/fact-check", factCheck.PublicRouter())

r.With(util.CheckAPIAcess).Group(func(r chi.Router) {
r.Mount("/core", core.PublicRouter())
r.Mount("/fact-check", factCheck.PublicRouter())
})
return r
}

Expand Down
2 changes: 1 addition & 1 deletion server/util/authors.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/spf13/viper"
)

func GetAuthors(orgID string, ids []string) (map[string]model.Author, error) {
func GetAuthors(token, orgID string, ids []string) (map[string]model.Author, error) {
zitadelUsers, err := zitadel.GetOrganisationUsers(viper.GetString("ZITADEL_PERSONAL_ACCESS_TOKEN"), orgID, ids)

if err != nil {
Expand Down

0 comments on commit e664029

Please sign in to comment.