Skip to content

Commit

Permalink
Implemented GetUserById. #12
Browse files Browse the repository at this point in the history
  • Loading branch information
ahilwers committed Jan 21, 2023
1 parent eb1c2cb commit a58cd6a
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 31 deletions.
35 changes: 33 additions & 2 deletions src/pkg/database/gorm_project_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"timeasy-server/pkg/domain/model"
"timeasy-server/pkg/domain/repository"

"github.com/gofrs/uuid"
"gorm.io/gorm"
)

Expand All @@ -17,9 +18,39 @@ func NewGormProjectRepository(database *gorm.DB) repository.ProjectRepository {
}
}

func (repo *gormProjectRepository) AddProject(project *model.Project) (*model.Project, error) {
func (repo *gormProjectRepository) AddProject(project *model.Project) error {
if err := repo.db.Create(project).Error; err != nil {
return err
}
return nil
}

func (repo *gormProjectRepository) GetProjectById(id uuid.UUID) (*model.Project, error) {
var project model.Project
if err := repo.db.First(&project, id).Error; err != nil {
return nil, err
}
return &project, nil
}

func (repo *gormProjectRepository) UpdateProject(project *model.Project) error {
if err := repo.db.Save(project).Error; err != nil {
return err
}
return nil
}

func (repo *gormProjectRepository) DeleteProject(project *model.Project) error {
if err := repo.db.Delete(project).Error; err != nil {
return err
}
return nil
}

func (repo *gormProjectRepository) GetAllProjects() ([]model.Project, error) {
var projects []model.Project
if err := repo.db.Order("name").Find(&projects).Error; err != nil {
return nil, err
}
return project, nil
return projects, nil
}
64 changes: 64 additions & 0 deletions src/pkg/delivery/http/project_handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package http

import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"timeasy-server/pkg/database"
"timeasy-server/pkg/domain/model"
"timeasy-server/pkg/test"
"timeasy-server/pkg/usecase"

"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
)

func Test_projectHandler_GetProjectById(t *testing.T) {
teardownTest := test.SetupTest(t)
defer teardownTest(t)

projectRepo := database.NewGormProjectRepository(test.DB)
projectUsecase := usecase.NewProjectUsecase(projectRepo)

userRepo := database.NewGormUserRepository(test.DB)
userUsecase := usecase.NewUserUsecase(userRepo)

userHandler := NewUserHandler(userUsecase)
projectHandler := NewProjectHandler(projectUsecase)

router := SetupRouter(userHandler, projectHandler)
token, user := loginUser(t, router, userUsecase, model.User{
Username: "user",
Password: "password",
})

project := model.Project{
Name: "testproject",
UserId: user.ID,
}
_, err := projectUsecase.AddProject(&project)
assert.Nil(t, err)

w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", fmt.Sprintf("/api/v1/projects/%v", user.ID), nil)
AddToken(req, token)
router.ServeHTTP(w, req)
assert.Equal(t, 200, w.Code)

var projectFromService model.Project
json.Unmarshal(w.Body.Bytes(), &projectFromService)
assert.Equal(t, project.Name, projectFromService.Name)
assert.Equal(t, user.ID, projectFromService.UserId)
}

func loginUser(t *testing.T, router *gin.Engine, userUsecase usecase.UserUsecase, user model.User) (string, *model.User) {
_, err := userUsecase.AddUser(&user)
assert.Nil(t, err)

token, err := Login(router, "user", "password")
assert.Nil(t, err)

return token, &user
}
12 changes: 10 additions & 2 deletions src/pkg/domain/repository/project_repository.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
package repository

import "timeasy-server/pkg/domain/model"
import (
"timeasy-server/pkg/domain/model"

"github.com/gofrs/uuid"
)

type ProjectRepository interface {
AddProject(project *model.Project) (*model.Project, error)
AddProject(project *model.Project) error
UpdateProject(project *model.Project) error
DeleteProject(project *model.Project) error
GetProjectById(id uuid.UUID) (*model.Project, error)
GetAllProjects() ([]model.Project, error)
}
11 changes: 8 additions & 3 deletions src/pkg/usecase/project_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
)

type ProjectUsecase interface {
AddProject(project *model.Project) (*model.Project, error)
AddProject(project *model.Project) error
GetProjectById(id uuid.UUID) (*model.Project, error)
}

type projectUsecase struct {
Expand All @@ -22,9 +23,13 @@ func NewProjectUsecase(repo repository.ProjectRepository) ProjectUsecase {
}
}

