-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip: creating system and associate operator
- Loading branch information
1 parent
bc720fb
commit 9c77529
Showing
13 changed files
with
1,177 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package db | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/google/uuid" | ||
"github.com/zeiss/typhoon/internal/api/models" | ||
) | ||
|
||
// GetSystem ... | ||
func (db *DB) GetSystem(ctx context.Context, id uuid.UUID) (*models.System, error) { | ||
system := &models.System{} | ||
if err := db.conn.Where("id = ?", id).Preload("Operator").First(system).Error; err != nil { | ||
return nil, err | ||
} | ||
|
||
return system, nil | ||
} | ||
|
||
// CreateSystem ... | ||
func (db *DB) CreateSystem(ctx context.Context, system *models.System) error { | ||
return db.conn.WithContext(ctx).Create(system).Error | ||
} | ||
|
||
// ListSystems ... | ||
func (db *DB) ListSystems(ctx context.Context, pagination models.Pagination[models.System]) (models.Pagination[models.System], error) { | ||
systems := []models.System{} | ||
|
||
err := db.conn.WithContext(ctx).Scopes(models.Paginate(&systems, &pagination, db.conn)).Limit(pagination.Limit).Offset(pagination.Offset).Find(&systems).Error | ||
if err != nil { | ||
return pagination, err | ||
} | ||
pagination.Rows = systems | ||
|
||
return pagination, nil | ||
} | ||
|
||
// DeleteSystem ... | ||
func (db *DB) DeleteSystem(ctx context.Context, id uuid.UUID) error { | ||
return db.conn.WithContext(ctx).Delete(&models.System{}, id).Error | ||
} | ||
|
||
// UpdateSystem ... | ||
func (db *DB) UpdateSystem(ctx context.Context, system *models.System) (*models.System, error) { | ||
if err := db.conn.WithContext(ctx).Save(system).Error; err != nil { | ||
return nil, err | ||
} | ||
|
||
return system, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,60 @@ | ||
package controllers | ||
|
||
import "github.com/zeiss/typhoon/internal/api/ports" | ||
import ( | ||
"context" | ||
|
||
"github.com/google/uuid" | ||
"github.com/zeiss/typhoon/internal/api/models" | ||
"github.com/zeiss/typhoon/internal/api/ports" | ||
) | ||
|
||
// SystemsController ... | ||
type SystemsController struct { | ||
db ports.Systems | ||
} | ||
|
||
// NewSystemsController ... | ||
func NewSystemsController(db ports.Teams) *SystemsController { | ||
func NewSystemsController(db ports.Systems) *SystemsController { | ||
return &SystemsController{db} | ||
} | ||
|
||
// CreateSystem ... | ||
func (c *SystemsController) CreateSystem(ctx context.Context, name, description string) (*models.System, error) { | ||
system := &models.System{ | ||
Name: name, | ||
Description: description, | ||
} | ||
|
||
if err := c.db.CreateSystem(ctx, system); err != nil { | ||
return nil, err | ||
} | ||
|
||
return system, nil | ||
} | ||
|
||
// DeleteSystem ... | ||
func (c *SystemsController) DeleteSystem(ctx context.Context, id uuid.UUID) error { | ||
return c.db.DeleteSystem(ctx, id) | ||
} | ||
|
||
// GetSystem ... | ||
func (c *SystemsController) GetSystem(ctx context.Context, id uuid.UUID) (*models.System, error) { | ||
return c.db.GetSystem(ctx, id) | ||
} | ||
|
||
// ListSystems ... | ||
func (c *SystemsController) ListSystems(ctx context.Context, pagination models.Pagination[models.System]) (models.Pagination[models.System], error) { | ||
return c.db.ListSystems(ctx, pagination) | ||
} | ||
|
||
// UpdateSystemOperator ... | ||
func (c *SystemsController) UpdateSystemOperator(ctx context.Context, systemId, operatorID uuid.UUID) (*models.System, error) { | ||
system, err := c.db.GetSystem(ctx, systemId) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
system.OperatorID = &operatorID | ||
|
||
return c.db.UpdateSystem(ctx, system) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,22 @@ | ||
package ports | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/google/uuid" | ||
"github.com/zeiss/typhoon/internal/api/models" | ||
) | ||
|
||
// Systems is the interface that wraps the methods to access data. | ||
type Systems interface{} | ||
type Systems interface { | ||
// CreateSystem creates a new system. | ||
CreateSystem(ctx context.Context, system *models.System) error | ||
// UpdateSystem updates a system. | ||
UpdateSystem(ctx context.Context, system *models.System) (*models.System, error) | ||
// GetSystem retrieves a system by its ID. | ||
GetSystem(ctx context.Context, id uuid.UUID) (*models.System, error) | ||
// ListSystems retrieves all systems. | ||
ListSystems(ctx context.Context, pagination models.Pagination[models.System]) (models.Pagination[models.System], error) | ||
// DeleteSystem deletes a system by its ID. | ||
DeleteSystem(ctx context.Context, id uuid.UUID) error | ||
} |
Oops, something went wrong.