static void server_deleteByIdJSON(http_request_t request, socket_t* client, worker_t** workers) { int id; char* getId = strpbrk(request.uri, "0123456"); if(getId) { id = atoi(getId); if(id<0 || id>6 || worker_getId(workers[id]) == -1) { socket_write_string(client, "<h1>Wrong ID</h1><p><a href=\"/workers/\">All workers</a></p>"); return; } } else { server_notFound(client); return; } char toSend[2000]; char buffer[2000]; sprintf(buffer, "Success!" "Worker %s successfully deleted!", worker_getName(workers[id])); workers[id] = worker_new(); sprintf(toSend, "HTTP/1.1 200 OK\n" "Content-Type: application/json\n" "Content-Length: %i\r\n\r\n" "\n%s", strlen(buffer), buffer); socket_write_string(client, toSend); socket_close(client); }
static char* server_getAllWorkersHTML(db_t* base) { char allOfTHem[10000] = ""; char one[1000]; int count = db_countWorkers(base); worker_t** workers[count]; for(int i = 0; i<count; i++) workers[i] = worker_new(); db_fillWorkerArr(base, workers); for(int i = 0; i < count; i++) { sprintf(one, " <p>Id: %i<br>" " Name: <a href=\"/workers/%i\">%s</a><br>" " <a href=\"/workers/delete/%i\">Free worker</a>" " <br><br></p>", worker_getId(workers[i]), worker_getId(workers[i]), worker_getName(workers[i]), worker_getId(workers[i])); strcat(allOfTHem, one); } strcat(allOfTHem, "<a href=\"/workers/new/\">New worker</a>"); for(int i = 0; i<count; i++) worker_free(workers[i]); return allOfTHem; }
static char* server_getWorkerHTML(worker_t* worker) { char one[500] = ""; sprintf(one, " Id: %i<br>" " Name: %s<br>" "Surname: %s<br>" " Salary: %i<br>" " Year: %i<br><br>" "<p><a href=\"/workers/delete/%i\">Free</a></p>", worker_getId(worker), worker_getName(worker), worker_getSurname(worker), worker_getSalary(worker), worker_getYear(worker), worker_getId(worker)); return one; }
static char* server_getWorkerHTML(worker_t* worker) { char one[500] = ""; sprintf(one, " Id: %i<br>" " Name: %s<br>" " Surname: %s<br>" "Birth date: %s<br>" "Experience: %i<br>" " Rating: %.2f<br><br>" "<p><a href=\"/workers/delete/%i\">Delete</a></p>", worker_getId(worker), worker_getName(worker), worker_getSurname(worker), worker_getBirthdate(worker), worker_getExp(worker), worker_getRate(worker), worker_getId(worker)); return one; }
static char* server_getAllWorkersHTML(worker_t** workers) { char all[10000] = ""; char one[1000]; for(int i = 0; i < MAX_WORKERS; i++) { if(worker_getId(workers[i]) != -1) { sprintf(one, " <p>Id: %i<br>" " Name: <a href=\"/workers/%i\">%s</a><br>" " <a href=\"/workers/delete/%i\">Free worker</a>" " <br><br></p>", i, worker_getId(workers[i]), worker_getName(workers[i]), worker_getId(workers[i])); strcat(all, one); //puts(allOfTHem); } } strcat(all, "<a href=\"/workers/new/\">New worker</a>"); return all; }
static void server_deleteByIdHTML(http_request_t request, socket_t* client, db_t* base) { int id; int count = db_countWorkers(base); char* getId = strpbrk(request.uri, "0123456789"); if(getId) { id = atoi(getId); if(id<=0 || !db_checkId(base, id)) { socket_write_string(client, "<h1>Wrong ID</h1><p><a href=\"/workers/\">All workers</a></p>"); return; } } else { server_notFound(client); return; } worker_t* worker = db_getWorkerById(base, id); char toSend[2000]; char buffer[2000]; sprintf(buffer, "<head><title>Delete</title></head>" "<h1>Success</h1><p><a href=\"/workers/\">All workers</a></p>" "<p>Worker %s successfully deleted!</p>", worker_getName(worker)); db_deleteWorker(base, id); sprintf(toSend, "HTTP/1.1 200 OK\n" "Content-Type: text/html\n" "Content-Length: %i\r\n\r\n" "\n%s", strlen(buffer), buffer); socket_write_string(client, toSend); socket_close(client); }
static void server_deleteByIdHTML(http_request_t request, socket_t* client, worker_t** workers) { int id; char* getId = strpbrk(request.uri, "0123456"); if(getId) { id = atoi(getId); if(id<0 || id>6 || worker_getId(workers[id]) == -1) { socket_write_string(client, "<h1>Wrong ID</h1><p><a href=\"/workers/\">All workers</a></p>"); return; } } else { server_notFound(client); return; } char toSend[2000]; char buffer[2000]; sprintf(buffer, "<head><title>Delete</title></head>" "<h1>Success</h1><p><a href=\"/workers/\">All workers</a></p>" "<p>Worker %s successfully deleted!</p>", worker_getName(workers[id])); //workers[id] = worker_new(); worker_fill(workers[id], -1, "qqq", "qqq", "1111-11-11", 0, 0); sprintf(toSend, "HTTP/1.1 200 OK\n" "Content-Type: text/html\n" "Content-Length: %i\r\n\r\n" "\n%s", strlen(buffer), buffer); socket_write_string(client, toSend); socket_close(client); }
static void server_deleteByIdJSON(http_request_t request, socket_t* client, db_t* base) { int id; char* getId = strpbrk(request.uri, "0123456789"); if(getId) { id = atoi(getId); if(id<=0 || !db_checkId(base, id)) { socket_write_string(client, "{\nWrong ID\n}"); return; } } else { server_notFound(client); return; } worker_t* worker = db_getWorkerById(base, id); char toSend[2000]; char buffer[2000]; sprintf(buffer, "Success!" "Worker %s successfully deleted!", worker_getName(worker)); db_deleteWorker(base, id); sprintf(toSend, "HTTP/1.1 200 OK\n" "Content-Type: application/json\n" "Content-Length: %i\r\n\r\n" "\n%s", strlen(buffer), buffer); socket_write_string(client, toSend); socket_close(client); }
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { static HINSTANCE hInst; static HWND hButtonEx, hLable; static HWND hStaticIndS, hStaticNameS, hStaticSurnameS, hStaticExpS, hStaticSalarS; static HWND hStaticName, hStaticSurname, hStaticExp, hStaticSalar; static worker_t * worker[WORKERS_COUNT]; static int index; static char buffer[10]; static int tick; switch(msg) { case WM_CREATE: CreateWindowW( L"button", L"Timer", WS_CHILD|WS_VISIBLE|BS_CHECKBOX|BS_AUTOCHECKBOX, 90, 150, 130, 23, hwnd, (HMENU)TIMER_CB, NULL, NULL); hButtonEx = CreateWindowEx(0, WC_BUTTON, "Exit", WS_CHILD|WS_VISIBLE|WS_TABSTOP|BS_DEFPUSHBUTTON, 90, 190, 130, 23, hwnd, (HMENU)BUTTON_EX, hInst, NULL); hStaticIndS = CreateWindowEx(0, WC_STATIC, "Index:", WS_CHILD|WS_VISIBLE, 10, 20, 130, 23, hwnd, (HMENU)STATIC_INDEX_S, hInst, NULL); hStaticNameS = CreateWindowEx(0, WC_STATIC, "Name:", WS_CHILD|WS_VISIBLE, 10, 44, 130, 23, hwnd, (HMENU)STATIC_NAME_S, hInst, NULL); hStaticSurnameS = CreateWindowEx(0, WC_STATIC, "Surname:", WS_CHILD|WS_VISIBLE, 10, 68, 130, 23, hwnd, (HMENU)STATIC_SURNAME_S, hInst, NULL); hStaticExpS = CreateWindowEx(0, WC_STATIC, "Experience:", WS_CHILD|WS_VISIBLE, 10, 92, 130, 23, hwnd, (HMENU)STATIC_EXP_S, hInst, NULL); hStaticSalarS = CreateWindowEx(0, WC_STATIC, "Salary:", WS_CHILD|WS_VISIBLE, 10, 116, 130, 23, hwnd, (HMENU)STATIC_SALARY_S, hInst, NULL); index = rand() % WORKERS_COUNT; worker[0] = worker_new(0, "George", "Horn", 6, 6000); worker[1] = worker_new(1, "Emily", "Wiggins", 2, 2000); worker[2] = worker_new(2, "Claude", "Jefferson", 3, 3000); worker[3] = worker_new(3, "Ursula", "Miller", 1, 1000); worker[4] = worker_new(4, "Damian", "Fields", 4, 4500); tick = worker_getIndex(worker[index]); hLable = CreateWindowEx(0, "STATIC", itoa(worker_getIndex(worker[index]), buffer, 10), WS_CHILD|WS_VISIBLE, 150, 20, 130, 23, hwnd, (HMENU)LABLE_ID, hInst, NULL); int ret = SetTimer(hwnd, TIMER_CB, TIMER_TICK, NULL); if(ret == 0) MessageBox(hwnd, "Could not set timer", "ERROR", MB_OK|MB_ICONEXCLAMATION); hStaticName = CreateWindowEx(0, WC_STATIC, worker_getName(worker[index]), WS_CHILD|WS_VISIBLE, 150, 44, 130, 23, hwnd, (HMENU)STATIC_NAME, hInst, NULL); hStaticSurname = CreateWindowEx(0, WC_STATIC, worker_getSurname(worker[index]), WS_CHILD|WS_VISIBLE, 150, 68, 130, 23, hwnd, (HMENU)STATIC_SURNAME, hInst, NULL); hStaticExp = CreateWindowEx(0, WC_STATIC, itoa(worker_getExp(worker[index]), buffer, 10), WS_CHILD|WS_VISIBLE, 150, 92, 130, 23, hwnd, (HMENU)STATIC_EXP, hInst, NULL); hStaticSalar = CreateWindowEx(0, WC_STATIC, itoa(worker_getSalary(worker[index]), buffer, 10), WS_CHILD|WS_VISIBLE, 150, 116, 130, 23, hwnd, (HMENU)STATIC_SALARY, hInst, NULL); break; case WM_TIMER: { int checked = IsDlgButtonChecked(hwnd, TIMER_CB); if(checked) { tick++; sprintf(buffer, "%i", tick); HWND hIndex = GetDlgItem(hwnd, LABLE_ID); SendMessage(hIndex, WM_SETTEXT, (WPARAM)256, (LPARAM)buffer); } } break; case WM_COMMAND: { switch(LOWORD(wParam)) { case BUTTON_EX: { for(int i = 0; i<WORKERS_COUNT; i++) worker_free(worker[i]); DestroyWindow(hwnd); break; } } break; } case WM_CLOSE: for(int i = 0; i<WORKERS_COUNT; i++) worker_free(worker[i]); DestroyWindow(hwnd); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd, msg, wParam, lParam); } return 0; }