Simplify string to int conversion where possible
This commit is contained in:
parent
2f027ce853
commit
32738aaae0
|
@ -4,6 +4,8 @@ import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
|
"git.zxq.co/ripple/rippleapi/common"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -23,7 +25,7 @@ func genmode(m string) string {
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
func genmodei(m string) int {
|
func genmodei(m string) int {
|
||||||
v, _ := strconv.Atoi(m)
|
v := common.Int(m)
|
||||||
if v > 3 || v < 0 {
|
if v > 3 || v < 0 {
|
||||||
v = 0
|
v = 0
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,6 @@ package v1
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"strconv"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.zxq.co/ripple/rippleapi/common"
|
"git.zxq.co/ripple/rippleapi/common"
|
||||||
|
@ -119,11 +118,11 @@ func BeatmapGET(md common.MethodData) common.CodeMessager {
|
||||||
if md.C.Query("s") == "" && md.C.Query("b") == "" {
|
if md.C.Query("s") == "" && md.C.Query("b") == "" {
|
||||||
return common.SimpleResponse(400, "Must pass either querystring param 'b' or 's'")
|
return common.SimpleResponse(400, "Must pass either querystring param 'b' or 's'")
|
||||||
}
|
}
|
||||||
setID, _ := strconv.Atoi(md.C.Query("s"))
|
setID := common.Int(md.C.Query("s"))
|
||||||
if setID != 0 {
|
if setID != 0 {
|
||||||
return getSet(md, setID)
|
return getSet(md, setID)
|
||||||
}
|
}
|
||||||
beatmapID, _ := strconv.Atoi(md.C.Query("b"))
|
beatmapID := common.Int(md.C.Query("b"))
|
||||||
if beatmapID != 0 {
|
if beatmapID != 0 {
|
||||||
return getBeatmap(md, beatmapID)
|
return getBeatmap(md, beatmapID)
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,6 @@ package v1
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"strconv"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.zxq.co/ripple/rippleapi/common"
|
"git.zxq.co/ripple/rippleapi/common"
|
||||||
|
@ -116,11 +115,11 @@ type friendsWithResponse struct {
|
||||||
|
|
||||||
// FriendsWithGET checks the current user is friends with the one passed in the request path.
|
// FriendsWithGET checks the current user is friends with the one passed in the request path.
|
||||||
func FriendsWithGET(md common.MethodData) common.CodeMessager {
|
func FriendsWithGET(md common.MethodData) common.CodeMessager {
|
||||||
r := friendsWithResponse{}
|
var r friendsWithResponse
|
||||||
r.Code = 200
|
r.Code = 200
|
||||||
uid, err := strconv.Atoi(md.C.Query("id"))
|
uid := common.Int(md.C.Query("id"))
|
||||||
if err != nil {
|
if uid == 0 {
|
||||||
return common.SimpleResponse(400, "That is not a number!")
|
return r
|
||||||
}
|
}
|
||||||
err = md.DB.QueryRow("SELECT EXISTS(SELECT 1 FROM users_relationships WHERE user1 = ? AND user2 = ? LIMIT 1), EXISTS(SELECT 1 FROM users_relationships WHERE user2 = ? AND user1 = ? LIMIT 1)", md.ID(), uid, md.ID(), uid).Scan(&r.Friends, &r.Mutual)
|
err = md.DB.QueryRow("SELECT EXISTS(SELECT 1 FROM users_relationships WHERE user1 = ? AND user2 = ? LIMIT 1), EXISTS(SELECT 1 FROM users_relationships WHERE user2 = ? AND user1 = ? LIMIT 1)", md.ID(), uid, md.ID(), uid).Scan(&r.Friends, &r.Mutual)
|
||||||
if err != sql.ErrNoRows && err != nil {
|
if err != sql.ErrNoRows && err != nil {
|
||||||
|
@ -135,12 +134,7 @@ func FriendsWithGET(md common.MethodData) common.CodeMessager {
|
||||||
|
|
||||||
// FriendsAddGET is the GET version of FriendsAddPOST.
|
// FriendsAddGET is the GET version of FriendsAddPOST.
|
||||||
func FriendsAddGET(md common.MethodData) common.CodeMessager {
|
func FriendsAddGET(md common.MethodData) common.CodeMessager {
|
||||||
uidS := md.C.Query("id")
|
return addFriend(md, common.Int(md.C.Query("id")))
|
||||||
uid, err := strconv.Atoi(uidS)
|
|
||||||
if err != nil {
|
|
||||||
return common.SimpleResponse(400, "Nope. That's not a number.")
|
|
||||||
}
|
|
||||||
return addFriend(md, uid)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func addFriend(md common.MethodData, u int) common.CodeMessager {
|
func addFriend(md common.MethodData, u int) common.CodeMessager {
|
||||||
|
@ -184,12 +178,7 @@ func userExists(md common.MethodData, u int) (r bool) {
|
||||||
|
|
||||||
// FriendsDelGET is the GET version of FriendDelPOST.
|
// FriendsDelGET is the GET version of FriendDelPOST.
|
||||||
func FriendsDelGET(md common.MethodData) common.CodeMessager {
|
func FriendsDelGET(md common.MethodData) common.CodeMessager {
|
||||||
uidS := md.C.Query("id")
|
return delFriend(md, common.Int(md.C.Query("id")))
|
||||||
uid, err := strconv.Atoi(uidS)
|
|
||||||
if err != nil {
|
|
||||||
return common.SimpleResponse(400, "Nope. That's not a number.")
|
|
||||||
}
|
|
||||||
return delFriend(md, uid)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func delFriend(md common.MethodData, u int) common.CodeMessager {
|
func delFriend(md common.MethodData, u int) common.CodeMessager {
|
||||||
|
|
|
@ -73,10 +73,8 @@ func badgesToArray(badges string) []int {
|
||||||
badgesSl := strings.Split(badges, ",")
|
badgesSl := strings.Split(badges, ",")
|
||||||
for _, badge := range badgesSl {
|
for _, badge := range badgesSl {
|
||||||
if badge != "" && badge != "0" {
|
if badge != "" && badge != "0" {
|
||||||
// We are ignoring errors because who really gives a shit if something's gone wrong on our end in this
|
nb := common.Int(badge)
|
||||||
// particular thing, we can just silently ignore this.
|
if nb != 0 {
|
||||||
nb, err := strconv.Atoi(badge)
|
|
||||||
if err == nil && nb != 0 {
|
|
||||||
end = append(end, nb)
|
end = append(end, nb)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
7
common/int.go
Normal file
7
common/int.go
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
package common
|
||||||
|
|
||||||
|
// Int converts s to an int. If s in an invalid int, it defaults to 0.
|
||||||
|
func Int(s string) int {
|
||||||
|
r, _ := strconv.Atoi(s)
|
||||||
|
return r
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user