Explorar el Código

add crud time

sainw hace 3 años
padre
commit
58fdaa9dba
Se han modificado 1 ficheros con 69 adiciones y 0 borrados
  1. 69 0
      fire/crud_time.go

+ 69 - 0
fire/crud_time.go

@@ -0,0 +1,69 @@
+package fire
+
+import (
+	"time"
+
+	"cloud.google.com/go/firestore"
+)
+
+type cudTime struct {
+	Time    time.Time
+	TimeSec int64
+}
+
+// newCudTime returns a new newCudTime pointer
+func NewCudTime() *cudTime {
+	now, _ := NowMM()
+	sec := TimestampMilli(*now)
+	return &cudTime{Time: *now, TimeSec: sec}
+}
+
+func (up *cudTime) toMapForCreate() map[string]interface{} {
+	val := make(map[string]interface{})
+	val["delete_time"] = 0
+	val["is_deleted"] = false
+	val["update_time"] = up.TimeSec
+	val["updated_date"] = up.Time
+	val["created_date"] = up.Time
+	return val
+}
+
+func (up *cudTime) toMapForUpdate() map[string]interface{} {
+	val := make(map[string]interface{})
+	val["update_time"] = up.TimeSec
+	val["updated_date"] = up.Time
+	return val
+}
+
+func (up *cudTime) toMapForDelete() map[string]interface{} {
+	val := make(map[string]interface{})
+	val["is_deleted"] = true
+	val["update_time"] = up.TimeSec
+	val["delete_time"] = up.TimeSec
+	val["updated_date"] = up.Time
+	val["deleted_date"] = up.Time
+	return val
+}
+
+func (up *cudTime) C(tx *firestore.Transaction, docRefs ...*firestore.DocumentRef) error {
+	return up._op(tx, up.toMapForCreate(), docRefs...)
+}
+func (up *cudTime) U(tx *firestore.Transaction, docRefs ...*firestore.DocumentRef) error {
+	return up._op(tx, up.toMapForUpdate(), docRefs...)
+}
+
+func (up *cudTime) D(tx *firestore.Transaction, docRefs ...*firestore.DocumentRef) error {
+	return up._op(tx, up.toMapForDelete(), docRefs...)
+}
+
+func (up *cudTime) _op(tx *firestore.Transaction, data map[string]interface{}, docRefs ...*firestore.DocumentRef) error {
+	if len(docRefs) <= 0 {
+		return nil
+	}
+	for _, docRef := range docRefs {
+		if err := tx.Set(docRef, data, firestore.MergeAll); err != nil {
+			return err
+		}
+	}
+	return nil
+}