group.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package api
  2. import (
  3. "../settings"
  4. "encoding/json"
  5. "net/http"
  6. "strconv"
  7. "strings"
  8. )
  9. var Groups = make(map[uint]Group)
  10. func CreateGroup(w http.ResponseWriter, r *http.Request) {
  11. var newGroup Group
  12. err := json.NewDecoder(r.Body).Decode(&newGroup)
  13. if err != nil {
  14. json.NewEncoder(w).Encode(struct{ Error string }{Error: "error add new Group"})
  15. return
  16. }
  17. settings.DB.Create(&newGroup)
  18. json.NewEncoder(w).Encode(struct{ Result string }{Result: "added new Group"})
  19. return
  20. }
  21. func UpdateGroup(w http.ResponseWriter, r *http.Request) {
  22. var newGroup Group
  23. err := json.NewDecoder(r.Body).Decode(&newGroup)
  24. if err != nil {
  25. json.NewEncoder(w).Encode(struct{ Error string }{Error: "error update Group"})
  26. return
  27. }
  28. if _, ok := Groups[newGroup.ID]; !ok {
  29. json.NewEncoder(w).Encode(struct{ Error string }{Error: "error update Group"})
  30. return
  31. }
  32. Groups[newGroup.ID] = newGroup
  33. json.NewEncoder(w).Encode(struct{ Error string }{Error: "Successfully updated Group"})
  34. return
  35. }
  36. func GroupRoute(w http.ResponseWriter, r *http.Request) {
  37. showAPIRequest(r)
  38. w.Header().Set("Content-Type", "application/json")
  39. if r.Method == "POST" {
  40. CreateGroup(w, r)
  41. }
  42. if r.Method == "UPDATE" || r.Method == "PATCH" {
  43. UpdateGroup(w, r)
  44. }
  45. path := replacePath(r.URL.Path, "/api/group/")
  46. if path == "" && r.Method == "GET"{
  47. GetGroups(w, r)
  48. return
  49. }
  50. if r.Method == "GET" {
  51. GetGroup(w, r)
  52. return
  53. }
  54. json.NewEncoder(w).Encode(struct{ Error string }{Error: "It is not GET method"})
  55. }
  56. func GetGroups(w http.ResponseWriter, r *http.Request){
  57. var output []Group
  58. settings.DB.Find(&output)
  59. json.NewEncoder(w).Encode(output)
  60. }
  61. func GetGroup(w http.ResponseWriter, r *http.Request) {
  62. path := replacePath(r.URL.Path, "/api/group/")
  63. num, err := strconv.Atoi(path)
  64. if err != nil || num < 0 {
  65. json.NewEncoder(w).Encode(struct{ Error string }{Error: "strconv error"})
  66. return
  67. }
  68. var groups Group
  69. settings.DB.Where("id = ?", num).First(&groups)
  70. json.NewEncoder(w).Encode(groups)
  71. return
  72. }
  73. func GetGroupBySpecialty(w http.ResponseWriter, r *http.Request) {
  74. showAPIRequest(r)
  75. w.Header().Set("Content-Type", "application/json")
  76. if r.Method == "GET" {
  77. path := r.URL.Path
  78. path = strings.Replace(path, "/api/group/specialty/", "", 1)
  79. var output []Group
  80. num, err := strconv.Atoi(path)
  81. if err != nil || num < 0 {
  82. json.NewEncoder(w).Encode(struct{ Error string }{Error: "strconv error"})
  83. }
  84. for _, group := range Groups {
  85. if group.IDSpecialty == uint(num) {
  86. output = append(output, group)
  87. }
  88. }
  89. json.NewEncoder(w).Encode(output)
  90. }
  91. }