155 lines
2.7 KiB
Go
155 lines
2.7 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"log"
|
|
"net/http"
|
|
)
|
|
|
|
func newBooking(w http.ResponseWriter, req *http.Request) {
|
|
body, err := ioutil.ReadAll(req.Body)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
log.Println(string(body))
|
|
var term Term
|
|
err = json.Unmarshal(body, &term)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
fmt.Println(term)
|
|
|
|
{
|
|
tx, err := db.Begin()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer tx.Rollback()
|
|
stmt, err := tx.Prepare("INSERT INTO contracts (date, status, contractno) VALUES ( $1, $2, $3);")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
defer stmt.Close()
|
|
for i := 0; i < 1; i++ {
|
|
|
|
_, err = stmt.Exec(term.Date, term.Status, term.Contractno)
|
|
if err != nil {
|
|
http.Error(w, "błąd zapisu danych", 500)
|
|
return
|
|
}
|
|
}
|
|
err = tx.Commit()
|
|
if err != nil {
|
|
http.Error(w, "błąd zapisu danych", 500)
|
|
return
|
|
}
|
|
}
|
|
|
|
{
|
|
tx, err := db.Begin()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer tx.Rollback()
|
|
stmt, err := tx.Prepare("UPDATE calendar SET status=$1, contractno=$2 WHERE date=$3;")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
defer stmt.Close()
|
|
for i := 0; i < 1; i++ {
|
|
|
|
_, err = stmt.Exec(term.Status, term.Contractno, term.Date)
|
|
if err != nil {
|
|
http.Error(w, "błąd zapisu danych", 500)
|
|
return
|
|
}
|
|
}
|
|
err = tx.Commit()
|
|
if err != nil {
|
|
http.Error(w, "błąd zapisu danych", 500)
|
|
return
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
func delBooking(w http.ResponseWriter, req *http.Request) {
|
|
|
|
enableCorsDelete(&w)
|
|
|
|
body, err := ioutil.ReadAll(req.Body)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
log.Println(string(body))
|
|
var term Term
|
|
err = json.Unmarshal(body, &term)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
fmt.Println(term)
|
|
|
|
{
|
|
tx, err := db.Begin()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer tx.Rollback()
|
|
stmt, err := tx.Prepare("DELETE FROM contracts WHERE date=$1;")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
defer stmt.Close()
|
|
for i := 0; i < 1; i++ {
|
|
|
|
_, err = stmt.Exec(term.Date)
|
|
if err != nil {
|
|
http.Error(w, "błąd zapisu danych", 500)
|
|
return
|
|
}
|
|
}
|
|
err = tx.Commit()
|
|
if err != nil {
|
|
http.Error(w, "błąd zapisu danych", 500)
|
|
return
|
|
}
|
|
}
|
|
|
|
{
|
|
tx, err := db.Begin()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer tx.Rollback()
|
|
stmt, err := tx.Prepare("UPDATE calendar SET status=$1, contractno=$2 WHERE date=$3;")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
defer stmt.Close()
|
|
for i := 0; i < 1; i++ {
|
|
|
|
_, err = stmt.Exec(term.Status, term.Contractno, term.Date)
|
|
if err != nil {
|
|
http.Error(w, "błąd zapisu danych", 500)
|
|
return
|
|
}
|
|
}
|
|
err = tx.Commit()
|
|
if err != nil {
|
|
http.Error(w, "błąd zapisu danych", 500)
|
|
return
|
|
}
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func enableCorsDelete(w *http.ResponseWriter) {
|
|
(*w).Header().Set("Access-Control-Allow-Origin", "*")
|
|
} |