You've already forked golang-saas-starter-kit
mirror of
https://github.com/raseels-repos/golang-saas-starter-kit.git
synced 2025-12-24 00:01:31 +02:00
Basic example cleanup
Rename sales-api to web-api and remove sales-admin
This commit is contained in:
43
example-project/internal/project/models.go
Normal file
43
example-project/internal/project/models.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package project
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gopkg.in/mgo.v2/bson"
|
||||
)
|
||||
|
||||
// Project is an item we sell.
|
||||
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.
|
||||
}
|
||||
|
||||
// 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"`
|
||||
}
|
||||
|
||||
// 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"`
|
||||
}
|
||||
|
||||
// Sale represents a transaction where we sold some quantity of a
|
||||
// Project.
|
||||
type Sale struct{}
|
||||
|
||||
// NewSale defines what we require when creating a Sale record.
|
||||
type NewSale struct{}
|
||||
161
example-project/internal/project/project.go
Normal file
161
example-project/internal/project/project.go
Normal file
@@ -0,0 +1,161 @@
|
||||
package project
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"geeks-accelerator/oss/saas-starter-kit/example-project/internal/platform/db"
|
||||
"github.com/pkg/errors"
|
||||
"go.opencensus.io/trace"
|
||||
mgo "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 *db.DB) ([]Project, error) {
|
||||
ctx, span := trace.StartSpan(ctx, "internal.project.List")
|
||||
defer span.End()
|
||||
|
||||
p := []Project{}
|
||||
|
||||
f := func(collection *mgo.Collection) error {
|
||||
return collection.Find(nil).All(&p)
|
||||
}
|
||||
if err := dbConn.Execute(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 *db.DB, id string) (*Project, error) {
|
||||
ctx, span := trace.StartSpan(ctx, "internal.project.Retrieve")
|
||||
defer span.End()
|
||||
|
||||
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.Execute(ctx, projectsCollection, f); err != nil {
|
||||
if err == mgo.ErrNotFound {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
return nil, errors.Wrap(err, fmt.Sprintf("db.projects.find(%s)", db.Query(q)))
|
||||
}
|
||||
|
||||
return p, nil
|
||||
}
|
||||
|
||||
// Create inserts a new project into the database.
|
||||
func Create(ctx context.Context, dbConn *db.DB, cp *NewProject, now time.Time) (*Project, error) {
|
||||
ctx, span := trace.StartSpan(ctx, "internal.project.Create")
|
||||
defer span.End()
|
||||
|
||||
// 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.Execute(ctx, projectsCollection, f); err != nil {
|
||||
return nil, errors.Wrap(err, fmt.Sprintf("db.projects.insert(%s)", db.Query(&p)))
|
||||
}
|
||||
|
||||
return &p, nil
|
||||
}
|
||||
|
||||
// Update replaces a project document in the database.
|
||||
func Update(ctx context.Context, dbConn *db.DB, id string, upd UpdateProject, now time.Time) error {
|
||||
ctx, span := trace.StartSpan(ctx, "internal.project.Update")
|
||||
defer span.End()
|
||||
|
||||
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.Execute(ctx, projectsCollection, f); err != nil {
|
||||
if err == mgo.ErrNotFound {
|
||||
return ErrNotFound
|
||||
}
|
||||
return errors.Wrap(err, fmt.Sprintf("db.customers.update(%s, %s)", db.Query(q), db.Query(m)))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Delete removes a project from the database.
|
||||
func Delete(ctx context.Context, dbConn *db.DB, id string) error {
|
||||
ctx, span := trace.StartSpan(ctx, "internal.project.Delete")
|
||||
defer span.End()
|
||||
|
||||
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.Execute(ctx, projectsCollection, f); err != nil {
|
||||
if err == mgo.ErrNotFound {
|
||||
return ErrNotFound
|
||||
}
|
||||
return errors.Wrap(err, fmt.Sprintf("db.projects.remove(%v)", q))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
129
example-project/internal/project/project_test.go
Normal file
129
example-project/internal/project/project_test.go
Normal file
@@ -0,0 +1,129 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user