/
get_students_by_login_with_pagination.go
51 lines (39 loc) 路 1.29 KB
/
get_students_by_login_with_pagination.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package studentservice
import (
"context"
"github.com/naofel1/api-golang-template/internal/ent"
"github.com/naofel1/api-golang-template/pkg/apistatus"
"github.com/naofel1/api-golang-template/pkg/pagination"
"go.uber.org/zap"
)
// GetStudentsWithSearchWithPagination retrieves a student based on their pseudo
func (s *studentService) GetStudentsWithSearchWithPagination(ctx context.Context, u *ent.Students, us *ent.Student, pag *pagination.Front) error {
enroll := func(ctx context.Context, repo Repository) error {
studTotal, err := repo.GetTotalStudentByPseudoOrFirstNameOrLastNameCount(ctx, us)
if err != nil {
s.Logger.Ctx(ctx).Info("Error when getting students paginated",
zap.Error(err),
)
return apistatus.NewInternal()
}
pag.Calculate(studTotal)
uFetched, err := repo.FindStudentsByLoginOrFirstNameOrLastNameAtOffset(ctx, us, pag.Offset, pag.ItemsPerPage)
if err != nil {
s.Logger.Ctx(ctx).Info("Error when getting User",
zap.String("Pseudo", us.Pseudo),
zap.Error(err),
)
// Student is not found
if ent.IsNotFound(err) {
return apistatus.NewNotFound("Pseudo", us.Pseudo)
}
return apistatus.NewInternal()
}
*u = uFetched
return nil
}
if err := s.StudentRepository.Execute(ctx, enroll); err != nil {
return err
}
return nil
}