Advertisement
Guest User

Untitled

a guest
Jan 19th, 2020
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 25.01 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using MySql.Data;
  7. using MySql.Data.MySqlClient;
  8.  
  9.  
  10. namespace Camping_App
  11. {
  12.     /*
  13.      * Data object responsible for storing lists of database entities
  14.      * Access information stored in database through this class
  15.      * Insert information into database through this class
  16.      *
  17.      * In orde to access fhict server, VPN connection is required
  18.      * Use Cisco AnyConnect Client - vdi.fhict.nl
  19.      */
  20.  
  21.     // documentation non-existent
  22.  
  23.     class DataObj
  24.     {
  25.         private List<users> users_list;
  26.         private List<orders> orders_list;
  27.         private List<order_items> order_items;
  28. <<<<<<< HEAD
  29.         private List<user_with_camping_reservation> reservation_list;
  30. =======
  31.       private List<user_with_camping_reservation> reservation_list;
  32. >>>>>>> 2090e70fcd7ca0c55c8bb955364c0aa999309ba8
  33.  
  34.         public DataObj()
  35.         {
  36.             users_list = new List<users>();
  37.             orders_list = new List<orders>();
  38.             order_items = new List<order_items>();
  39.             reservation_list = new List<user_with_camping_reservation>();
  40.         }
  41.  
  42.         public void createAndInsertUser(string name_f, string name_l, string email)
  43.         {
  44.             users user = new users(name_f, name_l, email);
  45.             user.insertUser();
  46.         }
  47.  
  48.         public bool userNotInList(users user)
  49.         {
  50.             foreach (users u in users_list)
  51.             {
  52.                 if (user.userID() == u.userID())
  53.                     return false;
  54.             }
  55.             return true;
  56.         }
  57.  
  58.         public bool only_uniqueId(orders order)
  59.         {
  60.             foreach (orders u in orders_list)
  61.             {
  62.                 if (order.orderID() == u.orderID())
  63.                     return false;
  64.             }
  65.             return true;
  66.         }
  67.  
  68.         public bool item_to_a_basket(order_items oi)
  69.         {
  70.             foreach (order_items o in order_items)
  71.             {
  72.                 if (oi.order_itemID() == o.order_itemID())
  73.                     return false;
  74.             }
  75.             return true;
  76.         }
  77.  
  78.         public void runQuerry_updateUserList_thatHaveBooked()
  79. <<<<<<< HEAD
  80.         {
  81.             //string input = $"select id, rfid from users";//first_name, last_name, email, rfid, password from users";//created_at, updated_at from users;";
  82.  
  83.             string input = "select user_id from orders where order_type = 'camping' AND order_status = 1;";
  84.  
  85.  
  86.             MySqlConnection databaseConnection = new MySqlConnection("Server=studmysql01.fhict.local;" +
  87.                                                                         "Uid=dbi428954;" +
  88.                                                                         "Database=dbi428954;" +
  89.                                                                         "Pwd=planetfun69planetfun;");
  90.             MySqlCommand comandDatabase = new MySqlCommand(input, databaseConnection);
  91.             comandDatabase.CommandTimeout = 60;
  92.             try
  93.             {
  94.                 databaseConnection.Open();
  95.                 MySqlDataReader sqlReader = comandDatabase.ExecuteReader();
  96.                 if (sqlReader.HasRows)
  97.                 {
  98.                     while (sqlReader.Read())
  99.                     {
  100.                         user_with_camping_reservation reserv_usr = new user_with_camping_reservation(sqlReader.GetInt64(0)
  101.                                                       //sqlReader.GetString(1)//,
  102.                                                       /*sqlReader.GetString(2),
  103.                                                       sqlReader.GetString(3),
  104.                                                       sqlReader.GetString(4),
  105.                                                       sqlReader.GetString(5)//,*/
  106.                                                       //sqlReader.GetDateTime(6),
  107.                                                       //sqlReader.GetDateTime(7)
  108.                                                       );
  109.                         if (onlyUniqueReservations(reserv_usr))
  110.                             this.reservation_list.Add(reserv_usr);
  111.                     }
  112.                 }
  113.                 databaseConnection.Close();
  114.             }
  115.             catch (Exception exception) { }
  116.         }
  117.  
  118.         public void runQuerry_updateUserList()
  119.         {
  120.             string input = $"select id, first_name, last_name , email, rfid from users";//email, password, created_at, updated_at from users;";
  121.             MySqlConnection databaseConnection = new MySqlConnection("Server=studmysql01.fhict.local;" +
  122. =======
  123.         {
  124.          //string input = $"select id, rfid from users";//first_name, last_name, email, rfid, password from users";//created_at, updated_at from users;";
  125.  
  126.          string input = "select user_id from orders where order_type = 'camping' AND order_status = 1;";
  127.  
  128.  
  129.          MySqlConnection databaseConnection = new MySqlConnection("Server=studmysql01.fhict.local;" +
  130. >>>>>>> 2090e70fcd7ca0c55c8bb955364c0aa999309ba8
  131.                                                                      "Uid=dbi428954;" +
  132.                                                                      "Database=dbi428954;" +
  133.                                                                      "Pwd=planetfun69planetfun;");
  134.             MySqlCommand comandDatabase = new MySqlCommand(input, databaseConnection);
  135.             comandDatabase.CommandTimeout = 60;
  136.             try
  137.             {
  138.                 databaseConnection.Open();
  139.                 MySqlDataReader sqlReader = comandDatabase.ExecuteReader();
  140.                 if (sqlReader.HasRows)
  141.                 {
  142.                     while (sqlReader.Read())
  143.                     {
  144. <<<<<<< HEAD
  145.                         users local_cust = new users(sqlReader.GetInt64(0),
  146.                                                       sqlReader.GetString(1),
  147.                                                       sqlReader.GetString(2),
  148.                                                       sqlReader.GetString(3),
  149.                                                       sqlReader.GetString(4)
  150.                                                       //,
  151.                                                       //sqlReader.GetString(4),
  152.                                                       //sqlReader.GetDateTime(5),
  153.                                                       //sqlReader.GetDateTime(6)*/
  154. =======
  155.                         user_with_camping_reservation reserv_usr = new user_with_camping_reservation(sqlReader.GetInt64(0)
  156.                                                       //sqlReader.GetString(1)//,
  157.                                                                             /*sqlReader.GetString(2),
  158.                                                                             sqlReader.GetString(3),
  159.                                                                             sqlReader.GetString(4),
  160.                                                                             sqlReader.GetString(5)//,*/
  161.                                                                             //sqlReader.GetDateTime(6),
  162.                                                                             //sqlReader.GetDateTime(7)
  163. >>>>>>> 2090e70fcd7ca0c55c8bb955364c0aa999309ba8
  164.                                                       );
  165.                         if (onlyUniqueReservations(reserv_usr))
  166.                             this.reservation_list.Add(reserv_usr);
  167.                     }
  168.                 }
  169.                 databaseConnection.Close();
  170.             }
  171.             catch (Exception exception) { }
  172.         }
  173.  
  174. <<<<<<< HEAD
  175.  
  176.         public bool onlyUniqueReservations(user_with_camping_reservation particular_user)
  177.         {
  178.             foreach (user_with_camping_reservation u in reservation_list)
  179.             {
  180.                 if (particular_user.reservation_user_id() == u.reservation_user_id())
  181.                     return false;
  182.             }
  183.             return true;
  184.         }
  185.  
  186.  
  187.         public void runQuerry_updateOrderList()
  188. =======
  189.          public void runQuerry_updateUserList()
  190.          {
  191.             string input = $"select id, first_name, last_name , email, rfid from users";//email, password, created_at, updated_at from users;";
  192.             MySqlConnection databaseConnection = new MySqlConnection("Server=studmysql01.fhict.local;" +
  193.                                                                      "Uid=dbi428954;" +
  194.                                                                      "Database=dbi428954;" +
  195.                                                                      "Pwd=planetfun69planetfun;");
  196.             MySqlCommand comandDatabase = new MySqlCommand(input, databaseConnection);
  197.             comandDatabase.CommandTimeout = 60;
  198.             try
  199.             {
  200.                databaseConnection.Open();
  201.                MySqlDataReader sqlReader = comandDatabase.ExecuteReader();
  202.                if (sqlReader.HasRows)
  203.                {
  204.                   while (sqlReader.Read())
  205.                   {
  206.                      if(!sqlReader.IsDBNull(0) && !sqlReader.IsDBNull(1) &&
  207.                         !sqlReader.IsDBNull(2) && !sqlReader.IsDBNull(3) &&
  208.                         !sqlReader.IsDBNull(4))
  209.                   {
  210.  
  211.                      users local_cust = new users(sqlReader.GetInt64(0),
  212.                                                    sqlReader.GetString(1),
  213.                                                    sqlReader.GetString(2),
  214.                                                    sqlReader.GetString(3),
  215.                                                    sqlReader.GetString(4)
  216.                                                    //,
  217.                                                                          //sqlReader.GetString(4),
  218.                                                                          //sqlReader.GetDateTime(5),
  219.                                                                          //sqlReader.GetDateTime(6)*/
  220.                                                    );
  221.                      if (userNotInList(local_cust))
  222.                         this.users_list.Add(local_cust);
  223.                   }
  224.                }
  225.                }
  226.                databaseConnection.Close();
  227.             }
  228.             catch (Exception exception) { }
  229.          }
  230.  
  231.  
  232.       public bool onlyUniqueReservations(user_with_camping_reservation particular_user)
  233.       {
  234.          foreach (user_with_camping_reservation u in reservation_list)
  235.          {
  236.             if (particular_user.reservation_user_id() == u.reservation_user_id())
  237.                return false;
  238.          }
  239.          return true;
  240.       }
  241.  
  242.  
  243.       public void runQuerry_updateOrderList()
  244. >>>>>>> 2090e70fcd7ca0c55c8bb955364c0aa999309ba8
  245.         {
  246.             string input = $"select * from orders;";
  247.             MySqlConnection databaseConnection = new MySqlConnection("Server=studmysql01.fhict.local;" +
  248.                                                                      "Uid=dbi428954;" +
  249.                                                                      "Database=dbi428954;" +
  250.                                                                      "Pwd=planetfun69planetfun;");
  251.             MySqlCommand comandDatabase = new MySqlCommand(input, databaseConnection);
  252.             comandDatabase.CommandTimeout = 60;
  253.             try
  254.             {
  255.                 databaseConnection.Open();
  256.                 MySqlDataReader sqlReader = comandDatabase.ExecuteReader();
  257.                 if (sqlReader.HasRows)
  258.                 {
  259.                     while (sqlReader.Read())
  260.                     {
  261.                         orders local_cust = new orders(sqlReader.GetInt64(0),
  262.                                                       sqlReader.GetInt64(1),
  263.                                                       sqlReader.GetDateTime(2),
  264.                                                       sqlReader.GetBoolean(3)
  265.                                                       );
  266.                         if (only_uniqueId(local_cust))
  267.                             this.orders_list.Add(local_cust);
  268.                     }
  269.                 }
  270.                 databaseConnection.Close();
  271.             }
  272.             catch (Exception exception) { }
  273.         }
  274.  
  275.         public void runQuerry_updateOrderItemList()
  276.         {
  277.             string input = $"select id, order_id, item_id, quantity from orders;";
  278.             MySqlConnection databaseConnection = new MySqlConnection("Server=studmysql01.fhict.local;" +
  279.                                                                      "Uid=dbi428954;" +
  280.                                                                      "Database=dbi428954;" +
  281.                                                                      "Pwd=planetfun69planetfun;");
  282.             MySqlCommand comandDatabase = new MySqlCommand(input, databaseConnection);
  283.             comandDatabase.CommandTimeout = 60;
  284.             try
  285.             {
  286.                 databaseConnection.Open();
  287.                 MySqlDataReader sqlReader = comandDatabase.ExecuteReader();
  288.                 if (sqlReader.HasRows)
  289.                 {
  290.                     while (sqlReader.Read())
  291.                     {
  292.                         order_items local_cust = new order_items(sqlReader.GetInt64(0),
  293.                                                                   sqlReader.GetInt64(1),
  294.                                                                   sqlReader.GetInt64(2),
  295.                                                                   sqlReader.GetInt64(3)
  296.                                                                 );
  297.                         if (item_to_a_basket(local_cust))
  298.                             this.order_items.Add(local_cust);
  299.                     }
  300.                 }
  301.                 databaseConnection.Close();
  302.             }
  303.             catch (Exception exception) { }
  304.         }
  305.  
  306.         public int userListSize()
  307.         {
  308.             return this.users_list.Count();
  309.         }
  310.  
  311.         public int orderListSize()
  312.         {
  313.             return this.orders_list.Count();
  314.         }
  315.  
  316.         public bool checkUser(string first_name, string last_name, string email)
  317.         {
  318.             foreach (users u in users_list)
  319.             {
  320.                 if ((u.userFirst_name() == first_name) &&
  321.                      (u.userLast_name() == last_name) &&
  322.                      (u.userEmail() == email))
  323.                     return true;
  324.             }
  325.             return false;
  326.         }
  327.  
  328.         public void assignRfidToUser(string first_name, string last_name, string email, string dummy_rfid)
  329.         {
  330.             string assigning_rfid;
  331.             assigning_rfid = $"UPDATE `users` SET `rfid` = '{dummy_rfid}' WHERE `users`.`first_name` = '{first_name}' AND `users`.`last_name` = '{last_name}' AND `users`.`email` = '{email}';";
  332.             runQuery(assigning_rfid);
  333.         }
  334.  
  335.         public void runQuery(string input)
  336.         {
  337.             MySqlConnection databaseConnection = new MySqlConnection("Server=studmysql01.fhict.local;" +
  338.                                                                      "Uid=dbi428954;" +
  339.                                                                      "Database=dbi428954;" +
  340.                                                                      "Pwd=planetfun69planetfun;");
  341.             MySqlCommand comandDatabase = new MySqlCommand(input, databaseConnection);
  342.             comandDatabase.CommandTimeout = 60;
  343.             try
  344.             {
  345.                 databaseConnection.Open();
  346.                 MySqlDataReader sqlReader = comandDatabase.ExecuteReader();
  347.             }
  348.             catch (Exception exception) { }
  349.             databaseConnection.Close();
  350.         }
  351.  
  352.         public Int64 getOrderIdForOrderItem(string input)
  353.         {
  354.             MySqlConnection databaseConnection = new MySqlConnection("Server=studmysql01.fhict.local;" +
  355.                                                                      "Uid=dbi428954;" +
  356.                                                                      "Database=dbi428954;" +
  357.                                                                      "Pwd=planetfun69planetfun;");
  358.             MySqlCommand comandDatabase = new MySqlCommand(input, databaseConnection);
  359.             comandDatabase.CommandTimeout = 60;
  360.             try
  361.             {
  362.                 databaseConnection.Open();
  363.                 MySqlDataReader sqlReader = comandDatabase.ExecuteReader();
  364.                 if (sqlReader.HasRows)
  365.                 {
  366.                     while (sqlReader.Read())
  367.                     {
  368.                         //users local_cust = new users(sqlReader.GetInt64(0),
  369.                         Int64 last_id;
  370.                         last_id = sqlReader.GetInt64(0);
  371.                         return last_id;
  372.                     }
  373.                 }
  374.                 databaseConnection.Close();
  375.             }
  376.             catch (Exception exception) { }
  377.             return 0;
  378.         }
  379.  
  380.         public Int64 checkRfid(string rfid_req)
  381.         {
  382.             foreach (users u in users_list)
  383.             {
  384.                 if (u.userRfid() == rfid_req)
  385.                 {
  386. <<<<<<< HEAD
  387.                     foreach (user_with_camping_reservation uwcr in reservation_list)
  388.                     {
  389.                         if (uwcr.reservation_user_id() == u.userID())
  390.                             return u.userID();
  391.                     }
  392.  
  393.                 }
  394.                 //return u.userID();
  395. =======
  396.                      foreach (user_with_camping_reservation uwcr in reservation_list)
  397.                      {
  398.                         if (uwcr.reservation_user_id() == u.userID())
  399.                            return u.userID();
  400.                      }
  401.  
  402.                 }
  403.                     //return u.userID();
  404. >>>>>>> 2090e70fcd7ca0c55c8bb955364c0aa999309ba8
  405.             }
  406.             return 0;
  407.         }
  408.  
  409.         public void addOrderItem(Int64 last_id, int item_type)
  410.         {
  411.             string add_order_record;
  412.             add_order_record = $"INSERT INTO `order_items` (`order_id`, `item_id`, `quantity`) VALUES ('{last_id}', '{item_type}', '1');";
  413.             runQuery(add_order_record);
  414.         }
  415.  
  416.         public void addTentToItems()
  417.         {
  418.             string s;
  419.             s = "INSERT INTO `items` (`id`, `name`, `list_price`) VALUES(NULL, 'Tent', '150.00');";
  420.             runQuery(s);
  421.         }
  422.  
  423.         public bool borrow(Int64 id, int item_id)
  424.         {
  425.             foreach (users u in users_list)
  426.             {
  427.                 if (u.userID() == id)
  428.                 {
  429.                     string borrow_tent_req;
  430.                     borrow_tent_req = $"INSERT INTO `orders` (`user_id`) VALUES ('{id}');";
  431.                     runQuery(borrow_tent_req);
  432.  
  433.                     // get orders into orders list
  434.                     runQuerry_updateOrderList();
  435.  
  436.                     // insert tent item
  437.                     string get_last_order;
  438.                     get_last_order = $"SELECT MAX(`id`) FROM `orders`;";
  439.                     Int64 last_id;
  440.                     last_id = getOrderIdForOrderItem(get_last_order);
  441.                     if (last_id != 0)
  442.                         addOrderItem(last_id, item_id);
  443.  
  444.                     // update orders items
  445.                     runQuerry_updateOrderItemList();
  446.  
  447.                     return true;
  448.                 }
  449.             }
  450.             return false;
  451.         }
  452.  
  453.     }
  454.  
  455.     class users : DataObj
  456.     {
  457.         private Int64 id;
  458.         private string first_name;
  459.         private string last_name;
  460.         private string email;
  461.         private string qr;
  462.         private string rfid;
  463.         private string password;
  464.         //private string remember_token;
  465.         private DateTime created_at;
  466.         private DateTime updated_at;
  467.  
  468.         public users(Int64 id)
  469.         {
  470.             this.id = id;
  471.         }
  472.  
  473.  
  474.         public users(Int64 id, string rfid)
  475.         {
  476.             this.id = id;
  477.             this.rfid = rfid;
  478.         }
  479.  
  480.         public users(string first_name, string last_name, string email)
  481.         {
  482.             this.first_name = first_name;
  483.             this.last_name = last_name;
  484.             this.email = email;
  485.         }
  486.  
  487. <<<<<<< HEAD
  488.         public users(Int64 id, string first_name, string last_name, string email, string rfid)
  489.         {
  490.             this.id = id;
  491.             this.first_name = first_name;
  492.             this.last_name = last_name;
  493.             this.email = email;
  494.             this.rfid = rfid;
  495.         }
  496. =======
  497.       public users(Int64 id, string first_name, string last_name, string email, string rfid)
  498.       {
  499.          this.id = id;
  500.          this.first_name = first_name;
  501.          this.last_name = last_name;
  502.          this.email = email;
  503.          this.rfid = rfid;
  504.       }
  505. >>>>>>> 2090e70fcd7ca0c55c8bb955364c0aa999309ba8
  506.  
  507.  
  508.       public users(Int64 id, string f_na, string l_na, string email, string rfid, string pass)//, DateTime creat, DateTime updat)
  509.         {
  510.             this.id = id;
  511.             this.first_name = f_na;
  512.             this.last_name = l_na;
  513.             this.email = email;
  514.             this.rfid = rfid;
  515.             this.password = pass;
  516.             //this.remember_token = tok;
  517.             //this.created_at = creat;
  518.             //this.updated_at = updat;
  519.         }
  520.  
  521.  
  522.         public users(Int64 id, string f_na, string l_na, string email, string rfid, string pass, DateTime creat, DateTime updat)
  523.         {
  524.             this.id = id;
  525.             this.first_name = f_na;
  526.             this.last_name = l_na;
  527.             this.email = email;
  528.             this.rfid = rfid;
  529.             this.password = pass;
  530.             //this.remember_token = tok;
  531.             this.created_at = creat;
  532.             this.updated_at = updat;
  533.         }
  534.  
  535.         public void insertUser()
  536.         {
  537.             string input = $"INSERT INTO `users` (`first_name`, `last_name`, `email`, `password`) VALUES ('{this.first_name}', '{this.last_name}', '{this.email}', 'at_entrance');";
  538.             runQuery(input);
  539.         }
  540.  
  541.         public void runQuery(string input)
  542.         {
  543.             MySqlConnection databaseConnection = new MySqlConnection("Server=studmysql01.fhict.local;" +
  544.                                                                      "Uid=dbi428954;" +
  545.                                                                      "Database=dbi428954;" +
  546.                                                                      "Pwd=planetfun69planetfun;");
  547.             MySqlCommand comandDatabase = new MySqlCommand(input, databaseConnection);
  548.             comandDatabase.CommandTimeout = 60;
  549.             try
  550.             {
  551.                 databaseConnection.Open();
  552.                 MySqlDataReader sqlReader = comandDatabase.ExecuteReader();
  553.             }
  554.             catch (Exception exception) { }
  555.             databaseConnection.Close();
  556.         }
  557.  
  558.         public Int64 userID()
  559.         {
  560.             return this.id;
  561.         }
  562.  
  563.         public string userFirst_name()
  564.         {
  565.             return this.first_name;
  566.         }
  567.  
  568.         public string userLast_name()
  569.         {
  570.             return this.last_name;
  571.         }
  572.  
  573.         public string userEmail()
  574.         {
  575.             return this.email;
  576.         }
  577.  
  578.         public string userRfid()
  579.         {
  580.             return this.rfid;
  581.         }
  582.  
  583.     }
  584.  
  585. <<<<<<< HEAD
  586.     class user_with_camping_reservation
  587.     {
  588.         private Int64 id;
  589.  
  590.         public user_with_camping_reservation(Int64 id)
  591.         {
  592.             this.id = id;
  593.         }
  594.  
  595.         public Int64 reservation_user_id()
  596.         {
  597.             return this.id;
  598.         }
  599.  
  600.     }
  601. =======
  602.    class user_with_camping_reservation
  603.    {
  604.       private Int64 id;
  605.  
  606.       public user_with_camping_reservation(Int64 id)
  607.       {
  608.          this.id = id;
  609.       }
  610.  
  611.       public Int64 reservation_user_id()
  612.       {
  613.          return this.id;
  614.       }
  615.  
  616.    }
  617. >>>>>>> 2090e70fcd7ca0c55c8bb955364c0aa999309ba8
  618.  
  619.     class orders
  620.     {
  621.         private Int64 id;
  622.         private Int64 user_id;
  623.         private DateTime order_date;
  624.         private bool order_status;
  625.  
  626.         public orders(Int64 user_id)
  627.         {
  628.             this.user_id = user_id;
  629.         }
  630.  
  631.         public orders(Int64 id, Int64 user_id, DateTime order_date, bool order_status)
  632.         {
  633.             this.id = id;
  634.             this.user_id = id;
  635.             this.order_date = order_date;
  636.             this.order_status = order_status;
  637.         }
  638.  
  639.         public Int64 orderID()
  640.         {
  641.             return this.id;
  642.         }
  643.     }
  644.  
  645.     class order_items
  646.     {
  647.         private Int64 id;
  648.         private Int64 order_id;
  649.         private Int64 item_id;
  650.         private Int64 quantity;
  651.  
  652.         public order_items(Int64 id, Int64 order_id, Int64 item_id, Int64 quantity)
  653.         {
  654.             this.id = id;
  655.             this.order_id = order_id;
  656.             this.item_id = item_id;
  657.             this.quantity = quantity;
  658.         }
  659.  
  660.         public Int64 order_itemID()
  661.         {
  662.             return this.id;
  663.         }
  664.  
  665.     }
  666. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement