diff --git a/app/module/articles/repository/articles.repository.go b/app/module/articles/repository/articles.repository.go index f193d1e..a91838a 100644 --- a/app/module/articles/repository/articles.repository.go +++ b/app/module/articles/repository/articles.repository.go @@ -37,37 +37,38 @@ func (_i *articlesRepository) GetAll(req request.ArticlesQueryRequest) (articles var count int64 query := _i.DB.DB.Model(&entity.Articles{}) - query = query.Where("is_active = ?", true) + if req.CategoryId != nil { + query = query.Joins("JOIN article_category_details acd ON acd.article_id = articles.id"). + Where("acd.category_id = ?", req.CategoryId) + } + query = query.Where("articles.is_active = ?", true) if req.Title != nil && *req.Title != "" { title := strings.ToLower(*req.Title) - query = query.Where("LOWER(title) LIKE ?", "%"+strings.ToLower(title)+"%") + query = query.Where("LOWER(articles.title) LIKE ?", "%"+strings.ToLower(title)+"%") } if req.Description != nil && *req.Description != "" { description := strings.ToLower(*req.Description) - query = query.Where("LOWER(description) LIKE ?", "%"+strings.ToLower(description)+"%") + query = query.Where("LOWER(articles.description) LIKE ?", "%"+strings.ToLower(description)+"%") } if req.Tags != nil && *req.Tags != "" { tags := strings.ToLower(*req.Tags) - query = query.Where("LOWER(tags) LIKE ?", "%"+strings.ToLower(tags)+"%") - } - if req.CategoryId != nil { - query = query.Where("category_id = ?", req.CategoryId) + query = query.Where("LOWER(articles.tags) LIKE ?", "%"+strings.ToLower(tags)+"%") } if req.TypeId != nil { - query = query.Where("type_id = ?", req.TypeId) + query = query.Where("articles.type_id = ?", req.TypeId) } if req.IsPublish != nil { - query = query.Where("is_publish = ?", req.IsPublish) + query = query.Where("articles.is_publish = ?", req.IsPublish) } if req.IsDraft != nil { - query = query.Where("is_draft = ?", req.IsDraft) + query = query.Where("articles.is_draft = ?", req.IsDraft) } if req.StatusId != nil { - query = query.Where("status_id = ?", req.StatusId) + query = query.Where("articles.status_id = ?", req.StatusId) } if req.CreatedById != nil { - query = query.Where("created_by_id = ?", req.CreatedById) + query = query.Where("articles.created_by_id = ?", req.CreatedById) } query.Count(&count) @@ -79,7 +80,7 @@ func (_i *articlesRepository) GetAll(req request.ArticlesQueryRequest) (articles query.Order(fmt.Sprintf("%s %s", req.Pagination.SortBy, direction)) } else { direction := "DESC" - sortBy := "created_at" + sortBy := "articles.created_at" query.Order(fmt.Sprintf("%s %s", sortBy, direction)) }