-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcheck.go
49 lines (42 loc) · 1.17 KB
/
check.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
package main
import (
"encoding/binary"
"encoding/hex"
"errors"
"github.com/gin-gonic/gin"
"github.com/jackc/pgx/v4"
"net/http"
)
const (
CheckMii = `SELECT entry_id FROM miis WHERE artisan_id = $1 AND mii_id = $2`
)
func check(c *gin.Context) {
artisanId := c.PostForm("craftsno")
strMiiId := c.PostForm("miiid")
data, _ := hex.DecodeString("434800000000000000000000000000000000000000000000FFFFFFFFFFFFFFFF454E000C00000001")
// Validity check for the Mii ID.
miiId, err := hex.DecodeString(strMiiId)
if err != nil {
// TODO: Find invalid Mii ID error code
c.Status(http.StatusBadRequest)
writeResult(c, 400)
return
}
// Check if the Mii is in the database.
var entryId uint32
err = pool.QueryRow(ctx, CheckMii, artisanId, miiId).Scan(&entryId)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
// Mii does not exist, tell the channel to update, not add.
data = binary.BigEndian.AppendUint32(data, 0)
} else {
c.Status(http.StatusInternalServerError)
writeResult(c, 500)
return
}
} else {
// Mii exists, set the entry id.
data = binary.BigEndian.AppendUint32(data, entryId)
}
c.Data(http.StatusOK, "application/octet-stream", data)
}