diff --git a/Database/os.go b/Database/os.go index eb7a672..2f6324a 100644 --- a/Database/os.go +++ b/Database/os.go @@ -77,3 +77,15 @@ func GetVersionByDistributionList(db *sql.DB, d string) ([]null.String, error) { } return list, nil } + +func CreateOS(os OS, db *sql.DB) error { + q, err := db.Prepare("INSERT INTO `dashboard_os` (`distribution`,`version`,`end_of_support`) VALUES (?,?,?)") + if err != nil { + log.Println("Error creating query", err) + } + _, err = q.Exec(&os.Distribution, &os.Version, &os.End_of_support) + if err != nil { + log.Println("Error inserting OS", err) + } + return err +} diff --git a/Http/utils.go b/Http/utils.go index 9605a24..5fb482d 100644 --- a/Http/utils.go +++ b/Http/utils.go @@ -102,6 +102,33 @@ func GetVersionByDistributionList(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(list) } +func CreateOS(w http.ResponseWriter, r *http.Request) { + var os db.OS + var err error + + t := time.Now() + status := 200 + + body, err := io.ReadAll(r.Body) + if err != nil { + log.Println("Error reading request body", err) + } + + params := make(map[string]null.String) + json.Unmarshal(body, ¶ms) + os.Distribution = params["distribution"] + os.Version = params["version"] + os.End_of_support = params["end_of_support"] + db_conn := db.GetDatabaseConnection() + err = db.CreateOS(os, db_conn) + if err != nil { + log.Println("Error creating new OS", err) + status = 500 + } + logRequest(t, r, status) + w.WriteHeader(status) +} + func logRequest(t time.Time, r *http.Request, s int) { log.Printf("%s - - %s \"%s %s %s\" %d 0 \"-\" \"%s\" %d\n", r.Host, diff --git a/main.go b/main.go index d028c5d..1559846 100644 --- a/main.go +++ b/main.go @@ -30,6 +30,7 @@ func main() { router.HandleFunc("/healthcheck", h.HealthHandler) router.HandleFunc("/os", h.GetOS).Methods("GET") router.HandleFunc("/os/{id:[0-9]+}", h.GetOSbyID).Methods("GET") + router.HandleFunc("/os/create", h.CreateOS).Methods("POST") router.HandleFunc("/distribution", h.GetDistributionList).Methods("GET") router.HandleFunc("/distribution/{distribution:[a-zA-Z]+}/version", h.GetVersionByDistributionList).Methods("GET")