Commit 9c7f5cac by krmaet

Update changedata.c, final

parent 76a35155
Showing with 245 additions and 40 deletions
...@@ -4,14 +4,59 @@ ...@@ -4,14 +4,59 @@
#include <stdio.h> #include <stdio.h>
#include <mysql/mysql.h> #include <mysql/mysql.h>
#include <ncurses.h> #include <ncurses.h>
#include <time.h>
#include "changedata.h" #include "changedata.h"
#include "readdata.h" #include "readdata.h"
#include "main.h" #include "main.h"
int CheckUser(MYSQL *con, char *username, char *password)
{
char query[512];
int position;
//Query for password associated with the username
sprintf(query, "SELECT password, position FROM profiles WHERE username = '%s';", username);
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);
char tempPassword[128] = {" "};
char tempPosition[128] = {" "};
MYSQL_ROW row;
while ((row = mysql_fetch_row(result)))
{
strcpy(tempPassword,row[0]);
strcpy(tempPosition,row[1]);
}
position = atoi(tempPosition);
//Check if passwords match
if (!strcmp(tempPassword, password))
{
if (position == ADMIN)
{
return ADMIN;
}
else if (position == ASSISTANT)
{
return ASSISTANT;
}
else if (position == CLIENT)
{
return CLIENT;
}
}
return -1;
}
int optionMenu(char *options[], int count, char buffer[STR_MAX], int *row, int *col) int optionMenu(char *options[], int count, char buffer[STR_MAX], int *row, int *col)
{ {
WINDOW *menu_win; WINDOW *menu_win;
//~ clear();
refresh(); refresh();
noecho(); noecho();
int i; int i;
...@@ -19,7 +64,7 @@ int optionMenu(char *options[], int count, char buffer[STR_MAX], int *row, int * ...@@ -19,7 +64,7 @@ int optionMenu(char *options[], int count, char buffer[STR_MAX], int *row, int *
int highlight = 0; int highlight = 0;
//Create new window //Create new window
menu_win = create_newwin(28, 100, 1, *col / 2 - 100/2); menu_win = create_newwin(WIN_LENGTH, WIN_WIDTH, 1, *col / 2 - WIN_WIDTH/2);
//Display char from parametre //Display char from parametre
mvwprintw(menu_win, 1, 1, "%s", buffer); mvwprintw(menu_win, 1, 1, "%s", buffer);
...@@ -36,13 +81,13 @@ int optionMenu(char *options[], int count, char buffer[STR_MAX], int *row, int * ...@@ -36,13 +81,13 @@ int optionMenu(char *options[], int count, char buffer[STR_MAX], int *row, int *
{ {
//Make that option highlighted //Make that option highlighted
wattron(menu_win, A_REVERSE); wattron(menu_win, A_REVERSE);
mvwprintw(menu_win, i + 3, 1, "%s", options[i]); mvwprintw(menu_win, i + count, 1, "%s", options[i]);
wattroff(menu_win, A_REVERSE); wattroff(menu_win, A_REVERSE);
} }
//Print options as a list //Print options as a list
else else
{ {
mvwprintw(menu_win, i + 3, 1, "%s", options[i]); mvwprintw(menu_win, i + count, 1, "%s", options[i]);
} }
} }
...@@ -85,19 +130,14 @@ void CheckIn(MYSQL *con, int *row, int *col) ...@@ -85,19 +130,14 @@ void CheckIn(MYSQL *con, int *row, int *col)
noecho(); noecho();
int i, choice, checkIn; int i, choice, checkIn;
int highlight = 0; int highlight = 0;
int count = 2;
char booking[BOOKING_NUM]; char booking[BOOKING_NUM];
char buffer[BUFFER_MAX]; char buffer[BUFFER_MAX];
char *options[2] = {"Yes", "No"}; char *options[2] = {"Yes", "No"};
//Create new window //Create new window
menu_win = create_newwin(28, 100, 1, *col / 2 - 100/2); menu_win = create_newwin(WIN_LENGTH, WIN_WIDTH, 1, *col / 2 - WIN_WIDTH/2);
//Asks booking number
//~ mvwprintw(menu_win, 1, 1, "Enter booking number");
//~ mvwprintw(menu_win, 2, 1, "--------------------");
//~ echo();
//~ mvwscanw(menu_win, 3, 1, "%s", booking);
//~ noecho();
//Check if booking exists
if (CheckBooking(con, booking, row, col)) if (CheckBooking(con, booking, row, col))
{ {
mvwprintw(menu_win, 5, 1, "Person checked in?"); mvwprintw(menu_win, 5, 1, "Person checked in?");
...@@ -105,9 +145,10 @@ void CheckIn(MYSQL *con, int *row, int *col) ...@@ -105,9 +145,10 @@ void CheckIn(MYSQL *con, int *row, int *col)
//Loop until choice is made, inside is commented in optionMenu() //Loop until choice is made, inside is commented in optionMenu()
while (1) while (1)
{ {
for (i = 0; i < 2; i++) //Print all choices
for (i = 0; i < count; i++)
{ {
if (i == highlight) if (i == highlight) //If current option is selected, higlight it
{ {
wattron(menu_win, A_REVERSE); wattron(menu_win, A_REVERSE);
mvwprintw(menu_win, i + 6, 1, "%s", options[i]); mvwprintw(menu_win, i + 6, 1, "%s", options[i]);
...@@ -118,21 +159,21 @@ void CheckIn(MYSQL *con, int *row, int *col) ...@@ -118,21 +159,21 @@ void CheckIn(MYSQL *con, int *row, int *col)
mvwprintw(menu_win, i + 6, 1, "%s", options[i]); mvwprintw(menu_win, i + 6, 1, "%s", options[i]);
} }
} }
//Ask for arrow key
choice = wgetch(menu_win); choice = wgetch(menu_win);
switch(choice){ switch(choice){
case UP_KEY: case UP_KEY:
highlight--; highlight--;
if (highlight == -1) if (highlight == -1)//If the selection is already at the top of the list
{ {
highlight = 0; highlight = 0;
} }
break; break;
case DOWN_KEY: case DOWN_KEY:
highlight++; highlight++;
if (highlight == 2) if (highlight == count) //If selection is at the bottom
{ {
highlight = 2 - 1; highlight = count - 1;
} }
break; break;
default: default:
...@@ -146,6 +187,7 @@ void CheckIn(MYSQL *con, int *row, int *col) ...@@ -146,6 +187,7 @@ void CheckIn(MYSQL *con, int *row, int *col)
} }
} }
//Change checkin accordingly
if (highlight == 1) if (highlight == 1)
{ {
checkIn = 0; checkIn = 0;
...@@ -162,10 +204,6 @@ void CheckIn(MYSQL *con, int *row, int *col) ...@@ -162,10 +204,6 @@ void CheckIn(MYSQL *con, int *row, int *col)
{ {
finish_with_error(con); finish_with_error(con);
} }
// SIIA LÄHEB SQL MUUTMINE
wrefresh(menu_win); wrefresh(menu_win);
} }
...@@ -178,14 +216,16 @@ int CheckBookingDocument(MYSQL *con, char *bookingNumber, char *documentNumber, ...@@ -178,14 +216,16 @@ int CheckBookingDocument(MYSQL *con, char *bookingNumber, char *documentNumber,
char document[STR_MAX]; char document[STR_MAX];
char buffer[BUFFER_MAX]; char buffer[BUFFER_MAX];
menu_win = create_newwin(28, 100, 1, *col / 2 - 100/2); menu_win = create_newwin(WIN_LENGTH, WIN_WIDTH, 1, *col / 2 - WIN_WIDTH/2);
//Ask for booking number
mvwprintw(menu_win, 1, 1, "Enter booking number"); mvwprintw(menu_win, 1, 1, "Enter booking number");
mvwprintw(menu_win, 2, 1, "--------------------"); mvwprintw(menu_win, 2, 1, "--------------------");
echo(); echo();
mvwscanw(menu_win, 3, 1, "%s", booking); mvwscanw(menu_win, 3, 1, "%s", booking);
noecho(); noecho();
//Ask for document number
mvwprintw(menu_win, 5, 1, "Enter document number"); mvwprintw(menu_win, 5, 1, "Enter document number");
mvwprintw(menu_win, 6, 1, "--------------------"); mvwprintw(menu_win, 6, 1, "--------------------");
echo(); echo();
...@@ -205,6 +245,7 @@ int CheckBookingDocument(MYSQL *con, char *bookingNumber, char *documentNumber, ...@@ -205,6 +245,7 @@ int CheckBookingDocument(MYSQL *con, char *bookingNumber, char *documentNumber,
MYSQL_ROW row1; MYSQL_ROW row1;
row1 = mysql_fetch_row(result); row1 = mysql_fetch_row(result);
//Check if booking number exists //Check if booking number exists
if (row1 == NULL) if (row1 == NULL)
{ {
...@@ -213,7 +254,7 @@ int CheckBookingDocument(MYSQL *con, char *bookingNumber, char *documentNumber, ...@@ -213,7 +254,7 @@ int CheckBookingDocument(MYSQL *con, char *bookingNumber, char *documentNumber,
wgetch(menu_win); wgetch(menu_win);
return 0; return 0;
} }
mysql_free_result(result);
//Check if documentNumbers match //Check if documentNumbers match
if (!strcasecmp(row1[0], document)) if (!strcasecmp(row1[0], document))
{ {
...@@ -221,7 +262,6 @@ int CheckBookingDocument(MYSQL *con, char *bookingNumber, char *documentNumber, ...@@ -221,7 +262,6 @@ int CheckBookingDocument(MYSQL *con, char *bookingNumber, char *documentNumber,
strcpy(documentNumber, document); strcpy(documentNumber, document);
wrefresh(menu_win); wrefresh(menu_win);
delwin(menu_win); delwin(menu_win);
mysql_free_result(result);
return 1; return 1;
} }
else else
...@@ -231,7 +271,6 @@ int CheckBookingDocument(MYSQL *con, char *bookingNumber, char *documentNumber, ...@@ -231,7 +271,6 @@ int CheckBookingDocument(MYSQL *con, char *bookingNumber, char *documentNumber,
wgetch(menu_win); wgetch(menu_win);
wrefresh(menu_win); wrefresh(menu_win);
delwin(menu_win); delwin(menu_win);
mysql_free_result(result);
return 0; return 0;
} }
} }
...@@ -244,8 +283,9 @@ int CheckBooking(MYSQL *con, char *bookingNumber, int *row, int *col) ...@@ -244,8 +283,9 @@ int CheckBooking(MYSQL *con, char *bookingNumber, int *row, int *col)
char temp[STR_MAX]; char temp[STR_MAX];
char buffer[200]; char buffer[200];
menu_win = create_newwin(28, 100, 1, *col / 2 - 100/2); menu_win = create_newwin(WIN_LENGTH, WIN_WIDTH, 1, *col / 2 - WIN_WIDTH/2);
//Ask for booking number
mvwprintw(menu_win, 1, 1, "Enter booking number"); mvwprintw(menu_win, 1, 1, "Enter booking number");
mvwprintw(menu_win, 2, 1, "--------------------"); mvwprintw(menu_win, 2, 1, "--------------------");
...@@ -264,14 +304,14 @@ int CheckBooking(MYSQL *con, char *bookingNumber, int *row, int *col) ...@@ -264,14 +304,14 @@ int CheckBooking(MYSQL *con, char *bookingNumber, int *row, int *col)
MYSQL_ROW row1; MYSQL_ROW row1;
row1 = mysql_fetch_row(result); row1 = mysql_fetch_row(result);
mysql_free_result(result);
//Check if booking number exists
if (row1 != NULL) if (row1 != NULL)
{ {
//~ wgetch(menu_win);
strcpy(bookingNumber, temp); strcpy(bookingNumber, temp);
wrefresh(menu_win); wrefresh(menu_win);
delwin(menu_win); delwin(menu_win);
mysql_free_result(result);
return 1; return 1;
} }
else else
...@@ -281,7 +321,6 @@ int CheckBooking(MYSQL *con, char *bookingNumber, int *row, int *col) ...@@ -281,7 +321,6 @@ int CheckBooking(MYSQL *con, char *bookingNumber, int *row, int *col)
wgetch(menu_win); wgetch(menu_win);
wrefresh(menu_win); wrefresh(menu_win);
delwin(menu_win); delwin(menu_win);
mysql_free_result(result);
return 0; return 0;
} }
} }
...@@ -295,14 +334,14 @@ void ChangeName(MYSQL *con, char *booking, int *row, int *col) ...@@ -295,14 +334,14 @@ void ChangeName(MYSQL *con, char *booking, int *row, int *col)
char newLastName[STR_MAX]; char newLastName[STR_MAX];
char buffer[BUFFER_MAX]; char buffer[BUFFER_MAX];
menu_win = create_newwin(28, 100, 1, *col / 2 - 100/2); menu_win = create_newwin(WIN_LENGTH, WIN_WIDTH, 1, *col / 2 - WIN_WIDTH/2);
//Display booking number //Display booking number
mvwprintw(menu_win, 1, 1, "Booking number"); mvwprintw(menu_win, 1, 1, "Booking number");
mvwprintw(menu_win, 2, 1, "--------------"); mvwprintw(menu_win, 2, 1, "--------------");
mvwprintw(menu_win, 3, 1, "%s", booking); mvwprintw(menu_win, 3, 1, "%s", booking);
// SIIA LÄHEB SQL NIME KÜSIMINE // Query for name associated with the booking number
sprintf(buffer,"SELECT firstName,lastName FROM Users WHERE id IN" sprintf(buffer,"SELECT firstName,lastName FROM Users WHERE id IN"
"(SELECT user_id FROM Bookings WHERE bookingNumber = '%s')",booking); "(SELECT user_id FROM Bookings WHERE bookingNumber = '%s')",booking);
if (mysql_query(con, buffer)) if (mysql_query(con, buffer))
...@@ -354,7 +393,7 @@ void ChangeDocument(MYSQL *con, char *booking, int *row, int *col) ...@@ -354,7 +393,7 @@ void ChangeDocument(MYSQL *con, char *booking, int *row, int *col)
char newNumber[STR_MAX]; char newNumber[STR_MAX];
char buffer[BUFFER_MAX]; char buffer[BUFFER_MAX];
//Create new window //Create new window
menu_win = create_newwin(28, 100, 1, *col / 2 - 100/2); menu_win = create_newwin(WIN_LENGTH, WIN_WIDTH, 1, *col / 2 - WIN_WIDTH/2);
//Display booking number //Display booking number
mvwprintw(menu_win, 1, 1, "Booking number"); mvwprintw(menu_win, 1, 1, "Booking number");
...@@ -402,12 +441,13 @@ void ChangeBaggage(MYSQL *con, char *booking, int *row, int *col) ...@@ -402,12 +441,13 @@ void ChangeBaggage(MYSQL *con, char *booking, int *row, int *col)
clear(); clear();
refresh(); refresh();
int i, choice, luggage; int i, choice, luggage;
int count = 3;
char *options[3] = {"Non priority Carry-On", "Priority Carry-On", "Check-In Baggage"}; char *options[3] = {"Non priority Carry-On", "Priority Carry-On", "Check-In Baggage"};
int highlight = 0; int highlight = 0;
char buffer[BUFFER_MAX]; char buffer[BUFFER_MAX];
//Create window //Create window
menu_win = create_newwin(28, 100, 1, *col / 2 - 100/2); menu_win = create_newwin(WIN_LENGTH, WIN_WIDTH, 1, *col / 2 - WIN_WIDTH/2);
//Display booking number //Display booking number
mvwprintw(menu_win, 1, 1, "Booking number"); mvwprintw(menu_win, 1, 1, "Booking number");
...@@ -425,7 +465,7 @@ void ChangeBaggage(MYSQL *con, char *booking, int *row, int *col) ...@@ -425,7 +465,7 @@ void ChangeBaggage(MYSQL *con, char *booking, int *row, int *col)
MYSQL_ROW row1; MYSQL_ROW row1;
row1 = mysql_fetch_row(result); row1 = mysql_fetch_row(result);
luggage = atoi(row1[0]); luggage = atoi(row1[0]); //Save current luggage class
//Display current name associated with the booking number //Display current name associated with the booking number
mvwprintw(menu_win, 5, 1, "Current baggage"); mvwprintw(menu_win, 5, 1, "Current baggage");
mvwprintw(menu_win, 6, 1, "---------------"); mvwprintw(menu_win, 6, 1, "---------------");
...@@ -438,9 +478,10 @@ void ChangeBaggage(MYSQL *con, char *booking, int *row, int *col) ...@@ -438,9 +478,10 @@ void ChangeBaggage(MYSQL *con, char *booking, int *row, int *col)
//Loop until choice is made //Loop until choice is made
while (1) while (1)
{ {
for (i = 0; i < 3; i++) //Print all choices
for (i = 0; i < count; i++)
{ {
if (i == highlight) if (i == highlight) //If current option is selected, highlight it
{ {
wattron(menu_win, A_REVERSE); wattron(menu_win, A_REVERSE);
mvwprintw(menu_win, i + 11, 1, "%s", options[i]); mvwprintw(menu_win, i + 11, 1, "%s", options[i]);
...@@ -452,20 +493,21 @@ void ChangeBaggage(MYSQL *con, char *booking, int *row, int *col) ...@@ -452,20 +493,21 @@ void ChangeBaggage(MYSQL *con, char *booking, int *row, int *col)
} }
} }
//Ask for arrow key
choice = wgetch(menu_win); choice = wgetch(menu_win);
switch(choice){ switch(choice){
case UP_KEY: case UP_KEY:
highlight--; highlight--;
if (highlight == -1) if (highlight == -1) //If selected is at the top of the list
{ {
highlight = 0; highlight = 0;
} }
break; break;
case DOWN_KEY: case DOWN_KEY:
highlight++; highlight++;
if (highlight == 3) if (highlight == count) //If selected is at the bottom of the list
{ {
highlight = 3 - 1; highlight = count - 1;
} }
break; break;
default: default:
...@@ -488,3 +530,166 @@ void ChangeBaggage(MYSQL *con, char *booking, int *row, int *col) ...@@ -488,3 +530,166 @@ void ChangeBaggage(MYSQL *con, char *booking, int *row, int *col)
delwin(menu_win); delwin(menu_win);
} }
void AddFlightDest(MYSQL* con, int *col, int *row)
{
char destination[STR_MAX];
char dot[STR_MAX];
int seats = 72;
char flightNum[STR_MAX];
char query[1024];
WINDOW *menu_win;
menu_win = create_newwin(WIN_LENGTH, WIN_WIDTH, 1, *col / 2 - WIN_WIDTH/2);
while(1)
{
skip: ;
//Ask for new flight number
mvwprintw(menu_win, 1, 1, "Enter flight number for new flight");
mvwprintw(menu_win, 2, 1, "----------------------------------");
mvwprintw(menu_win, 3, 1, " ");
echo();
mvwscanw(menu_win, 3, 1, "%s", flightNum);
noecho();
sprintf(query, "SELECT flightNum FROM Flights WHERE flightNum = '%s';"
,flightNum);
if(mysql_query(con, query))
{
mvwprintw(menu_win, 4, 1, "Error checking if flightNum exists %s\n", mysql_error(con));
exit(EXIT_FAILURE);
}
MYSQL_RES *result = mysql_store_result(con);
MYSQL_ROW row;
row = mysql_fetch_row(result);
//Check if flight number already exists
if (row != NULL)
{
mvwprintw(menu_win, 4, 1, "Flight number already exists, choose another");
goto skip;
}
else
break;
mysql_free_result(result);
break;
}
//Ask for destination
mvwprintw(menu_win, 5, 1, "Enter destination");
mvwprintw(menu_win, 6, 1, "-----------------");
echo();
mvwscanw(menu_win, 7, 1, "%s", destination);
noecho();
//Ask for the date and time
mvwprintw(menu_win, 9, 1, "Enter date of time (MM:HH DD.Month.Year)");
mvwprintw(menu_win, 10, 1, "--------------------------------------");
echo();
mvwscanw(menu_win, 11, 1, "%[^\n]%*c", dot); //Read to the end of line
noecho();
sprintf(query, "INSERT INTO flights (destination, time,"
"seats, flightNum) VALUES ('%s', '%s',%d, '%s');"
,destination, dot, seats, flightNum);
if (mysql_query(con, query)) { //Query and check to database
printf("Error inserting flight into database: %s\n", mysql_error(con));
exit(EXIT_FAILURE);
}
else
{
mvwprintw(menu_win, 13, 1, "New flight successfully created!");
mvwprintw(menu_win, 14, 1, "Press any key to continue!");
wgetch(menu_win);
}
wrefresh(menu_win);
delwin(menu_win);
}
void ChangeSeat(MYSQL *con, char *booking, int *row, int *col)
{
int seatmap[ROWS][COLUMNS]= {0};
int flightId;
char buffer[BUFFER_MAX];
char* newSeat = malloc(10 * sizeof(*newSeat));
WINDOW *menu_win;
menu_win = create_newwin(WIN_LENGTH, WIN_WIDTH, 1, *col / 2 - WIN_WIDTH/2);
//Ask database for flightId to fill seat map
sprintf(buffer,"SELECT flight_id FROM Bookings WHERE bookingNumber = '%s';", booking);
if (mysql_query(con, buffer))
{
finish_with_error(con);
}
MYSQL_RES *result = mysql_store_result(con);
MYSQL_ROW row1;
row1 = mysql_fetch_row(result);
flightId = atoi(row1[0]); //Save the flightId
//Ask database for current seat
sprintf(buffer,"SELECT seat FROM Users WHERE id IN"
"(SELECT user_id FROM Bookings WHERE bookingNumber = '%s');", booking);
if (mysql_query(con, buffer))
{
finish_with_error(con);
}
MYSQL_RES *result1 = mysql_store_result(con);
MYSQL_ROW row2;
row2 = mysql_fetch_row(result1);
mvwprintw(menu_win, 1, 1, "Current booked seat is %s", row2[0]);
mvwprintw(menu_win, 2, 1, "------------------------------");
mvwprintw(menu_win, 4, 1, "Press any key to choose new seat");
wgetch(menu_win);
wrefresh(menu_win);
delwin(menu_win);
//Show seatmap for the booking
FillSeatmap(seatmap, ROWS, COLUMNS, con, flightId);
PrintSeats(seatmap, ROWS, COLUMNS, row, col, newSeat);
//Insert new seat into database
sprintf(buffer,"UPDATE Users SET seat = '%s' WHERE id IN"
"(SELECT user_id FROM Bookings WHERE bookingNumber = '%s');",newSeat, booking);
if (mysql_query(con, buffer))
{
finish_with_error(con);
}
mysql_free_result(result);
mysql_free_result(result1);
free(newSeat);
clear();
}
void DeleteFlight(MYSQL *con, int *rows, int *cols)
{
int choice;
char buffer[BUFFER_MAX];
choice = ShowDestinations(con, rows, cols);
//Delete all bookings connected to flight
sprintf(buffer,"DELETE FROM Bookings WHERE flight_id = %d;", choice);
if (mysql_query(con, buffer))
{
finish_with_error(con);
}
//Delete all users connected to flight
sprintf(buffer,"DELETE FROM Users WHERE flight_id = %d;", choice);
if (mysql_query(con, buffer))
{
finish_with_error(con);
}
//Delete flight
sprintf(buffer,"DELETE FROM Flights WHERE id = %d;", choice);
if (mysql_query(con, buffer))
{
finish_with_error(con);
}
}
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