-
Notifications
You must be signed in to change notification settings - Fork 0
/
ss.txt
93 lines (83 loc) · 2.68 KB
/
ss.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// ? gorm unique
// Register the custom validator
// if err := validate.RegisterValidation("unique", uniqueValidator); err != nil {
// panic(err)
// }
func uniqueValidator(fl validator.FieldLevel) bool {
// db := fl.Parent().Addr().Interface().(*gorm.DB)
// db := D.DB()
db := dbEnv.GetDatabaseUrl()
field := fl.FieldName()
// Get the struct's value and use reflection to extract the field's value
var s reflect.Value = fl.Parent()
val := reflect.Indirect(s).FieldByName(field).String()
var count int64
db.Model(fl.Parent()).Where(fmt.Sprintf("%s = ?", field), val).Count(&count)
return count == 0
}
// dev.go
func GormG(c *fiber.Ctx) error {
type pashm struct {
Name string `json:"name" validate:"unique"`
}
payload := new(pashm)
// parse payload
if err := c.BodyParser(payload); err != nil {
U.ResErr(c, err.Error())
}
if errs := U.Validate(payload); errs != nil {
return c.Status(400).JSON(fiber.Map{"errors": errs})
}
return c.SendString("no error")
}
// db.go
// type DBConfig struct {
// Host string
// Port string
// Username string
// Password string
// Server string
// Name string
// }
// dbConfig := config.GetDBConfig()
// dbConfig := DBConfig{}
// dsn := fmt.Sprintf("postgres://%s:%s@%s:%s/%s", dbConfig.Username, dbConfig.Password, dbConfig.Server, dbConfig.Port, dbConfig.Name)
func AdvancedUploadImage(c *fiber.Ctx) error {
// Create a new file to store the uploaded data
// dst, err := os.Create("uploaded_file.txt")
// if err != nil {
// return err
// }
// defer dst.Close()
// // Get the request body stream
// reader := c.Request().BodyStream()
// // Read 1MiB at a time
// buffer := make([]byte, 0, 1024*1024)
// for {
// length, err := io.ReadFull(reader, buffer[:cap(buffer)])
// // Cap the buffer based on the actual length read
// buffer = buffer[:length]
// if err != nil {
// // When the error is EOF, there are no longer any bytes to read
// // meaning the request is completed
// if err == io.EOF {
// break
// }
// // If the error is an unexpected EOF, the requested size to read
// // was larger than what was available. This is not an issue for
// // as long as the length returned above is used, or the buffer
// // is capped as it is above. Any error that is not an unexpected
// // EOF is an actual error, which we handle accordingly
// if err != io.ErrUnexpectedEOF {
// return err
// }
// }
// // Write the buffered data to the destination file
// _, err = dst.Write(buffer)
// if err != nil {
// return err
// }
// }
return c.SendString("DONE")
// return c.JSON(fiber.Map{"data": fiber.Map{"img": c.BaseURL() + "/public/uploads/" + newFileName}})
}