1
1
package app
2
2
3
3
import (
4
+ "crypto/rand"
5
+ "errors"
6
+ "fmt"
7
+ "log/slog"
8
+ "math/big"
9
+
4
10
"github.com/ghostrepo00/go-note/config"
11
+ "github.com/ghostrepo00/go-note/internal/pkg/model"
5
12
"github.com/supabase-community/supabase-go"
6
13
)
7
14
@@ -10,10 +17,57 @@ type appService struct {
10
17
DbClient * supabase.Client
11
18
}
12
19
20
+ type AppService interface {
21
+ GetbyId (id string ) (result []* model.FormData , err error )
22
+ DeletebyId (id string ) (err error )
23
+ Save (data * model.FormData ) error
24
+ }
25
+
13
26
func NewAppService (appConfig * config.AppConfig , dbClient * supabase.Client ) * appService {
14
27
return & appService {appConfig , dbClient }
15
28
}
16
29
17
- func (r * appService ) do () {
30
+ func GenerateRandomId (length int ) (string , error ) {
31
+ const letters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
32
+ result := make ([]byte , length )
33
+ for i := 0 ; i < length ; i ++ {
34
+ num , err := rand .Int (rand .Reader , big .NewInt (int64 (len (letters ))))
35
+ if err != nil {
36
+ return "" , err
37
+ }
38
+ result [i ] = letters [num .Int64 ()]
39
+ }
40
+
41
+ return string (result ), nil
42
+ }
43
+
44
+ func (r * appService ) GetbyId (id string ) (result []* model.FormData , err error ) {
45
+ if id != "" {
46
+ _ , err = r .DbClient .From ("notes" ).Select ("id, content, password, is_encrypted" , "" , false ).Eq ("id" , id ).ExecuteTo (& result )
47
+ fmt .Print (result , err )
48
+ }
49
+ return
50
+ }
51
+
52
+ func (r * appService ) DeletebyId (id string ) (err error ) {
53
+ if id != "" {
54
+ _ , _ , err = r .DbClient .From ("notes" ).Delete ("" , "" ).Eq ("id" , id ).Execute ()
55
+ }
56
+ return
57
+ }
58
+
59
+ func (r * appService ) Save (data * model.FormData ) (err error ) {
60
+ if data .Id == "" {
61
+ if data .Id , err = GenerateRandomId (5 ); err == nil {
62
+ a , cc , d := r .DbClient .From ("notes" ).Select ("id" , "" , false ).Eq ("id" , data .Id ).ExecuteString ()
63
+ slog .Info ("" , "a" , a , "e" , d , "c" , cc )
64
+ }
65
+ }
18
66
67
+ if a , b , err := r .DbClient .From ("notes" ).Upsert (& data , "" , "" , "" ).Execute (); err != nil {
68
+ return errors .New ("ssssssssssssssssssssssssss" )
69
+ } else {
70
+ slog .Info ("supabase" , "a" , a , "b" , b , "c" , err )
71
+ }
72
+ return nil
19
73
}
0 commit comments