1
0
mirror of https://github.com/raseels-repos/golang-saas-starter-kit.git synced 2025-06-15 00:15:15 +02:00

Completed truss code gen for generating model requests and crud.

This commit is contained in:
Lee Brown
2019-06-24 01:30:18 -08:00
parent efaeeb7103
commit bdbe3c587a
25 changed files with 3554 additions and 351 deletions

View File

@ -1,43 +1,96 @@
package project
import (
"database/sql/driver"
"time"
"gopkg.in/mgo.v2/bson"
"github.com/lib/pq"
"github.com/pkg/errors"
"gopkg.in/go-playground/validator.v9"
)
// Project is an item we sell.
// Project represents a workflow.
type Project struct {
ID bson.ObjectId `bson:"_id" json:"id"` // Unique identifier.
Name string `bson:"name" json:"name"` // Display name of the project.
Cost int `bson:"cost" json:"cost"` // Price for one item in cents.
Quantity int `bson:"quantity" json:"quantity"` // Original number of items available.
DateCreated time.Time `bson:"date_created" json:"date_created"` // When the project was added.
DateModified time.Time `bson:"date_modified" json:"date_modified"` // When the project record was lost modified.
ID string `json:"id" validate:"required,uuid"`
AccountID string `json:"account_id" validate:"required,uuid" truss:"api-create"`
Name string `json:"name" validate:"required"`
Status ProjectStatus `json:"status" validate:"omitempty,oneof=active disabled"`
CreatedAt time.Time `json:"created_at" truss:"api-read"`
UpdatedAt time.Time `json:"updated_at" truss:"api-read"`
ArchivedAt pq.NullTime `json:"archived_at" truss:"api-hide"`
}
// NewProject is what we require from clients when adding a Project.
type NewProject struct {
Name string `json:"name" validate:"required"`
Cost int `json:"cost" validate:"required,gte=0"`
Quantity int `json:"quantity" validate:"required,gte=1"`
// CreateProjectRequest contains information needed to create a new Project.
type ProjectCreateRequest struct {
AccountID string `json:"account_id" validate:"required,uuid"`
Name string `json:"name" validate:"required"`
Status *ProjectStatus `json:"status" validate:"omitempty,oneof=active disabled"`
}
// UpdateProject defines what information may be provided to modify an
// existing 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 was not provided and a field that was provided as
// explicitly blank. Normally we do not want to use pointers to basic types but
// we make exceptions around marshalling/unmarshalling.
type UpdateProject struct {
Name *string `json:"name"`
Cost *int `json:"cost" validate:"omitempty,gte=0"`
Quantity *int `json:"quantity" validate:"omitempty,gte=1"`
// UpdateProjectRequest defines what information may be provided to modify an existing
// 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
// was not provided and a field that was provided as explicitly blank. Normally
// we do not want to use pointers to basic types but we make exceptions around
// marshalling/unmarshalling.
type ProjectUpdateRequest struct {
ID string `validate:"required,uuid"`
Name *string `json:"name" validate:"omitempty"`
Status *ProjectStatus `json:"status" validate:"omitempty,oneof=active pending disabled"`
}
// Sale represents a transaction where we sold some quantity of a
// Project.
type Sale struct{}
// ProjectFindRequest defines the possible options to search for projects. By default
// archived projects will be excluded from response.
type ProjectFindRequest struct {
Where *string
Args []interface{}
Order []string
Limit *uint
Offset *uint
IncludedArchived bool
}
// NewSale defines what we require when creating a Sale record.
type NewSale struct{}
// ProjectStatus represents the status of an project.
type ProjectStatus string
// ProjectStatus values define the status field of a user project.
const (
// ProjectStatus_Active defines the state when a user can access an project.
ProjectStatus_Active ProjectStatus = "active"
// ProjectStatus_Disabled defines the state when a user has been disabled from
// accessing an project.
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")
}
*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)
}

View File

