diff --git a/Backend/helper/web.go b/Backend/helper/web.go index 4759f4d..0bae9bf 100644 --- a/Backend/helper/web.go +++ b/Backend/helper/web.go @@ -11,10 +11,11 @@ import ( // // in DEBUG == "true" everything is set to "*" so that no cors errors will be happen func SetCors(w http.ResponseWriter) { - if os.Getenv("DEBUG") == "true" { + if os.Getenv("NO_CORS") == "true" { w.Header().Set("Access-Control-Allow-Origin", "*") w.Header().Set("Access-Control-Allow-Methods", "*") w.Header().Set("Access-Control-Allow-Headers", "*") + // log.Println("Setting cors to *") } } diff --git a/Backend/models/booking.go b/Backend/models/booking.go index 65c24d4..14d231e 100644 --- a/Backend/models/booking.go +++ b/Backend/models/booking.go @@ -203,7 +203,7 @@ func checkLastBooking(b Booking) bool { log.Println("Error checking last booking: ", err) return false } - if int16(check_in_out) == b.CheckInOut { + if int16(check_in_out)%2 == b.CheckInOut%2 { return false } return true diff --git a/Backend/models/workDay.go b/Backend/models/workDay.go index fad1a5d..196de05 100644 --- a/Backend/models/workDay.go +++ b/Backend/models/workDay.go @@ -7,8 +7,8 @@ import ( ) type WorkDay struct { - Day time.Time - Bookings []Booking + Day time.Time `json:"day"` + Bookings []Booking `json:"bookings"` workTime time.Duration pauseTime time.Duration } diff --git a/DB/initdb/02_create_user.sh b/DB/initdb/02_create_user.sh index 589b189..983e896 100644 --- a/DB/initdb/02_create_user.sh +++ b/DB/initdb/02_create_user.sh @@ -7,7 +7,7 @@ psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-E CREATE USER $POSTGRES_API_USER WITH ENCRYPTED PASSWORD '$POSTGRES_API_PASSWORD'; GRANT CONNECT ON DATABASE $POSTGRES_DB TO $POSTGRES_API_USER; GRANT USAGE ON SCHEMA public TO $POSTGRES_API_USER; - GRANT SELECT, INSERT, UPDATE ON anwesenheit, personal_daten, user_password, buchung_wochen TO $POSTGRES_API_USER; + GRANT SELECT, INSERT, UPDATE ON anwesenheit, personal_daten, user_password, wochen_report TO $POSTGRES_API_USER; GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public TO $POSTGRES_API_USER; EOSQL diff --git a/Docker/arbeitszeitmessung.json b/Docker/arbeitszeitmessung.json index 5f20b9d..d020214 100644 --- a/Docker/arbeitszeitmessung.json +++ b/Docker/arbeitszeitmessung.json @@ -1,292 +1,493 @@ { - "openapi": "3.0.3", - "info": { - "title": "Arbeitszeitmessung - OpenAPI 3.0", - "description": "This demos the API for the Arbeitszeitmessung Project ", - "version": "0.1.0" - }, - "externalDocs": { - "description": "Git-Repository", - "url": "https://git.letsstein.de/tom/arbeitszeitmessung" - }, - "servers": [ - { - "url": "http://localhost:8000" - } - ], - "tags": [ - { - "name": "booking", - "description": "all Bookings" - } - ], - "paths": { - "/time": { - "put": { - "tags": ["booking"], - "summary": "Update a existing booking", - "description": "Update an existing booking by Id", - "operationId": "updateBooking", - "parameters": [ - { - "name": "counter_id", - "in": "query", - "description": "Booking ID to update", - "required": true, - "schema": { - "type": "string" - } - } - ], - "requestBody": { - "description": "Update an existent booking in the db. Not all values have to be updated", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/Booking" - } - } - }, - "required": true - }, - "responses": { - "200": { - "description": "Booking Updated", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/Booking" - } - } - } - }, - "400": { - "description": "Invalid ID supplied" - }, - "500": { - "description": "Server Error" - } - } - }, - "get": { - "tags": ["booking"], - "summary": "Gets all the bookings limited", - "description": "Returns all the bookings optionally filtered with cardID", - "operationId": "getBooking", - "parameters": [ - { - "name": "card_uid", - "in": "query", - "description": "CardID to filter for", - "required": false, - "schema": { - "type": "string" - } - } - ], - "responses": { - "200": { - "description": "Successful operation", - "content": { - "application/json": { - "schema": { - "type": "array", - "items": { - "$ref": "#/components/schemas/Booking" - } - } - } - } - }, - "400": { - "description": "Invalid cardID" - } - } - } - }, - "/time/new": { - "put": { - "tags": ["booking"], - "summary": "Create new Booking", - "description": "Creates a new booking with the supplied parameters", - "operationId": "pcreateBooking", - "parameters": [ - { - "name": "card_uid", - "in": "query", - "description": "id of the RFID card scanned", - "required": true, - "schema": { - "type": "string" - } - }, - { - "name": "geraet_id", - "in": "query", - "description": "id of the RFID reader scanning the card", - "required": true, - "schema": { - "type": "string" - } - }, - { - "name": "check_in_out", - "in": "query", - "description": "booking Type", - "required": true, - "schema": { - "type": "integer", - "enum": [1, 2, 255] - } - } - ], - "responses": { - "200": { - "description": "successfully created booking", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/Booking" - } - } - } - }, - "409": { - "description": "Same booking type as last booking" - } - } - }, - "get": { - "tags": ["booking"], - "summary": "Create new Booking", - "description": "Creates a new booking with the supplied parameters", - "operationId": "gcreateBooking", - "parameters": [ - { - "name": "card_uid", - "in": "query", - "description": "id of the RFID card scanned", - "required": true, - "schema": { - "type": "string" - } - }, - { - "name": "geraet_id", - "in": "query", - "description": "id of the RFID reader scanning the card", - "required": true, - "schema": { - "type": "string" - } - }, - { - "name": "check_in_out", - "in": "query", - "description": "booking Type", - "required": true, - "schema": { - "type": "integer", - "enum": [1, 2, 255] - } - } - ], - "responses": { - "200": { - "description": "successfully created booking", - "content": { - "application/json": { - "schema": { - "$ref": "#/components/schemas/Booking" - } - } - } - }, - "409": { - "description": "Same booking type as last booking" - } - } - } - }, - "/logout": { - "get": { - "tags": ["booking"], - "summary": "Logs out all logged in users", - "description": "With this call all actively logged in users (last booking today has check_in_out=1) will be logged out automaticly (check_in_out=255)", - "operationId": "autoLogout", - "responses": { - "200": { - "description": "Succesful", - "content": { - "application/json": { - "schema": { - "type": "array", - "items": { - "$ref": "#/components/schemas/User" - } - } - } - } - } - } - } - } - }, - "components": { - "schemas": { - "Booking": { - "type": "object", - "properties": { - "counter_id": { - "type": "integer", - "format": "int64", - "example": 100 - }, - "card_uid": { - "type": "string", - "example": "test_card" - }, - "geraet_id": { - "type": "string", - "example": "test_reader" - }, - "check_in_out": { - "type": "integer", - "example": 1, - "enum": [1, 2, 255] - }, - "timestamp": { - "type": "string", - "format": "date-time", - "example": "2024-09-05T08:51:12.670Z" - } - }, - "xml": { - "name": "booking" - } - }, - "User": { - "type": "object", - "properties": { - "card_uid": { - "type": "string", - "example": "test_card" - }, - "name": { - "type": "string", - "example": "Mustermann" - }, - "vorname": { - "type": "string", - "example": "Max" - }, - "hauptbeschäftigungsort": { - "type": "integer", - "format": "int8", - "example": 1 - } - } - } - } - } + "openapi": "3.0.3", + "info": { + "title": "Arbeitszeitmessung - OpenAPI 3.0", + "description": "This demos the API for the Arbeitszeitmessung Project ", + "version": "0.1.0" + }, + "externalDocs": { + "description": "Git-Repository", + "url": "https://git.letsstein.de/tom/arbeitszeitmessung" + }, + "servers": [ + { + "url": "http://localhost:8000", + "description": "Docker Server" + }, + { + "url": "http://localhost:8080", + "description": "Local Development" + } + ], + "tags": [ + { + "name": "booking", + "description": "all Bookings" + } + ], + "paths": { + "/time": { + "get": { + "tags": [ + "booking" + ], + "summary": "Gets all the bookings from one card_uid", + "description": "Returns all the bookings optionally filtered with cardID", + "operationId": "getBooking", + "parameters": [ + { + "name": "card_uid", + "in": "query", + "description": "CardID to filter for", + "required": true, + "schema": { + "type": "string" + } + }, + { + "name": "time_from", + "in": "query", + "description": "Timestamp since when all bookings are shown (default=1 month ago)", + "required": false, + "schema": { + "type": "string", + "example": "2025-02-28" + } + }, + { + "name": "time_to", + "in": "query", + "description": "Timestamp till when all bookings are shown (default=today)", + "required": false, + "schema": { + "type": "string", + "example": "2025-02-28" + } + } + ], + "responses": { + "200": { + "description": "Successful operation", + "content": { + "application/json": { + "schema": { + "type": "array", + "items": { + "type": "object", + "properties": { + "day": { + "type": "string", + "format": "date" + }, + "bookings": { + "type": "array", + "items": { + "type": "object", + "properties": { + "counter_id": { + "type": "integer", + "format": "int64", + "example": 100 + }, + "card_uid": { + "type": "string", + "example": "test_card" + }, + "geraet_id": { + "type": "string", + "example": "test_reader" + }, + "check_in_out": { + "type": "integer", + "example": 1, + "enum": [ + 1, + 2, + 255 + ] + }, + "timestamp": { + "type": "string", + "format": "date-time", + "example": "2024-09-05T08:51:12.670Z" + } + }, + "xml": { + "name": "booking" + } + } + } + } + } + } + } + } + }, + "400": { + "description": "Invalid cardID" + } + } + } + }, + "/time/new": { + "put": { + "tags": [ + "booking" + ], + "summary": "Create new Booking", + "description": "Creates a new booking with the supplied parameters", + "operationId": "pcreateBooking", + "parameters": [ + { + "in": "header", + "name": "Authorization", + "description": "Predefined API Key to authorize access", + "schema": { + "type": "string" + } + }, + { + "name": "card_uid", + "in": "query", + "description": "id of the RFID card scanned", + "required": true, + "schema": { + "type": "string" + } + }, + { + "name": "geraet_id", + "in": "query", + "description": "id of the RFID reader scanning the card", + "required": true, + "schema": { + "type": "string" + } + }, + { + "name": "check_in_out", + "in": "query", + "description": "booking Type", + "required": true, + "schema": { + "type": "integer", + "enum": [ + 1, + 2, + 255 + ] + } + } + ], + "responses": { + "200": { + "description": "successfully created booking", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "counter_id": { + "type": "integer", + "format": "int64", + "example": 100 + }, + "card_uid": { + "type": "string", + "example": "test_card" + }, + "geraet_id": { + "type": "string", + "example": "test_reader" + }, + "check_in_out": { + "type": "integer", + "example": 1, + "enum": [ + 1, + 2, + 255 + ] + }, + "timestamp": { + "type": "string", + "format": "date-time", + "example": "2024-09-05T08:51:12.670Z" + } + }, + "xml": { + "name": "booking" + } + } + } + } + }, + "409": { + "description": "Same booking type as last booking" + } + } + }, + "get": { + "tags": [ + "booking" + ], + "summary": "Create new Booking", + "description": "Creates a new booking with the supplied parameters", + "operationId": "gcreateBooking", + "parameters": [ + { + "in": "header", + "name": "Authorization", + "description": "Predefined API Key to authorize access", + "schema": { + "type": "string" + } + }, + { + "in": "query", + "name": "api_key", + "description": "Predefined API Key to authorize access", + "required": false, + "schema": { + "type": "string" + } + }, + { + "name": "card_uid", + "in": "query", + "description": "id of the RFID card scanned", + "required": true, + "schema": { + "type": "string" + } + }, + { + "name": "geraet_id", + "in": "query", + "description": "id of the RFID reader scanning the card", + "required": true, + "schema": { + "type": "string" + } + }, + { + "name": "check_in_out", + "in": "query", + "description": "booking Type", + "required": true, + "schema": { + "type": "integer", + "enum": [ + 1, + 2, + 255 + ] + } + } + ], + "responses": { + "200": { + "description": "successfully created booking", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "counter_id": { + "type": "integer", + "format": "int64", + "example": 100 + }, + "card_uid": { + "type": "string", + "example": "test_card" + }, + "geraet_id": { + "type": "string", + "example": "test_reader" + }, + "check_in_out": { + "type": "integer", + "example": 1, + "enum": [ + 1, + 2, + 255 + ] + }, + "timestamp": { + "type": "string", + "format": "date-time", + "example": "2024-09-05T08:51:12.670Z" + } + }, + "xml": { + "name": "booking" + } + } + } + } + }, + "401": { + "description": "none or wrong api key provided!" + }, + "409": { + "description": "Same booking type as last booking" + } + } + } + }, + "/logout": { + "get": { + "tags": [ + "booking" + ], + "summary": "Logs out all logged in users", + "description": "With this call all actively logged in users (last booking today has check_in_out=1) will be logged out automaticly (check_in_out=255)", + "operationId": "autoLogout", + "responses": { + "200": { + "description": "Succesful", + "content": { + "application/json": { + "schema": { + "type": "array", + "items": { + "type": "object", + "properties": { + "card_uid": { + "type": "string", + "example": "test_card" + }, + "name": { + "type": "string", + "example": "Mustermann" + }, + "vorname": { + "type": "string", + "example": "Max" + }, + "hauptbeschäftigungsort": { + "type": "integer", + "format": "int8", + "example": 1 + } + } + } + } + } + } + } + } + } + } + }, + "components": { + "schemas": { + "BookingGrouped": { + "type": "object", + "properties": { + "day": { + "type": "string", + "format": "date" + }, + "bookings": { + "type": "array", + "items": { + "type": "object", + "properties": { + "counter_id": { + "type": "integer", + "format": "int64", + "example": 100 + }, + "card_uid": { + "type": "string", + "example": "test_card" + }, + "geraet_id": { + "type": "string", + "example": "test_reader" + }, + "check_in_out": { + "type": "integer", + "example": 1, + "enum": [ + 1, + 2, + 255 + ] + }, + "timestamp": { + "type": "string", + "format": "date-time", + "example": "2024-09-05T08:51:12.670Z" + } + }, + "xml": { + "name": "booking" + } + } + } + } + }, + "Booking": { + "type": "object", + "properties": { + "counter_id": { + "type": "integer", + "format": "int64", + "example": 100 + }, + "card_uid": { + "type": "string", + "example": "test_card" + }, + "geraet_id": { + "type": "string", + "example": "test_reader" + }, + "check_in_out": { + "type": "integer", + "example": 1, + "enum": [ + 1, + 2, + 255 + ] + }, + "timestamp": { + "type": "string", + "format": "date-time", + "example": "2024-09-05T08:51:12.670Z" + } + }, + "xml": { + "name": "booking" + } + }, + "User": { + "type": "object", + "properties": { + "card_uid": { + "type": "string", + "example": "test_card" + }, + "name": { + "type": "string", + "example": "Mustermann" + }, + "vorname": { + "type": "string", + "example": "Max" + }, + "hauptbeschäftigungsort": { + "type": "integer", + "format": "int8", + "example": 1 + } + } + } + } + } } diff --git a/Docker/arbeitszeitmessung.yaml b/Docker/arbeitszeitmessung.yaml index cff5cf2..a0a43a4 100644 --- a/Docker/arbeitszeitmessung.yaml +++ b/Docker/arbeitszeitmessung.yaml @@ -1,74 +1,58 @@ openapi: 3.0.3 info: title: Arbeitszeitmessung - OpenAPI 3.0 - description: 'This demos the API for the Arbeitszeitmessung Project ' + description: "This demos the API for the Arbeitszeitmessung Project " version: 0.1.0 externalDocs: description: Git-Repository url: https://git.letsstein.de/tom/arbeitszeitmessung servers: - url: http://localhost:8000 + description: Docker Server + - url: http://localhost:8080 + description: Local Development tags: - name: booking description: all Bookings paths: /time: - put: - tags: - - booking - summary: Update a existing booking - description: Update an existing booking by Id - operationId: updateBooking - parameters: - - name: counter_id - in: query - description: Booking ID to update - required: true - schema: - type: string - requestBody: - description: >- - Update an existent booking in the db. Not all values have to be - updated - content: - application/json: - schema: - $ref: '#/components/schemas/Booking' - required: true - responses: - '200': - description: Booking Updated - content: - application/json: - schema: - $ref: '#/components/schemas/Booking' - '400': - description: Invalid ID supplied - '500': - description: Server Error get: tags: - booking - summary: Gets all the bookings limited + summary: Gets all the bookings from one card_uid description: Returns all the bookings optionally filtered with cardID operationId: getBooking parameters: - name: card_uid in: query description: CardID to filter for + required: true + schema: + type: string + - name: time_from + in: query + description: Timestamp since when all bookings are shown (default=1 month ago) required: false schema: type: string + example: "2025-02-28" + - name: time_to + in: query + description: Timestamp till when all bookings are shown (default=today) + required: false + schema: + type: string + example: "2025-02-28" responses: - '200': + "200": description: Successful operation content: application/json: schema: type: array items: - $ref: '#/components/schemas/Booking' - '400': + $ref: "#/components/schemas/BookingGrouped" + "400": description: Invalid cardID /time/new: put: @@ -78,6 +62,11 @@ paths: description: Creates a new booking with the supplied parameters operationId: pcreateBooking parameters: + - in: header + name: Authorization + description: Predefined API Key to authorize access + schema: + type: string - name: card_uid in: query description: id of the RFID card scanned @@ -101,13 +90,13 @@ paths: - 2 - 255 responses: - '200': + "200": description: successfully created booking content: application/json: schema: - $ref: '#/components/schemas/Booking' - '409': + $ref: "#/components/schemas/Booking" + "409": description: Same booking type as last booking get: tags: @@ -116,6 +105,17 @@ paths: description: Creates a new booking with the supplied parameters operationId: gcreateBooking parameters: + - in: header + name: Authorization + description: Predefined API Key to authorize access + schema: + type: string + - in: query + name: api_key + description: Predefined API Key to authorize access + required: false + schema: + type: string - name: card_uid in: query description: id of the RFID card scanned @@ -139,13 +139,15 @@ paths: - 2 - 255 responses: - '200': + "200": description: successfully created booking content: application/json: schema: - $ref: '#/components/schemas/Booking' - '409': + $ref: "#/components/schemas/Booking" + "401": + description: none or wrong api key provided! + "409": description: Same booking type as last booking /logout: get: @@ -155,16 +157,26 @@ paths: description: With this call all actively logged in users (last booking today has check_in_out=1) will be logged out automaticly (check_in_out=255) operationId: autoLogout responses: - '200': + "200": description: Succesful content: application/json: schema: type: array items: - $ref: '#/components/schemas/User' + $ref: "#/components/schemas/User" components: schemas: + BookingGrouped: + type: object + properties: + day: + type: string + format: date + bookings: + type: array + items: + $ref: "#/components/schemas/Booking" Booking: type: object properties: @@ -188,7 +200,7 @@ components: timestamp: type: string format: date-time - example: '2024-09-05T08:51:12.670Z' + example: "2024-09-05T08:51:12.670Z" xml: name: booking User: diff --git a/Docker/docker-compose.dev.yml b/Docker/docker-compose.dev.yml index 0339f22..123d0b4 100644 --- a/Docker/docker-compose.dev.yml +++ b/Docker/docker-compose.dev.yml @@ -20,16 +20,16 @@ services: - 8001:8080 backend: build: ../Backend - image: git.letsstein.de/tom/arbeitszeit-backend + image: git.letsstein.de/tom/arbeitszeit-backend:0.1.1 env_file: - .env environment: POSTGRES_HOST: db POSTGRES_DB: ${POSTGRES_DB} EXPOSED_PORT: ${EXPOSED_PORT} - DEBUG: true + NO_CORS: true ports: - - 8000:8080 + - ${EXPOSED_PORT}:8080 depends_on: - db swagger: diff --git a/Docker/docker-compose.yml b/Docker/docker-compose.yml index 56d7c43..f484d08 100644 --- a/Docker/docker-compose.yml +++ b/Docker/docker-compose.yml @@ -13,6 +13,8 @@ services: volumes: - ${POSTGRES_PATH}:/var/lib/postgresql/data - ${POSTGRES_PATH}/initdb:/docker-entrypoint-initdb.d + ports: + - 5432:5432 backend: image: git.letsstein.de/tom/arbeitszeit-backend