Commit 61284295 by krmaet

Add new file

parent 6d558436
Showing with 455 additions and 0 deletions
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mysql/mysql.h>
#include <time.h>
#include <ncurses.h>
#include "readdata.h"
#include "main.h"
int CheckUser(MYSQL *con, char *username, char *password)
{
char query[STR_MAX];
char temp[STR_MAX];
int position;
sprintf(query, "SELECT password FROM profiles WHERE username = krmaet;");
if (mysql_query(con, query)) { //päring serverile
printf("Error getting data: %s\n", mysql_error(con));
exit(EXIT_FAILURE);
}
MYSQL_RES *result = mysql_store_result(con);
mysql_num_fields(result);
MYSQL_ROW row;
strcpy(temp,row[0]);
if (!strstr(temp, password))
{
sprintf(query, "SELECT position FROM profiles WHERE username = %s;", username);
if (mysql_query(con, query)) { //päring serverile
printf("Error getting seat data: %s\n", mysql_error(con));
exit(EXIT_FAILURE);
}
MYSQL_RES *result = mysql_store_result(con);
mysql_num_fields(result);
MYSQL_ROW row;
position = atoi(row[0]);
if (position == ADMIN)
{
return ADMIN;
}
else if (position == ASSISTANT)
{
return ASSISTANT;
}
else
{
return CLIENT;
}
}
else
{
return -1;
}
}
void NewTicket(int *row, int *col, char *seat, int flightID, MYSQL *con, int count)
{
srand(time(NULL));
int i;
users data[SEAT_MAX];
int highlight = 0;
int choice;
clear();
refresh();
WINDOW *ticket_win;
ticket_win = create_newwin(28, 100, 1, *col / 2 - 100/2);
wrefresh(ticket_win);
int flightId = flightID;
data[count].flight_id = flightId;
strcpy(data[count].seat, seat);
echo();
mvwprintw(ticket_win, 1, 1,"Enter Your document number:");
char docNum[DOC_MAX];
wscanw(ticket_win, "%s", docNum);
strcpy(data[count].documentNum, docNum);
mvwprintw(ticket_win, 3, 1,"Enter first name:"); //lisada juurde et kui on tühik siis
char fName[NAME_MAX]; //muudetakse see alakriipsuks.
wscanw(ticket_win, "%s", fName);
strcpy(data[count].fName, fName);
mvwprintw(ticket_win, 5, 1,"Enter last name:"); //sama kommentaar mis eelmisele
char lName[NAME_MAX];
wscanw(ticket_win, "%s", lName);
strcpy(data[count].lName, lName);
mvwprintw(ticket_win, 7, 1,"Enter date of birth (Format: 'DD.Month.Year'):");
char dateOB[DOB_MAX];
wscanw(ticket_win, "%s", dateOB);
strcpy(data[count].dateOfBirth, dateOB);
mvwprintw(ticket_win, 9, 1,"Enter email:");
char meil[STR_MAX];
wscanw(ticket_win, "%s", meil);
strcpy(data[count].email, meil);
mvwprintw(ticket_win, 11, 1,"Enter residency:");
char residency[STR_MAX];
wscanw(ticket_win, "%s", residency);
strcpy(data[count].residency, residency);
data[count].checkedIn = false;
noecho();
mvwprintw(ticket_win, 13, 1, "Choose luggage type");
mvwprintw(ticket_win, 14, 1, "-------------------");
char *options[3] = {"Small bag + Carry-On(default included in fare)",
"1-Check-In 20kg suitcase", "2-Oversized(bikes, 20+kg)"};
while (1)
{
for (i = 0; i < 3; i++)
{
if (i == highlight)
{
wattron(ticket_win, A_REVERSE);
mvwprintw(ticket_win, i + 15, 1, "%s", options[i]);
wattroff(ticket_win, A_REVERSE);
}
else
{
mvwprintw(ticket_win, i + 15, 1, "%s", options[i]);
}
}
choice = wgetch(ticket_win);
switch(choice){
case UP_KEY:
highlight--;
if (highlight == -1)
{
highlight = 0;
}
break;
case DOWN_KEY:
highlight++;
if (highlight == 3)
{
highlight = 3 - 1;
}
break;
default:
break;
}
if (choice == ENTER_KEY)
{
break;
}
}
data[count].luggageClass = highlight;
delwin(ticket_win);
insertPersonIntoTable(con, data[count], row, col);
}
void FillSeatmap(int seats[ROWS][COLUMNS], int rows, int cols, MYSQL *con, int flightId)
{
char query[512];
sprintf(query, "SELECT seat FROM users WHERE flight_id = %d;", flightId);
if (mysql_query(con, query)) { //päring serverile
printf("Error getting seat data: %s\n", mysql_error(con));
exit(EXIT_FAILURE);
}
MYSQL_RES *result = mysql_store_result(con);
int num_fields = mysql_num_fields(result);
char temp[4];
char tempRow[3];
int tempRowInt;
char colNum;
int col;
MYSQL_ROW row;
while ((row = mysql_fetch_row(result)))
{
for(int i = 0; i < num_fields; i++)
{
//printf("%s ", row[i] ? row[i] : "NULL");
strcpy(temp,row[i]);
strncpy(tempRow, temp, 2);
tempRow[2] = '\n';
tempRowInt = atoi(tempRow);
colNum = temp[2];
if (colNum == 'A')
{
col = 0;
}
else if (colNum == 'B')
{
col = 1;
}
else if (colNum == 'C')
{
col = 2;
}
else if (colNum == 'D')
{
col = 3;
}
seats[tempRowInt][col] = 1;
}
//printf("\n");
}
}
void PrintSeats(int data[ROWS][COLUMNS], int rows, int cols, int *row, int *col, char *seat)
{
int i, j;
char colId;
char temp[STR_MAX];
WINDOW *menu_win;
menu_win = create_newwin(28, 100, 1, *col / 2 - 100/2);
wrefresh(menu_win);
mvwprintw(menu_win, 1, 1, "Choose your seat");
mvwprintw(menu_win, 2, 1, "----------------");
wmove(menu_win, 3, 1);
for(i = 0; i < rows; i++)
{
for(j = 0; j < cols; j++)
{
if (j == 0)
{
colId = 'A';
}
else if (j == 1)
{
colId= 'B';
}
else if (j == 2)
{
colId = 'C';
}
else if (j == 3)
{
colId = 'D';
}
if(data[i][j] == 1)
{
//~ attron(COLOR_RED);
wprintw(menu_win, " X ");
//~ attroff(COLOR_RED);
}
else if(data[i][j] == 0)
{
//~ attron(COLOR_GREEN);
if(i < 10)
wprintw(menu_win, "0%d%c", i, colId);
else
wprintw(menu_win, "%d%c", i, colId);
//~ attroff(COLOR_GREEN);
}
if(j == 1)
wprintw(menu_win, " ");
else
wprintw(menu_win, " ");
wrefresh(menu_win);
}
wmove(menu_win, i + 3, 1);
}
mvwprintw(menu_win, 23, 1, "Please type your seat and press enter: ");
echo();
wscanw(menu_win, "%s", temp);
noecho();
strcpy(seat, temp);
//~ mvwprintw(menu_win, 25, 1, "%s", seat);
//~ for (i = 0; i < 5; i++)
//~ {
//~ seat[i] = temp[i];
//~ }
wrefresh(menu_win);
delwin(menu_win);
}
void insertPersonIntoTable(MYSQL* con, users data, int *rows, int *cols)
{
WINDOW *ticket_win;
ticket_win = create_newwin(28, 100, 1, *cols / 2 - 100/2);
wrefresh(ticket_win);
char query[1024]; //salvestan mysql päringu stringi
sprintf(query, "INSERT INTO users (documentNum, firstName, "
"lastName, dateOfBirth, email, residency, checkedIn, seat,"
"luggageClass, flight_id) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', %d, '%s', %d, %d);"
,data.documentNum, data.fName, data.lName, data.dateOfBirth,
data.email, data.residency, data.checkedIn, data.seat, data.luggageClass,
data.flight_id);
//~ printf("DEBUG:%s\n\n", query);
if (mysql_query(con, query)) { //päring serverile
clear();
printf("Error inserting user into database: %s\n", mysql_error(con));
exit(EXIT_FAILURE);
}
char bookingCode[9];
const char letters[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int i;
for (i = 0; i < 8; i++) //broneeringunumbri genereerimine
{
bookingCode[i] = letters[GetRand(0, 35)];
}
bookingCode[8] = '\0';
mvwprintw(ticket_win, 3, 1, "Your booking number: %s", bookingCode);
mvwprintw(ticket_win, 4, 1, "Write this down to change your booking before the flight");
//päring et saada kätte viimane sisestatud väljale loodud ID
if (mysql_query(con, "SELECT last_insert_id();"))
{
finish_with_error(con);
}
MYSQL_RES *result = mysql_store_result(con);
MYSQL_ROW row;
row = mysql_fetch_row(result);
int res = atoi(row[0]);
sprintf(query, "INSERT INTO bookings (bookingNumber, flight_id, user_id)"
"VALUES ('%s', %d, %d);"
,bookingCode, data.flight_id, res);
if (mysql_query(con, query))
{
clear();
printf("Error creating bookings for the users: %s\n", mysql_error(con));
exit(EXIT_FAILURE);
}
mvwprintw(ticket_win, 5, 1, "Press any key to continue");
wgetch(ticket_win);
wrefresh(ticket_win);
}
int ShowDestinations(MYSQL *con, int *rows, int *cols)
{
if (mysql_query(con, "SELECT id,destination,time FROM Flights;"))
{
finish_with_error(con);
}
MYSQL_RES *result = mysql_store_result(con);
int num_fields = mysql_num_fields(result);
WINDOW *menu_win;
menu_win = create_newwin(28, 100, 1, *cols / 2 - 100/2);
mvwprintw(menu_win, 1, 1, "Available destinations and times");
mvwprintw(menu_win, 2, 1, "--------------------------------");
MYSQL_ROW row;
int count = 0;
while ((row = mysql_fetch_row(result)))
{
wmove(menu_win, count + 4, 1);
for(int i = 0; i < num_fields; i++)
{
wprintw(menu_win, "%s ", row[i] ? row[i] : "NULL");
}
count++;
}
mvwprintw(menu_win, 11, 1, "Count is %d", count);
mvwprintw(menu_win, 12, 1, "To select press (1-%d)", count);
int selection;
do
{
selection = wgetch(menu_win) - 48;
if (selection > count || selection < 1)
{
mvwprintw(menu_win, 11, 1, "selection-%d", selection);
mvwprintw(menu_win, 12, 1, "Invalid selection, retry (1-%d)", count);
}
}while(selection > count || selection < 1);
char query[512];
sprintf(query, "SELECT destination FROM Flights WHERE Flights.id = %d;",
selection);
if (mysql_query(con, query))
{
finish_with_error(con);
}
result = mysql_store_result(con);
//~ PrintRes(result);
wrefresh(menu_win);
mysql_free_result(result);
return selection;
}
MYSQL* connectToMySQLServer()
{
MYSQL* con = mysql_init(NULL);
if (con == NULL)
{
fprintf(stderr, "mysql_init() failed\n");
exit(1);
}
if (mysql_real_connect(con, "127.0.0.1", "root", "teretere",
"broneeringud", 0, NULL, 0) == NULL)
{
finish_with_error(con);
}
return con;
}
void finish_with_error(MYSQL *con)
{
fprintf(stderr, "%s\n", mysql_error(con));
mysql_close(con);
exit(EXIT_FAILURE);
}
void PrintRes(MYSQL_RES *result)
{
int num_fields = mysql_num_fields(result);
MYSQL_ROW row;
while ((row = mysql_fetch_row(result)))
{
for(int i = 0; i < num_fields; i++)
{
printf("%s ", row[i] ? row[i] : "NULL");
}
printf("\n");
}
}
int GetRand(int min, int max)
{
return (rand() % (max - min + 1)) + min;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment