Commit c4dc2d97 by krmaet

Update changedata.c

parent 4de8e0ad
Showing with 220 additions and 49 deletions
...@@ -5,35 +5,41 @@ ...@@ -5,35 +5,41 @@
#include <mysql/mysql.h> #include <mysql/mysql.h>
#include <ncurses.h> #include <ncurses.h>
#include "changedata.h" #include "changedata.h"
#include "readdata.h"
#include "main.h" #include "main.h"
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(); //~ clear();
refresh(); refresh();
noecho(); noecho();
int i; int i;
int choice; int choice;
int highlight = 0; int highlight = 0;
//Create new window
menu_win = create_newwin(28, 100, 1, *col / 2 - 100/2); menu_win = create_newwin(28, 100, 1, *col / 2 - 100/2);
//Display char from parametre
mvwprintw(menu_win, 2, 1, "%s", buffer); mvwprintw(menu_win, 1, 1, "%s", buffer);
mvwprintw(menu_win, 2, 1, "Choose what to change:"); mvwprintw(menu_win, 2, 1, "Choose what to change:");
//Loop until choice is made
while (1) while (1)
{ {
//Print out the list with the current selection highlighted //Print out the list with the current selection highlighted
for (i = 0; i < count; i++) for (i = 0; i < count; i++)
{ {
//Check if option printed is currently selected
if (i == highlight) if (i == highlight)
{ {
//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 + 3, 1, "%s", options[i]);
wattroff(menu_win, A_REVERSE); wattroff(menu_win, A_REVERSE);
} }
//Print options as a list
else else
{ {
mvwprintw(menu_win, i + 3, 1, "%s", options[i]); mvwprintw(menu_win, i + 3, 1, "%s", options[i]);
...@@ -43,18 +49,18 @@ int optionMenu(char *options[], int count, char buffer[STR_MAX], int *row, int * ...@@ -43,18 +49,18 @@ int optionMenu(char *options[], int count, char buffer[STR_MAX], int *row, int *
//Read if arrow up or down was pressed //Read if arrow up or down was pressed
choice = wgetch(menu_win); choice = wgetch(menu_win);
switch(choice){ switch(choice){
case UP_KEY: case UP_KEY: //Key up was pressed
highlight--; highlight--; //Decrease highlighted option
if (highlight == -1) if (highlight == -1) //If list is at the top, but up key is pressed
{ {
highlight = 0; highlight = 0; //Keep the top option highloghted
} }
break; break;
case DOWN_KEY: case DOWN_KEY: //Key down is pressed
highlight++; highlight++; //Increase highlighted option
if (highlight == count) if (highlight == count) //If list is at the bottom, but down key is pressed
{ {
highlight = count - 1; highlight = count - 1; //Keep the lowest option highlighted
} }
break; break;
default: default:
...@@ -66,34 +72,37 @@ int optionMenu(char *options[], int count, char buffer[STR_MAX], int *row, int * ...@@ -66,34 +72,37 @@ int optionMenu(char *options[], int count, char buffer[STR_MAX], int *row, int *
break; break;
} }
} }
delwin(menu_win);
wrefresh(menu_win); wrefresh(menu_win);
delwin(menu_win);
return highlight; return highlight;
} }
void CheckIn(int *row, int *col) void CheckIn(MYSQL *con, int *row, int *col)
{ {
WINDOW *menu_win; WINDOW *menu_win;
clear(); clear();
refresh(); refresh();
noecho(); noecho();
int i, choice; int i, choice, checkIn;
int highlight = 0; int highlight = 0;
char bookingNum[BOOKING_NUM]; char booking[BOOKING_NUM];
char buffer[BUFFER_MAX];
char *options[2] = {"Yes", "No"}; char *options[2] = {"Yes", "No"};
//Create new window
menu_win = create_newwin(28, 100, 1, *col / 2 - 100/2); menu_win = create_newwin(28, 100, 1, *col / 2 - 100/2);
mvwprintw(menu_win, 1, 1, "Enter booking number"); //Asks booking number
mvwprintw(menu_win, 2, 1, "--------------------"); //~ mvwprintw(menu_win, 1, 1, "Enter booking number");
echo(); //~ mvwprintw(menu_win, 2, 1, "--------------------");
mvwscanw(menu_win, 3, 1, "%s", bookingNum); //~ echo();
noecho(); //~ mvwscanw(menu_win, 3, 1, "%s", booking);
//~ noecho();
//Check if booking exists!!!
// SIIA LÄHEB SQL KÜSIMINE NUMNBRI JA CHECK IN KOHTA
if (CheckBooking(con, booking, row, col))
{
mvwprintw(menu_win, 5, 1, "Person checked in?"); mvwprintw(menu_win, 5, 1, "Person checked in?");
//Loop until choice is made, inside is commented in optionMenu()
while (1) while (1)
{ {
for (i = 0; i < 2; i++) for (i = 0; i < 2; i++)
...@@ -135,19 +144,105 @@ void CheckIn(int *row, int *col) ...@@ -135,19 +144,105 @@ void CheckIn(int *row, int *col)
break; break;
} }
} }
//~ mvwprintw(menu_win, 2 + 4, 1, "Your choice was: %s", options[highlight]); }
if (highlight == 1)
{
checkIn = 0;
}
else
{
checkIn = 1;
}
// Change checkin in sql
sprintf(buffer,"UPDATE Users SET checkedIn = '%d' WHERE id IN"
"(SELECT user_id FROM Bookings WHERE bookingNumber = '%s');",checkIn, booking);
if (mysql_query(con, buffer))
{
finish_with_error(con);
}
// SIIA LÄHEB SQL MUUTMINE // SIIA LÄHEB SQL MUUTMINE
wrefresh(menu_win); wrefresh(menu_win);
} }
void CheckBooking(char *bookingNumber, int *row, int *col) int CheckBookingDocument(MYSQL *con, char *bookingNumber, char *documentNumber, int *row, int *col)
{
WINDOW *menu_win;
clear();
refresh();
char booking[STR_MAX];
char document[STR_MAX];
char buffer[BUFFER_MAX];
menu_win = create_newwin(28, 100, 1, *col / 2 - 100/2);
mvwprintw(menu_win, 1, 1, "Enter booking number");
mvwprintw(menu_win, 2, 1, "--------------------");
echo();
mvwscanw(menu_win, 3, 1, "%s", booking);
noecho();
mvwprintw(menu_win, 5, 1, "Enter document number");
mvwprintw(menu_win, 6, 1, "--------------------");
echo();
mvwscanw(menu_win, 7, 1, "%s", document);
noecho();
//Find document number associated with the booking number
sprintf(buffer, "SELECT documentNum 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 *result = mysql_store_result(con);
MYSQL_ROW row1;
row1 = mysql_fetch_row(result);
//Check if booking number exists
if (row1 == NULL)
{
mvwprintw(menu_win, 9, 1, "Booking number not found!");
mvwprintw(menu_win, 10, 1, "Press any key to continue!");
wgetch(menu_win);
return 0;
}
//Check if documentNumbers match
if (!strcasecmp(row1[0], document))
{
strcpy(bookingNumber, booking);
strcpy(documentNumber, document);
wrefresh(menu_win);
delwin(menu_win);
mysql_free_result(result);
return 1;
}
else
{
mvwprintw(menu_win, 9, 1, "Document number does not match!");
mvwprintw(menu_win, 10, 1, "Press any key to continue!");
wgetch(menu_win);
wrefresh(menu_win);
delwin(menu_win);
mysql_free_result(result);
return 0;
}
}
int CheckBooking(MYSQL *con, char *bookingNumber, int *row, int *col)
{ {
WINDOW *menu_win; WINDOW *menu_win;
clear(); clear();
refresh(); refresh();
char temp[STR_MAX]; char temp[STR_MAX];
int bookingCheck = 1; char buffer[200];
menu_win = create_newwin(28, 100, 1, *col / 2 - 100/2); menu_win = create_newwin(28, 100, 1, *col / 2 - 100/2);
...@@ -158,28 +253,47 @@ void CheckBooking(char *bookingNumber, int *row, int *col) ...@@ -158,28 +253,47 @@ void CheckBooking(char *bookingNumber, int *row, int *col)
mvwscanw(menu_win, 3, 1, "%s", temp); mvwscanw(menu_win, 3, 1, "%s", temp);
noecho(); noecho();
// SIIA LÄHEB SQL BOOKING KÜSIMINE sprintf(buffer, "SELECT id FROM Bookings WHERE bookingNumber = '%s';", temp);
if (bookingCheck == 1) if (mysql_query(con, buffer))
{ {
finish_with_error(con);
}
MYSQL_RES *result = mysql_store_result(con);
MYSQL_ROW row1;
row1 = mysql_fetch_row(result);
if (row1 != NULL)
{
//~ wgetch(menu_win);
strcpy(bookingNumber, temp); strcpy(bookingNumber, temp);
wrefresh(menu_win);
delwin(menu_win);
mysql_free_result(result);
return 1;
} }
else else
{ {
mvwprintw(menu_win, 5, 1, "Booking number does not exist!"); mvwprintw(menu_win, 5, 1, "Booking number does not exist!");
mvwprintw(menu_win, 6, 1, "Press any key to continue!"); mvwprintw(menu_win, 6, 1, "Press any key to continue!");
wgetch(menu_win); wgetch(menu_win);
}
wrefresh(menu_win); wrefresh(menu_win);
delwin(menu_win); delwin(menu_win);
mysql_free_result(result);
return 0;
}
} }
void ChangeName(char *booking, int *row, int *col) void ChangeName(MYSQL *con, char *booking, int *row, int *col)
{ {
WINDOW *menu_win; WINDOW *menu_win;
clear(); clear();
refresh(); refresh();
char newName[STR_MAX]; char newFirstName[STR_MAX];
char newLastName[STR_MAX];
char buffer[BUFFER_MAX];
menu_win = create_newwin(28, 100, 1, *col / 2 - 100/2); menu_win = create_newwin(28, 100, 1, *col / 2 - 100/2);
...@@ -189,32 +303,57 @@ void ChangeName(char *booking, int *row, int *col) ...@@ -189,32 +303,57 @@ void ChangeName(char *booking, int *row, int *col)
mvwprintw(menu_win, 3, 1, "%s", booking); mvwprintw(menu_win, 3, 1, "%s", booking);
// SIIA LÄHEB SQL NIME KÜSIMINE // SIIA LÄHEB SQL NIME KÜSIMINE
sprintf(buffer,"SELECT firstName,lastName 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 *result = mysql_store_result(con);
MYSQL_ROW row1;
//Display current name associated with the booking number //Display current name associated with the booking number
mvwprintw(menu_win, 5, 1, "Name"); mvwprintw(menu_win, 5, 1, "Name");
mvwprintw(menu_win, 6, 1, "----"); mvwprintw(menu_win, 6, 1, "----");
mvwprintw(menu_win, 7, 1, "%s", "Mari Mets");
row1 = mysql_fetch_row(result);
mvwprintw(menu_win, 7, 1, "%s %s", row1[0], row1[1]);
//Change the name associated with the booking number //Change the name associated with the booking number
mvwprintw(menu_win, 9, 1, "Change name"); mvwprintw(menu_win, 9, 1, "Change first name");
mvwprintw(menu_win, 10, 1, "----"); mvwprintw(menu_win, 10, 1, "----");
echo(); echo();
mvwscanw(menu_win, 11, 1, "%s", newName); mvwscanw(menu_win, 11, 1, "%[^\n]%*c", newFirstName); //Reads until newline
noecho();
mvwprintw(menu_win, 13, 1, "Change last name");
mvwprintw(menu_win, 14, 1, "----");
echo();
mvwscanw(menu_win, 15, 1, "%[^\n]%*c", newLastName); //Reads until newline
noecho(); noecho();
// SIIA LÄHEB SQL NIME MUUTMINE
// Change name in sql
sprintf(buffer,"UPDATE Users SET firstName = '%s',lastName = '%s' WHERE id IN"
"(SELECT user_id FROM Bookings WHERE bookingNumber = '%s');",newFirstName, newLastName, booking);
if (mysql_query(con, buffer))
{
finish_with_error(con);
}
wrefresh(menu_win); wrefresh(menu_win);
delwin(menu_win); delwin(menu_win);
} }
void ChangeDocument(char *booking, int *row, int *col) void ChangeDocument(MYSQL *con, char *booking, int *row, int *col)
{ {
WINDOW *menu_win; WINDOW *menu_win;
clear(); clear();
refresh(); refresh();
char newNumber[STR_MAX]; char newNumber[STR_MAX];
char buffer[BUFFER_MAX];
//Create new window
menu_win = create_newwin(28, 100, 1, *col / 2 - 100/2); menu_win = create_newwin(28, 100, 1, *col / 2 - 100/2);
//Display booking number //Display booking number
...@@ -222,12 +361,21 @@ void ChangeDocument(char *booking, int *row, int *col) ...@@ -222,12 +361,21 @@ void ChangeDocument(char *booking, int *row, int *col)
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 DOKUMENDI KÜSIMINE //Ask sql for current document number
sprintf(buffer,"SELECT documentNum 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 *result = mysql_store_result(con);
MYSQL_ROW row1;
row1 = mysql_fetch_row(result);
//Display current name associated with the booking number //Display current name associated with the booking number
mvwprintw(menu_win, 5, 1, "Document number"); mvwprintw(menu_win, 5, 1, "Document number");
mvwprintw(menu_win, 6, 1, "---------------"); mvwprintw(menu_win, 6, 1, "---------------");
mvwprintw(menu_win, 7, 1, "%s", "A51567"); mvwprintw(menu_win, 7, 1, "%s", row1[0]);
//Change the name associated with the booking number //Change the name associated with the booking number
mvwprintw(menu_win, 9, 1, "Change nunber"); mvwprintw(menu_win, 9, 1, "Change nunber");
...@@ -236,21 +384,29 @@ void ChangeDocument(char *booking, int *row, int *col) ...@@ -236,21 +384,29 @@ void ChangeDocument(char *booking, int *row, int *col)
mvwscanw(menu_win, 11, 1, "%s", newNumber); mvwscanw(menu_win, 11, 1, "%s", newNumber);
noecho(); noecho();
// SIIA LÄHEB SQL DOKUMENDI MUUTMINE //Change document number in sql
sprintf(buffer,"UPDATE Users SET documentNum = '%s' WHERE id IN"
"(SELECT user_id FROM Bookings WHERE bookingNumber = '%s');",newNumber, booking);
if (mysql_query(con, buffer))
{
finish_with_error(con);
}
wrefresh(menu_win); wrefresh(menu_win);
delwin(menu_win); delwin(menu_win);
} }
void ChangeBaggage(char *booking, int *row, int *col) void ChangeBaggage(MYSQL *con, char *booking, int *row, int *col)
{ {
WINDOW *menu_win; WINDOW *menu_win;
clear(); clear();
refresh(); refresh();
int i, choice; int i, choice, luggage;
char *options[3] = {"Non priority Carry-On", "Prioriy Carry-On", "Check-In"}; char *options[3] = {"Non priority Carry-On", "Priority Carry-On", "Check-In Baggage"};
int highlight = 0; int highlight = 0;
char buffer[BUFFER_MAX];
//Create window
menu_win = create_newwin(28, 100, 1, *col / 2 - 100/2); menu_win = create_newwin(28, 100, 1, *col / 2 - 100/2);
//Display booking number //Display booking number
...@@ -258,17 +414,28 @@ void ChangeBaggage(char *booking, int *row, int *col) ...@@ -258,17 +414,28 @@ void ChangeBaggage(char *booking, int *row, int *col)
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 PAGASI KÜSIMINE //Ask sql for current luggage class
sprintf(buffer,"SELECT luggageClass 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 *result = mysql_store_result(con);
MYSQL_ROW row1;
row1 = mysql_fetch_row(result);
luggage = atoi(row1[0]);
//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, "---------------");
mvwprintw(menu_win, 7, 1, "%s", "Carry-On"); mvwprintw(menu_win, 7, 1, "%s", options[luggage]);
//Change the name associated with the booking number //Change the name associated with the booking number
mvwprintw(menu_win, 9, 1, "Change baggage"); mvwprintw(menu_win, 9, 1, "Change baggage");
mvwprintw(menu_win, 10, 1, "----"); mvwprintw(menu_win, 10, 1, "----");
//Loop until choice is made
while (1) while (1)
{ {
for (i = 0; i < 3; i++) for (i = 0; i < 3; i++)
...@@ -306,13 +473,17 @@ void ChangeBaggage(char *booking, int *row, int *col) ...@@ -306,13 +473,17 @@ void ChangeBaggage(char *booking, int *row, int *col)
} }
if (choice == ENTER_KEY) if (choice == ENTER_KEY)
{ {
break; break;
} }
} }
// SIIA LÄHEB SQL PAGASI MUUTMINE //Change luggage class in sql
sprintf(buffer,"UPDATE Users SET luggageClass = '%d' WHERE id IN"
"(SELECT user_id FROM Bookings WHERE bookingNumber = '%s');",highlight, booking);
if (mysql_query(con, buffer))
{
finish_with_error(con);
}
wrefresh(menu_win); wrefresh(menu_win);
delwin(menu_win); delwin(menu_win);
} }
......
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