search query
Cette révision appartient à :
Parent
6a49afab5f
révision
71b2b798b8
5 fichiers modifiés avec 65 ajouts et 18 suppressions
|
@ -8,14 +8,14 @@ import (
|
|||
type omit bool
|
||||
|
||||
type User struct {
|
||||
Id uint `gorm:"column:user_id;primary_key"`
|
||||
Username string `gorm:"column:username"`
|
||||
Password string `gorm:"column:password"`
|
||||
Email string `gorm:"column:email"`
|
||||
Status int `gorm:"column:status"`
|
||||
CreatedAt time.Time `gorm:"column:created_at"`
|
||||
UpdatedAt time.Time `gorm:"column:updated_at"`
|
||||
/*Api*/Token string `gorm:"column:api_token"`
|
||||
Id uint `gorm:"column:user_id;primary_key"`
|
||||
Username string `gorm:"column:username"`
|
||||
Password string `gorm:"column:password"`
|
||||
Email string `gorm:"column:email"`
|
||||
Status int `gorm:"column:status"`
|
||||
CreatedAt time.Time `gorm:"column:created_at"`
|
||||
UpdatedAt time.Time `gorm:"column:updated_at"`
|
||||
/*Api*/ Token string `gorm:"column:api_token"`
|
||||
//ApiTokenExpiry
|
||||
|
||||
// Liking
|
||||
|
@ -24,14 +24,14 @@ type User struct {
|
|||
Likings []User `gorm:"foreignkey:userId;associationforeignkey:follower_id;many2many:users_followers;"`
|
||||
Liked []User `gorm:"foreignkey:follower_id;associationforeignkey:userId;many2many:users_followers;"`
|
||||
|
||||
Md5 string `json:"md5"`
|
||||
TokenExpiration time.Time `gorm:"column:api_token_expiry"`
|
||||
Language string `gorm:"column:language"`
|
||||
Torrents []Torrents `gorm:"ForeignKey:UploaderId"`
|
||||
Md5 string `json:"md5"`
|
||||
TokenExpiration time.Time `gorm:"column:api_token_expiry"`
|
||||
Language string `gorm:"column:language"`
|
||||
Torrents []Torrents `gorm:"ForeignKey:UploaderId"`
|
||||
}
|
||||
|
||||
type PublicUser struct {
|
||||
User *User
|
||||
User *User
|
||||
}
|
||||
|
||||
// UsersFollowers is a relation table to relate users each other.
|
||||
|
|
|
@ -5,6 +5,7 @@ import (
|
|||
"fmt"
|
||||
"html"
|
||||
"net/http"
|
||||
"sort"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
|
@ -19,19 +20,65 @@ import (
|
|||
func ApiHandler(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
page := vars["page"]
|
||||
whereParams := torrentService.WhereParams{}
|
||||
|
||||
maxPerPage, errConv := strconv.Atoi(r.URL.Query().Get("max"))
|
||||
if errConv != nil {
|
||||
maxPerPage = 50 // default Value maxPerPage
|
||||
}
|
||||
|
||||
nbTorrents := 0
|
||||
pagenum, _ := strconv.Atoi(html.EscapeString(page))
|
||||
if pagenum == 0 {
|
||||
pagenum = 1
|
||||
}
|
||||
|
||||
torrents, nbTorrents, err := torrentService.GetAllTorrents(maxPerPage, maxPerPage*(pagenum-1))
|
||||
contentType := r.Header.Get("Content-Type")
|
||||
if contentType == "application/json" {
|
||||
d := json.NewDecoder(r.Body)
|
||||
d.UseNumber()
|
||||
|
||||
var f interface{}
|
||||
if err := d.Decode(&f); err != nil {
|
||||
util.SendError(w, err, 502)
|
||||
}
|
||||
|
||||
m := f.(map[string]interface{})
|
||||
var keys []string
|
||||
for k := range m {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
|
||||
conditions := ""
|
||||
for i, k := range keys {
|
||||
if k == "page" {
|
||||
pagenum64, _ := m[k].(json.Number).Int64()
|
||||
pagenum = int(pagenum64)
|
||||
} else if k == "limit" {
|
||||
maxPerPage64, _ := m[k].(json.Number).Int64()
|
||||
maxPerPage = int(maxPerPage64)
|
||||
} else {
|
||||
if i != 0 {
|
||||
conditions += "AND "
|
||||
}
|
||||
conditions += k
|
||||
switch m[k].(type) {
|
||||
case json.Number:
|
||||
conditions += " = ? "
|
||||
case string:
|
||||
conditions += " ILIKE ? "
|
||||
}
|
||||
}
|
||||
}
|
||||
whereParams.Conditions = conditions
|
||||
whereParams.Params = make([]interface{}, len(keys))
|
||||
for i, k := range keys {
|
||||
whereParams.Params[i] = m[k]
|
||||
}
|
||||
}
|
||||
|
||||
nbTorrents := 0
|
||||
torrents, nbTorrents, err := torrentService.GetTorrents(whereParams, maxPerPage, maxPerPage*(pagenum-1))
|
||||
if err != nil {
|
||||
util.SendError(w, err, 400)
|
||||
return
|
||||
|
|
|
@ -46,7 +46,7 @@ func init() {
|
|||
Router.Handle("/page/{page:[0-9]+}", gzipHomeHandler).Name("home_page")
|
||||
Router.Handle("/search", gzipSearchHandler).Name("search")
|
||||
Router.Handle("/search/{page}", gzipSearchHandler).Name("search_page")
|
||||
Router.Handle("/api/{page}", gzipAPIHandler).Methods("GET")
|
||||
Router.Handle("/api/{page:[0-9]*}", gzipAPIHandler).Methods("GET")
|
||||
Router.Handle("/api/upload", gzipAPIUploadHandler).Methods("POST")
|
||||
Router.Handle("/api/view/{id}", gzipAPIViewHandler).Methods("GET")
|
||||
Router.Handle("/faq", gzipFaqHandler).Name("faq")
|
||||
|
|
1
service/api/api.go
Fichier normal
1
service/api/api.go
Fichier normal
|
@ -0,0 +1 @@
|
|||
package apiService
|
|
@ -61,7 +61,7 @@ func CreateUserFromForm(registrationForm formStruct.RegistrationForm) (model.Use
|
|||
var user model.User
|
||||
log.Debugf("registrationForm %+v\n", registrationForm)
|
||||
modelHelper.AssignValue(&user, ®istrationForm)
|
||||
user.Md5 = crypto.GenerateMD5Hash(user.Email) // Gravatar
|
||||
user.Md5 = crypto.GenerateMD5Hash(user.Email) // Gravatar
|
||||
token, err := crypto.GenerateRandomToken32()
|
||||
if err != nil {
|
||||
return user, errors.New("Token not generated.")
|
||||
|
@ -127,7 +127,6 @@ func RetrieveUser(r *http.Request, id string) (*model.PublicUser, bool, uint, in
|
|||
func RetrieveUsers() []*model.PublicUser {
|
||||
var users []*model.User
|
||||
var userArr []*model.PublicUser
|
||||
db.ORM.Find(&users)
|
||||
for _, user := range users {
|
||||
userArr = append(userArr, &model.PublicUser{User: user})
|
||||
}
|
||||
|
|
Référencer dans un nouveau ticket