github.com/k0marov/go-socnet@v0.0.0-20220715154813-90d07867c782/features/comments/store/store.go (about)

     1  package store
     2  
     3  import (
     4  	"github.com/k0marov/go-socnet/core/abstract/likeable"
     5  	"github.com/k0marov/go-socnet/core/general/core_err"
     6  	"github.com/k0marov/go-socnet/core/general/core_values"
     7  	"time"
     8  
     9  	"github.com/k0marov/go-socnet/features/comments/domain/entities"
    10  	"github.com/k0marov/go-socnet/features/comments/domain/models"
    11  	"github.com/k0marov/go-socnet/features/comments/domain/store"
    12  	"github.com/k0marov/go-socnet/features/comments/domain/values"
    13  	post_values "github.com/k0marov/go-socnet/features/posts/domain/values"
    14  )
    15  
    16  type (
    17  	DBCommentsGetter func(post post_values.PostId) ([]models.CommentModel, error)
    18  	DBAuthorGetter   func(post post_values.PostId) (core_values.UserId, error)
    19  	DBCommentCreator func(newComment values.NewCommentValue, createdAt time.Time) (values.CommentId, error)
    20  )
    21  
    22  func NewCommentsGetter(getComments DBCommentsGetter, getLikes likeable.LikesCountGetter) store.CommentsGetter {
    23  	return func(post post_values.PostId) (comments []entities.Comment, error error) {
    24  		commentModels, err := getComments(post)
    25  		if err != nil {
    26  			return []entities.Comment{}, core_err.Rethrow("getting post comments from db", err)
    27  		}
    28  		for _, model := range commentModels {
    29  			likes, err := getLikes(model.Id)
    30  			if err != nil {
    31  				return []entities.Comment{}, core_err.Rethrow("getting likes count for comment", err)
    32  			}
    33  			comment := entities.Comment{
    34  				CommentModel: model,
    35  				Likes:        likes,
    36  			}
    37  			comments = append(comments, comment)
    38  		}
    39  		return
    40  	}
    41  }
    42  
    43  func NewCommentCreator(createComment DBCommentCreator) store.Creator {
    44  	return store.Creator(createComment)
    45  }