@ -1,162 +0,0 @@
package project
import (
"context"
"fmt"
"time"
"github.com/jmoiron/sqlx"
"github.com/pkg/errors"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
const projectsCollection = "projects"
var (
// ErrNotFound abstracts the mgo not found error.
ErrNotFound = errors.New("Entity not found")
// ErrInvalidID occurs when an ID is not in a valid form.
ErrInvalidID = errors.New("ID is not in its proper form")
)
// List retrieves a list of existing projects from the database.
func List(ctx context.Context, dbConn *sqlx.DB) ([]Project, error) {
span, ctx := tracer.StartSpanFromContext(ctx, "internal.project.List")
defer span.Finish()
p := []Project{}
f := func(collection *mgo.Collection) error {
return collection.Find(nil).All(&p)
}
if _, err := dbConn.ExecContext(ctx, projectsCollection, f); err != nil {
return nil, errors.Wrap(err, "db.projects.find()")
}
return p, nil
}
// Retrieve gets the specified project from the database.
func Retrieve(ctx context.Context, dbConn *sqlx.DB, id string) (*Project, error) {
span, ctx := tracer.StartSpanFromContext(ctx, "internal.project.Retrieve")
defer span.Finish()
if !bson.IsObjectIdHex(id) {
return nil, ErrInvalidID
}
q := bson.M{"_id": bson.ObjectIdHex(id)}
var p *Project
f := func(collection *mgo.Collection) error {
return collection.Find(q).One(&p)
}
if _, err := dbConn.ExecContext(ctx, projectsCollection, f); err != nil {
if err == mgo.ErrNotFound {
return nil, ErrNotFound
}
return nil, errors.Wrap(err, fmt.Sprintf("db.projects.find(%s)", q))
}
return p, nil
}
// Create inserts a new project into the database.
func Create(ctx context.Context, dbConn *sqlx.DB, cp *NewProject, now time.Time) (*Project, error) {
span, ctx := tracer.StartSpanFromContext(ctx, "internal.project.Create")
defer span.Finish()
// Mongo truncates times to milliseconds when storing. We and do the same
// here so the value we return is consistent with what we store.
now = now.Truncate(time.Millisecond)
p := Project{
ID: bson.NewObjectId(),
Name: cp.Name,
Cost: cp.Cost,
Quantity: cp.Quantity,
DateCreated: now,
DateModified: now,
}
f := func(collection *mgo.Collection) error {
return collection.Insert(&p)
}
if _, err := dbConn.ExecContext(ctx, projectsCollection, f); err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("db.projects.insert(%v)", &p))
}
return &p, nil
}
// Update replaces a project document in the database.
func Update(ctx context.Context, dbConn *sqlx.DB, id string, upd UpdateProject, now time.Time) error {
span, ctx := tracer.StartSpanFromContext(ctx, "internal.project.Update")
defer span.Finish()
if !bson.IsObjectIdHex(id) {
return ErrInvalidID
}
fields := make(bson.M)
if upd.Name != nil {
fields["name"] = *upd.Name
}
if upd.Cost != nil {
fields["cost"] = *upd.Cost
}
if upd.Quantity != nil {
fields["quantity"] = *upd.Quantity
}
// If there's nothing to update we can quit early.
if len(fields) == 0 {
return nil
}
fields["date_modified"] = now
m := bson.M{"$set": fields}
q := bson.M{"_id": bson.ObjectIdHex(id)}
f := func(collection *mgo.Collection) error {
return collection.Update(q, m)
}
if _, err := dbConn.ExecContext(ctx, projectsCollection, f); err != nil {
if err == mgo.ErrNotFound {
return ErrNotFound
}
return errors.Wrap(err, fmt.Sprintf("db.customers.update(%s, %s)", q, m))
}
return nil
}
// Delete removes a project from the database.
func Delete(ctx context.Context, dbConn *sqlx.DB, id string) error {
span, ctx := tracer.StartSpanFromContext(ctx, "internal.project.Delete")
defer span.Finish()
if !bson.IsObjectIdHex(id) {
return ErrInvalidID
}
q := bson.M{"_id": bson.ObjectIdHex(id)}
f := func(collection *mgo.Collection) error {
return collection.Remove(q)
}
if _, err := dbConn.ExecContext(ctx, projectsCollection, f); err != nil {
if err == mgo.ErrNotFound {
return ErrNotFound
}
return errors.Wrap(err, fmt.Sprintf("db.projects.remove(%v)", q))
}
return nil
}

