Create Shift
Purpose:
This function handles the creation of a new shift within the system. It involves:
-
Binding and Validating Request Payload:
-
Extracts the CreateShiftRequest payload from the incoming HTTP request.
-
Validates the payload data against defined constraints.
-
-
Preparing Flexible Start and End Dates:
- If the shift has a flexible start or end time, calculates the appropriate start and end dates based on the provided time ranges.
-
Database Transaction:
-
Initiates a database transaction to ensure data consistency.
-
Inserts the new shift into the shifts table.
-
Inserts associated shift breaks into the shift_breaks table.
-
-
Returning Response:
- Sends an HTTP OK response with an empty JSON body upon successful shift creation.
Function Signature:
func (h *ShiftHandler) CreateShift(c echo.Context) error
Parameters:
- c: An echo.Context object representing the incoming HTTP request.
Return Value:
- An error value indicating if an error occurred during the shift creation process.
Error Handling:
The function handles errors at various stages:
-
Binding and Validation: Errors during payload binding or validation are returned directly.
-
Flexible Date Preparation: Errors in calculating flexible start and end dates are returned.
-
Database Transaction: Errors during database operations (insert shifts, insert breaks) are returned.
Example Usage:
// Assuming an HTTP POST request with a valid JSON payload:
{
"name": "Morning Shift",
"start_time": "09:00:00",
"end_time": "17:00:00",
"is_flexible": true,
"flexible_start_time_range": "08:00:00-10:00:00",
"flexible_end_time_range": "16:00:00-18:00:00",
"breaks": [
{
"start_time": "12:00:00",
"end_time": "13:00:00"
}
]
}
// The function will: 1. Parse the JSON payload into a `CreateShiftRequest` struct. 2. Validate the payload. 3. Calculate the actual start and end times based on the flexible ranges. 4. Begin a database transaction. 5. Insert the shift into the `shifts` table. 6. Insert the shift break into the `shift_breaks` table. 7. Commit the transaction. 8. Return an HTTP 200 OK response.
Additional Notes:
-
The entity.CreateShiftRequest struct likely defines the expected fields for a new shift.
-
The daos.Dao struct probably provides database access methods.
-
The queries package might contain SQL queries for database operations.
-
The response package might be a custom library for handling HTTP responses.