func (pu *projectUsecase) AddProject(project *model.Project) (*model.Project, error) {
func (pu *projectUsecase) AddProject(project *model.Project) error {
if project.UserId == uuid.Nil {
return nil, fmt.Errorf("The user id must not be empty.")
return fmt.Errorf("The user id must not be empty.")
}
return pu.repo.AddProject(project)
}

func (pu *projectUsecase) GetProjectById(id uuid.UUID) (*model.Project, error) {
return pu.repo.GetProjectById(id)
}
83 changes: 59 additions & 24 deletions src/pkg/usecase/project_usecase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,35 @@ import (
"timeasy-server/pkg/database"
"timeasy-server/pkg/domain/model"
"timeasy-server/pkg/test"

"github.com/gofrs/uuid"
"github.com/stretchr/testify/assert"
)

func Test_projectService_AddProject(t *testing.T) {
func Test_projectUsecase_AddProject(t *testing.T) {
teardownTest := test.SetupTest(t)
defer teardownTest(t)

userRepo := database.NewGormUserRepository(test.DB)
userUsecase := NewUserUsecase(userRepo)

user := model.User{
Username: "user",
Password: "password",
}
_, err := userUsecase.AddUser(&user)
if err != nil {
t.Errorf("user can not be added: %v", err)
}
user := addUser(t, "user", "password", model.RoleList{model.RoleUser})

projectRepo := database.NewGormProjectRepository(test.DB)
projectUsecase := NewProjectUsecase(projectRepo)
prj := model.Project{
Name: "Testproject",
UserId: user.ID,
}
_, err = projectUsecase.AddProject(&prj)
if err != nil {
t.Errorf("Project could not be created: %s", err)
}
err := projectUsecase.AddProject(&prj)
assert.Nil(t, err)

var projectFromDb model.Project
if err := test.DB.First(&projectFromDb, prj.ID).Error; err != nil {
t.Errorf("project could not be retrieved: %s", err)
}

if projectFromDb.Name != prj.Name {
t.Error("project names are not equal.")
}
assert.Equal(t, prj.Name, projectFromDb.Name)
assert.Equal(t, user.ID, projectFromDb.UserId)
}

func Test_projectService_AddProjectFailsWithoutUserId(t *testing.T) {
func Test_projectUsecase_AddProjectFailsWithoutUserId(t *testing.T) {
teardownTest := test.SetupTest(t)
defer teardownTest(t)

Expand All @@ -53,8 +43,53 @@ func Test_projectService_AddProjectFailsWithoutUserId(t *testing.T) {
prj := model.Project{
Name: "Testproject",
}
_, err := projectUsecase.AddProject(&prj)
if err == nil {
t.Error("adding a project without userid is not allowed.")
err := projectUsecase.AddProject(&prj)
assert.NotNil(t, err)
}

func Test_projectUsecase_GetProjectById(t *testing.T) {
teardownTest := test.SetupTest(t)
defer teardownTest(t)

user := addUser(t, "user", "password", model.RoleList{model.RoleUser})

projectRepo := database.NewGormProjectRepository(test.DB)
projectUsecase := NewProjectUsecase(projectRepo)
prj := model.Project{
Name: "Testproject",
UserId: user.ID,
}
err := projectUsecase.AddProject(&prj)
assert.Nil(t, err)

projectFromDb, err := projectUsecase.GetProjectById(prj.ID)
assert.Nil(t, err)
assert.Equal(t, prj.ID, projectFromDb.ID)
assert.Equal(t, prj.Name, projectFromDb.Name)
}

func Test_projectUsecase_GetProjectByIdFailsIfProjectDoesNotExist(t *testing.T) {
teardownTest := test.SetupTest(t)
defer teardownTest(t)

projectRepo := database.NewGormProjectRepository(test.DB)
projectUsecase := NewProjectUsecase(projectRepo)
notExistingId, err := uuid.NewV4()
assert.Nil(t, err)

_, err = projectUsecase.GetProjectById(notExistingId)
assert.NotNil(t, err)
}

func addUser(t *testing.T, username string, password string, roles model.RoleList) model.User {
userRepo := database.NewGormUserRepository(test.DB)
userUsecase := NewUserUsecase(userRepo)
user := model.User{
Username: "user",
Password: "password",
Roles: roles,
}
_, err := userUsecase.AddUser(&user)
assert.Nil(t, err)
return user
}

0 comments on commit a58cd6a

Please sign in to comment.