scheduleOfGroup.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package api
  2. import (
  3. "../settings"
  4. "encoding/json"
  5. "fmt"
  6. "net/http"
  7. "strconv"
  8. "strings"
  9. )
  10. var ScheduleOfGroups []Scheduleofgroup
  11. func ScheduleOfGroupRoute(w http.ResponseWriter, r *http.Request) {
  12. showAPIRequest(r)
  13. w.Header().Set("Content-Type", "application/json")
  14. if r.Method == "POST" {
  15. CreateScheduleOfGroup(w, r)
  16. return
  17. }
  18. if r.Method == "UPDATE" || r.Method == "PATCH" {
  19. UpdateScheduleOfGroup(w, r)
  20. return
  21. }
  22. path := replacePath(r.URL.Path, "/api/groupschedule/")
  23. if path == "" {
  24. GetScheduleOfGroups(w, r)
  25. return
  26. }
  27. if strings.Contains(path, "day/") {
  28. day := strings.Replace(path, "day/", "", 1)
  29. switch day {
  30. case "monday", "tuesday", "wednesday", "thursday", "friday", "saturday":
  31. default:
  32. json.NewEncoder(w).Encode(struct{ Error string }{Error: "undefined day error"})
  33. return
  34. }
  35. GetScheduleByDay(w, r, day)
  36. return
  37. }
  38. num, err := strconv.Atoi(path)
  39. if err != nil {
  40. json.NewEncoder(w).Encode(struct{ Error string }{Error: "strconv error"})
  41. fmt.Println(err)
  42. return
  43. }
  44. settings.DB.Find(&ScheduleOfGroups)
  45. if num < 0 || num > len(ScheduleOfGroups)+1 {
  46. json.NewEncoder(w).Encode(struct{ Error string }{Error: "strconv error"})
  47. fmt.Println(err)
  48. return
  49. }
  50. if r.Method == "GET" {
  51. GetScheduleOfGroupById(w, r, num)
  52. return
  53. }
  54. json.NewEncoder(w).Encode(struct{ Error string }{Error: "This method is not supported"})
  55. }
  56. func GetScheduleByDay(w http.ResponseWriter, r *http.Request, day string) {
  57. var ScheduleOfday []Schedule
  58. settings.DB.Where("weekday = ?", day).Find(&ScheduleOfday)
  59. json.NewEncoder(w).Encode(ScheduleOfday)
  60. return
  61. }
  62. func UpdateScheduleOfGroup(w http.ResponseWriter, r *http.Request) {
  63. var ScheduleOfGroup Scheduleofgroup
  64. var ScheduleOfGroupToUpdate Scheduleofgroup
  65. err := json.NewDecoder(r.Body).Decode(&ScheduleOfGroup)
  66. if err != nil {
  67. showError(r, err)
  68. json.NewEncoder(w).Encode(struct{ Error string }{Error: "error update ScheduleOfGroup"})
  69. return
  70. }
  71. settings.DB.First(&ScheduleOfGroupToUpdate, ScheduleOfGroup.ID)
  72. settings.DB.Model(&ScheduleOfGroupToUpdate).Updates(ScheduleOfGroup)
  73. json.NewEncoder(w).Encode(struct{ Result string }{Result: "updated ScheduleOfGroup"})
  74. return
  75. }
  76. func CreateScheduleOfGroup(w http.ResponseWriter, r *http.Request) {
  77. var newScheduleOfGroup Scheduleofgroup
  78. err := json.NewDecoder(r.Body).Decode(&newScheduleOfGroup)
  79. if err != nil {
  80. json.NewEncoder(w).Encode(struct{ Error string }{Error: "error add new ScheduleOfGroup"})
  81. fmt.Println(err)
  82. return
  83. }
  84. settings.DB.Create(&newScheduleOfGroup)
  85. json.NewEncoder(w).Encode(struct{ Result string }{Result: "added new ScheduleOfGroup"})
  86. return
  87. }
  88. func GetScheduleOfGroupById(w http.ResponseWriter, r *http.Request, num int) {
  89. var ScheduleOfGroup Scheduleofgroup
  90. settings.DB.Where("id = ?", num).First(&ScheduleOfGroup)
  91. json.NewEncoder(w).Encode(ScheduleOfGroup)
  92. return
  93. }
  94. func GetScheduleOfGroups(w http.ResponseWriter, r *http.Request) {
  95. showAPIRequest(r)
  96. w.Header().Set("Content-Type", "application/json")
  97. if r.Method == "GET" {
  98. var ScheduleOfGroups []Scheduleofgroup
  99. settings.DB.Find(&ScheduleOfGroups)
  100. json.NewEncoder(w).Encode(ScheduleOfGroups)
  101. }
  102. return
  103. }