mirror of
https://github.com/navidrome/navidrome.git
synced 2025-04-15 03:30:39 +03:00
Removed example code, introduced tests for controllers
This commit is contained in:
parent
ce240cfeff
commit
1a20a1217b
@ -12,7 +12,7 @@ func (c *MainController) Get() {
|
||||
}
|
||||
|
||||
func (c *MainController) Error404() {
|
||||
if beego.BConfig.RunMode == beego.DEV || beego.BConfig.Log.AccessLogs {
|
||||
if beego.BConfig.RunMode != beego.PROD || beego.BConfig.Log.AccessLogs {
|
||||
r := c.Ctx.Request
|
||||
devInfo := fmt.Sprintf(" | %-10s | %-40s | %-16s | %-10s |", r.Method, r.URL.Path, " ", "notmatch")
|
||||
if beego.DefaultAccessLogFilter == nil || !beego.DefaultAccessLogFilter.Filter(c.Ctx) {
|
||||
|
28
controllers/main_test.go
Normal file
28
controllers/main_test.go
Normal file
@ -0,0 +1,28 @@
|
||||
package controllers_test
|
||||
|
||||
import (
|
||||
"github.com/deluan/gosonic/tests"
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
"testing"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"github.com/astaxie/beego"
|
||||
"fmt"
|
||||
_ "github.com/deluan/gosonic/routers"
|
||||
)
|
||||
|
||||
func TestMainController(t *testing.T) {
|
||||
tests.Init(t, false)
|
||||
|
||||
r, _ := http.NewRequest("GET", "/INVALID_PATH", nil)
|
||||
w := httptest.NewRecorder()
|
||||
beego.BeeApp.Handlers.ServeHTTP(w, r)
|
||||
|
||||
beego.Debug("testing", "TestMainController", fmt.Sprintf("\nUrl: %s\nStatus Code: [%d]\n%s", r.URL, w.Code, w.Body.String()))
|
||||
|
||||
Convey("Subject: Error404\n", t, func() {
|
||||
Convey("Status code should be 404", func() {
|
||||
So(w.Code, ShouldEqual, 404)
|
||||
})
|
||||
})
|
||||
}
|
@ -1,91 +0,0 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/deluan/gosonic/models"
|
||||
|
||||
"github.com/astaxie/beego"
|
||||
)
|
||||
|
||||
// Operations about object
|
||||
type ObjectController struct {
|
||||
beego.Controller
|
||||
}
|
||||
|
||||
// @Title create
|
||||
// @Description create object
|
||||
// @Param body body models.Object true "The object content"
|
||||
// @Success 200 {string} models.Object.Id
|
||||
// @Failure 403 body is empty
|
||||
// @router / [post]
|
||||
func (o *ObjectController) Post() {
|
||||
var ob models.Object
|
||||
json.Unmarshal(o.Ctx.Input.RequestBody, &ob)
|
||||
objectid := models.AddOne(ob)
|
||||
o.Data["json"] = map[string]string{"ObjectId": objectid}
|
||||
o.ServeJSON()
|
||||
}
|
||||
|
||||
// @Title Get
|
||||
// @Description find object by objectid
|
||||
// @Param objectId path string true "the objectid you want to get"
|
||||
// @Success 200 {object} models.Object
|
||||
// @Failure 403 :objectId is empty
|
||||
// @router /:objectId [get]
|
||||
func (o *ObjectController) Get() {
|
||||
objectId := o.Ctx.Input.Param(":objectId")
|
||||
if objectId != "" {
|
||||
ob, err := models.GetOne(objectId)
|
||||
if err != nil {
|
||||
o.Data["json"] = err.Error()
|
||||
} else {
|
||||
o.Data["json"] = ob
|
||||
}
|
||||
}
|
||||
o.ServeJSON()
|
||||
}
|
||||
|
||||
// @Title GetAll
|
||||
// @Description get all objects
|
||||
// @Success 200 {object} models.Object
|
||||
// @Failure 403 :objectId is empty
|
||||
// @router / [get]
|
||||
func (o *ObjectController) GetAll() {
|
||||
obs := models.GetAll()
|
||||
o.Data["json"] = obs
|
||||
o.ServeJSON()
|
||||
}
|
||||
|
||||
// @Title update
|
||||
// @Description update the object
|
||||
// @Param objectId path string true "The objectid you want to update"
|
||||
// @Param body body models.Object true "The body"
|
||||
// @Success 200 {object} models.Object
|
||||
// @Failure 403 :objectId is empty
|
||||
// @router /:objectId [put]
|
||||
func (o *ObjectController) Put() {
|
||||
objectId := o.Ctx.Input.Param(":objectId")
|
||||
var ob models.Object
|
||||
json.Unmarshal(o.Ctx.Input.RequestBody, &ob)
|
||||
|
||||
err := models.Update(objectId, ob.Score)
|
||||
if err != nil {
|
||||
o.Data["json"] = err.Error()
|
||||
} else {
|
||||
o.Data["json"] = "update success!"
|
||||
}
|
||||
o.ServeJSON()
|
||||
}
|
||||
|
||||
// @Title delete
|
||||
// @Description delete the object
|
||||
// @Param objectId path string true "The objectId you want to delete"
|
||||
// @Success 200 {string} delete success!
|
||||
// @Failure 403 objectId is empty
|
||||
// @router /:objectId [delete]
|
||||
func (o *ObjectController) Delete() {
|
||||
objectId := o.Ctx.Input.Param(":objectId")
|
||||
models.Delete(objectId)
|
||||
o.Data["json"] = "delete success!"
|
||||
o.ServeJSON()
|
||||
}
|
@ -1,118 +0,0 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/deluan/gosonic/models"
|
||||
|
||||
"github.com/astaxie/beego"
|
||||
)
|
||||
|
||||
// Operations about Users
|
||||
type UserController struct {
|
||||
beego.Controller
|
||||
}
|
||||
|
||||
// @Title createUser
|
||||
// @Description create users
|
||||
// @Param body body models.User true "body for user content"
|
||||
// @Success 200 {int} models.User.Id
|
||||
// @Failure 403 body is empty
|
||||
// @router / [post]
|
||||
func (u *UserController) Post() {
|
||||
var user models.User
|
||||
json.Unmarshal(u.Ctx.Input.RequestBody, &user)
|
||||
uid := models.AddUser(user)
|
||||
u.Data["json"] = map[string]string{"uid": uid}
|
||||
u.ServeJSON()
|
||||
}
|
||||
|
||||
// @Title Get
|
||||
// @Description get all Users
|
||||
// @Success 200 {object} models.User
|
||||
// @router / [get]
|
||||
func (u *UserController) GetAll() {
|
||||
users := models.GetAllUsers()
|
||||
u.Data["json"] = users
|
||||
u.ServeJSON()
|
||||
}
|
||||
|
||||
// @Title Get
|
||||
// @Description get user by uid
|
||||
// @Param uid path string true "The key for staticblock"
|
||||
// @Success 200 {object} models.User
|
||||
// @Failure 403 :uid is empty
|
||||
// @router /:uid [get]
|
||||
func (u *UserController) Get() {
|
||||
uid := u.GetString(":uid")
|
||||
if uid != "" {
|
||||
user, err := models.GetUser(uid)
|
||||
if err != nil {
|
||||
u.Data["json"] = err.Error()
|
||||
} else {
|
||||
u.Data["json"] = user
|
||||
}
|
||||
}
|
||||
u.ServeJSON()
|
||||
}
|
||||
|
||||
// @Title update
|
||||
// @Description update the user
|
||||
// @Param uid path string true "The uid you want to update"
|
||||
// @Param body body models.User true "body for user content"
|
||||
// @Success 200 {object} models.User
|
||||
// @Failure 403 :uid is not int
|
||||
// @router /:uid [put]
|
||||
func (u *UserController) Put() {
|
||||
uid := u.GetString(":uid")
|
||||
if uid != "" {
|
||||
var user models.User
|
||||
json.Unmarshal(u.Ctx.Input.RequestBody, &user)
|
||||
uu, err := models.UpdateUser(uid, &user)
|
||||
if err != nil {
|
||||
u.Data["json"] = err.Error()
|
||||
} else {
|
||||
u.Data["json"] = uu
|
||||
}
|
||||
}
|
||||
u.ServeJSON()
|
||||
}
|
||||
|
||||
// @Title delete
|
||||
// @Description delete the user
|
||||
// @Param uid path string true "The uid you want to delete"
|
||||
// @Success 200 {string} delete success!
|
||||
// @Failure 403 uid is empty
|
||||
// @router /:uid [delete]
|
||||
func (u *UserController) Delete() {
|
||||
uid := u.GetString(":uid")
|
||||
models.DeleteUser(uid)
|
||||
u.Data["json"] = "delete success!"
|
||||
u.ServeJSON()
|
||||
}
|
||||
|
||||
// @Title login
|
||||
// @Description Logs user into the system
|
||||
// @Param username query string true "The username for login"
|
||||
// @Param password query string true "The password for login"
|
||||
// @Success 200 {string} login success
|
||||
// @Failure 403 user not exist
|
||||
// @router /login [get]
|
||||
func (u *UserController) Login() {
|
||||
username := u.GetString("username")
|
||||
password := u.GetString("password")
|
||||
if models.Login(username, password) {
|
||||
u.Data["json"] = "login success"
|
||||
} else {
|
||||
u.Data["json"] = "user not exist"
|
||||
}
|
||||
u.ServeJSON()
|
||||
}
|
||||
|
||||
// @Title logout
|
||||
// @Description Logs out current logged in user session
|
||||
// @Success 200 {string} logout success
|
||||
// @router /logout [get]
|
||||
func (u *UserController) Logout() {
|
||||
u.Data["json"] = "logout success"
|
||||
u.ServeJSON()
|
||||
}
|
@ -1,53 +0,0 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
Objects map[string]*Object
|
||||
)
|
||||
|
||||
type Object struct {
|
||||
ObjectId string
|
||||
Score int64
|
||||
PlayerName string
|
||||
}
|
||||
|
||||
func init() {
|
||||
Objects = make(map[string]*Object)
|
||||
Objects["hjkhsbnmn123"] = &Object{"hjkhsbnmn123", 100, "astaxie"}
|
||||
Objects["mjjkxsxsaa23"] = &Object{"mjjkxsxsaa23", 101, "someone"}
|
||||
}
|
||||
|
||||
func AddOne(object Object) (ObjectId string) {
|
||||
object.ObjectId = "astaxie" + strconv.FormatInt(time.Now().UnixNano(), 10)
|
||||
Objects[object.ObjectId] = &object
|
||||
return object.ObjectId
|
||||
}
|
||||
|
||||
func GetOne(ObjectId string) (object *Object, err error) {
|
||||
if v, ok := Objects[ObjectId]; ok {
|
||||
return v, nil
|
||||
}
|
||||
return nil, errors.New("ObjectId Not Exist")
|
||||
}
|
||||
|
||||
func GetAll() map[string]*Object {
|
||||
return Objects
|
||||
}
|
||||
|
||||
func Update(ObjectId string, Score int64) (err error) {
|
||||
if v, ok := Objects[ObjectId]; ok {
|
||||
v.Score = Score
|
||||
return nil
|
||||
}
|
||||
return errors.New("ObjectId Not Exist")
|
||||
}
|
||||
|
||||
func Delete(ObjectId string) {
|
||||
delete(Objects, ObjectId)
|
||||
}
|
||||
|
@ -1,86 +0,0 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
UserList map[string]*User
|
||||
)
|
||||
|
||||
func init() {
|
||||
UserList = make(map[string]*User)
|
||||
u := User{"user_11111", "astaxie", "11111", Profile{"male", 20, "Singapore", "astaxie@gmail.com"}}
|
||||
UserList["user_11111"] = &u
|
||||
}
|
||||
|
||||
type User struct {
|
||||
Id string
|
||||
Username string
|
||||
Password string
|
||||
Profile Profile
|
||||
}
|
||||
|
||||
type Profile struct {
|
||||
Gender string
|
||||
Age int
|
||||
Address string
|
||||
Email string
|
||||
}
|
||||
|
||||
func AddUser(u User) string {
|
||||
u.Id = "user_" + strconv.FormatInt(time.Now().UnixNano(), 10)
|
||||
UserList[u.Id] = &u
|
||||
return u.Id
|
||||
}
|
||||
|
||||
func GetUser(uid string) (u *User, err error) {
|
||||
if u, ok := UserList[uid]; ok {
|
||||
return u, nil
|
||||
}
|
||||
return nil, errors.New("User not exists")
|
||||
}
|
||||
|
||||
func GetAllUsers() map[string]*User {
|
||||
return UserList
|
||||
}
|
||||
|
||||
func UpdateUser(uid string, uu *User) (a *User, err error) {
|
||||
if u, ok := UserList[uid]; ok {
|
||||
if uu.Username != "" {
|
||||
u.Username = uu.Username
|
||||
}
|
||||
if uu.Password != "" {
|
||||
u.Password = uu.Password
|
||||
}
|
||||
if uu.Profile.Age != 0 {
|
||||
u.Profile.Age = uu.Profile.Age
|
||||
}
|
||||
if uu.Profile.Address != "" {
|
||||
u.Profile.Address = uu.Profile.Address
|
||||
}
|
||||
if uu.Profile.Gender != "" {
|
||||
u.Profile.Gender = uu.Profile.Gender
|
||||
}
|
||||
if uu.Profile.Email != "" {
|
||||
u.Profile.Email = uu.Profile.Email
|
||||
}
|
||||
return u, nil
|
||||
}
|
||||
return nil, errors.New("User Not Exist")
|
||||
}
|
||||
|
||||
func Login(username, password string) bool {
|
||||
for _, u := range UserList {
|
||||
if u.Username == username && u.Password == password {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func DeleteUser(uid string) {
|
||||
delete(UserList, uid)
|
||||
}
|
@ -9,6 +9,12 @@ import (
|
||||
)
|
||||
|
||||
func init() {
|
||||
mapEndpoints()
|
||||
mapControllers()
|
||||
mapFilters()
|
||||
}
|
||||
|
||||
func mapEndpoints() {
|
||||
ns := beego.NewNamespace("/rest",
|
||||
beego.NSRouter("/ping.view", &api.PingController{}),
|
||||
beego.NSRouter("/getLicense.view", &api.GetLicenseController{}),
|
||||
@ -16,13 +22,19 @@ func init() {
|
||||
)
|
||||
beego.AddNamespace(ns)
|
||||
|
||||
}
|
||||
|
||||
func mapControllers() {
|
||||
beego.Router("/", &controllers.MainController{})
|
||||
beego.Router("/sync", &controllers.SyncController{})
|
||||
|
||||
beego.ErrorController(&controllers.MainController{})
|
||||
}
|
||||
|
||||
func mapFilters() {
|
||||
var ValidateRequest = func(ctx *context.Context) {
|
||||
api.Validate(&beego.Controller{Ctx: ctx})
|
||||
}
|
||||
|
||||
beego.InsertFilter("/rest/*", beego.BeforeRouter, ValidateRequest)
|
||||
beego.ErrorController(&controllers.MainController{})
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user