2019-01-07 23:16:26 +01:00
package main
import (
"encoding/json"
"fmt"
"github.com/gorilla/mux"
"io/ioutil"
"log"
"net/http"
)
func newTerm ( w http . ResponseWriter , req * http . Request ) {
2019-01-11 17:56:26 +01:00
log . Println ( "weszlo" )
2019-01-07 23:16:26 +01:00
body , err := ioutil . ReadAll ( req . Body )
if err != nil {
2019-01-11 17:56:26 +01:00
http . Error ( w , "błąd zapisu danych" , 500 )
return
2019-01-07 23:16:26 +01:00
}
log . Println ( string ( body ) )
var term term_struct
err = json . Unmarshal ( body , & term )
if err != nil {
2019-01-11 17:56:26 +01:00
http . Error ( w , "błąd zapisu danych" , 500 )
return
2019-01-07 23:16:26 +01:00
}
for i := 0 ; i < len ( term . Months ) ; i ++ {
2019-01-11 17:56:26 +01:00
daysIn ( term . Months [ i ] , term . Year , w )
2019-01-07 23:16:26 +01:00
}
}
2019-01-11 17:56:26 +01:00
func addRow ( data string , w http . ResponseWriter ) {
2019-01-07 23:16:26 +01:00
tx , err := db . Begin ( )
if err != nil {
2019-01-11 17:56:26 +01:00
http . Error ( w , "błąd zapisu danych" , 500 )
return
2019-01-07 23:16:26 +01:00
}
defer tx . Rollback ( )
2019-01-11 22:22:08 +01:00
stmt , err := tx . Prepare ( "INSERT INTO calendar (date, status, contractNo) VALUES ( $1, 'wolny', '');" )
2019-01-07 23:16:26 +01:00
if err != nil {
2019-01-11 17:56:26 +01:00
http . Error ( w , "błąd zapisu danych" , 500 )
return
2019-01-07 23:16:26 +01:00
}
defer stmt . Close ( ) // danger!
for i := 0 ; i < 1 ; i ++ {
_ , err = stmt . Exec ( data )
if err != nil {
2019-01-11 17:56:26 +01:00
http . Error ( w , "błąd zapisu danych" , 500 )
return
2019-01-07 23:16:26 +01:00
}
}
err = tx . Commit ( )
if err != nil {
2019-01-11 17:56:26 +01:00
http . Error ( w , "błąd zapisu danych" , 500 )
return
2019-01-07 23:16:26 +01:00
}
}
2019-01-08 20:08:33 +01:00
func getTerms2 ( w http . ResponseWriter , r * http . Request ) {
2019-01-07 23:16:26 +01:00
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 ) )
}
2019-01-08 20:08:33 +01:00
func getTerms ( w http . ResponseWriter , r * http . Request ) {
2019-01-07 23:16:26 +01:00
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 {
2019-01-11 17:56:26 +01:00
http . Error ( w , "błąd zapisu danych" , 500 )
return
2019-01-07 23:16:26 +01:00
}
defer stmt . Close ( )
rows , err := stmt . Query ( vars [ "month" ] , vars [ "year" ] )
if err != nil {
2019-01-11 17:56:26 +01:00
http . Error ( w , "błąd zapisu danych" , 500 )
return
2019-01-07 23:16:26 +01:00
}
defer rows . Close ( )
for rows . Next ( ) {
term := Term { }
err = rows . Scan (
& term . Date ,
& term . Status ,
& term . Contractno ,
)
if err != nil {
2019-01-11 17:56:26 +01:00
http . Error ( w , "błąd zapisu danych" , 500 )
2019-01-07 23:16:26 +01:00
return
}
terms = append ( terms , term )
}
if err = rows . Err ( ) ; err != nil {
2019-01-11 17:56:26 +01:00
http . Error ( w , "błąd danych" , 500 )
return
2019-01-07 23:16:26 +01:00
}
out , err := json . Marshal ( terms )
if err != nil {
http . Error ( w , err . Error ( ) , 500 )
return
}
fmt . Fprint ( w , string ( out ) )
}
2019-01-08 20:08:33 +01:00
func deleteTerm ( w http . ResponseWriter , req * http . Request ) {
body , err := ioutil . ReadAll ( req . Body )
if err != nil {
2019-01-11 17:56:26 +01:00
http . Error ( w , "błąd zapisu danych" , 500 )
return
2019-01-08 20:08:33 +01:00
}
log . Println ( string ( body ) )
var term Term
err = json . Unmarshal ( body , & term )
if err != nil {
2019-01-11 17:56:26 +01:00
http . Error ( w , "błąd zapisu danych" , 500 )
return
2019-01-08 20:08:33 +01:00
}
fmt . Println ( term )
{
tx , err := db . Begin ( )
if err != nil {
2019-01-11 17:56:26 +01:00
http . Error ( w , "błąd zapisu danych" , 500 )
return
2019-01-08 20:08:33 +01:00
}
defer tx . Rollback ( )
stmt , err := tx . Prepare ( "DELETE FROM contracts WHERE date=$1;" )
if err != nil {
2019-01-11 17:56:26 +01:00
http . Error ( w , "błąd zapisu danych" , 500 )
return
2019-01-08 20:08:33 +01:00
}
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 {
2019-01-11 17:56:26 +01:00
http . Error ( w , "błąd zapisu danych" , 500 )
return
2019-01-08 20:08:33 +01:00
}
defer tx . Rollback ( )
stmt , err := tx . Prepare ( "UPDATE calendar SET status=$1, contractno=$2 WHERE date=$3;" )
if err != nil {
2019-01-11 17:56:26 +01:00
http . Error ( w , "błąd zapisu danych" , 500 )
return
2019-01-08 20:08:33 +01:00
}
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 {
2019-01-11 17:56:26 +01:00
http . Error ( w , "błąd zapisu danych" , 500 )
return
2019-01-08 20:08:33 +01:00
}
log . Println ( string ( body ) )
var term Term
err = json . Unmarshal ( body , & term )
if err != nil {
2019-01-11 17:56:26 +01:00
http . Error ( w , "błąd zapisu danych" , 500 )
return
2019-01-08 20:08:33 +01:00
}
fmt . Println ( term )
{
tx , err := db . Begin ( )
if err != nil {
2019-01-11 17:56:26 +01:00
http . Error ( w , "błąd zapisu danych" , 500 )
return
2019-01-08 20:08:33 +01:00
}
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 {
2019-01-11 17:56:26 +01:00
http . Error ( w , "błąd zapisu danych" , 500 )
return
2019-01-08 20:08:33 +01:00
}
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 {
2019-01-11 17:56:26 +01:00
http . Error ( w , "błąd zapisu danych" , 500 )
return
2019-01-08 20:08:33 +01:00
}
defer tx . Rollback ( )
stmt , err := tx . Prepare ( "UPDATE calendar SET status=$1, contractno=$2 WHERE date=$3;" )
if err != nil {
2019-01-11 17:56:26 +01:00
http . Error ( w , "błąd zapisu danych" , 500 )
return
2019-01-08 20:08:33 +01:00
}
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
}
2019-01-07 23:16:26 +01:00
func postTest ( w http . ResponseWriter , req * http . Request ) {
w . Header ( ) . Set ( "Access-Control-Allow-Origin" , "*" )
w . Header ( ) . Set ( "Access-Control-Allow-Headers" , "Content-Type" )
body , err := ioutil . ReadAll ( req . Body )
if err != nil {
2019-01-11 17:56:26 +01:00
http . Error ( w , "błąd zapisu danych" , 500 )
return
2019-01-07 23:16:26 +01:00
}
fmt . Println ( "postTest" )
log . Println ( string ( body ) ) ;
}
func deleteTest ( w http . ResponseWriter , req * http . Request ) {
w . Header ( ) . Set ( "Access-Control-Allow-Origin" , "*" )
w . Header ( ) . Set ( "Access-Control-Allow-Methods" , "*" )
w . Header ( ) . Set ( "Access-Control-Allow-Headers" , "content-Type" )
2019-01-08 20:08:33 +01:00
body , err := ioutil . ReadAll ( req . Body )
if err != nil {
2019-01-11 17:56:26 +01:00
http . Error ( w , "błąd zapisu danych" , 500 )
return
2019-01-08 20:08:33 +01:00
}
2019-01-07 23:16:26 +01:00
fmt . Println ( "deleteTest" ) ;
2019-01-08 20:08:33 +01:00
log . Println ( string ( body ) ) ;
2019-01-07 23:16:26 +01:00
}