예제 #1
0
        public async Task <IActionResult> DeleteMultipleShifts([FromBody] DeleteShiftsRequest request)
        {
            // As this request includes an array, check to see if array was input correctly (correct commas and quotations)
            if (request == null)
            {
                return(StatusCode(StatusCodes.Status406NotAcceptable, "Bad user input! Check the request input again."));
            }

            if (request.DeleteAllShifts)
            {
                bool deleted = await _shiftService.DeleteAllShiftsAsync();

                if (deleted)
                {
                    return(StatusCode(StatusCodes.Status204NoContent, "All shifts in the database have been successfully deleted!"));
                }
                return(NotFound());
            }
            else
            {
                /* Convert string array to Guid array, by parsing each individual string as Guid.
                 * If any of the strings cannot be parsed, or any of the parsed Guids do not match
                 * employee Ids from the database, return an appropriate error message.
                 */
                int             length       = request.employeeIdsToDeleteShifts.Length;
                Guid[]          employeeIds  = new Guid[length];
                List <Employee> allEmployees = await _employeeService.GetAllEmployeesAsync();

                for (int i = 0; i < length; i++)
                {
                    if (!Guid.TryParse(request.employeeIdsToDeleteShifts[i], out Guid requestEmployeeId))
                    {
                        return(StatusCode(StatusCodes.Status406NotAcceptable, "While trying to delete specific employee(s) shifts, " +
                                          "a given employee Id was not a valid Guid! Aborting..."));
                    }
                    bool _employeeIdExists = false;
                    for (int j = 0; j < allEmployees.Count; j++)
                    {
                        if (allEmployees[j].Id == requestEmployeeId)
                        {
                            _employeeIdExists = true;
                            break;
                        }
                    }
                    if (!_employeeIdExists)
                    {
                        return(StatusCode(StatusCodes.Status406NotAcceptable, "While trying to delete specific employee(s) shifts, " +
                                          "a given employee Id did not exist in the database! Aborting..."));
                    }
                    else
                    {
                        employeeIds[i] = requestEmployeeId;
                    }
                }
                bool deleted = await _shiftService.DeleteShiftsForSpecificEmployeesByIdAsync(employeeIds);

                if (deleted)
                {
                    return(StatusCode(StatusCodes.Status204NoContent, "All the specified employees' shifts were successfully deleted!"));
                }
                return(NotFound());
            }
        }