github.com/ngocphuongnb/tetua@v0.0.7-alpha/app/mock/repository/comment.go (about)

     1  package mockrepository
     2  
     3  import (
     4  	"context"
     5  	"math"
     6  	"strings"
     7  
     8  	"github.com/ngocphuongnb/tetua/app/entities"
     9  	"github.com/ngocphuongnb/tetua/app/repositories"
    10  	"github.com/ngocphuongnb/tetua/app/utils"
    11  )
    12  
    13  type CommentRepository struct {
    14  	*Repository[entities.Comment]
    15  }
    16  
    17  func (m *CommentRepository) Find(ctx context.Context, filters ...*entities.CommentFilter) ([]*entities.Comment, error) {
    18  	if len(filters) == 0 {
    19  		return m.entities, nil
    20  	}
    21  	filter := *filters[0]
    22  	result := make([]*entities.Comment, 0)
    23  	if filter.Page < 1 {
    24  		filter.Page = 1
    25  	}
    26  	if filter.Limit < 1 {
    27  		filter.Limit = 10
    28  	}
    29  	offset := (filter.Page - 1) * filter.Limit
    30  
    31  	for index, comment := range m.entities {
    32  		if index < offset {
    33  			continue
    34  		}
    35  		if index >= offset+filter.Limit {
    36  			break
    37  		}
    38  
    39  		if filter.Search != "" {
    40  			if !strings.Contains(comment.Content, filter.Search) && !strings.Contains(comment.Content, filter.Search) {
    41  				continue
    42  			}
    43  		}
    44  
    45  		if len(filter.ExcludeIDs) > 0 && utils.SliceContains(filter.ExcludeIDs, comment.ID) {
    46  			continue
    47  		}
    48  
    49  		if len(filter.UserIDs) > 0 && !utils.SliceContains(filter.UserIDs, comment.UserID) {
    50  			continue
    51  		}
    52  
    53  		if len(filter.PostIDs) > 0 && !utils.SliceContains(filter.PostIDs, comment.UserID) {
    54  			continue
    55  		}
    56  
    57  		if len(filter.ParentIDs) > 0 && !utils.SliceContains(filter.ParentIDs, comment.UserID) {
    58  			continue
    59  		}
    60  
    61  		result = append(result, comment)
    62  	}
    63  
    64  	return result, nil
    65  }
    66  
    67  func (m *CommentRepository) FindWithPost(ctx context.Context, filters ...*entities.CommentFilter) ([]*entities.Comment, error) {
    68  	var err error
    69  	posts, err := m.Find(ctx, filters...)
    70  
    71  	if err != nil {
    72  		return nil, err
    73  	}
    74  
    75  	return utils.SliceMap(posts, func(comment *entities.Comment) *entities.Comment {
    76  		comment.Post, err = repositories.Post.ByID(ctx, comment.PostID)
    77  		return comment
    78  	}), err
    79  }
    80  
    81  func (m *CommentRepository) Count(ctx context.Context, filters ...*entities.CommentFilter) (int, error) {
    82  	if len(filters) == 0 {
    83  		return len(m.entities), nil
    84  	}
    85  	filter := *filters[0]
    86  	count := 0
    87  	if filter.Page < 1 {
    88  		filter.Page = 1
    89  	}
    90  	if filter.Limit < 1 {
    91  		filter.Limit = 10
    92  	}
    93  	offset := (filter.Page - 1) * filter.Limit
    94  
    95  	for index, comment := range m.entities {
    96  		if index < offset {
    97  			continue
    98  		}
    99  		if index >= offset+filter.Limit {
   100  			break
   101  		}
   102  
   103  		if filter.Search != "" {
   104  			if !strings.Contains(comment.Content, filter.Search) && !strings.Contains(comment.Content, filter.Search) {
   105  				continue
   106  			}
   107  		}
   108  
   109  		if len(filter.ExcludeIDs) > 0 && utils.SliceContains(filter.ExcludeIDs, comment.ID) {
   110  			continue
   111  		}
   112  
   113  		if len(filter.UserIDs) > 0 && !utils.SliceContains(filter.UserIDs, comment.UserID) {
   114  			continue
   115  		}
   116  
   117  		if len(filter.PostIDs) > 0 && !utils.SliceContains(filter.PostIDs, comment.UserID) {
   118  			continue
   119  		}
   120  
   121  		if len(filter.ParentIDs) > 0 && !utils.SliceContains(filter.ParentIDs, comment.UserID) {
   122  			continue
   123  		}
   124  
   125  		count++
   126  	}
   127  
   128  	return count, nil
   129  }
   130  
   131  func (m *CommentRepository) Paginate(ctx context.Context, filters ...*entities.CommentFilter) (*entities.Paginate[entities.Comment], error) {
   132  	comments, err := m.Find(ctx, filters...)
   133  	if err != nil {
   134  		return nil, err
   135  	}
   136  
   137  	count, err := m.Count(ctx, filters...)
   138  	if err != nil {
   139  		return nil, err
   140  	}
   141  
   142  	filter := filters[0]
   143  	if filter.Page < 1 {
   144  		filter.Page = 1
   145  	}
   146  	if filter.Limit < 1 {
   147  		filter.Limit = 10
   148  	}
   149  	return &entities.Paginate[entities.Comment]{
   150  		Data:        comments,
   151  		PageSize:    filter.Limit,
   152  		PageCurrent: filter.Page,
   153  		Total:       int(math.Ceil(float64(count) / float64(filter.Limit))),
   154  	}, nil
   155  }
   156  
   157  func (m *CommentRepository) PaginateWithPost(ctx context.Context, filters ...*entities.CommentFilter) (*entities.Paginate[entities.Comment], error) {
   158  	comments, err := m.FindWithPost(ctx, filters...)
   159  	if err != nil {
   160  		return nil, err
   161  	}
   162  
   163  	count, err := m.Count(ctx, filters...)
   164  	if err != nil {
   165  		return nil, err
   166  	}
   167  
   168  	filter := filters[0]
   169  	if filter.Page < 1 {
   170  		filter.Page = 1
   171  	}
   172  	if filter.Limit < 1 {
   173  		filter.Limit = 10
   174  	}
   175  	return &entities.Paginate[entities.Comment]{
   176  		Data:        comments,
   177  		PageSize:    filter.Limit,
   178  		PageCurrent: filter.Page,
   179  		Total:       int(math.Ceil(float64(count) / float64(filter.Limit))),
   180  	}, nil
   181  }