72 lines
1.8 KiB
Go
72 lines
1.8 KiB
Go
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 {
|
|
DB *sql.DB
|
|
}
|
|
|
|
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
|
|
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")
|
|
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
|
|
func (ctrl *ProjectsController) Update(ctx context.Context, data *ProjectData) error {
|
|
return nil
|
|
}
|
|
|
|
// Get an existing project by ID
|
|
func (ctrl *ProjectsController) Get(ctx context.Context, projectID string) (data *ProjectData, err error) {
|
|
return nil, nil
|
|
}
|
|
|
|
// List projects available for a user
|
|
func (ctrl *ProjectsController) List(ctx context.Context) (data []*ProjectData, err error) {
|
|
return nil, nil
|
|
}
|