2019-05-16 18:05:39 -04:00
|
|
|
package project
|
2019-05-16 10:39:25 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2019-05-23 14:32:24 -05:00
|
|
|
"github.com/jmoiron/sqlx"
|
2019-05-16 10:39:25 -04:00
|
|
|
"github.com/pkg/errors"
|
2019-05-23 14:32:24 -05:00
|
|
|
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
|
|
|
|
"gopkg.in/mgo.v2"
|
2019-05-16 10:39:25 -04:00
|
|
|
"gopkg.in/mgo.v2/bson"
|
|
|
|
)
|
|
|
|
|
2019-05-16 18:05:39 -04:00
|
|
|
const projectsCollection = "projects"
|
2019-05-16 10:39:25 -04:00
|
|
|
|
|
|
|
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")
|
|
|
|
)
|
|
|
|
|
2019-05-16 18:05:39 -04:00
|
|
|
// List retrieves a list of existing projects from the database.
|
2019-05-23 14:32:24 -05:00
|
|
|
func List(ctx context.Context, dbConn *sqlx.DB) ([]Project, error) {
|
|
|
|
span, ctx := tracer.StartSpanFromContext(ctx, "internal.project.List")
|
|
|
|
defer span.Finish()
|
2019-05-16 10:39:25 -04:00
|
|
|
|
2019-05-16 18:05:39 -04:00
|
|
|
p := []Project{}
|
2019-05-16 10:39:25 -04:00
|
|
|
|
|
|
|
f := func(collection *mgo.Collection) error {
|
|
|
|
return collection.Find(nil).All(&p)
|
|
|
|
}
|
2019-05-23 14:32:24 -05:00
|
|
|
|
|
|
|
if _, err := dbConn.ExecContext(ctx, projectsCollection, f); err != nil {
|
2019-05-16 18:05:39 -04:00
|
|
|
return nil, errors.Wrap(err, "db.projects.find()")
|
2019-05-16 10:39:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
2019-05-16 18:05:39 -04:00
|
|
|
// Retrieve gets the specified project from the database.
|
2019-05-23 14:32:24 -05:00
|
|
|
func Retrieve(ctx context.Context, dbConn *sqlx.DB, id string) (*Project, error) {
|
|
|
|
span, ctx := tracer.StartSpanFromContext(ctx, "internal.project.Retrieve")
|
|
|
|
defer span.Finish()
|
2019-05-16 10:39:25 -04:00
|
|
|
|
|
|
|
if !bson.IsObjectIdHex(id) {
|
|
|
|
return nil, ErrInvalidID
|
|
|
|
}
|
|
|
|
|
|
|
|
q := bson.M{"_id": bson.ObjectIdHex(id)}
|
|
|
|
|
2019-05-16 18:05:39 -04:00
|
|
|
var p *Project
|
2019-05-16 10:39:25 -04:00
|
|
|
f := func(collection *mgo.Collection) error {
|
|
|
|
return collection.Find(q).One(&p)
|
|
|
|
}
|
2019-05-23 14:32:24 -05:00
|
|
|
if _, err := dbConn.ExecContext(ctx, projectsCollection, f); err != nil {
|
2019-05-16 10:39:25 -04:00
|
|
|
if err == mgo.ErrNotFound {
|
|
|
|
return nil, ErrNotFound
|
|
|
|
}
|
2019-05-23 14:32:24 -05:00
|
|
|
return nil, errors.Wrap(err, fmt.Sprintf("db.projects.find(%s)", q))
|
2019-05-16 10:39:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
2019-05-16 18:05:39 -04:00
|
|
|
// Create inserts a new project into the database.
|
2019-05-23 14:32:24 -05:00
|
|
|
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()
|
2019-05-16 10:39:25 -04:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
2019-05-16 18:05:39 -04:00
|
|
|
p := Project{
|
2019-05-16 10:39:25 -04:00
|
|
|
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)
|
|
|
|
}
|
2019-05-23 14:32:24 -05:00
|
|
|
if _, err := dbConn.ExecContext(ctx, projectsCollection, f); err != nil {
|
|
|
|
return nil, errors.Wrap(err, fmt.Sprintf("db.projects.insert(%v)", &p))
|
2019-05-16 10:39:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return &p, nil
|
|
|
|
}
|
|
|
|
|
2019-05-16 18:05:39 -04:00
|
|
|
// Update replaces a project document in the database.
|
2019-05-23 14:32:24 -05:00
|
|
|
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()
|
2019-05-16 10:39:25 -04:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2019-05-23 14:32:24 -05:00
|
|
|
if _, err := dbConn.ExecContext(ctx, projectsCollection, f); err != nil {
|
2019-05-16 10:39:25 -04:00
|
|
|
if err == mgo.ErrNotFound {
|
|
|
|
return ErrNotFound
|
|
|
|
}
|
2019-05-23 14:32:24 -05:00
|
|
|
return errors.Wrap(err, fmt.Sprintf("db.customers.update(%s, %s)", q, m))
|
2019-05-16 10:39:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-16 18:05:39 -04:00
|
|
|
// Delete removes a project from the database.
|
2019-05-23 14:32:24 -05:00
|
|
|
func Delete(ctx context.Context, dbConn *sqlx.DB, id string) error {
|
|
|
|
span, ctx := tracer.StartSpanFromContext(ctx, "internal.project.Delete")
|
|
|
|
defer span.Finish()
|
2019-05-16 10:39:25 -04:00
|
|
|
|
|
|
|
if !bson.IsObjectIdHex(id) {
|
|
|
|
return ErrInvalidID
|
|
|
|
}
|
|
|
|
|
|
|
|
q := bson.M{"_id": bson.ObjectIdHex(id)}
|
|
|
|
|
|
|
|
f := func(collection *mgo.Collection) error {
|
|
|
|
return collection.Remove(q)
|
|
|
|
}
|
2019-05-23 14:32:24 -05:00
|
|
|
if _, err := dbConn.ExecContext(ctx, projectsCollection, f); err != nil {
|
2019-05-16 10:39:25 -04:00
|
|
|
if err == mgo.ErrNotFound {
|
|
|
|
return ErrNotFound
|
|
|
|
}
|
2019-05-16 18:05:39 -04:00
|
|
|
return errors.Wrap(err, fmt.Sprintf("db.projects.remove(%v)", q))
|
2019-05-16 10:39:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|