Signed-off-by: Nikolai Rodionov <iam@allanger.xyz>
This commit is contained in:
@@ -2,21 +2,57 @@ package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"gitea.badhouseplants.net/softplayer/softplayer-backend/internal/helpers/logger"
|
||||
"gitea.badhouseplants.net/softplayer/softplayer-backend/internal/repository"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type ProjectsController struct{}
|
||||
type ProjectsController struct {
|
||||
DB *sql.DB
|
||||
}
|
||||
|
||||
type ProjectData struct{}
|
||||
type ProjectData struct {
|
||||
UUID string
|
||||
Name string
|
||||
Slug string
|
||||
Description string
|
||||
}
|
||||
|
||||
var (
|
||||
ErrProjectExists = errors.New("project exists")
|
||||
ErrInvalidProject = errors.New("invalid project data")
|
||||
)
|
||||
|
||||
// Create a new project
|
||||
// It should create a project and set an owner to it
|
||||
func (ctrl *ProjectsController) Create(ctx context.Context, data *ProjectData) (id string, err error) {
|
||||
log := logger.FromContext(ctx)
|
||||
func (ctrl *ProjectsController) Create(ctx context.Context, data *ProjectData, userID string) (id string, err error) {
|
||||
id = uuid.NewString()
|
||||
log := logger.FromContext(ctx).WithValues("id", id)
|
||||
log.V(2).Info("Creating a project")
|
||||
|
||||
return "", nil
|
||||
queryData := &repository.ProjectData{
|
||||
UUID: id,
|
||||
Name: data.Name,
|
||||
Slug: data.Slug,
|
||||
Description: data.Description,
|
||||
CreatedBy: userID,
|
||||
CreatedAt: time.Now(),
|
||||
UpdatedAt: time.Now(),
|
||||
UpdatedBy: userID,
|
||||
}
|
||||
err = repository.CreateProject(ctx, ctrl.DB, queryData)
|
||||
if err != nil {
|
||||
if errors.Is(err, repository.ErrAlreadyExists) {
|
||||
return "", ErrProjectExists
|
||||
} else if errors.Is(err, repository.ErrCheckNotPassed) {
|
||||
return "", ErrInvalidProject
|
||||
}
|
||||
log.Error(err, "Couldn't create a project")
|
||||
return "", ErrServerError
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
|
||||
// Update an existing project
|
||||
|
||||
38
internal/services/projects_test.go
Normal file
38
internal/services/projects_test.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package services_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"gitea.badhouseplants.net/softplayer/softplayer-backend/internal/services"
|
||||
"github.com/google/uuid"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func newTestProjectsController(ctx context.Context) *services.ProjectsController {
|
||||
return &services.ProjectsController{
|
||||
DB: newTestDBConnection(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func TestIntegrationCreateProject_Success(t *testing.T) {
|
||||
ctrlAccount := newTestAccountController(t.Context())
|
||||
accountData := &services.AccountData{
|
||||
Password: "qwertyu9",
|
||||
Email: newTestUniqueEmail("projects"),
|
||||
}
|
||||
|
||||
userID, err := ctrlAccount.Create(t.Context(), accountData)
|
||||
assert.NoError(t, err)
|
||||
|
||||
ctrlProjects := newTestProjectsController(t.Context())
|
||||
projectData := &services.ProjectData{
|
||||
UUID: uuid.NewString(),
|
||||
Name: "Test project",
|
||||
Slug: uuid.NewString(),
|
||||
Description: "Test project",
|
||||
}
|
||||
projectID, err := ctrlProjects.Create(t.Context(), projectData, userID)
|
||||
assert.NoError(t, err)
|
||||
assert.NotEmpty(t, projectID)
|
||||
}
|
||||
Reference in New Issue
Block a user