-
Notifications
You must be signed in to change notification settings - Fork 0
/
updateprofile.go
87 lines (77 loc) · 2.83 KB
/
updateprofile.go
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
// Package api provides API for chestnut.
package api
import (
"bytes"
"fmt"
"image"
"net/http"
"strings"
"github.com/go-playground/validator/v10"
"github.com/labstack/echo/v4"
"github.com/lixvyang/chestnut/chain"
chestnutpb "github.com/lixvyang/chestnut/pb"
)
type CustomValidatorProfile struct {
Validator *validator.Validate
}
func (cv *CustomValidatorProfile) Validate(i interface{}) error {
switch i.(type) {
case *chestnutpb.Activity:
inputobj := i.(*chestnutpb.Activity)
if inputobj.Type == Update {
if inputobj.Person != nil && inputobj.Target != nil {
if inputobj.Target.Type == Group && inputobj.Target.Id != "" {
if inputobj.Person.Name != "" || inputobj.Person.Image != nil || inputobj.Person.Wallet != nil {
return nil
}
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("Person must have name or image fields"))
}
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("Target Group must not be nil"))
}
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("Object and Target Object must not be nil"))
}
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("unknown type of Actitity: %s", inputobj.Type))
default:
if err := cv.Validator.Struct(i); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
}
return nil
}
func (h *Handler) UpdateProfile(c echo.Context) (err error) {
output := make(map[string]string)
paramspb := new(chestnutpb.Activity)
if err = c.Bind(paramspb); err != nil {
output[ERROR_INFO] = err.Error()
return c.JSON(http.StatusBadRequest, output)
}
validate := &CustomValidatorProfile{Validator: validator.New()}
if err = validate.Validate(paramspb); err != nil {
output[ERROR_INFO] = err.Error()
return c.JSON(http.StatusBadRequest, output)
}
groupmgr := chain.GetGroupMgr()
if group, ok := groupmgr.Groups[paramspb.Target.Id]; ok {
if paramspb.Person.Image != nil {
_, formatname, err := image.Decode(bytes.NewReader(paramspb.Person.Image.Content))
if err != nil {
output[ERROR_INFO] = err.Error()
return c.JSON(http.StatusBadRequest, output)
}
if fmt.Sprintf("image/%s", formatname) != strings.ToLower(paramspb.Person.Image.MediaType) {
output[ERROR_INFO] = fmt.Sprintf("image format don't match, mediatype is %s but the file is %s", strings.ToLower(paramspb.Person.Image.MediaType), fmt.Sprintf("image/%s", formatname))
return c.JSON(http.StatusBadRequest, output)
}
}
trxId, err := group.PostToGroup(paramspb.Person)
if err != nil {
output[ERROR_INFO] = err.Error()
return c.JSON(http.StatusBadRequest, output)
}
output[TRX_ID] = trxId
return c.JSON(http.StatusOK, output)
} else {
output[ERROR_INFO] = fmt.Sprintf("Group %s not exist ", paramspb.Target.Id)
return c.JSON(http.StatusBadRequest, output)
}
}