View File

@ -1,129 +0,0 @@
package project_test
import (
"os"
"testing"
"time"
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/platform/tests"
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/project"
"github.com/google/go-cmp/cmp"
"github.com/pkg/errors"
)
var test *tests.Test
// TestMain is the entry point for testing.
func TestMain(m *testing.M) {
os.Exit(testMain(m))
}
func testMain(m *testing.M) int {
test = tests.New()
defer test.TearDown()
return m.Run()
}
// TestProject validates the full set of CRUD operations on Project values.
func TestProject(t *testing.T) {
defer tests.Recover(t)
t.Log("Given the need to work with Project records.")
{
t.Log("\tWhen handling a single Project.")
{
ctx := tests.Context()
dbConn := test.MasterDB.Copy()
defer dbConn.Close()
np := project.NewProject{
Name: "Comic Books",
Cost: 25,
Quantity: 60,
}
p, err := project.Create(ctx, dbConn, &np, time.Now().UTC())
if err != nil {
t.Fatalf("\t%s\tShould be able to create a project : %s.", tests.Failed, err)
}
t.Logf("\t%s\tShould be able to create a project.", tests.Success)
savedP, err := project.Retrieve(ctx, dbConn, p.ID.Hex())
if err != nil {
t.Fatalf("\t%s\tShould be able to retrieve project by ID: %s.", tests.Failed, err)
}
t.Logf("\t%s\tShould be able to retrieve project by ID.", tests.Success)
if diff := cmp.Diff(p, savedP); diff != "" {
t.Fatalf("\t%s\tShould get back the same project. Diff:\n%s", tests.Failed, diff)
}
t.Logf("\t%s\tShould get back the same project.", tests.Success)
upd := project.UpdateProject{
Name: tests.StringPointer("Comics"),
Cost: tests.IntPointer(50),
Quantity: tests.IntPointer(40),
}
if err := project.Update(ctx, dbConn, p.ID.Hex(), upd, time.Now().UTC()); err != nil {
t.Fatalf("\t%s\tShould be able to update project : %s.", tests.Failed, err)
}
t.Logf("\t%s\tShould be able to update project.", tests.Success)
savedP, err = project.Retrieve(ctx, dbConn, p.ID.Hex())
if err != nil {
t.Fatalf("\t%s\tShould be able to retrieve updated project : %s.", tests.Failed, err)
}
t.Logf("\t%s\tShould be able to retrieve updated project.", tests.Success)
// Build a project matching what we expect to see. We just use the
// modified time from the database.
want := &project.Project{
ID: p.ID,
Name: *upd.Name,
Cost: *upd.Cost,
Quantity: *upd.Quantity,
DateCreated: p.DateCreated,
DateModified: savedP.DateModified,
}
if diff := cmp.Diff(want, savedP); diff != "" {
t.Fatalf("\t%s\tShould get back the same project. Diff:\n%s", tests.Failed, diff)
}
t.Logf("\t%s\tShould get back the same project.", tests.Success)
upd = project.UpdateProject{
Name: tests.StringPointer("Graphic Novels"),
}
if err := project.Update(ctx, dbConn, p.ID.Hex(), upd, time.Now().UTC()); err != nil {
t.Fatalf("\t%s\tShould be able to update just some fields of project : %s.", tests.Failed, err)
}
t.Logf("\t%s\tShould be able to update just some fields of project.", tests.Success)
savedP, err = project.Retrieve(ctx, dbConn, p.ID.Hex())
if err != nil {
t.Fatalf("\t%s\tShould be able to retrieve updated project : %s.", tests.Failed, err)
}
t.Logf("\t%s\tShould be able to retrieve updated project.", tests.Success)
if savedP.Name != *upd.Name {
t.Fatalf("\t%s\tShould be able to see updated Name field : got %q want %q.", tests.Failed, savedP.Name, *upd.Name)
} else {
t.Logf("\t%s\tShould be able to see updated Name field.", tests.Success)
}
if err := project.Delete(ctx, dbConn, p.ID.Hex()); err != nil {
t.Fatalf("\t%s\tShould be able to delete project : %s.", tests.Failed, err)
}
t.Logf("\t%s\tShould be able to delete project.", tests.Success)
savedP, err = project.Retrieve(ctx, dbConn, p.ID.Hex())
if errors.Cause(err) != project.ErrNotFound {
t.Fatalf("\t%s\tShould NOT be able to retrieve deleted project : %s.", tests.Failed, err)
}
t.Logf("\t%s\tShould NOT be able to retrieve deleted project.", tests.Success)
}
}
}

