Advertisement
Guest User

Untitled

a guest
Jan 19th, 2020
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 15.06 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.  
  29.       public DataObj()
  30.       {
  31.          users_list = new List<users>();
  32.          orders_list = new List<orders>();
  33.          order_items = new List<order_items>();
  34.       }
  35.  
  36.       public void createAndInsertUser(string name_f, string name_l, string email)
  37.       {
  38.          users user = new users(name_f, name_l, email);
  39.          user.insertUser();
  40.       }
  41.  
  42.       public bool userNotInList(users user)
  43.       {
  44.          foreach (users u in users_list)
  45.          {
  46.             if (user.userID() == u.userID())
  47.                return false;
  48.          }
  49.          return true;
  50.       }
  51.  
  52.       public bool only_uniqueId(orders order)
  53.       {
  54.          foreach (orders u in orders_list)
  55.          {
  56.             if (order.orderID() == u.orderID())
  57.                return false;
  58.          }
  59.          return true;
  60.       }
  61.  
  62.       public bool item_to_a_basket(order_items oi)
  63.       {
  64.          foreach (order_items o in order_items)
  65.          {
  66.             if (oi.order_itemID() == o.order_itemID())
  67.                return false;
  68.          }
  69.          return true;
  70.       }
  71.  
  72.       public void runQuerry_updateUserList()
  73.       {
  74.          string input = $"select id, rfid from users";//first_name, last_name, email, rfid, password from users";//created_at, updated_at from users;";
  75.          MySqlConnection databaseConnection = new MySqlConnection("Server=studmysql01.fhict.local;" +
  76.                                                                   "Uid=dbi428954;" +
  77.                                                                   "Database=dbi428954;" +
  78.                                                                   "Pwd=planetfun69planetfun;");
  79.          MySqlCommand comandDatabase = new MySqlCommand(input, databaseConnection);
  80.          comandDatabase.CommandTimeout = 60;
  81.          try
  82.          {
  83.             databaseConnection.Open();
  84.             MySqlDataReader sqlReader = comandDatabase.ExecuteReader();
  85.             if (sqlReader.HasRows)
  86.             {
  87.                while (sqlReader.Read())
  88.                {
  89.                   users local_cust = new users(sqlReader.GetInt64(0),
  90.                                                 sqlReader.GetString(1)//,
  91.                                                 /*sqlReader.GetString(2),
  92.                                                 sqlReader.GetString(3),
  93.                                                 sqlReader.GetString(4),
  94.                                                 sqlReader.GetString(5)//,*/
  95.                                                 //sqlReader.GetDateTime(6),
  96.                                                 //sqlReader.GetDateTime(7)
  97.                                                 );
  98.                   if (userNotInList(local_cust))
  99.                      this.users_list.Add(local_cust);
  100.                }
  101.             }
  102.             databaseConnection.Close();
  103.          }
  104.          catch (Exception exception) { }
  105.       }
  106.  
  107.       public void runQuerry_updateOrderList()
  108.       {
  109.          string input = $"select * from orders;";
  110.          MySqlConnection databaseConnection = new MySqlConnection("Server=studmysql01.fhict.local;" +
  111.                                                                   "Uid=dbi428954;" +
  112.                                                                   "Database=dbi428954;" +
  113.                                                                   "Pwd=planetfun69planetfun;");
  114.          MySqlCommand comandDatabase = new MySqlCommand(input, databaseConnection);
  115.          comandDatabase.CommandTimeout = 60;
  116.          try
  117.          {
  118.             databaseConnection.Open();
  119.             MySqlDataReader sqlReader = comandDatabase.ExecuteReader();
  120.             if (sqlReader.HasRows)
  121.             {
  122.                while (sqlReader.Read())
  123.                {
  124.                   orders local_cust = new orders(sqlReader.GetInt64(0),
  125.                                                 sqlReader.GetInt64(1),
  126.                                                 sqlReader.GetDateTime(2),
  127.                                                 sqlReader.GetBoolean(3)
  128.                                                 );
  129.                   if (only_uniqueId(local_cust))
  130.                      this.orders_list.Add(local_cust);
  131.                }
  132.             }
  133.             databaseConnection.Close();
  134.          }
  135.          catch (Exception exception) { }
  136.       }
  137.      
  138.       public void runQuerry_updateOrderItemList()
  139.       {
  140.          string input = $"select id, order_id, item_id, quantity from orders;";
  141.          MySqlConnection databaseConnection = new MySqlConnection("Server=studmysql01.fhict.local;" +
  142.                                                                   "Uid=dbi428954;" +
  143.                                                                   "Database=dbi428954;" +
  144.                                                                   "Pwd=planetfun69planetfun;");
  145.          MySqlCommand comandDatabase = new MySqlCommand(input, databaseConnection);
  146.          comandDatabase.CommandTimeout = 60;
  147.          try
  148.          {
  149.             databaseConnection.Open();
  150.             MySqlDataReader sqlReader = comandDatabase.ExecuteReader();
  151.             if (sqlReader.HasRows)
  152.             {
  153.                while (sqlReader.Read())
  154.                {
  155.                   order_items local_cust = new order_items( sqlReader.GetInt64(0),
  156.                                                             sqlReader.GetInt64(1),
  157.                                                             sqlReader.GetInt64(2),
  158.                                                             sqlReader.GetInt64(3)
  159.                                                           );
  160.                   if (item_to_a_basket(local_cust))
  161.                      this.order_items.Add(local_cust);
  162.                }
  163.             }
  164.             databaseConnection.Close();
  165.          }
  166.          catch (Exception exception) { }
  167.       }
  168.  
  169.       public int userListSize()
  170.       {
  171.          return this.users_list.Count();
  172.       }
  173.  
  174.       public int orderListSize()
  175.       {
  176.          return this.orders_list.Count();
  177.       }
  178.  
  179.       public bool checkUser(string first_name, string last_name, string email)
  180.       {
  181.          foreach (users u in users_list)
  182.          {
  183.             if ((u.userFirst_name() == first_name) &&
  184.                  (u.userLast_name() == last_name) &&
  185.                  (u.userEmail() == email))
  186.                return true;
  187.          }
  188.          return false;
  189.       }
  190.  
  191.       public void assignRfidToUser(string first_name, string last_name, string email, string dummy_rfid)
  192.       {
  193.          string assigning_rfid;
  194.          assigning_rfid = $"UPDATE `users` SET `rfid` = '{dummy_rfid}' WHERE `users`.`first_name` = '{first_name}' AND `users`.`last_name` = '{last_name}' AND `users`.`email` = '{email}';";
  195.          runQuery(assigning_rfid);
  196.       }
  197.  
  198.       public void runQuery(string input)
  199.       {
  200.          MySqlConnection databaseConnection = new MySqlConnection("Server=studmysql01.fhict.local;" +
  201.                                                                   "Uid=dbi428954;" +
  202.                                                                   "Database=dbi428954;" +
  203.                                                                   "Pwd=planetfun69planetfun;");
  204.          MySqlCommand comandDatabase = new MySqlCommand(input, databaseConnection);
  205.          comandDatabase.CommandTimeout = 60;
  206.          try
  207.          {
  208.             databaseConnection.Open();
  209.             MySqlDataReader sqlReader = comandDatabase.ExecuteReader();
  210.          }
  211.          catch (Exception exception) { }
  212.          databaseConnection.Close();
  213.       }
  214.  
  215.       public Int64 getOrderIdForOrderItem(string input)
  216.       {
  217.          MySqlConnection databaseConnection = new MySqlConnection("Server=studmysql01.fhict.local;" +
  218.                                                                   "Uid=dbi428954;" +
  219.                                                                   "Database=dbi428954;" +
  220.                                                                   "Pwd=planetfun69planetfun;");
  221.          MySqlCommand comandDatabase = new MySqlCommand(input, databaseConnection);
  222.          comandDatabase.CommandTimeout = 60;
  223.          try
  224.          {
  225.             databaseConnection.Open();
  226.             MySqlDataReader sqlReader = comandDatabase.ExecuteReader();
  227.             if (sqlReader.HasRows)
  228.             {
  229.                while (sqlReader.Read())
  230.                {
  231.                   //users local_cust = new users(sqlReader.GetInt64(0),
  232.                   Int64 last_id;
  233.                   last_id = sqlReader.GetInt64(0);
  234.                   return last_id;
  235.                }
  236.             }
  237.             databaseConnection.Close();
  238.          }
  239.          catch (Exception exception) { }
  240.          return 0;
  241.       }
  242.  
  243.       public Int64 checkRfid(string rfid_req)
  244.       {
  245.          foreach (users u in users_list)
  246.          {
  247.             if (u.userRfid() == rfid_req)
  248.                return u.userID();
  249.          }
  250.          return 0;
  251.       }
  252.  
  253.       public void addOrderItem(Int64 last_id, int item_type)
  254.       {
  255.          string add_order_record;
  256.          add_order_record = $"INSERT INTO `order_items` (`order_id`, `item_id`, `quantity`) VALUES ('{last_id}', '{item_type}', '1');";
  257.          runQuery(add_order_record);
  258.       }
  259.  
  260.       public void addTentToItems()
  261.       {
  262.          string s;
  263.          s = "INSERT INTO `items` (`id`, `name`, `list_price`) VALUES(NULL, 'Tent', '150.00');";
  264.          runQuery(s);
  265.       }
  266.  
  267.       public bool borrow(Int64 id, int item_id)
  268.       {
  269.          foreach (users u in users_list)
  270.          {
  271.             if (u.userID() == id)
  272.             {
  273.                string borrow_tent_req;
  274.                borrow_tent_req = $"INSERT INTO `orders` (`user_id`) VALUES ('{id}');";
  275.                runQuery(borrow_tent_req);
  276.  
  277.                // get orders into orders list
  278.                runQuerry_updateOrderList();
  279.  
  280.                // insert tent item
  281.                string get_last_order;
  282.                get_last_order = $"SELECT MAX(`id`) FROM `orders`;";
  283.                Int64 last_id;
  284.                last_id = getOrderIdForOrderItem(get_last_order);
  285.                if (last_id != 0)
  286.                   addOrderItem(last_id, item_id);
  287.  
  288.                // update orders items
  289.                runQuerry_updateOrderItemList();
  290.  
  291.                return true;
  292.             }  
  293.          }
  294.          return false;
  295.       }
  296.  
  297.    }
  298.  
  299.    class users : DataObj
  300.    {
  301.       private Int64 id;
  302.       private string first_name;
  303.       private string last_name;
  304.       private string email;
  305.       private string qr;
  306.       private string rfid;
  307.       private string password;
  308.       //private string remember_token;
  309.       private DateTime created_at;
  310.       private DateTime updated_at;
  311.  
  312.       public users(Int64 id)
  313.       {
  314.          this.id = id;
  315.       }
  316.  
  317.  
  318.       public users(Int64 id, string rfid)
  319.       {
  320.          this.id = id;
  321.          this.rfid = rfid;
  322.       }
  323.  
  324.       public users(string first_name, string last_name, string email)
  325.       {
  326.          this.first_name = first_name;
  327.          this.last_name = last_name;
  328.          this.email = email;
  329.       }
  330.  
  331.  
  332.       public users(Int64 id, string f_na, string l_na, string email, string rfid, string pass)//, DateTime creat, DateTime updat)
  333.       {
  334.          this.id = id;
  335.          this.first_name = f_na;
  336.          this.last_name = l_na;
  337.          this.email = email;
  338.          this.rfid = rfid;
  339.          this.password = pass;
  340.          //this.remember_token = tok;
  341.          //this.created_at = creat;
  342.          //this.updated_at = updat;
  343.       }
  344.  
  345.  
  346.       public users(Int64 id, string f_na, string l_na, string email, string rfid, string pass, DateTime creat, DateTime updat)
  347.       {
  348.          this.id = id;
  349.          this.first_name = f_na;
  350.          this.last_name = l_na;
  351.          this.email = email;
  352.          this.rfid = rfid;
  353.          this.password = pass;
  354.          //this.remember_token = tok;
  355.          this.created_at = creat;
  356.          this.updated_at = updat;
  357.       }
  358.  
  359.       public void insertUser()
  360.       {
  361.          string input = $"INSERT INTO `users` (`first_name`, `last_name`, `email`, `password`) VALUES ('{this.first_name}', '{this.last_name}', '{this.email}', 'at_entrance');";
  362.          runQuery(input);
  363.       }
  364.  
  365.       public void runQuery(string input)
  366.       {
  367.          MySqlConnection databaseConnection = new MySqlConnection("Server=studmysql01.fhict.local;" +
  368.                                                                   "Uid=dbi428954;" +
  369.                                                                   "Database=dbi428954;" +
  370.                                                                   "Pwd=planetfun69planetfun;");
  371.          MySqlCommand comandDatabase = new MySqlCommand(input, databaseConnection);
  372.          comandDatabase.CommandTimeout = 60;
  373.          try
  374.          {
  375.             databaseConnection.Open();
  376.             MySqlDataReader sqlReader = comandDatabase.ExecuteReader();
  377.          }
  378.          catch (Exception exception) { }
  379.          databaseConnection.Close();
  380.       }
  381.  
  382.       public Int64 userID()
  383.       {
  384.          return this.id;
  385.       }
  386.  
  387.       public string userFirst_name()
  388.       {
  389.          return this.first_name;
  390.       }
  391.  
  392.       public string userLast_name()
  393.       {
  394.          return this.last_name;
  395.       }
  396.  
  397.       public string userEmail()
  398.       {
  399.          return this.email;
  400.       }
  401.  
  402.       public string userRfid()
  403.       {
  404.          return this.rfid;
  405.       }
  406.  
  407.    }
  408.  
  409.    class orders
  410.    {
  411.       private Int64 id;
  412.       private Int64 user_id;
  413.       private DateTime order_date;
  414.       private bool order_status;
  415.  
  416.       public orders(Int64 user_id)
  417.       {
  418.          this.user_id = user_id;
  419.       }
  420.  
  421.       public orders(Int64 id, Int64 user_id, DateTime order_date, bool order_status)
  422.       {
  423.          this.id = id;
  424.          this.user_id = id;
  425.          this.order_date = order_date;
  426.          this.order_status = order_status;
  427.       }
  428.  
  429.       public Int64 orderID()
  430.       {
  431.          return this.id;
  432.       }
  433.    }
  434.  
  435.    class order_items
  436.    {
  437.       private Int64 id;
  438.       private Int64 order_id;
  439.       private Int64 item_id;
  440.       private Int64 quantity;
  441.  
  442.       public order_items(Int64 id, Int64 order_id, Int64 item_id, Int64 quantity)
  443.       {
  444.          this.id = id;
  445.          this.order_id = order_id;
  446.          this.item_id = item_id;
  447.          this.quantity = quantity;
  448.       }
  449.  
  450.       public Int64 order_itemID()
  451.       {
  452.          return this.id;
  453.       }
  454.  
  455.    }
  456. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement