Skip to content

Commit

Permalink
feat(class): add list endpoint
Browse files Browse the repository at this point in the history
- split auth service into auth and user service

Closes #93
  • Loading branch information
michaelcoll committed Jun 11, 2023
1 parent 253a229 commit 451848f
Show file tree
Hide file tree
Showing 24 changed files with 941 additions and 456 deletions.
9 changes: 9 additions & 0 deletions db/queries/quiz.sql
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,12 @@ LIMIT ? OFFSET ?;
SELECT COUNT(1)
FROM quiz
WHERE active = 1;

-- name: CountAllActiveQuizRestrictedToClass :one
SELECT COUNT(1)
FROM quiz q
JOIN quiz_class_visibility qcv ON q.sha1 = qcv.quiz_sha1
JOIN student_class sc ON sc.uuid = qcv.class_uuid
JOIN user u ON sc.uuid = u.class_uuid
WHERE q.active = 1
AND u.id = ?;
4 changes: 4 additions & 0 deletions db/queries/student_class.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ SELECT *
FROM student_class
LIMIT ? OFFSET ?;

-- name: CountAllClasses :one
SELECT COUNT(1)
FROM student_class;

-- name: DeleteClassById :exec
DELETE
FROM student_class
Expand Down
File renamed without changes.
7 changes: 5 additions & 2 deletions internal/back/back.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,16 @@ func New() Module {

quizRepository := infrastructure.NewQuizRepository(connection)
authRepository := infrastructure.NewAuthRepository(connection)
userRepository := infrastructure.NewUserRepository(connection)
classRepository := infrastructure.NewClassRepository(connection)

quizService := domain.NewQuizService(quizRepository)
authService := domain.NewAuthService(authRepository)
authService := domain.NewAuthService(authRepository, userRepository)
classService := domain.NewClassService(classRepository)

return Module{
quizServ: quizService,
authServ: authService,
quizCtrl: presentation.NewApiController(&quizService, &authService),
quizCtrl: presentation.NewApiController(&quizService, &authService, &classService),
}
}
21 changes: 11 additions & 10 deletions internal/back/domain/authService.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,18 @@ import (
)

type AuthService struct {
r AuthRepository
authRepository AuthRepository
userRepository UserRepository
}

func NewAuthService(r AuthRepository) AuthService {
func NewAuthService(authRepository AuthRepository, userRepository UserRepository) AuthService {
adminEmail := viper.GetString("default-admin-email")

if len(adminEmail) > 0 {
fmt.Printf("%s Default admin email set to %s\n", color.HiYellowString("i"), color.BlueString(adminEmail))
}

return AuthService{r: r}
return AuthService{authRepository: authRepository, userRepository: userRepository}
}

func (s *AuthService) Login(ctx context.Context, idToken string) (*User, error) {
Expand Down Expand Up @@ -68,7 +69,7 @@ func (s *AuthService) Login(ctx context.Context, idToken string) (*User, error)
user.Role = Student
}

err = s.r.CreateOrReplaceUser(ctx, user)
err = s.userRepository.CreateOrReplaceUser(ctx, user)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -125,7 +126,7 @@ func (s *AuthService) ValidateTokenAndGetUser(ctx context.Context, accessToken s
return nil, err
}

user, err := s.r.FindUserById(ctx, token.Sub)
user, err := s.userRepository.FindUserById(ctx, token.Sub)
if err != nil {
return nil, err
}
Expand All @@ -135,7 +136,7 @@ func (s *AuthService) ValidateTokenAndGetUser(ctx context.Context, accessToken s
}

if token.Provenance == Parse {
err := s.r.CacheToken(token)
err := s.authRepository.CacheToken(token)
if err != nil {
return nil, err
}
Expand All @@ -145,7 +146,7 @@ func (s *AuthService) ValidateTokenAndGetUser(ctx context.Context, accessToken s
}

func (s *AuthService) FindUserById(ctx context.Context, id string) (*User, error) {
user, err := s.r.FindUserById(ctx, id)
user, err := s.userRepository.FindUserById(ctx, id)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -175,13 +176,13 @@ func (role Role) CanAccess(other Role) bool {
}

func (s *AuthService) FindAllUser(ctx context.Context) ([]*User, error) {
return s.r.FindAllUser(ctx)
return s.userRepository.FindAllUser(ctx)
}

func (s *AuthService) DeactivateUser(ctx context.Context, id string) error {
return s.r.UpdateUserActive(ctx, id, false)
return s.userRepository.UpdateUserActive(ctx, id, false)
}

func (s *AuthService) ActivateUser(ctx context.Context, id string) error {
return s.r.UpdateUserActive(ctx, id, true)
return s.userRepository.UpdateUserActive(ctx, id, true)
}
5 changes: 3 additions & 2 deletions internal/back/domain/authService_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ import (

func TestAuthService_FindUserById(t *testing.T) {
mockAuthRepository := NewMockAuthRepository(t)
service := NewAuthService(mockAuthRepository)
mockUserRepository := NewMockUserRepository(t)
service := NewAuthService(mockAuthRepository, mockUserRepository)

mockAuthRepository.On("FindUserById", context.Background(), sub).Return(nil, nil)
mockUserRepository.On("FindUserById", context.Background(), sub).Return(nil, nil)

_, err := service.FindUserById(context.Background(), sub)
if err != nil {
Expand Down
41 changes: 41 additions & 0 deletions internal/back/domain/classService.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (c) 2023 Michaël COLL.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package domain

import "context"

type ClassService struct {
r ClassRepository
}

func NewClassService(classRepository ClassRepository) ClassService {
return ClassService{r: classRepository}
}

func (s *ClassService) FindAllClasses(ctx context.Context, limit uint16, offset uint16) ([]*Class, uint32, error) {
classes, err := s.r.FindAll(ctx, limit, offset)
if err != nil {
return nil, 0, err
}

total, err := s.r.CountAll(ctx)
if err != nil {
return nil, 0, err
}

return classes, total, nil
}
Loading

0 comments on commit 451848f

Please sign in to comment.