View File

@ -65,8 +65,8 @@ func migrationList(db *sqlx.DB, log *log.Logger) []*sqlxmigrate.Migration {
zipcode varchar(20) NOT NULL DEFAULT '',
status account_status_t NOT NULL DEFAULT 'active',
timezone varchar(128) NOT NULL DEFAULT 'America/Anchorage',
signup_user_id char(36) DEFAULT NULL,
billing_user_id char(36) DEFAULT NULL,
signup_user_id char(36) DEFAULT NULL REFERENCES users(id),
billing_user_id char(36) DEFAULT NULL REFERENCES users(id),
created_at TIMESTAMP WITH TIME ZONE NOT NULL,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NULL,
archived_at TIMESTAMP WITH TIME ZONE DEFAULT NULL,
@ -107,8 +107,8 @@ func migrationList(db *sqlx.DB, log *log.Logger) []*sqlxmigrate.Migration {
q3 := `CREATE TABLE IF NOT EXISTS users_accounts (
id char(36) NOT NULL,
account_id char(36) NOT NULL,
user_id char(36) NOT NULL,
account_id char(36) NOT NULL REFERENCES accounts(id),
user_id char(36) NOT NULL REFERENCES users(id),
roles user_account_role_t[] NOT NULL,
status user_account_status_t NOT NULL DEFAULT 'active',
created_at TIMESTAMP WITH TIME ZONE NOT NULL,
@ -142,5 +142,42 @@ func migrationList(db *sqlx.DB, log *log.Logger) []*sqlxmigrate.Migration {
return nil
},
},
// create new table projects
{
ID: "20190622-01",
Migrate: func(tx *sql.Tx) error {
q1 := `CREATE TYPE project_status_t as enum('active','disabled')`
if _, err := tx.Exec(q1); err != nil {
return errors.WithMessagef(err, "Query failed %s", q1)
}
q2 := `CREATE TABLE IF NOT EXISTS projects (
id char(36) NOT NULL,
account_id char(36) NOT NULL REFERENCES accounts(id),
name varchar(255) NOT NULL,
status project_status_t NOT NULL DEFAULT 'active',
created_at TIMESTAMP WITH TIME ZONE NOT NULL,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NULL,
archived_at TIMESTAMP WITH TIME ZONE DEFAULT NULL,
PRIMARY KEY (id)
)`
if _, err := tx.Exec(q2); err != nil {
return errors.WithMessagef(err, "Query failed %s", q2)
}
return nil
},
Rollback: func(tx *sql.Tx) error {
q1 := `DROP TYPE project_status_t`
if _, err := tx.Exec(q1); err != nil {
return errors.WithMessagef(err, "Query failed %s", q1)
}
q2 := `DROP TABLE IF EXISTS projects`
if _, err := tx.Exec(q2); err != nil {
return errors.WithMessagef(err, "Query failed %s", q2)
}
return nil
},
},
}
}