1
0

2 Ревизии dfb82e7ada ... 58fdaa9dba

Автор SHA1 Съобщение Дата
  sainw 58fdaa9dba add crud time преди 3 години
  sainw debb5501d1 fix module name преди 3 години
променени са 5 файла, в които са добавени 73 реда и са изтрити 4 реда
  1. 1 1
      fire/auth.go
  2. 69 0
      fire/crud_time.go
  3. 1 1
      fire/handler.go
  4. 1 1
      fire/payload.go
  5. 1 1
      go.mod

+ 1 - 1
fire/auth.go

@@ -6,7 +6,7 @@ import (
 	"strings"
 
 	firebase "firebase.google.com/go"
-	"git.mokkon.com/sainw/server_helper/util"
+	"git.mokkon.com/sainw/server_helper.git/util"
 )
 
 // ContextKey to be used as context key type

+ 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
+}

+ 1 - 1
fire/handler.go

@@ -3,7 +3,7 @@ package fire
 import (
 	"net/http"
 
-	"git.mokkon.com/sainw/server_helper/util"
+	"git.mokkon.com/sainw/server_helper.git/util"
 )
 
 func ApiHandler(ep *EndPoint) http.Handler {

+ 1 - 1
fire/payload.go

@@ -3,7 +3,7 @@ package fire
 import (
 	"errors"
 
-	"git.mokkon.com/sainw/server_helper/util"
+	"git.mokkon.com/sainw/server_helper.git/util"
 )
 
 // Payload defines the payload

+ 1 - 1
go.mod

@@ -1,4 +1,4 @@
-module git.mokkon.com/sainw/server_helper
+module git.mokkon.com/sainw/server_helper.git
 
 go 1.17