-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathownsearch.go
50 lines (41 loc) · 1.18 KB
/
ownsearch.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
package main
import (
"github.com/WiiLink24/MiiContestChannel/common"
"github.com/WiiLink24/MiiContestChannel/plaza"
"github.com/gin-gonic/gin"
"net/http"
"strconv"
)
const GetMiisForArtisan = `SELECT entry_id, initials, perm_likes, skill, country_id, mii_data
FROM miis WHERE artisan_id = $1
ORDER BY likes LIMIT 50`
func ownSearch(c *gin.Context) {
artisanId := c.Query("craftsno")
var miis []common.MiiWithArtisan
rows, err := pool.Query(ctx, GetMiisForArtisan, artisanId)
if err != nil {
c.Status(http.StatusInternalServerError)
writeResult(c, 500)
return
}
for rows.Next() {
var likes int
mii := common.MiiWithArtisan{}
err = rows.Scan(&mii.EntryNumber, &mii.Initials, &likes, &mii.Skill, &mii.CountryCode, &mii.MiiData)
if err != nil {
c.Status(http.StatusInternalServerError)
writeResult(c, 500)
return
}
// Downcast to u8 as database contains numbers larger.
mii.Likes = uint8(likes)
miis = append(miis, mii)
}
intArtisanId, err := strconv.Atoi(artisanId)
if err != nil {
c.Status(http.StatusBadRequest)
writeResult(c, 400)
return
}
c.Data(http.StatusOK, "application/octet-stream", plaza.MakeOwnSearch(miis, uint32(intArtisanId)))
}