object.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package controllers
  2. import (
  3. "hongze_edb_lib/models"
  4. "encoding/json"
  5. beego "github.com/beego/beego/v2/server/web"
  6. )
  7. // Operations about object
  8. type ObjectController struct {
  9. beego.Controller
  10. }
  11. // @Title Create
  12. // @Description create object
  13. // @Param body body models.Object true "The object content"
  14. // @Success 200 {string} models.Object.Id
  15. // @Failure 403 body is empty
  16. // @router / [post]
  17. func (o *ObjectController) Post() {
  18. var ob models.Object
  19. json.Unmarshal(o.Ctx.Input.RequestBody, &ob)
  20. objectid := models.AddOne(ob)
  21. o.Data["json"] = map[string]string{"ObjectId": objectid}
  22. o.ServeJSON()
  23. }
  24. // @Title Get
  25. // @Description find object by objectid
  26. // @Param objectId path string true "the objectid you want to get"
  27. // @Success 200 {object} models.Object
  28. // @Failure 403 :objectId is empty
  29. // @router /:objectId [get]
  30. func (o *ObjectController) Get() {
  31. objectId := o.Ctx.Input.Param(":objectId")
  32. if objectId != "" {
  33. ob, err := models.GetOne(objectId)
  34. if err != nil {
  35. o.Data["json"] = err.Error()
  36. } else {
  37. o.Data["json"] = ob
  38. }
  39. }
  40. o.ServeJSON()
  41. }
  42. // @Title GetAll
  43. // @Description get all objects
  44. // @Success 200 {object} models.Object
  45. // @Failure 403 :objectId is empty
  46. // @router / [get]
  47. func (o *ObjectController) GetAll() {
  48. obs := models.GetAll()
  49. o.Data["json"] = obs
  50. o.ServeJSON()
  51. }
  52. // @Title Update
  53. // @Description update the object
  54. // @Param objectId path string true "The objectid you want to update"
  55. // @Param body body models.Object true "The body"
  56. // @Success 200 {object} models.Object
  57. // @Failure 403 :objectId is empty
  58. // @router /:objectId [put]
  59. func (o *ObjectController) Put() {
  60. objectId := o.Ctx.Input.Param(":objectId")
  61. var ob models.Object
  62. json.Unmarshal(o.Ctx.Input.RequestBody, &ob)
  63. err := models.Update(objectId, ob.Score)
  64. if err != nil {
  65. o.Data["json"] = err.Error()
  66. } else {
  67. o.Data["json"] = "update success!"
  68. }
  69. o.ServeJSON()
  70. }
  71. // @Title Delete
  72. // @Description delete the object
  73. // @Param objectId path string true "The objectId you want to delete"
  74. // @Success 200 {string} delete success!
  75. // @Failure 403 objectId is empty
  76. // @router /:objectId [delete]
  77. func (o *ObjectController) Delete() {
  78. objectId := o.Ctx.Input.Param(":objectId")
  79. models.Delete(objectId)
  80. o.Data["json"] = "delete success!"
  81. o.ServeJSON()
  82. }