-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnamesearch.go
74 lines (59 loc) · 1.89 KB
/
namesearch.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
package main
import (
"github.com/WiiLink24/MiiContestChannel/common"
"github.com/WiiLink24/MiiContestChannel/plaza"
"github.com/gin-gonic/gin"
"net/http"
"strconv"
)
const (
GetMiiName = `SELECT miis.nickname FROM miis WHERE entry_id = $1`
GetRelatedMiis = `SELECT miis.entry_id, miis.artisan_id, miis.initials, miis.perm_likes, miis.skill, miis.country_id, miis.mii_data, artisans.mii_data, artisans.is_master FROM miis, artisans WHERE artisans.artisan_id = miis.artisan_id AND miis.nickname ILIKE '%' || $1 || '%' LIMIT 50`
)
func nameSearch(c *gin.Context) {
strEntryNumber := c.Query("entryno")
entryId, err := strconv.Atoi(strEntryNumber)
if err != nil {
// TODO: Figure out invalid entry ID
// It should never happen if this was sent by MCC, but people will be people.
writeResult(c, 108)
return
}
// Next make sure the Mii exists.
var exists bool
err = pool.QueryRow(ctx, DoesMiiExist, entryId).Scan(&exists)
if err != nil {
writeResult(c, 500)
return
}
if !exists {
// Mii does not exist.
c.Data(http.StatusOK, "application/octet-stream", plaza.MakeSearchList(common.NameSearch, nil, uint32(entryId)))
return
}
var nickname string
err = pool.QueryRow(ctx, GetMiiName, entryId).Scan(&nickname)
if err != nil {
writeResult(c, 500)
return
}
rows, err := pool.Query(ctx, GetRelatedMiis, nickname)
if err != nil {
writeResult(c, 500)
return
}
var miis []common.MiiWithArtisan
defer rows.Close()
for rows.Next() {
mii := common.MiiWithArtisan{}
var likes int
var isMaster bool
err = rows.Scan(&mii.EntryNumber, &mii.ArtisanId, &mii.Initials, &likes, &mii.Skill, &mii.CountryCode, &mii.MiiData, &mii.ArtisanMiiData, &isMaster)
if isMaster {
mii.IsMasterArtisan = 1
}
mii.Likes = uint8(likes)
miis = append(miis, mii)
}
c.Data(http.StatusOK, "application/octet-stream", plaza.MakeSearchList(common.NameSearch, miis, uint32(entryId)))
}