1
0

subjectofplan.go 2.5 KB

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