2019-05-16 18:05:39 -04:00
|
|
|
package project
|
2019-05-16 10:39:25 -04:00
|
|
|
|
|
|
|
import (
|
2019-06-25 22:31:54 -08:00
|
|
|
"context"
|
2019-06-24 01:30:18 -08:00
|
|
|
"database/sql/driver"
|
2019-07-13 12:16:28 -08:00
|
|
|
"geeks-accelerator/oss/saas-starter-kit/internal/platform/web"
|
2019-06-24 01:30:18 -08:00
|
|
|
"github.com/lib/pq"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"gopkg.in/go-playground/validator.v9"
|
2019-06-24 04:26:48 -08:00
|
|
|
"time"
|
2019-05-16 10:39:25 -04:00
|
|
|
)
|
|
|
|
|
2019-06-24 01:30:18 -08:00
|
|
|
// Project represents a workflow.
|
2019-05-16 18:05:39 -04:00
|
|
|
type Project struct {
|
2019-06-25 02:40:29 -08:00
|
|
|
ID string `json:"id" validate:"required,uuid" example:"985f1746-1d9f-459f-a2d9-fc53ece5ae86"`
|
2019-06-24 04:26:48 -08:00
|
|
|
AccountID string `json:"account_id" validate:"required,uuid" truss:"api-create"`
|
2019-06-25 22:31:54 -08:00
|
|
|
Name string `json:"name" validate:"required" example:"Rocket Launch"`
|
|
|
|
Status ProjectStatus `json:"status" validate:"omitempty,oneof=active disabled" enums:"active,disabled" swaggertype:"string" example:"active"`
|
2019-06-24 04:26:48 -08:00
|
|
|
CreatedAt time.Time `json:"created_at" truss:"api-read"`
|
|
|
|
UpdatedAt time.Time `json:"updated_at" truss:"api-read"`
|
2019-06-25 02:40:29 -08:00
|
|
|
ArchivedAt *pq.NullTime `json:"archived_at,omitempty" truss:"api-hide"`
|
2019-06-24 01:30:18 -08:00
|
|
|
}
|
|
|
|
|
2019-06-25 22:31:54 -08:00
|
|
|
// ProjectResponse represents a workflow that is returned for display.
|
|
|
|
type ProjectResponse struct {
|
|
|
|
ID string `json:"id" validate:"required,uuid" example:"985f1746-1d9f-459f-a2d9-fc53ece5ae86"`
|
|
|
|
AccountID string `json:"account_id" validate:"required,uuid" truss:"api-create" example:"c4653bf9-5978-48b7-89c5-95704aebb7e2"`
|
|
|
|
Name string `json:"name" validate:"required" example:"Rocket Launch"`
|
|
|
|
Status web.EnumResponse `json:"status"` // Status is enum with values [active, disabled].
|
|
|
|
CreatedAt web.TimeResponse `json:"created_at"` // CreatedAt contains multiple format options for display.
|
|
|
|
UpdatedAt web.TimeResponse `json:"updated_at"` // UpdatedAt contains multiple format options for display.
|
|
|
|
ArchivedAt *web.TimeResponse `json:"archived_at,omitempty"` // ArchivedAt contains multiple format options for display.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Response transforms Project and ProjectResponse that is used for display.
|
|
|
|
// Additional filtering by context values or translations could be applied.
|
|
|
|
func (m *Project) Response(ctx context.Context) *ProjectResponse {
|
|
|
|
if m == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
r := &ProjectResponse{
|
|
|
|
ID: m.ID,
|
|
|
|
AccountID: m.AccountID,
|
|
|
|
Name: m.Name,
|
|
|
|
Status: web.NewEnumResponse(ctx, m.Status, ProjectStatus_Values),
|
|
|
|
CreatedAt: web.NewTimeResponse(ctx, m.CreatedAt),
|
|
|
|
UpdatedAt: web.NewTimeResponse(ctx, m.UpdatedAt),
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.ArchivedAt != nil && !m.ArchivedAt.Time.IsZero() {
|
|
|
|
at := web.NewTimeResponse(ctx, m.ArchivedAt.Time)
|
|
|
|
r.ArchivedAt = &at
|
|
|
|
}
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2019-06-24 04:26:48 -08:00
|
|
|
// ProjectCreateRequest contains information needed to create a new Project.
|
2019-06-24 01:30:18 -08:00
|
|
|
type ProjectCreateRequest struct {
|
2019-06-25 22:31:54 -08:00
|
|
|
AccountID string `json:"account_id" validate:"required,uuid" example:"c4653bf9-5978-48b7-89c5-95704aebb7e2"`
|
|
|
|
Name string `json:"name" validate:"required" example:"Rocket Launch"`
|
|
|
|
Status *ProjectStatus `json:"status,omitempty" validate:"omitempty,oneof=active disabled" enums:"active,disabled" swaggertype:"string" example:"active"`
|
2019-06-24 01:30:18 -08:00
|
|
|
}
|
|
|
|
|
2019-08-04 14:48:43 -08:00
|
|
|
// ProjectReadRequest defines the information needed to read a project.
|
|
|
|
type ProjectReadRequest struct {
|
|
|
|
ID string `json:"id" validate:"required,uuid" example:"985f1746-1d9f-459f-a2d9-fc53ece5ae86"`
|
|
|
|
IncludeArchived bool `json:"include-archived" example:"false"`
|
|
|
|
}
|
|
|
|
|
2019-06-24 04:26:48 -08:00
|
|
|
// ProjectUpdateRequest defines what information may be provided to modify an existing
|
2019-06-24 01:30:18 -08:00
|
|
|
// Project. All fields are optional so clients can send just the fields they want
|
|
|
|
// changed. It uses pointer fields so we can differentiate between a field that
|
2019-06-24 04:26:48 -08:00
|
|
|
// was not provided and a field that was provided as explicitly blank.
|
2019-06-24 01:30:18 -08:00
|
|
|
type ProjectUpdateRequest struct {
|
2019-06-25 22:31:54 -08:00
|
|
|
ID string `json:"id" validate:"required,uuid" example:"985f1746-1d9f-459f-a2d9-fc53ece5ae86"`
|
|
|
|
Name *string `json:"name,omitempty" validate:"omitempty" example:"Rocket Launch to Moon"`
|
|
|
|
Status *ProjectStatus `json:"status,omitempty" validate:"omitempty,oneof=active disabled" enums:"active,disabled" swaggertype:"string" example:"disabled"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// ProjectArchiveRequest defines the information needed to archive a project. This will archive (soft-delete) the
|
|
|
|
// existing database entry.
|
|
|
|
type ProjectArchiveRequest struct {
|
|
|
|
ID string `json:"id" validate:"required,uuid" example:"985f1746-1d9f-459f-a2d9-fc53ece5ae86"`
|
2019-06-24 01:30:18 -08:00
|
|
|
}
|
|
|
|
|
2019-08-04 14:48:43 -08:00
|
|
|
// ProjectDeleteRequest defines the information needed to delete a project.
|
|
|
|
type ProjectDeleteRequest struct {
|
|
|
|
ID string `json:"id" validate:"required,uuid" example:"985f1746-1d9f-459f-a2d9-fc53ece5ae86"`
|
|
|
|
}
|
|
|
|
|
2019-06-24 01:30:18 -08:00
|
|
|
// ProjectFindRequest defines the possible options to search for projects. By default
|
2019-06-24 04:26:48 -08:00
|
|
|
// archived project will be excluded from response.
|
2019-06-24 01:30:18 -08:00
|
|
|
type ProjectFindRequest struct {
|
2019-08-04 14:48:43 -08:00
|
|
|
Where *string `json:"where" example:"name = ? and status = ?"`
|
|
|
|
Args []interface{} `json:"args" swaggertype:"array,string" example:"Moon Launch,active"`
|
|
|
|
Order []string `json:"order" example:"created_at desc"`
|
|
|
|
Limit *uint `json:"limit" example:"10"`
|
|
|
|
Offset *uint `json:"offset" example:"20"`
|
|
|
|
IncludeArchived bool `json:"include-archived" example:"false"`
|
2019-06-24 01:30:18 -08:00
|
|
|
}
|
|
|
|
|
2019-06-24 04:26:48 -08:00
|
|
|
// ProjectStatus represents the status of project.
|
2019-06-24 01:30:18 -08:00
|
|
|
type ProjectStatus string
|
|
|
|
|
2019-06-24 04:26:48 -08:00
|
|
|
// ProjectStatus values define the status field of project.
|
2019-06-24 01:30:18 -08:00
|
|
|
const (
|
2019-06-24 04:26:48 -08:00
|
|
|
// ProjectStatus_Active defines the status of active for project.
|
2019-06-24 01:30:18 -08:00
|
|
|
ProjectStatus_Active ProjectStatus = "active"
|
2019-06-24 04:26:48 -08:00
|
|
|
// ProjectStatus_Disabled defines the status of disabled for project.
|
2019-06-24 01:30:18 -08:00
|
|
|
ProjectStatus_Disabled ProjectStatus = "disabled"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ProjectStatus_Values provides list of valid ProjectStatus values.
|
|
|
|
var ProjectStatus_Values = []ProjectStatus{
|
|
|
|
ProjectStatus_Active,
|
|
|
|
ProjectStatus_Disabled,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan supports reading the ProjectStatus value from the database.
|
|
|
|
func (s *ProjectStatus) Scan(value interface{}) error {
|
|
|
|
asBytes, ok := value.([]byte)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("Scan source is not []byte")
|
|
|
|
}
|
2019-06-24 04:26:48 -08:00
|
|
|
|
2019-06-24 01:30:18 -08:00
|
|
|
*s = ProjectStatus(string(asBytes))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value converts the ProjectStatus value to be stored in the database.
|
|
|
|
func (s ProjectStatus) Value() (driver.Value, error) {
|
|
|
|
v := validator.New()
|
|
|
|
errs := v.Var(s, "required,oneof=active disabled")
|
|
|
|
if errs != nil {
|
|
|
|
return nil, errs
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(s), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// String converts the ProjectStatus value to a string.
|
|
|
|
func (s ProjectStatus) String() string {
|
|
|
|
return string(s)
|
|
|
|
}
|