2019-02-22 03:23:03 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
|
|
// This file contains the APIs for component management
|
2019-02-26 17:06:21 +00:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
2019-03-10 10:20:00 +00:00
|
|
|
|
2019-02-26 17:06:21 +00:00
|
|
|
"net/http"
|
|
|
|
|
|
2019-03-10 10:20:00 +00:00
|
|
|
"gitlab.com/mobtools/internal-tools-server/models"
|
|
|
|
|
"gitlab.com/mobtools/internal-tools-server/services"
|
2019-02-26 17:06:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// GetComponents fetches the list of components from the DB
|
2019-03-16 10:17:47 +00:00
|
|
|
func GetComponents(w http.ResponseWriter, r *http.Request) {
|
2019-02-26 17:52:37 +00:00
|
|
|
queryValues := r.URL.Query()
|
2019-02-26 17:06:21 +00:00
|
|
|
|
2019-02-27 03:59:30 +00:00
|
|
|
components, err := services.GetComponent(queryValues)
|
|
|
|
|
if err != nil {
|
|
|
|
|
HandleAPIError(w, r, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2019-02-26 17:06:21 +00:00
|
|
|
|
|
|
|
|
// Write content-type, statuscode, payload
|
2019-02-27 03:59:30 +00:00
|
|
|
componentsJSON, _ := json.Marshal(components)
|
2019-02-26 17:06:21 +00:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
w.WriteHeader(200)
|
|
|
|
|
fmt.Fprintf(w, "%s", componentsJSON)
|
|
|
|
|
}
|
2019-02-26 17:52:37 +00:00
|
|
|
|
|
|
|
|
// CreateComponents creates components in the DB
|
2019-03-16 10:17:47 +00:00
|
|
|
func CreateComponents(w http.ResponseWriter, r *http.Request) {
|
2019-02-26 17:52:37 +00:00
|
|
|
component := models.Component{}
|
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&component)
|
|
|
|
|
if err != nil {
|
2019-02-27 03:59:30 +00:00
|
|
|
HandleAPIError(w, r, err)
|
|
|
|
|
return
|
2019-02-26 17:52:37 +00:00
|
|
|
}
|
|
|
|
|
|
2019-02-27 03:59:30 +00:00
|
|
|
component, err = services.CreateComponent(component)
|
|
|
|
|
if err != nil {
|
|
|
|
|
HandleAPIError(w, r, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2019-02-26 17:52:37 +00:00
|
|
|
|
|
|
|
|
// Write content-type, statuscode, payload
|
|
|
|
|
componentJSON, _ := json.Marshal(component)
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
w.WriteHeader(200)
|
|
|
|
|
fmt.Fprintf(w, "%s", componentJSON)
|
|
|
|
|
}
|
2019-02-27 16:35:03 +00:00
|
|
|
|
2019-03-16 10:17:47 +00:00
|
|
|
func UpdateComponent(w http.ResponseWriter, r *http.Request) {
|
2019-02-27 16:35:03 +00:00
|
|
|
component := models.Component{}
|
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&component)
|
|
|
|
|
if err != nil {
|
|
|
|
|
HandleAPIError(w, r, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
component, err = services.UpdateComponent(component)
|
|
|
|
|
if err != nil {
|
|
|
|
|
HandleAPIError(w, r, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Write content-type, statuscode, payload
|
|
|
|
|
componentJSON, _ := json.Marshal(component)
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
w.WriteHeader(200)
|
|
|
|
|
fmt.Fprintf(w, "%s", componentJSON)
|
|
|
|
|
}
|