Advertisement
Uhuuuyy

handlerMahasiswa.go

May 23rd, 2023
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.55 KB | Source Code | 0 0
  1. package handler
  2.  
  3. import (
  4.     "encoding/json"
  5.     "io/ioutil"
  6.     "nama_npm_pert4/model" //sesuaikan dengan nama folder (case sensitive)
  7.     "net/http"
  8. )
  9.  
  10. func HandlerMahasiswaGet(w http.ResponseWriter, r *http.Request) {
  11.  
  12.     var data interface{}
  13.     var err error
  14.  
  15.     npm := r.URL.Query()["npm"]
  16.     if len(npm) != 0 {
  17.         data, err = model.GetMahasiswa(db, npm[0])
  18.     } else {
  19.         data, err = model.GetAllMahasiswa(db)
  20.     }
  21.  
  22.     if err != nil {
  23.         http.Error(w, err.Error(), http.StatusInternalServerError)
  24.     }
  25.     jsonData, _ := json.Marshal(data)
  26.     w.Write(jsonData)
  27. }
  28.  
  29. func HandlerMahasiswaPost(w http.ResponseWriter, r *http.Request) {
  30.     defer r.Body.Close()
  31.     body, err := ioutil.ReadAll(r.Body)
  32.     if err != nil {
  33.         http.Error(w, err.Error(), http.StatusInternalServerError)
  34.         return
  35.     }
  36.     var data model.Mahasiswa
  37.     if err = json.Unmarshal(body, &data); err != nil {
  38.         http.Error(w, err.Error(), http.StatusInternalServerError)
  39.         return
  40.     }
  41.     if err = data.Insert(db); err != nil {
  42.         http.Error(w, err.Error(), http.StatusInternalServerError)
  43.         return
  44.     }
  45.     jsonData, err := json.Marshal(data)
  46.     if err != nil {
  47.         http.Error(w, err.Error(), http.StatusInternalServerError)
  48.     }
  49.     w.Write(jsonData)
  50. }
  51.  
  52. func HandlerMahasiswaDelete(w http.ResponseWriter, r *http.Request) {
  53.     npm := r.URL.Query()["npm"]
  54.     if len(npm) != 0 {
  55.         data := model.Mahasiswa{NPM: npm[0]}
  56.         if err := data.Delete(db); err != nil {
  57.             http.Error(w, "ID tidak ditemukan", http.StatusBadRequest)
  58.             return
  59.         }
  60.         w.Write([]byte("Data telah dihapus"))
  61.     } else {
  62.         http.Error(w, "ID tidak ditemukan", http.StatusInternalServerError)
  63.     }
  64. }
  65.  
  66. func HandlerMahasiswaPut(w http.ResponseWriter, r *http.Request) {
  67.     npm := r.URL.Query()["npm"]
  68.     if len(npm) == 0 {
  69.         http.Error(w, "ID tidak ditemukan", http.StatusBadRequest)
  70.         return
  71.     }
  72.     defer r.Body.Close()
  73.     body, err := ioutil.ReadAll(r.Body)
  74.     if err != nil {
  75.         http.Error(w, err.Error(), http.StatusInternalServerError)
  76.         return
  77.     }
  78.     jsonmap := make(map[string]interface{})
  79.     err = json.Unmarshal(body, &jsonmap)
  80.     if err != nil {
  81.         http.Error(w, err.Error(), http.StatusInternalServerError)
  82.         return
  83.     }
  84.     data := model.Mahasiswa{NPM: npm[0]}
  85.     err = data.Update(db, jsonmap)
  86.     if err != nil {
  87.         http.Error(w, err.Error(), http.StatusInternalServerError)
  88.         return
  89.     }
  90.     result, err := model.GetMahasiswa(db, npm[0])
  91.     if err != nil {
  92.         http.Error(w, err.Error(), http.StatusInternalServerError)
  93.         return
  94.     }
  95.     jsonData, err := json.Marshal(result)
  96.     if err != nil {
  97.         http.Error(w, err.Error(), http.StatusInternalServerError)
  98.         return
  99.     }
  100.     w.Write(jsonData)
  101. }
  102.  
Tags: inter4
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement