Skip to content

Commit

Permalink
wip: delete project
Browse files Browse the repository at this point in the history
  • Loading branch information
katallaxie committed Jul 16, 2024
1 parent 7b2ebd0 commit 4213f06
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 6 deletions.
8 changes: 5 additions & 3 deletions internal/adapters/database/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package database
import (
"context"
"errors"
"fmt"

"github.com/zeiss/fiber-htmx/components/tables"
"github.com/zeiss/knox/internal/models"
Expand Down Expand Up @@ -112,6 +111,11 @@ func (rw *writeTxImpl) CreateProject(ctx context.Context, project *models.Projec
return rw.conn.Create(project).Error
}

// DeleteProject deletes a project.
func (rw *writeTxImpl) DeleteProject(ctx context.Context, project *models.Project) error {
return rw.conn.Delete(project).Error
}

// UpdateState...
func (rw *writeTxImpl) UpdateState(ctx context.Context, state *models.State) error {
latest := models.State{}
Expand All @@ -129,8 +133,6 @@ func (rw *writeTxImpl) UpdateState(ctx context.Context, state *models.State) err
state.Version = latest.Version + 1
}

fmt.Println(state.Version)

if latest.Version > 0 {
err := rw.conn.Delete(&latest).Error
if err != nil {
Expand Down
9 changes: 8 additions & 1 deletion internal/adapters/handlers/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,14 @@ func (h *apiHandlers) CreateProject(ctx context.Context, request openapi.CreateP
// Delete a project
// (DELETE /project/{id})
func (h *apiHandlers) DeleteProject(ctx context.Context, request openapi.DeleteProjectRequestObject) (openapi.DeleteProjectResponseObject, error) {
return nil, fiber.NewError(fiber.StatusNotImplemented, "not implemented")
cmd := dto.FromDeleteProjectRequestObject(request)

err := h.project.DeleteProject(ctx, cmd)
if err != nil {
return nil, fiber.NewError(fiber.StatusInternalServerError, err.Error())
}

return dto.ToDeleteProjectResponseObject(), nil
}

// Get a project
Expand Down
23 changes: 23 additions & 0 deletions internal/controllers/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ type ListProjectsQuery struct {
Sort string `json:"sort" form:"sort"`
}

// DeleteProjectCommand ...
type DeleteProjectCommand struct {
Name string `json:"name" form:"name" validate:"required"`
}

// ProjectControllerImpl ...
type ProjectControllerImpl struct {
store seed.Database[ports.ReadTx, ports.ReadWriteTx]
Expand All @@ -40,6 +45,8 @@ type ProjectController interface {
CreateProject(ctx context.Context, cmd CreateProjectCommand) error
// ListProjects ...
ListProjects(ctx context.Context, cmd ListProjectsQuery) (tables.Results[models.Project], error)
// DeleteProject ...
DeleteProject(ctx context.Context, cmd DeleteProjectCommand) error
}

// NewProjectController ...
Expand Down Expand Up @@ -95,3 +102,19 @@ func (c *ProjectControllerImpl) ListProjects(ctx context.Context, cmd ListProjec

return teams, nil
}

// DeleteProject ...
func (c *ProjectControllerImpl) DeleteProject(ctx context.Context, cmd DeleteProjectCommand) error {
err := validate.Struct(cmd)
if err != nil {
return err
}

project := models.Project{
Name: cmd.Name,
}

return c.store.ReadWriteTx(ctx, func(ctx context.Context, tx ports.ReadWriteTx) error {
return tx.DeleteProject(ctx, &project)
})
}
2 changes: 1 addition & 1 deletion internal/models/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"gorm.io/gorm"
)

// Environment ...
// Environment is a specific environment for a project.
type Environment struct {
// ID is the primary key of the team.
ID uuid.UUID `json:"id" gorm:"type:uuid;default:gen_random_uuid()"`
Expand Down
2 changes: 1 addition & 1 deletion internal/models/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/zeiss/fiber-goth/adapters"
)

// Lock ...
// Lock is a lock for a project.
type Lock struct {
// ID is the primary key of the lock.
ID uuid.UUID `json:"id" gorm:"type:uuid;default:gen_random_uuid()" params:"id"`
Expand Down
2 changes: 2 additions & 0 deletions internal/ports/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ type ReadWriteTx interface {
DeleteTeam(context.Context, *adapters.GothTeam) error
// CreateProject creates a new project.
CreateProject(context.Context, *models.Project) error
// DeleteProject deletes a project.
DeleteProject(context.Context, *models.Project) error
// CreateEnvironment creates a new environment.
CreateEnvironment(context.Context, *models.Environment) error

Expand Down
14 changes: 14 additions & 0 deletions pkg/dto/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,17 @@ func ToGetProjectsResponseObject(results tables.Results[models.Project]) openapi

return res
}

// FromDeleteProjectRequestObject ...
func FromDeleteProjectRequestObject(req openapi.DeleteProjectRequestObject) controllers.DeleteProjectCommand {
return controllers.DeleteProjectCommand{
Name: req.ProjectId,
}
}

// ToDeleteProjectResponseObject ...
func ToDeleteProjectResponseObject() openapi.DeleteProject204Response {
res := openapi.DeleteProject204Response{}

return res
}

0 comments on commit 4213f06

Please sign in to comment.