specialty.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package api
  2. import (
  3. "../settings"
  4. "encoding/json"
  5. "fmt"
  6. "net/http"
  7. "strconv"
  8. )
  9. func CreateSpecialty(w http.ResponseWriter, r *http.Request) {
  10. var newSpecialty Specialty
  11. err := json.NewDecoder(r.Body).Decode(&newSpecialty)
  12. if err != nil {
  13. json.NewEncoder(w).Encode(struct{ Error string }{Error: "error add new Specialty"})
  14. fmt.Println(err)
  15. return
  16. }
  17. settings.DB.Create(&newSpecialty)
  18. json.NewEncoder(w).Encode(struct{ Result string }{Result: "added new Specialty"})
  19. return
  20. }
  21. func GetSpecialty(w http.ResponseWriter, r *http.Request) {
  22. var Specialty Specialty
  23. path := replacePath(r.URL.Path, "/api/specialty/")
  24. num, err := strconv.Atoi(path)
  25. if err != nil {
  26. showError(r, err)
  27. json.NewEncoder(w).Encode(struct{ Error string }{Error: "strconv error"})
  28. return
  29. }
  30. settings.DB.Where("id = ?", num).First(&Specialty)
  31. json.NewEncoder(w).Encode(Specialty)
  32. return
  33. }
  34. func GetSpecialties(w http.ResponseWriter, r *http.Request) {
  35. var Specialties []Specialty
  36. settings.DB.Find(&Specialties)
  37. json.NewEncoder(w).Encode(Specialties)
  38. }
  39. func UpdateSpecialty(w http.ResponseWriter, r *http.Request) {
  40. var specialty Teacher
  41. var specialtyToUpdate Teacher
  42. err := json.NewDecoder(r.Body).Decode(&specialty)
  43. if err != nil {
  44. showError(r, err)
  45. json.NewEncoder(w).Encode(struct{ Error string }{Error: "error update specialty"})
  46. return
  47. }
  48. settings.DB.First(&specialtyToUpdate, specialty.ID)
  49. settings.DB.Model(&specialtyToUpdate).Updates(specialty)
  50. json.NewEncoder(w).Encode(struct{ Result string }{Result: "updated specialty"})
  51. return
  52. }
  53. func SpecialtyRoute(w http.ResponseWriter, r *http.Request) {
  54. showAPIRequest(r)
  55. w.Header().Set("Content-Type", "application/json")
  56. if r.Method == "POST" {
  57. CreateSpecialty(w, r)
  58. return
  59. }
  60. if r.Method == "UPDATE" || r.Method == "PATCH" {
  61. UpdateSpecialty(w, r)
  62. return
  63. }
  64. path := replacePath(r.URL.Path, "/api/specialty/")
  65. if path == "" && r.Method == "GET" {
  66. GetSpecialties(w, r)
  67. return
  68. }
  69. num, err := strconv.Atoi(path)
  70. if err != nil {
  71. json.NewEncoder(w).Encode(struct{ Error string }{Error: "strconv error"})
  72. fmt.Println(err)
  73. return
  74. }
  75. if num < 0 {
  76. json.NewEncoder(w).Encode(struct{ Error string }{Error: "strconv error"})
  77. fmt.Println(err)
  78. return
  79. }
  80. if r.Method == "GET" {
  81. GetSpecialty(w, r)
  82. return
  83. }
  84. json.NewEncoder(w).Encode(struct{ Error string }{Error: "It is not GET method"})
  85. }