talent/api.go

74 lines
1.4 KiB
Go
Raw Normal View History

2022-01-03 05:06:26 +00:00
package main
import (
"path/filepath"
"strconv"
"strings"
"time"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/rs/zerolog/log"
)
func ApiUpdateEmail(c *gin.Context) {
data := struct {
2022-01-04 05:01:42 +00:00
ID string `json:"id" binding:"required"`
Email string `json:"email" binding:"required,email"`
2022-01-03 05:06:26 +00:00
}{}
if err := c.ShouldBindJSON(&data); err != nil {
panic(err)
}
usr := &User{}
if err := DB.First(usr, "id = ?", data.ID).Error; err != nil {
// create user fallback
log.Error().Err(err).Msg("No User")
}
DB.Model(&User{}).Where("id = ?", data.ID).Update("email", data.Email)
OK(c, "ok")
}
func ApiPostReceipt(c *gin.Context) {
oid := c.Param("oid")
if oid == "" {
panic(ErrorBadRequest)
}
// order exists
order := &Order{}
if err := DB.First(order, "id = ?", oid).Error; err != nil {
panic(err)
}
// get file
f, err := c.FormFile("file")
if err != nil {
panic(err)
}
id := uuid.New()
ext := filepath.Ext(f.Filename)
storedFileName := strings.Replace(id.String(), "-", "", -1) + ext
t := time.Now()
dir := filepath.Join(
strconv.Itoa(t.Year()),
strconv.Itoa(int(t.Month())),
)
dest := filepath.Join(
"attachment",
dir,
)
Mkdir(dest)
// save file
dest = filepath.Join(dest, storedFileName)
if err = c.SaveUploadedFile(f, dest); err != nil {
panic(err)
}
if err = DB.Model(order).Update("attachment", filepath.Join(dir, storedFileName)).
Error; err != nil {
panic(err)
}
OK(c, "ok")
}