Advertisement
Uhuuuyy

handlerNilai.go

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