Skip to content

Commit

Permalink
published/draft states on articles
Browse files Browse the repository at this point in the history
  • Loading branch information
khanzadimahdi committed May 22, 2024
1 parent 2bd6bbd commit e5ec968
Show file tree
Hide file tree
Showing 10 changed files with 198 additions and 19 deletions.
2 changes: 1 addition & 1 deletion backend/application/article/getArticle/useCase.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func NewUseCase(
}

func (uc *UseCase) GetArticle(UUID string) (*GetArticleResponse, error) {
a, err := uc.articleRepository.GetOne(UUID)
a, err := uc.articleRepository.GetOnePublished(UUID)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion backend/application/article/getArticle/useCase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ type MockArticlesRepository struct {
GetOneErr error
}

func (r *MockArticlesRepository) GetOne(UUID string) (article.Article, error) {
func (r *MockArticlesRepository) GetOnePublished(UUID string) (article.Article, error) {
r.GetOneCount++

if r.GetOneErr != nil {
Expand Down
4 changes: 2 additions & 2 deletions backend/application/article/getArticles/useCase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ type MockArticlesRepository struct {
GetCountErr error
}

func (r *MockArticlesRepository) GetAll(offset uint, limit uint) ([]article.Article, error) {
func (r *MockArticlesRepository) GetAllPublished(offset uint, limit uint) ([]article.Article, error) {
r.GetAllCount++

if r.GetAllErr != nil {
Expand All @@ -108,7 +108,7 @@ func (r *MockArticlesRepository) GetAll(offset uint, limit uint) ([]article.Arti
return []article.Article{}, nil
}

func (r *MockArticlesRepository) Count() (uint, error) {
func (r *MockArticlesRepository) CountPublished() (uint, error) {
r.GetCountCount++

if r.GetAllErr != nil {
Expand Down
4 changes: 2 additions & 2 deletions backend/application/article/getArticles/usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func NewUseCase(articleRepository article.Repository) *UseCase {
}

func (uc *UseCase) GetArticles(request *Request) (*GetArticlesResponse, error) {
totalArticles, err := uc.articleRepository.Count()
totalArticles, err := uc.articleRepository.CountPublished()
if err != nil {
return nil, err
}
Expand All @@ -34,7 +34,7 @@ func (uc *UseCase) GetArticles(request *Request) (*GetArticlesResponse, error) {
totalPages++
}

a, err := uc.articleRepository.GetAll(offset, limit)
a, err := uc.articleRepository.GetAllPublished(offset, limit)
if err != nil {
return nil, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ func (uc *UseCase) CreateArticle(request Request) (*CreateArticleResponse, error
}

article := article.Article{
Cover: request.Cover,
Title: request.Title,
Excerpt: request.Excerpt,
Body: request.Body,
Cover: request.Cover,
Title: request.Title,
Excerpt: request.Excerpt,
Body: request.Body,
PublishedAt: request.PublishedAt,
Author: author.Author{
UUID: request.AuthorUUID,
},
Expand Down
11 changes: 6 additions & 5 deletions backend/application/dashboard/article/updateArticle/usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ func (uc *UseCase) UpdateArticle(request Request) (*UpdateArticleResponse, error
}

article := article.Article{
UUID: request.UUID,
Cover: request.Cover,
Title: request.Title,
Excerpt: request.Excerpt,
Body: request.Body,
UUID: request.UUID,
Cover: request.Cover,
Title: request.Title,
Excerpt: request.Excerpt,
Body: request.Body,
PublishedAt: request.PublishedAt,
Author: author.Author{
UUID: request.AuthorUUID,
},
Expand Down
2 changes: 1 addition & 1 deletion backend/application/home/useCase.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (uc *UseCase) Execute() (*Response, error) {
return nil, err
}

all, err := uc.articleRepository.GetAll(0, 3)
all, err := uc.articleRepository.GetAllPublished(0, 3)
if err != nil {
return nil, err
}
Expand Down
3 changes: 3 additions & 0 deletions backend/domain/article/article.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ type Article struct {

type Repository interface {
GetAll(offset uint, limit uint) ([]Article, error)
GetAllPublished(offset uint, limit uint) ([]Article, error)
GetOne(UUID string) (Article, error)
GetOnePublished(UUID string) (Article, error)
GetByUUIDs(UUIDs []string) ([]Article, error)
GetMostViewed(limit uint) ([]Article, error)
GetByHashtag(hashtags []string, offset uint, limit uint) ([]Article, error)
Count() (uint, error)
CountPublished() (uint, error)
Save(*Article) (string, error)
Delete(UUID string) error
IncreaseView(uuid string, inc uint) error
Expand Down
53 changes: 53 additions & 0 deletions backend/infrastructure/repository/memory/articles/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package repository
import (
"slices"
"sync"
"time"

"github.com/gofrs/uuid/v5"
"github.com/khanzadimahdi/testproject/domain"
Expand Down Expand Up @@ -47,6 +48,30 @@ func (r *ArticlesRepository) GetAll(offset uint, limit uint) ([]article.Article,
return a, nil
}

func (r *ArticlesRepository) GetAllPublished(offset uint, limit uint) ([]article.Article, error) {
var (
a []article.Article
i uint
j uint
)

r.datastore.Range(func(key, value any) bool {
if i < offset {
i++
return true
}

if article := value.(article.Article); article.PublishedAt.Before(time.Now()) {
a = append(a, article)
j++
}

return j < limit
})

return a, nil
}

func (r *ArticlesRepository) GetByUUIDs(UUIDs []string) ([]article.Article, error) {
a := make([]article.Article, 0, len(UUIDs))

Expand Down Expand Up @@ -78,6 +103,20 @@ func (r *ArticlesRepository) GetOne(UUID string) (article.Article, error) {
return a.(article.Article), nil
}

func (r *ArticlesRepository) GetOnePublished(UUID string) (article.Article, error) {
a, ok := r.datastore.Load(UUID)
if !ok {
return article.Article{}, domain.ErrNotExists
}

item := a.(article.Article)
if item.PublishedAt.After(time.Now()) {
return article.Article{}, domain.ErrNotExists
}

return item, nil
}

func (r *ArticlesRepository) Count() (uint, error) {
var c uint

Expand All @@ -90,6 +129,20 @@ func (r *ArticlesRepository) Count() (uint, error) {
return c, nil
}

func (r *ArticlesRepository) CountPublished() (uint, error) {
var c uint

r.datastore.Range(func(_, value any) bool {
if article := value.(article.Article); article.PublishedAt.Before(time.Now()) {
c++
}

return true
})

return c, nil
}

func (r *ArticlesRepository) Save(a *article.Article) (string, error) {
if len(a.UUID) == 0 {
UUID, err := uuid.NewV7()
Expand Down
127 changes: 124 additions & 3 deletions backend/infrastructure/repository/mongodb/articles/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/khanzadimahdi/testproject/domain/article"
"github.com/khanzadimahdi/testproject/domain/author"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
Expand Down Expand Up @@ -82,6 +83,59 @@ func (r *ArticlesRepository) GetAll(offset uint, limit uint) ([]article.Article,
return items, nil
}

func (r *ArticlesRepository) GetAllPublished(offset uint, limit uint) ([]article.Article, error) {
ctx, cancel := context.WithTimeout(context.Background(), queryTimeout)
defer cancel()

o := int64(offset)
l := int64(limit)
desc := bson.D{{Key: "_id", Value: -1}}

filter := bson.M{
"published_at": bson.M{
"$lte": primitive.NewDateTimeFromTime(time.Now()),
},
}

cur, err := r.collection.Find(ctx, filter, &options.FindOptions{
Skip: &o,
Limit: &l,
Sort: desc,
})

if err != nil {
return nil, err
}

defer cur.Close(ctx)

items := make([]article.Article, 0, limit)
for cur.Next(ctx) {
var a ArticleBson

if err := cur.Decode(&a); err != nil {
return nil, err
}
items = append(items, article.Article{
UUID: a.UUID,
Cover: a.Cover,
Title: a.Title,
Excerpt: a.Excerpt,
Tags: a.Tags,
PublishedAt: a.PublishedAt,
Author: author.Author{
UUID: a.AuthorUUID,
},
})
}

if err := cur.Err(); err != nil {
return nil, err
}

return items, nil
}

func (r *ArticlesRepository) GetByUUIDs(UUIDs []string) ([]article.Article, error) {
ctx, cancel := context.WithTimeout(context.Background(), queryTimeout)
defer cancel()
Expand Down Expand Up @@ -134,7 +188,13 @@ func (r *ArticlesRepository) GetMostViewed(limit uint) ([]article.Article, error
l := int64(limit)
desc := bson.D{{Key: "view_count", Value: -1}}

cur, err := r.collection.Find(ctx, bson.D{}, &options.FindOptions{
filter := bson.M{
"published_at": bson.M{
"$lte": primitive.NewDateTimeFromTime(time.Now()),
},
}

cur, err := r.collection.Find(ctx, filter, &options.FindOptions{
Limit: &l,
Sort: desc,
})
Expand Down Expand Up @@ -180,7 +240,14 @@ func (r *ArticlesRepository) GetByHashtag(hashtags []string, offset uint, limit
l := int64(limit)
desc := bson.D{{Key: "_id", Value: -1}}

filter := bson.M{"tags": bson.M{"$in": hashtags}}
filter := bson.M{
"tags": bson.M{
"$in": hashtags,
},
"published_at": bson.M{
"$lte": primitive.NewDateTimeFromTime(time.Now()),
},
}

cur, err := r.collection.Find(ctx, filter, &options.FindOptions{
Skip: &o,
Expand Down Expand Up @@ -225,8 +292,44 @@ func (r *ArticlesRepository) GetOne(UUID string) (article.Article, error) {
ctx, cancel := context.WithTimeout(context.Background(), queryTimeout)
defer cancel()

filter := bson.D{{Key: "_id", Value: UUID}}

var a ArticleBson
if err := r.collection.FindOne(ctx, bson.D{{Key: "_id", Value: UUID}}, nil).Decode(&a); err != nil {
if err := r.collection.FindOne(ctx, filter, nil).Decode(&a); err != nil {
if errors.Is(err, mongo.ErrNoDocuments) {
err = domain.ErrNotExists
}
return article.Article{}, err
}

return article.Article{
UUID: a.UUID,
Cover: a.Cover,
Title: a.Title,
Excerpt: a.Excerpt,
Body: a.Body,
PublishedAt: a.PublishedAt,
Author: author.Author{
UUID: a.AuthorUUID,
},
Tags: a.Tags,
ViewCount: a.ViewCount,
}, nil
}

func (r *ArticlesRepository) GetOnePublished(UUID string) (article.Article, error) {
ctx, cancel := context.WithTimeout(context.Background(), queryTimeout)
defer cancel()

filter := bson.M{
"_id": UUID,
"published_at": bson.M{
"$lte": primitive.NewDateTimeFromTime(time.Now()),
},
}

var a ArticleBson
if err := r.collection.FindOne(ctx, filter, nil).Decode(&a); err != nil {
if errors.Is(err, mongo.ErrNoDocuments) {
err = domain.ErrNotExists
}
Expand Down Expand Up @@ -260,6 +363,24 @@ func (r *ArticlesRepository) Count() (uint, error) {
return uint(c), nil
}

func (r *ArticlesRepository) CountPublished() (uint, error) {
ctx, cancel := context.WithTimeout(context.Background(), queryTimeout)
defer cancel()

filter := bson.M{
"published_at": bson.M{
"$lte": primitive.NewDateTimeFromTime(time.Now()),
},
}

c, err := r.collection.CountDocuments(ctx, filter, nil)
if err != nil {
return uint(c), err
}

return uint(c), nil
}

func (r *ArticlesRepository) Save(a *article.Article) (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), queryTimeout)
defer cancel()
Expand Down

0 comments on commit e5ec968

Please sign in to comment.