290 lines
5.6 KiB
Go
290 lines
5.6 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"github.com/gorilla/mux"
|
|
"io/ioutil"
|
|
"log"
|
|
"net/http"
|
|
)
|
|
|
|
func newTerm(w http.ResponseWriter, req *http.Request) {
|
|
|
|
log.Println("weszlo")
|
|
body, err := ioutil.ReadAll(req.Body)
|
|
if err != nil {
|
|
http.Error(w, "błąd zapisu danych", 500)
|
|
return
|
|
}
|
|
log.Println(string(body))
|
|
var term term_struct
|
|
err = json.Unmarshal(body, &term)
|
|
if err != nil {
|
|
http.Error(w, "błąd zapisu danych", 500)
|
|
return
|
|
}
|
|
for i := 0; i < len(term.Months); i++ {
|
|
daysIn(term.Months[i], term.Year, w)
|
|
}
|
|
}
|
|
|
|
func addRow(data string, w http.ResponseWriter) {
|
|
tx, err := db.Begin()
|
|
if err != nil {
|
|
http.Error(w, "błąd zapisu danych", 500)
|
|
return
|
|
}
|
|
defer tx.Rollback()
|
|
stmt, err := tx.Prepare("INSERT INTO calendar (date, status, contractNo) VALUES ( $1, 'wolny', '');")
|
|
if err != nil {
|
|
http.Error(w, "błąd zapisu danych", 500)
|
|
return
|
|
}
|
|
defer stmt.Close() // danger!
|
|
for i := 0; i < 1; i++ {
|
|
_, err = stmt.Exec(data)
|
|
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
|
|
}
|
|
}
|
|
|
|
func getTerms2(w http.ResponseWriter, r *http.Request) {
|
|
enableCors(&w)
|
|
|
|
terms := []Term{}
|
|
|
|
rows, err := db.Query("SELECT * FROM calendar;")
|
|
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
defer rows.Close()
|
|
for rows.Next() {
|
|
term := Term{}
|
|
err = rows.Scan(
|
|
&term.Date,
|
|
&term.Status,
|
|
&term.Contractno,
|
|
)
|
|
if err != nil {
|
|
return
|
|
}
|
|
terms = append(terms, term)
|
|
}
|
|
fmt.Println(terms)
|
|
out, err := json.Marshal(terms)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), 500)
|
|
return
|
|
}
|
|
|
|
fmt.Fprint(w, string(out))
|
|
}
|
|
|
|
func getTerms(w http.ResponseWriter, r *http.Request) {
|
|
enableCors(&w)
|
|
vars := mux.Vars(r)
|
|
|
|
terms := []Term{}
|
|
stmt, err := db.Prepare("SELECT * FROM calendar WHERE EXTRACT(MONTH FROM date) = $1 AND EXTRACT(YEAR FROM date) = $2 ORDER BY date ASC;")
|
|
if err != nil {
|
|
http.Error(w, "błąd zapisu danych", 500)
|
|
return
|
|
}
|
|
defer stmt.Close()
|
|
rows, err := stmt.Query(vars["month"], vars["year"])
|
|
if err != nil {
|
|
http.Error(w, "błąd zapisu danych", 500)
|
|
return
|
|
}
|
|
defer rows.Close()
|
|
for rows.Next() {
|
|
term := Term{}
|
|
err = rows.Scan(
|
|
&term.Date,
|
|
&term.Status,
|
|
&term.Contractno,
|
|
)
|
|
if err != nil {
|
|
http.Error(w, "błąd zapisu danych", 500)
|
|
return
|
|
}
|
|
terms = append(terms, term)
|
|
}
|
|
if err = rows.Err(); err != nil {
|
|
http.Error(w, "błąd danych", 500)
|
|
return
|
|
}
|
|
out, err := json.Marshal(terms)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), 500)
|
|
return
|
|
}
|
|
|
|
fmt.Fprint(w, string(out))
|
|
}
|
|
|
|
func deleteTerm(w http.ResponseWriter, req *http.Request) {
|
|
|
|
body, err := ioutil.ReadAll(req.Body)
|
|
if err != nil {
|
|
http.Error(w, "błąd zapisu danych", 500)
|
|
return
|
|
}
|
|
log.Println(string(body))
|
|
var term Term
|
|
err = json.Unmarshal(body, &term)
|
|
if err != nil {
|
|
http.Error(w, "błąd zapisu danych", 500)
|
|
return
|
|
}
|
|
fmt.Println(term)
|
|
|
|
{
|
|
tx, err := db.Begin()
|
|
if err != nil {
|
|
http.Error(w, "błąd zapisu danych", 500)
|
|
return
|
|
}
|
|
defer tx.Rollback()
|
|
stmt, err := tx.Prepare("DELETE FROM contracts WHERE date=$1;")
|
|
if err != nil {
|
|
http.Error(w, "błąd zapisu danych", 500)
|
|
return
|
|
}
|
|
|
|
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 {
|
|
http.Error(w, "błąd zapisu danych", 500)
|
|
return
|
|
}
|
|
defer tx.Rollback()
|
|
stmt, err := tx.Prepare("UPDATE calendar SET status=$1, contractno=$2 WHERE date=$3;")
|
|
if err != nil {
|
|
http.Error(w, "błąd zapisu danych", 500)
|
|
return
|
|
}
|
|
|
|
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 putTerm(w http.ResponseWriter, req *http.Request) {
|
|
|
|
body, err := ioutil.ReadAll(req.Body)
|
|
if err != nil {
|
|
http.Error(w, "błąd zapisu danych", 500)
|
|
return
|
|
}
|
|
log.Println(string(body))
|
|
var term Term
|
|
err = json.Unmarshal(body, &term)
|
|
if err != nil {
|
|
http.Error(w, "błąd zapisu danych", 500)
|
|
return
|
|
}
|
|
fmt.Println(term)
|
|
|
|
{
|
|
tx, err := db.Begin()
|
|
if err != nil {
|
|
http.Error(w, "błąd zapisu danych", 500)
|
|
return
|
|
}
|
|
defer tx.Rollback()
|
|
stmt, err := tx.Prepare("INSERT INTO contracts (date, status, contractno) VALUES ($1, $2, $3) ON CONFLICT (date) DO UPDATE SET status=$2, contractno=$3;")
|
|
if err != nil {
|
|
http.Error(w, "błąd zapisu danych", 500)
|
|
return
|
|
}
|
|
|
|
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 {
|
|
http.Error(w, "błąd zapisu danych", 500)
|
|
return
|
|
}
|
|
defer tx.Rollback()
|
|
stmt, err := tx.Prepare("UPDATE calendar SET status=$1, contractno=$2 WHERE date=$3;")
|
|
if err != nil {
|
|
http.Error(w, "błąd zapisu danych", 500)
|
|
return
|
|
}
|
|
|
|
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
|
|
}
|
|
|