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.

98 lines
2.1 KiB

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