Simplify string to int conversion where possible
This commit is contained in:
parent
2f027ce853
commit
32738aaae0
|
@ -4,6 +4,8 @@ import (
|
|||
"database/sql"
|
||||
"strconv"
|
||||
|
||||
"git.zxq.co/ripple/rippleapi/common"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
|
@ -23,7 +25,7 @@ func genmode(m string) string {
|
|||
return m
|
||||
}
|
||||
func genmodei(m string) int {
|
||||
v, _ := strconv.Atoi(m)
|
||||
v := common.Int(m)
|
||||
if v > 3 || v < 0 {
|
||||
v = 0
|
||||
}
|
||||
|
|
|
@ -2,7 +2,6 @@ package v1
|
|||
|
||||
import (
|
||||
"database/sql"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"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") == "" {
|
||||
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 {
|
||||
return getSet(md, setID)
|
||||
}
|
||||
beatmapID, _ := strconv.Atoi(md.C.Query("b"))
|
||||
beatmapID := common.Int(md.C.Query("b"))
|
||||
if beatmapID != 0 {
|
||||
return getBeatmap(md, beatmapID)
|
||||
}
|
||||
|
|
|
@ -2,7 +2,6 @@ package v1
|
|||
|
||||
import (
|
||||
"database/sql"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"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.
|
||||
func FriendsWithGET(md common.MethodData) common.CodeMessager {
|
||||
r := friendsWithResponse{}
|
||||
var r friendsWithResponse
|
||||
r.Code = 200
|
||||
uid, err := strconv.Atoi(md.C.Query("id"))
|
||||
if err != nil {
|
||||
return common.SimpleResponse(400, "That is not a number!")
|
||||
uid := common.Int(md.C.Query("id"))
|
||||
if uid == 0 {
|
||||
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)
|
||||
if err != sql.ErrNoRows && err != nil {
|
||||
|
@ -135,12 +134,7 @@ func FriendsWithGET(md common.MethodData) common.CodeMessager {
|
|||
|
||||
// FriendsAddGET is the GET version of FriendsAddPOST.
|
||||
func FriendsAddGET(md common.MethodData) common.CodeMessager {
|
||||
uidS := 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)
|
||||
return addFriend(md, common.Int(md.C.Query("id")))
|
||||
}
|
||||
|
||||
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.
|
||||
func FriendsDelGET(md common.MethodData) common.CodeMessager {
|
||||
uidS := 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)
|
||||
return delFriend(md, common.Int(md.C.Query("id")))
|
||||
}
|
||||
|
||||
func delFriend(md common.MethodData, u int) common.CodeMessager {
|
||||
|
|
|
@ -73,10 +73,8 @@ func badgesToArray(badges string) []int {
|
|||
badgesSl := strings.Split(badges, ",")
|
||||
for _, badge := range badgesSl {
|
||||
if badge != "" && badge != "0" {
|
||||
// We are ignoring errors because who really gives a shit if something's gone wrong on our end in this
|
||||
// particular thing, we can just silently ignore this.
|
||||
nb, err := strconv.Atoi(badge)
|
||||
if err == nil && nb != 0 {
|
||||
nb := common.Int(badge)
|
||||
if nb != 0 {
|
||||
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