// Create inserts a new product into the database.
Create inserts a new product into the database.
// Create inserts a new product into the database .
func Create(ctx context.Context, dbConn *db.DB, cp *NewProduct, now time.Time) (*Product, error) {
	ctx, span := trace.StartSpan(ctx, "internal.product.Create")
	defer span.End()
	now = now.Truncate(time.Millisecond)
	p := Product{
		ID:           bson.NewObjectId(),
		Name:         cp.Name,
		Notes:        cp.Notes,
		UnitPrice:    cp.UnitPrice,
		Quantity:     cp.Quantity,
		Family:       cp.Family,
		DateCreated:  now,
		DateModified: now,
	}
	f := func(collection *mgo.Collection) error {
		return collection.Insert(&p)
	}
	if err := dbConn.Execute(ctx, productsCollection, f); err != nil {
		return nil, errors.Wrap(err, fmt.Sprintf("db.products.insert(%s)", db.Query(&p)))
	}
	return &p, nil
}
func Create ( ctx context . Context , dbConn * db . DB , cp * NewProduct , now time . Time ) ( * Product , error ) { ctx , span := trace . StartSpan ( ctx , " " ) defer span . End ( ) now = now . Truncate ( time . Millisecond ) p := Product { ID : bson . NewObjectId ( ) , Name : cp . Name , Notes : cp . Notes , UnitPrice : cp . UnitPrice , Quantity : cp . Quantity , Family : cp . Family , DateCreated : now , DateModified : now , } f := func ( collection * mgo . Collection ) error { return collection . Insert ( & p ) } if err := dbConn . Execute ( ctx , productsCollection , f ) ; err != nil { return nil , errors . Wrap ( err , fmt . Sprintf ( " " , db . Query ( & p ) ) ) } return & p , nil }
Create inserts a new product into the database.
Create inserts a new product into the database .
// Mongo truncates times to milliseconds when storing. We and do the same // here so the value we return is consistent with what we store.