You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
1.7 KiB

3 years ago
  1. package api
  2. import (
  3. "git.aiterp.net/lucifer/new-server/app/config"
  4. "git.aiterp.net/lucifer/new-server/models"
  5. "github.com/gin-gonic/gin"
  6. )
  7. func ColorPresets(r gin.IRoutes) {
  8. r.GET("", handler(func(c *gin.Context) (interface{}, error) {
  9. return config.ColorPresetRepository().FetchAll(ctxOf(c))
  10. }))
  11. r.GET("/:id", handler(func(c *gin.Context) (interface{}, error) {
  12. return config.ColorPresetRepository().Find(ctxOf(c), intParam(c, "id"))
  13. }))
  14. r.POST("", handler(func(c *gin.Context) (interface{}, error) {
  15. var body struct {
  16. Name string `json:"name"`
  17. ColorString string `json:"colorString"`
  18. }
  19. err := parseBody(c, &body)
  20. if err != nil {
  21. return nil, err
  22. }
  23. newColor, err := models.ParseColorValue(body.ColorString)
  24. if err != nil {
  25. return nil, err
  26. }
  27. preset := models.ColorPreset{
  28. Name: body.Name,
  29. Value: newColor,
  30. }
  31. preset.Validate()
  32. err = config.ColorPresetRepository().Save(ctxOf(c), &preset)
  33. if err != nil {
  34. return nil, err
  35. }
  36. return preset, nil
  37. }))
  38. r.PUT("/:id", handler(func(c *gin.Context) (interface{}, error) {
  39. var body struct {
  40. Name *string `json:"name"`
  41. ColorString *string `json:"colorString"`
  42. }
  43. err := parseBody(c, &body)
  44. if err != nil {
  45. return nil, err
  46. }
  47. preset, err := config.ColorPresetRepository().Find(ctxOf(c), intParam(c, "id"))
  48. if err != nil {
  49. return nil, err
  50. }
  51. if body.Name != nil {
  52. preset.Name = *body.Name
  53. }
  54. if body.ColorString != nil {
  55. newColor, err := models.ParseColorValue(*body.ColorString)
  56. if err != nil {
  57. return nil, err
  58. }
  59. preset.Value = newColor
  60. }
  61. preset.Validate()
  62. err = config.ColorPresetRepository().Save(ctxOf(c), &preset)
  63. if err != nil {
  64. return nil, err
  65. }
  66. return preset, nil
  67. }))
  68. }