Advertisement
erkayes935

main.go

Dec 5th, 2024 (edited)
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 39.84 KB | Source Code | 0 0
  1. package main
  2.  
  3. import (
  4.     "bufio"
  5.     "challenge-godb/customer"
  6.     "challenge-godb/order"
  7.     "challenge-godb/order_detail"
  8.     "challenge-godb/service"
  9.     "database/sql"
  10.     "fmt"
  11.     "os"
  12.     "strconv"
  13.     "time"
  14.  
  15.     _ "github.com/lib/pq"
  16.     "github.com/olekukonko/tablewriter"
  17. )
  18.  
  19. // simplified function main
  20.  
  21. func main() {
  22.  
  23.     main_app()
  24. }
  25.  
  26. //setup_database
  27.  
  28. const (
  29.     host     = "localhost"
  30.     port     = 5432
  31.     user     = "postgres"
  32.     password = "root"
  33.     dbname   = "enigma_laundry"
  34. )
  35.  
  36. var psqlInfo = fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", host, port, user, password, dbname)
  37.  
  38. func connectDb() *sql.DB {
  39.     db, err := sql.Open("postgres", psqlInfo)
  40.     if err != nil {
  41.         panic(err)
  42.     }
  43.     err = db.Ping()
  44.     if err != nil {
  45.         panic(err)
  46.     } else {
  47.         fmt.Println("OK")
  48.     }
  49.     return db
  50. }
  51.  
  52. //detail dari function main diatas
  53.  
  54. func main_app() {
  55.     fmt.Println("|---------------------------------|")
  56.     fmt.Println("|  ~ Welcome to Enigma Laundry ~  |")
  57.     fmt.Println("|---------------------------------|")
  58.     fmt.Println("|\t   Choices Menu :\t  |")
  59.     fmt.Println("|1. Customer\t\t\t  |")
  60.     fmt.Println("|2. Service\t\t\t  |")
  61.     fmt.Println("|3. Order\t\t\t  |")
  62.     fmt.Println("|---------------------------------|")
  63.     fmt.Print("==> Input your Choice : ")
  64.     scanner := bufio.NewScanner(os.Stdin)
  65.     scanner.Scan()
  66.     confirm, _ := strconv.Atoi(scanner.Text())
  67.     if confirm == 1 {
  68.         //simplified dari menu customer untuk dibuat function terpisah
  69.         customer_menu()
  70.     } else if confirm == 2 {
  71.         //simplified dari menu service untuk dibuat function terpisah
  72.         service_menu()
  73.     } else if confirm == 3 {
  74.         //simplified dari menu order untuk dibuat function terpisah
  75.         order_menu()
  76.     } else {
  77.         fmt.Println("unrecognized input")
  78.         os.Exit(0)
  79.     }
  80. }
  81.  
  82. //detail dari function customer_menu
  83.  
  84. func customer_menu() error {
  85.     fmt.Println("|---------------------------------|")
  86.     fmt.Println("|   ~ Customer Dashboard Menu ~   |")
  87.     fmt.Println("|---------------------------------|")
  88.     fmt.Println("|\t   Choices :\t\t  |")
  89.     fmt.Println("|1. Create New Customer\t\t  |")
  90.     fmt.Println("|2. View All Customers\t\t  |")
  91.     fmt.Println("|3. View Selected Customer\t  |")
  92.     fmt.Println("|4. Update Customer\t\t  |")
  93.     fmt.Println("|5. Delete Customer\t\t  |")
  94.     fmt.Println("|6. Back to Main Menu\t\t  |")
  95.     fmt.Println("|---------------------------------|")
  96.     fmt.Print("==> Input your Choice : ")
  97.     scanner := bufio.NewScanner(os.Stdin)
  98.     scanner.Scan()
  99.     confirm, _ := strconv.Atoi(scanner.Text())
  100.     if confirm == 1 {
  101.         //simplified dari proses menambah customer untuk dibuat function terpisah
  102.         add_customer()
  103.     } else if confirm == 2 {
  104.         //simplified dari proses melihat customer untuk dibuat function terpisah
  105.         view_customer()
  106.     } else if confirm == 3 {
  107.         //simplified dari proses melihat customer tertentu untuk dibuat function terpisah
  108.         view_selectedCustomer()
  109.     } else if confirm == 4 {
  110.         //simplified dari proses mengubah customer untuk dibuat function terpisah
  111.         edit_customer()
  112.     } else if confirm == 5 {
  113.         //simplified dari proses menghapus customer untuk dibuat function terpisah
  114.         delete_customer(order.Order{})
  115.     } else if confirm == 6 {
  116.         main_app()
  117.     } else {
  118.         fmt.Println("unrecognized input")
  119.         os.Exit(0)
  120.     }
  121.     return nil
  122. }
  123.  
  124. //detail dari function service_menu
  125.  
  126. func service_menu() {
  127.     fmt.Println("|---------------------------------|")
  128.     fmt.Println("|    ~ Service Dashboard Menu ~   |")
  129.     fmt.Println("|---------------------------------|")
  130.     fmt.Println("|\t   Choices :\t\t  |")
  131.     fmt.Println("|1. Create New Service\t\t  |")
  132.     fmt.Println("|2. View All Services\t\t  |")
  133.     fmt.Println("|3. View Selected Service\t  |")
  134.     fmt.Println("|4. Update Service\t\t  |")
  135.     fmt.Println("|5. Delete Service\t\t  |")
  136.     fmt.Println("|6. Back to Main Menu\t\t  |")
  137.     fmt.Println("|---------------------------------|")
  138.     fmt.Print("==> Input your Choice : ")
  139.     scanner := bufio.NewScanner(os.Stdin)
  140.     scanner.Scan()
  141.     confirm, _ := strconv.Atoi(scanner.Text())
  142.     if confirm == 1 {
  143.         //simplified dari proses menambah service untuk dibuat function terpisah
  144.         add_service()
  145.     } else if confirm == 2 {
  146.         //simplified dari proses melihat service untuk dibuat function terpisah
  147.         view_service()
  148.     } else if confirm == 3 {
  149.         //simplified dari proses melihat service untuk dibuat function terpisah
  150.         view_selectedService()
  151.     } else if confirm == 4 {
  152.         //simplified dari proses mengubah service untuk dibuat function terpisah
  153.         edit_service()
  154.     } else if confirm == 5 {
  155.         //simplified dari proses menghapus service untuk dibuat function terpisah
  156.         delete_service(order.Order{}, order_detail.Order_detail{})
  157.     } else if confirm == 6 {
  158.         main_app()
  159.     } else {
  160.         fmt.Println("unrecognized input")
  161.         os.Exit(0)
  162.     }
  163. }
  164.  
  165. //detail dari function order_menu
  166.  
  167. func order_menu() {
  168.     fmt.Println("|---------------------------------|")
  169.     fmt.Println("|     ~ Order Dashboard Menu ~    |")
  170.     fmt.Println("|---------------------------------|")
  171.     fmt.Println("|\t   Choices :\t\t  |")
  172.     fmt.Println("|1. Create New Order\t\t  |")
  173.     fmt.Println("|2. View All Orders\t\t  |")
  174.     fmt.Println("|3. View Selected Order\t\t  |")
  175.     fmt.Println("|4. Complete Order\t\t  |")
  176.     fmt.Println("|5. Back to Main Menu\t\t  |")
  177.     fmt.Println("|---------------------------------|")
  178.     fmt.Print("==> Input your Choice : ")
  179.     scanner := bufio.NewScanner(os.Stdin)
  180.     scanner.Scan()
  181.     confirm, _ := strconv.Atoi(scanner.Text())
  182.     if confirm == 1 {
  183.         //simplified dari proses menambah order untuk dibuat function terpisah
  184.         add_order(customer.Customer{}, service.Service{})
  185.     } else if confirm == 2 {
  186.         //simplified dari proses melihat order untuk dibuat function terpisah
  187.         view_order()
  188.     } else if confirm == 3 {
  189.         //simplified dari proses melihat order tertentu untuk dibuat function terpisah
  190.         view_selectedOrder()
  191.     } else if confirm == 4 {
  192.         //simplified dari proses menyelesaikan order untuk dibuat function terpisah
  193.         complete_order(order.Order{})
  194.     } else if confirm == 5 {
  195.         main_app()
  196.     } else {
  197.         fmt.Println("unrecognized input")
  198.         os.Exit(0)
  199.     }
  200. }
  201.  
  202. //detail proses menambah customer
  203.  
  204. func add_customer() error {
  205.     db := connectDb()
  206.     scanner := bufio.NewScanner(os.Stdin)
  207.     new_Customer := customer.Customer{}
  208.     fmt.Print("Input Customer id : ")
  209.     scanner.Scan()
  210.     new_Customer.Customer_id, _ = strconv.Atoi(scanner.Text())
  211.     id_customerExist, err := check_IDCustomer(new_Customer.Customer_id)
  212.     if err != nil {
  213.         return err
  214.     }
  215.     if id_customerExist {
  216.         fmt.Printf("customer id %d already exist\n", new_Customer.Customer_id)
  217.         fmt.Print("Try again?? (y/n) -> ")
  218.         scanner := bufio.NewScanner(os.Stdin)
  219.         scanner.Scan()
  220.         answer := scanner.Text()
  221.         if answer == "y" {
  222.             add_customer()
  223.         } else if answer == "n" {
  224.             customer_menu()
  225.         } else {
  226.             fmt.Println("unrecognized input")
  227.             os.Exit(0)
  228.         }
  229.     }
  230.     if new_Customer.Customer_id == 0 {
  231.         fmt.Println("customer id is invalid")
  232.         fmt.Print("Try again?? (y/n) -> ")
  233.         scanner.Scan()
  234.         answer := scanner.Text()
  235.         if answer == "y" {
  236.             add_customer()
  237.         } else if answer == "n" {
  238.             customer_menu()
  239.         } else {
  240.             fmt.Println("unrecognized input")
  241.             os.Exit(0)
  242.         }
  243.     }
  244.     fmt.Print("Input Customer name : ")
  245.     scanner.Scan()
  246.     new_Customer.Name = scanner.Text()
  247.     fmt.Print("Input Customer phone : ")
  248.     scanner.Scan()
  249.     new_Customer.Phone = scanner.Text()
  250.     fmt.Print("Input Customer address : ")
  251.     scanner.Scan()
  252.     new_Customer.Address = scanner.Text()
  253.     fmt.Print("Are you sure to add this customer?? (y/n) -> ")
  254.     scanner.Scan()
  255.     confirm := scanner.Text()
  256.     if confirm == "y" {
  257.         new_Customer = customer.Customer{
  258.             Customer_id: new_Customer.Customer_id,
  259.             Name:        new_Customer.Name,
  260.             Phone:       new_Customer.Phone,
  261.             Address:     new_Customer.Address,
  262.         }
  263.         if _, err := db.Exec("insert into customer (customer_id,name,phone,address) values ($1,$2,$3,$4)", new_Customer.Customer_id, new_Customer.Name, new_Customer.Phone, new_Customer.Address); err != nil {
  264.         } else {
  265.             fmt.Println("New Customer has been added to database successfully")
  266.         }
  267.     } else {
  268.         fmt.Println("failed to add new customer")
  269.     }
  270.     fmt.Print("Input new customer again?? (y/n) -> ")
  271.     scanner.Scan()
  272.     answer := scanner.Text()
  273.     if answer == "y" {
  274.         add_customer()
  275.     } else if answer == "n" {
  276.         customer_menu()
  277.     } else {
  278.         fmt.Println("unrecognized input")
  279.         os.Exit(0)
  280.     }
  281.     return nil
  282. }
  283.  
  284. //cek customer by id
  285.  
  286. func check_IDCustomer(id int) (bool, error) {
  287.     db := connectDb()
  288.     rows_Customer, err := db.Query("select customer_id from customer where customer_id = $1", id)
  289.     if err != nil {
  290.         return false, err
  291.     }
  292.     defer rows_Customer.Close()
  293.     if rows_Customer.Next() {
  294.         return true, nil
  295.     }
  296.     return false, nil
  297. }
  298.  
  299. //cek service by id
  300.  
  301. func check_IDService(id int) (bool, error) {
  302.     db := connectDb()
  303.     rows_Service, err := db.Query("select service_id from service where service_id = $1", id)
  304.     if err != nil {
  305.         return false, err
  306.     }
  307.     defer rows_Service.Close()
  308.     if rows_Service.Next() {
  309.         return true, nil
  310.     }
  311.     return false, nil
  312. }
  313.  
  314. func check_IDOrder(id int) (bool, error) {
  315.     db := connectDb()
  316.     rows_Order, err := db.Query("select order_id from public.order where order_id = $1", id)
  317.     if err != nil {
  318.         return false, err
  319.     }
  320.     defer rows_Order.Close()
  321.     if rows_Order.Next() {
  322.         return true, nil
  323.     }
  324.     return false, nil
  325. }
  326.  
  327. func check_IDDetailOrder(id int) (bool, error) {
  328.     db := connectDb()
  329.     rows_DetailOrder, err := db.Query("select order_detail_id from public.order where order_detail_id = $1", id)
  330.     if err != nil {
  331.         return false, err
  332.     }
  333.     defer rows_DetailOrder.Close()
  334.     if rows_DetailOrder.Next() {
  335.         return true, nil
  336.     }
  337.     return false, nil
  338. }
  339.  
  340. //melihat semua data customer yang ada di db
  341.  
  342. func view_customer() {
  343.     db := connectDb()
  344.     scanner := bufio.NewScanner(os.Stdin)
  345.     rows_viewCustomer, err := db.Query("select * from customer order by customer_id asc")
  346.     if err != nil {
  347.         fmt.Println(err)
  348.     } else {
  349.         headers := []string{"Customer ID", "Customer Name", "Phone Number", "Customer Address", "Time Created", "Time Updated"}
  350.         scanDest := []interface{}{new(int), new(string), new(string), new(string), new(string), new(string)}
  351.         tableWriter := tablewriter.NewWriter(os.Stdout)
  352.         tableWriter.SetHeader(headers)
  353.         for rows_viewCustomer.Next() {
  354.             err := rows_viewCustomer.Scan(scanDest...)
  355.             if err != nil {
  356.                 panic(err)
  357.             }
  358.             row_Values := make([]string, len(scanDest))
  359.             for i, dest := range scanDest {
  360.                 switch v := dest.(type) {
  361.                 case *int:
  362.                     row_Values[i] = strconv.Itoa(*v)
  363.                 case *string:
  364.                     row_Values[i] = *v
  365.                 default:
  366.                     row_Values[i] = fmt.Sprintf("%v", v)
  367.                 }
  368.             }
  369.             tableWriter.Append(row_Values)
  370.         }
  371.         tableWriter.Render()
  372.     }
  373.     fmt.Print("Back to previous menu?? (y/n) -> ")
  374.     scanner.Scan()
  375.     answer := scanner.Text()
  376.     if answer == "y" {
  377.         customer_menu()
  378.     } else if answer == "n" {
  379.         main_app()
  380.     } else {
  381.         fmt.Println("unrecognized input")
  382.         os.Exit(0)
  383.     }
  384. }
  385.  
  386. //melihat data customer tertentu dari db
  387.  
  388. func view_selectedCustomer() error {
  389.     db := connectDb()
  390.     scanner := bufio.NewScanner(os.Stdin)
  391.     fmt.Print("Input Customer id to view : ")
  392.     scanner.Scan()
  393.     id, err := strconv.Atoi(scanner.Text())
  394.     if err != nil {
  395.         fmt.Println("customer id is invalid")
  396.         fmt.Print("Try again?? (y/n) -> ")
  397.         scanner.Scan()
  398.         answer := scanner.Text()
  399.         if answer == "y" {
  400.             view_selectedCustomer()
  401.         } else if answer == "n" {
  402.             customer_menu()
  403.         } else {
  404.             fmt.Println("unrecognized input")
  405.             os.Exit(0)
  406.         }
  407.     }
  408.     id_viewCustomer, err := check_IDCustomer(id)
  409.     if err != nil {
  410.         return err
  411.     }
  412.     if !id_viewCustomer {
  413.         fmt.Printf("customer id %d is not found\n", id)
  414.         fmt.Print("Try again?? (y/n) -> ")
  415.         scanner.Scan()
  416.         answer := scanner.Text()
  417.         if answer == "y" {
  418.             view_selectedCustomer()
  419.         } else if answer == "n" {
  420.             customer_menu()
  421.         } else {
  422.             fmt.Println("unrecognized input")
  423.             os.Exit(0)
  424.         }
  425.     }
  426.     rows_Customer, err := db.Query("select * from customer where customer_id = $1", id)
  427.     if err != nil {
  428.         panic(err)
  429.     } else {
  430.         headers := []string{"Customer ID", "Customer Name", "Phone Number", "Customer Address", "Time Created", "Time Updated"}
  431.         scanDest := []interface{}{new(int), new(string), new(string), new(string), new(string), new(string)}
  432.         tableWriter := tablewriter.NewWriter(os.Stdout)
  433.         tableWriter.SetHeader(headers)
  434.         for rows_Customer.Next() {
  435.             err := rows_Customer.Scan(scanDest...)
  436.             if err != nil {
  437.                 panic(err)
  438.             }
  439.             row_Values := make([]string, len(scanDest))
  440.             for i, dest := range scanDest {
  441.                 switch v := dest.(type) {
  442.                 case *int:
  443.                     row_Values[i] = strconv.Itoa(*v)
  444.                 case *string:
  445.                     row_Values[i] = *v
  446.                 default:
  447.                     row_Values[i] = fmt.Sprintf("%v", v)
  448.                 }
  449.             }
  450.             tableWriter.Append(row_Values)
  451.         }
  452.         tableWriter.Render()
  453.     }
  454.     fmt.Print("Back to previous menu?? (y/n) -> ")
  455.     scanner.Scan()
  456.     answer := scanner.Text()
  457.     if answer == "y" {
  458.         customer_menu()
  459.     } else if answer == "n" {
  460.         main_app()
  461.     } else {
  462.         fmt.Println("unrecognized input")
  463.         os.Exit(0)
  464.     }
  465.     return nil
  466. }
  467.  
  468. //detail proses mengedit customer
  469.  
  470. func edit_customer() error {
  471.     db := connectDb()
  472.     scanner := bufio.NewScanner(os.Stdin)
  473.     fmt.Print("Input Customer id to update : ")
  474.     scanner.Scan()
  475.     id, err := strconv.Atoi(scanner.Text())
  476.     if err != nil {
  477.         fmt.Println("customer id is invalid")
  478.         fmt.Print("Try again?? (y/n) -> ")
  479.         scanner.Scan()
  480.         answer := scanner.Text()
  481.         if answer == "y" {
  482.             edit_customer()
  483.         } else if answer == "n" {
  484.             customer_menu()
  485.         } else {
  486.             fmt.Println("unrecognized input")
  487.             os.Exit(0)
  488.         }
  489.     }
  490.     id_Found, err := check_IDCustomer(id)
  491.     if err != nil {
  492.         return err
  493.     }
  494.     if !id_Found {
  495.         fmt.Printf("customer id %d is not found\n", id)
  496.         fmt.Print("Try again?? (y/n) -> ")
  497.         scanner.Scan()
  498.         answer := scanner.Text()
  499.         if answer == "y" {
  500.             edit_customer()
  501.         } else if answer == "n" {
  502.             customer_menu()
  503.         } else {
  504.             fmt.Println("unrecognized input")
  505.             os.Exit(0)
  506.         }
  507.     }
  508.     update_Customer := customer.Customer{
  509.         Customer_id: id,
  510.     }
  511.     fmt.Print("Input New Customer name : ")
  512.     scanner.Scan()
  513.     update_Customer.Name = scanner.Text()
  514.     fmt.Print("Input New Customer phone : ")
  515.     scanner.Scan()
  516.     update_Customer.Phone = scanner.Text()
  517.     fmt.Print("Input New Customer address : ")
  518.     scanner.Scan()
  519.     update_Customer.Address = scanner.Text()
  520.     fmt.Print("Are you sure to update this customer?? (y/n) -> ")
  521.     scanner.Scan()
  522.     confirm := scanner.Text()
  523.     if confirm == "y" {
  524.         new_Name := update_Customer.Name != ""
  525.         new_Phone := update_Customer.Phone != ""
  526.         new_Address := update_Customer.Address != ""
  527.         process_Update := new_Name || new_Phone || new_Address
  528.         if !process_Update {
  529.             return nil
  530.         }
  531.         i := 2
  532.         q := "update customer set "
  533.         if new_Name {
  534.             q += fmt.Sprintf("name = $%d", i)
  535.             i++
  536.         }
  537.         if new_Phone {
  538.             if i > 2 {
  539.                 q += fmt.Sprintf(",phone = $%d", i)
  540.             } else {
  541.                 q += fmt.Sprintf("phone = $%d", i)
  542.             }
  543.             i++
  544.         }
  545.         if new_Address {
  546.             if i > 2 {
  547.                 q += fmt.Sprintf(",address = $%d", i)
  548.             } else {
  549.                 q += fmt.Sprintf("address = $%d", i)
  550.             }
  551.             i++
  552.         }
  553.         q += " ,updated_at = now() where customer_id = $1"
  554.         arr := []any{}
  555.         arr = append(arr, update_Customer.Customer_id)
  556.         if new_Name {
  557.             arr = append(arr, update_Customer.Name)
  558.         }
  559.         if new_Phone {
  560.             arr = append(arr, update_Customer.Phone)
  561.         }
  562.         if new_Address {
  563.             arr = append(arr, update_Customer.Address)
  564.         }
  565.         if _, err := db.Exec(q, arr...); err != nil {
  566.             return err
  567.         } else {
  568.             fmt.Println("Current Customer has been updated to database successfully")
  569.             fmt.Print("Edit current customer again?? (y/n) -> ")
  570.             scanner.Scan()
  571.             answer := scanner.Text()
  572.             if answer == "y" {
  573.                 edit_customer()
  574.             } else if answer == "n" {
  575.                 customer_menu()
  576.             } else {
  577.                 fmt.Println("unrecognized input")
  578.                 os.Exit(0)
  579.             }
  580.         }
  581.     } else {
  582.         fmt.Println("failed to update customer")
  583.     }
  584.     return nil
  585. }
  586.  
  587. //detail proses menghapus customer
  588.  
  589. func delete_customer(new_order order.Order) error {
  590.     db := connectDb()
  591.     scanner := bufio.NewScanner(os.Stdin)
  592.     fmt.Print("Input Customer id to delete : ")
  593.     scanner.Scan()
  594.     id, err := strconv.Atoi(scanner.Text())
  595.     if err != nil {
  596.         fmt.Println("Customer id is invalid")
  597.         fmt.Print("Try again?? (y/n) -> ")
  598.         scanner.Scan()
  599.         answer := scanner.Text()
  600.         if answer == "y" {
  601.             delete_customer(new_order)
  602.         } else if answer == "n" {
  603.             customer_menu()
  604.         } else {
  605.             fmt.Println("unrecognized input")
  606.             os.Exit(0)
  607.         }
  608.     }
  609.     id_Found, err := check_IDCustomer(id)
  610.     if err != nil {
  611.         return err
  612.     }
  613.     if !id_Found {
  614.         fmt.Printf("customer id %d is not found\n", id)
  615.         fmt.Print("Try again?? (y/n) -> ")
  616.         scanner.Scan()
  617.         answer := scanner.Text()
  618.         if answer == "y" {
  619.             delete_customer(new_order)
  620.         } else if answer == "n" {
  621.             customer_menu()
  622.         } else {
  623.             fmt.Println("unrecognized input")
  624.             os.Exit(0)
  625.         }
  626.     }
  627.     fmt.Print("Are you sure to delete this customer?? (y/n) -> ")
  628.     scanner.Scan()
  629.     confirm := scanner.Text()
  630.     if confirm == "y" {
  631.         check_IDOrder(new_order.Order_id)
  632.         if new_order.Completion_date.String() != "0001-01-01 00:00:00 +0000 UTC" {
  633.             if _, err := db.Exec("delete from customer where customer_id = $1", id); err != nil {
  634.                 return err
  635.             } else {
  636.                 fmt.Println("Current Customer has been deleted from database successfully")
  637.             }
  638.         } else {
  639.             fmt.Printf("customer id %d is have order transaction, please delete order first\n", id)
  640.             fmt.Print("Try again?? (y/n) -> ")
  641.             scanner.Scan()
  642.             answer := scanner.Text()
  643.             if answer == "y" {
  644.                 delete_customer(new_order)
  645.             } else if answer == "n" {
  646.                 customer_menu()
  647.             } else {
  648.                 fmt.Println("unrecognized input")
  649.                 os.Exit(0)
  650.             }
  651.         }
  652.     } else {
  653.         fmt.Println("failed to delete customer")
  654.     }
  655.     fmt.Print("Delete current customer again?? (y/n) -> ")
  656.     scanner.Scan()
  657.     answer := scanner.Text()
  658.     if answer == "y" {
  659.         delete_customer(new_order)
  660.     } else if answer == "n" {
  661.         customer_menu()
  662.     } else {
  663.         fmt.Println("unrecognized input")
  664.         os.Exit(0)
  665.     }
  666.     return nil
  667. }
  668.  
  669. func add_service() error {
  670.     db := connectDb()
  671.     scanner := bufio.NewScanner(os.Stdin)
  672.     new_Service := service.Service{}
  673.     fmt.Print("Input Service id : ")
  674.     scanner.Scan()
  675.     new_Service.Service_id, _ = strconv.Atoi(scanner.Text())
  676.     id_serviceExist, err := check_IDService(new_Service.Service_id)
  677.     if err != nil {
  678.         return err
  679.     }
  680.     if id_serviceExist {
  681.         fmt.Printf("service id %d already exist\n", new_Service.Service_id)
  682.         fmt.Print("Try again?? (y/n) -> ")
  683.         scanner := bufio.NewScanner(os.Stdin)
  684.         scanner.Scan()
  685.         answer := scanner.Text()
  686.         if answer == "y" {
  687.             add_service()
  688.         } else if answer == "n" {
  689.             service_menu()
  690.         } else {
  691.             fmt.Println("unrecognized input")
  692.             os.Exit(0)
  693.         }
  694.     }
  695.     if new_Service.Service_id == 0 {
  696.         fmt.Println("service id is invalid")
  697.         fmt.Print("Try again?? (y/n) -> ")
  698.         scanner.Scan()
  699.         answer := scanner.Text()
  700.         if answer == "y" {
  701.             add_service()
  702.         } else if answer == "n" {
  703.             service_menu()
  704.         } else {
  705.             fmt.Println("unrecognized input")
  706.             os.Exit(0)
  707.         }
  708.     }
  709.     fmt.Print("Input Service name : ")
  710.     scanner.Scan()
  711.     new_Service.Service_name = scanner.Text()
  712.     fmt.Print("Input Service Unit : ")
  713.     scanner.Scan()
  714.     new_Service.Unit = scanner.Text()
  715.     fmt.Print("Input Service Price : ")
  716.     scanner.Scan()
  717.     new_Service.Price, _ = strconv.Atoi(scanner.Text())
  718.     fmt.Print("Are you sure to add this service?? (y/n) -> ")
  719.     scanner.Scan()
  720.     confirm := scanner.Text()
  721.     if confirm == "y" {
  722.         new_Service = service.Service{
  723.             Service_id:   new_Service.Service_id,
  724.             Service_name: new_Service.Service_name,
  725.             Unit:         new_Service.Unit,
  726.             Price:        new_Service.Price,
  727.         }
  728.         if _, err := db.Exec("insert into service (service_id,service_name,unit,price) values ($1,$2,$3,$4)", new_Service.Service_id, new_Service.Service_name, new_Service.Unit, new_Service.Price); err != nil {
  729.         } else {
  730.             fmt.Println("New Service has been added to database successfully")
  731.         }
  732.     } else {
  733.         fmt.Println("failed to add new service")
  734.     }
  735.     fmt.Print("Input new service again?? (y/n) -> ")
  736.     scanner.Scan()
  737.     answer := scanner.Text()
  738.     if answer == "y" {
  739.         add_service()
  740.     } else if answer == "n" {
  741.         service_menu()
  742.     } else {
  743.         fmt.Println("unrecognized input")
  744.         os.Exit(0)
  745.     }
  746.     return nil
  747. }
  748. func view_service() {
  749.     scanner := bufio.NewScanner(os.Stdin)
  750.     db := connectDb()
  751.     rows_Service, err := db.Query("select * from service order by service_id asc")
  752.     if err != nil {
  753.         fmt.Println(err)
  754.     } else {
  755.         headers := []string{"Service ID", "Service Name", "Service Unit", "Service Price", "Time Created", "Time Updated"}
  756.         scanDest := []interface{}{new(int), new(string), new(string), new(int), new(string), new(string)}
  757.         tableWriter := tablewriter.NewWriter(os.Stdout)
  758.         tableWriter.SetHeader(headers)
  759.         for rows_Service.Next() {
  760.             err := rows_Service.Scan(scanDest...)
  761.             if err != nil {
  762.                 panic(err)
  763.             }
  764.             row_Values := make([]string, len(scanDest))
  765.             for i, dest := range scanDest {
  766.                 switch v := dest.(type) {
  767.                 case *int:
  768.                     row_Values[i] = strconv.Itoa(*v)
  769.                 case *string:
  770.                     row_Values[i] = *v
  771.                 default:
  772.                     row_Values[i] = fmt.Sprintf("%v", v)
  773.                 }
  774.             }
  775.             tableWriter.Append(row_Values)
  776.         }
  777.         tableWriter.Render()
  778.     }
  779.     fmt.Print("Back to previous menu?? (y/n)")
  780.     scanner.Scan()
  781.     answer := scanner.Text()
  782.     if answer == "y" {
  783.         service_menu()
  784.     } else if answer == "n" {
  785.         main_app()
  786.     } else {
  787.         fmt.Println("unrecognized input")
  788.         os.Exit(0)
  789.     }
  790. }
  791.  
  792. func view_selectedService() error {
  793.     db := connectDb()
  794.     scanner := bufio.NewScanner(os.Stdin)
  795.     fmt.Print("Input Service Id to view : ")
  796.     scanner.Scan()
  797.     id, err := strconv.Atoi(scanner.Text())
  798.     if err != nil {
  799.         fmt.Println("Service id is invalid")
  800.         fmt.Print("Try again?? (y/n) -> ")
  801.         scanner.Scan()
  802.         answer := scanner.Text()
  803.         if answer == "y" {
  804.             view_selectedService()
  805.         } else if answer == "n" {
  806.             service_menu()
  807.         } else {
  808.             fmt.Println("unrecognized input")
  809.             os.Exit(0)
  810.         }
  811.     }
  812.     id_ViewService, err := check_IDService(id)
  813.     if err != nil {
  814.         return err
  815.     }
  816.     if !id_ViewService {
  817.         fmt.Printf("service id %d is not found\n", id)
  818.         fmt.Print("Try again?? (y/n) -> ")
  819.         scanner.Scan()
  820.         answer := scanner.Text()
  821.         if answer == "y" {
  822.             view_selectedService()
  823.         } else if answer == "n" {
  824.             service_menu()
  825.         } else {
  826.             fmt.Println("unrecognized input")
  827.             os.Exit(0)
  828.         }
  829.     }
  830.     rows_Service, err := db.Query("select * from service where service_id = $1", id)
  831.     if err != nil {
  832.         panic(err)
  833.     } else {
  834.         headers := []string{"Service ID", "Service Name", "Service Unit", "Service Price", "Time Created", "Time Updated"}
  835.         scanDest := []interface{}{new(int), new(string), new(string), new(int), new(string), new(string)}
  836.         tableWriter := tablewriter.NewWriter(os.Stdout)
  837.         tableWriter.SetHeader(headers)
  838.         for rows_Service.Next() {
  839.             err := rows_Service.Scan(scanDest...)
  840.             if err != nil {
  841.                 panic(err)
  842.             }
  843.             row_Values := make([]string, len(scanDest))
  844.             for i, dest := range scanDest {
  845.                 switch v := dest.(type) {
  846.                 case *int:
  847.                     row_Values[i] = strconv.Itoa(*v)
  848.                 case *string:
  849.                     row_Values[i] = *v
  850.                 default:
  851.                     row_Values[i] = fmt.Sprintf("%v", v)
  852.                 }
  853.             }
  854.             tableWriter.Append(row_Values)
  855.         }
  856.         tableWriter.Render()
  857.     }
  858.     fmt.Print("Back to previous menu?? (y/n) -> ")
  859.     scanner.Scan()
  860.     answer := scanner.Text()
  861.     if answer == "y" {
  862.         service_menu()
  863.     } else if answer == "n" {
  864.         main_app()
  865.     } else {
  866.         fmt.Println("unrecognized input")
  867.         os.Exit(0)
  868.     }
  869.     return nil
  870. }
  871.  
  872. func edit_service() error {
  873.     db := connectDb()
  874.     scanner := bufio.NewScanner(os.Stdin)
  875.     fmt.Print("Input Service id to update : ")
  876.     scanner.Scan()
  877.     id, err := strconv.Atoi(scanner.Text())
  878.     if err != nil {
  879.         fmt.Println("Service id is invalid")
  880.         fmt.Print("Try again?? (y/n) -> ")
  881.         scanner.Scan()
  882.         answer := scanner.Text()
  883.         if answer == "y" {
  884.             edit_service()
  885.         } else if answer == "n" {
  886.             service_menu()
  887.         } else {
  888.             fmt.Println("unrecognized input")
  889.             os.Exit(0)
  890.         }
  891.     }
  892.     id_Found, err := check_IDService(id)
  893.     if err != nil {
  894.         return err
  895.     }
  896.     if !id_Found {
  897.         fmt.Printf("service id %d is not found\n", id)
  898.         fmt.Print("Try again?? (y/n) -> ")
  899.         scanner.Scan()
  900.         answer := scanner.Text()
  901.         if answer == "y" {
  902.             edit_service()
  903.         } else if answer == "n" {
  904.             service_menu()
  905.         } else {
  906.             fmt.Println("unrecognized input")
  907.             os.Exit(0)
  908.         }
  909.     }
  910.     update_Service := service.Service{
  911.         Service_id: id,
  912.     }
  913.     fmt.Print("Input New Service Name : ")
  914.     scanner.Scan()
  915.     update_Service.Service_name = scanner.Text()
  916.     fmt.Print("Input New Service Unit : ")
  917.     scanner.Scan()
  918.     update_Service.Unit = scanner.Text()
  919.     fmt.Print("Input New Service Price : ")
  920.     scanner.Scan()
  921.     price := scanner.Text()
  922.     fmt.Print("Are you sure to update this service?? (y/n) -> ")
  923.     scanner.Scan()
  924.     confirm := scanner.Text()
  925.     if confirm == "y" {
  926.         new_ServiceName := update_Service.Service_name != ""
  927.         new_Unit := update_Service.Unit != ""
  928.         new_Price := price != ""
  929.         process_Update := new_ServiceName || new_Unit || new_Price
  930.         if !process_Update {
  931.             return nil
  932.         }
  933.         i := 2
  934.         q := "update service set "
  935.         if new_ServiceName {
  936.             q += fmt.Sprintf("service_name = $%d", i)
  937.             i++
  938.         }
  939.         if new_Unit {
  940.             if i > 2 {
  941.                 q += fmt.Sprintf(",unit = $%d", i)
  942.             } else {
  943.                 q += fmt.Sprintf("unit = $%d", i)
  944.             }
  945.             i++
  946.         }
  947.         if new_Price {
  948.             p, err := strconv.Atoi(price)
  949.             if err != nil {
  950.                 return fmt.Errorf("invalid price %w", err)
  951.             }
  952.             update_Service.Price = p
  953.             if i > 2 {
  954.                 q += fmt.Sprintf(",price = $%d", i)
  955.             } else {
  956.                 q += fmt.Sprintf("price = $%d", i)
  957.             }
  958.             i++
  959.         }
  960.         q += " ,updated_at = now() where service_id = $1"
  961.         arr := []any{}
  962.         arr = append(arr, update_Service.Service_id)
  963.         if new_ServiceName {
  964.             arr = append(arr, update_Service.Service_name)
  965.         }
  966.         if new_Unit {
  967.             arr = append(arr, update_Service.Unit)
  968.         }
  969.         if new_Price {
  970.             arr = append(arr, update_Service.Price)
  971.         }
  972.         if _, err := db.Exec(q, arr...); err != nil {
  973.             return err
  974.         } else {
  975.             fmt.Println("Current Service has been updated to database successfully")
  976.             fmt.Print("Edit current service again?? (y/n) -> ")
  977.             scanner.Scan()
  978.             answer := scanner.Text()
  979.             if answer == "y" {
  980.                 edit_service()
  981.             } else if answer == "n" {
  982.                 service_menu()
  983.             } else {
  984.                 fmt.Println("unrecognized input")
  985.                 os.Exit(0)
  986.             }
  987.         }
  988.     } else {
  989.         fmt.Println("failed to update service")
  990.     }
  991.     return nil
  992. }
  993. func delete_service(new_order order.Order, new_detailOrder order_detail.Order_detail) error {
  994.     db := connectDb()
  995.     scanner := bufio.NewScanner(os.Stdin)
  996.     fmt.Print("Input Service id to delete : ")
  997.     scanner.Scan()
  998.     id, err := strconv.Atoi(scanner.Text())
  999.     if err != nil {
  1000.         fmt.Println("Service id is invalid")
  1001.         fmt.Print("Try again?? (y/n) -> ")
  1002.         scanner.Scan()
  1003.         answer := scanner.Text()
  1004.         if answer == "y" {
  1005.             delete_service(new_order, new_detailOrder)
  1006.         } else if answer == "n" {
  1007.             service_menu()
  1008.         } else {
  1009.             fmt.Println("unrecognized input")
  1010.             os.Exit(0)
  1011.         }
  1012.     }
  1013.     id_Found, err := check_IDService(id)
  1014.     if err != nil {
  1015.         return err
  1016.     }
  1017.     if !id_Found {
  1018.         fmt.Printf("service id %d is not found\n", id)
  1019.         fmt.Print("Try again?? (y/n) -> ")
  1020.         scanner.Scan()
  1021.         answer := scanner.Text()
  1022.         if answer == "y" {
  1023.             delete_service(new_order, new_detailOrder)
  1024.         } else if answer == "n" {
  1025.             service_menu()
  1026.         } else {
  1027.             fmt.Println("unrecognized input")
  1028.             os.Exit(0)
  1029.         }
  1030.     }
  1031.     fmt.Print("Are you sure to delete this service?? (y/n) -> ")
  1032.     scanner.Scan()
  1033.     confirm := scanner.Text()
  1034.     if confirm == "y" {
  1035.         check_IDDetailOrder(id)
  1036.         if new_order.Completion_date.String() == "0001-01-01 00:00:00 +0000 UTC" {
  1037.             fmt.Printf("service id %d is used in order transaction, please delete order first\n", id)
  1038.             fmt.Print("Try again?? (y/n) -> ")
  1039.             scanner.Scan()
  1040.             answer := scanner.Text()
  1041.             if answer == "y" {
  1042.                 delete_service(new_order, new_detailOrder)
  1043.             } else if answer == "n" {
  1044.                 service_menu()
  1045.             } else {
  1046.                 fmt.Println("unrecognized input")
  1047.                 os.Exit(0)
  1048.             }
  1049.         } else {
  1050.             if _, err := db.Exec("delete from service where service_id = $1", id); err != nil {
  1051.                 return err
  1052.             } else {
  1053.                 fmt.Println("Current Service has been deleted from database successfully")
  1054.             }
  1055.         }
  1056.     } else {
  1057.         fmt.Println("failed to delete service")
  1058.     }
  1059.     fmt.Print("Delete current service again?? (y/n) -> ")
  1060.     scanner.Scan()
  1061.     answer := scanner.Text()
  1062.     if answer == "y" {
  1063.         delete_service(new_order, new_detailOrder)
  1064.     } else if answer == "n" {
  1065.         service_menu()
  1066.     } else {
  1067.         fmt.Println("unrecognized input")
  1068.         os.Exit(0)
  1069.     }
  1070.     return nil
  1071. }
  1072. func add_order(new_Customer customer.Customer, new_Service service.Service) error {
  1073.     scanner := bufio.NewScanner(os.Stdin)
  1074.     new_order := order.Order{}
  1075.     new_detailOrder := order_detail.Order_detail{}
  1076.     fmt.Print("Input Order id : ")
  1077.     scanner.Scan()
  1078.     new_order.Order_id, _ = strconv.Atoi(scanner.Text())
  1079.     id_orderExist, err := check_IDOrder(new_order.Order_id)
  1080.     if err != nil {
  1081.         return err
  1082.     }
  1083.     if id_orderExist {
  1084.         fmt.Printf("order id %d already exist\n", new_order.Order_id)
  1085.         fmt.Print("Try again?? (y/n) -> ")
  1086.         scanner := bufio.NewScanner(os.Stdin)
  1087.         scanner.Scan()
  1088.         answer := scanner.Text()
  1089.         if answer == "y" {
  1090.             add_order(new_Customer, new_Service)
  1091.         } else if answer == "n" {
  1092.             order_menu()
  1093.         } else {
  1094.             fmt.Println("unrecognized input")
  1095.             os.Exit(0)
  1096.         }
  1097.     }
  1098.     if new_order.Order_id == 0 {
  1099.         fmt.Println("order id is invalid")
  1100.         fmt.Print("Try again?? (y/n) -> ")
  1101.         scanner.Scan()
  1102.         answer := scanner.Text()
  1103.         if answer == "y" {
  1104.             add_order(new_Customer, new_Service)
  1105.         } else if answer == "n" {
  1106.             order_menu()
  1107.         } else {
  1108.             fmt.Println("unrecognized input")
  1109.             os.Exit(0)
  1110.         }
  1111.     }
  1112.     fmt.Print("Input Customer id : ")
  1113.     scanner.Scan()
  1114.     new_Customer.Customer_id, _ = strconv.Atoi(scanner.Text())
  1115.     id_Found, err := check_IDCustomer(new_Customer.Customer_id)
  1116.     if err != nil {
  1117.         return err
  1118.     }
  1119.     if !id_Found {
  1120.         fmt.Printf("customer id %d is not found\n", new_Customer.Customer_id)
  1121.         fmt.Print("Try again?? (y/n) -> ")
  1122.         scanner.Scan()
  1123.         answer := scanner.Text()
  1124.         if answer == "y" {
  1125.             add_order(new_Customer, new_Service)
  1126.         } else if answer == "n" {
  1127.             order_menu()
  1128.         } else {
  1129.             fmt.Println("unrecognized input")
  1130.             os.Exit(0)
  1131.         }
  1132.     }
  1133.     if new_Customer.Customer_id == 0 {
  1134.         fmt.Println("customer id is invalid")
  1135.         fmt.Print("Try again?? (y/n) -> ")
  1136.         scanner.Scan()
  1137.         answer := scanner.Text()
  1138.         if answer == "y" {
  1139.             add_order(new_Customer, new_Service)
  1140.         } else if answer == "n" {
  1141.             order_menu()
  1142.         } else {
  1143.             fmt.Println("unrecognized input")
  1144.             os.Exit(0)
  1145.         }
  1146.     }
  1147.     fmt.Print("Input Service id : ")
  1148.     scanner.Scan()
  1149.     new_Service.Service_id, _ = strconv.Atoi(scanner.Text())
  1150.     id_Found, err = check_IDService(new_Service.Service_id)
  1151.     if err != nil {
  1152.         return err
  1153.     }
  1154.     if !id_Found {
  1155.         fmt.Printf("service id %d is not found\n", new_Service.Service_id)
  1156.         fmt.Print("Try again?? (y/n) -> ")
  1157.         scanner.Scan()
  1158.         answer := scanner.Text()
  1159.         if answer == "y" {
  1160.             add_order(new_Customer, new_Service)
  1161.         } else if answer == "n" {
  1162.             order_menu()
  1163.         } else {
  1164.             fmt.Println("unrecognized input")
  1165.             os.Exit(0)
  1166.         }
  1167.     }
  1168.     if new_Service.Service_id == 0 {
  1169.         fmt.Println("service id is invalid")
  1170.         fmt.Print("Try again?? (y/n) -> ")
  1171.         scanner.Scan()
  1172.         answer := scanner.Text()
  1173.         if answer == "y" {
  1174.             add_order(new_Customer, new_Service)
  1175.         } else if answer == "n" {
  1176.             order_menu()
  1177.         } else {
  1178.             fmt.Println("unrecognized input")
  1179.             os.Exit(0)
  1180.         }
  1181.     }
  1182.     fmt.Print("Input Quantity : ")
  1183.     scanner.Scan()
  1184.     new_detailOrder.Qty, _ = strconv.Atoi(scanner.Text())
  1185.     var data, layoutFormat string
  1186.     layoutFormat = "2006-01-02 15:04:05"
  1187.     fmt.Print("Input Order Date (yyyy-mm-dd hh:mm:ss) : ")
  1188.     scanner.Scan()
  1189.     data = scanner.Text()
  1190.     new_order.Order_date, _ = time.Parse(layoutFormat, data)
  1191.     fmt.Print("Input Received by : ")
  1192.     scanner.Scan()
  1193.     new_order.Received_by = scanner.Text()
  1194.     new_order = order.Order{
  1195.         Order_id:    new_order.Order_id,
  1196.         Customer_id: new_Customer.Customer_id,
  1197.         Order_date:  new_order.Order_date,
  1198.         Received_by: new_order.Received_by,
  1199.     }
  1200.     new_detailOrder = order_detail.Order_detail{
  1201.         Order_id:   new_order.Order_id,
  1202.         Service_id: new_Service.Service_id,
  1203.         Qty:        new_detailOrder.Qty,
  1204.     }
  1205.     fmt.Print("Are you sure to add this order?? (y/n) -> ")
  1206.     scanner.Scan()
  1207.     confirm := scanner.Text()
  1208.     if confirm == "y" {
  1209.         exec(new_order, new_Customer, new_Service, new_detailOrder)
  1210.     } else {
  1211.         fmt.Println("failed to add new order")
  1212.     }
  1213.     fmt.Print("Input new order again?? (y/n)")
  1214.     scanner.Scan()
  1215.     answer := scanner.Text()
  1216.     if answer == "y" {
  1217.         add_order(new_Customer, new_Service)
  1218.     } else if answer == "n" {
  1219.         order_menu()
  1220.     } else {
  1221.         fmt.Println("unrecognized input")
  1222.         os.Exit(0)
  1223.     }
  1224.     return nil
  1225. }
  1226.  
  1227. func exec(new_order order.Order, new_Customer customer.Customer, new_Service service.Service, new_detailOrder order_detail.Order_detail) {
  1228.     db := connectDb()
  1229.     defer db.Close()
  1230.     tx, err := db.Begin()
  1231.     if err != nil {
  1232.         fmt.Println(err)
  1233.     }
  1234.     insert_Order(new_order, new_Customer, tx)
  1235.     insert_DetailOrder(new_detailOrder, new_Service, tx)
  1236.     err = tx.Commit()
  1237.     if err != nil {
  1238.         fmt.Println(err)
  1239.     } else {
  1240.         fmt.Println("OK! Committed!")
  1241.         fmt.Println("New Order has been added to database successfully :)")
  1242.     }
  1243. }
  1244.  
  1245. func insert_Order(new_order order.Order, new_Customer customer.Customer, tx *sql.Tx) {
  1246.     sql_statement := "insert into public.order (order_id,customer_id,order_date,received_by,completion_date) values ($1,$2,$3,$4,'0001-01-01 00:00:00')"
  1247.     _, err := tx.Exec(sql_statement, new_order.Order_id, new_Customer.Customer_id, new_order.Order_date, new_order.Received_by)
  1248.     validate_Orders(err, "insert", tx)
  1249. }
  1250.  
  1251. func insert_DetailOrder(new_detailOrder order_detail.Order_detail, new_Service service.Service, tx *sql.Tx) {
  1252.     sql_statement := "insert into order_detail (order_id,service_id,qty) values ($1,$2,$3)"
  1253.     _, err := tx.Exec(sql_statement, new_detailOrder.Order_id, new_Service.Service_id, new_detailOrder.Qty)
  1254.     validate_detailOrders(err, "insert", tx)
  1255. }
  1256.  
  1257. func validate_Orders(err error, message string, tx *sql.Tx) error {
  1258.     if err != nil {
  1259.         tx.Rollback()
  1260.         fmt.Println(err, "Failed! Rollback!")
  1261.     } else {
  1262.         fmt.Println("OK! " + message + " order query no issue")
  1263.     }
  1264.     return nil
  1265. }
  1266.  
  1267. func validate_detailOrders(err error, message string, tx *sql.Tx) error {
  1268.     if err != nil {
  1269.         tx.Rollback()
  1270.         fmt.Println(err, "Failed! Rollback!")
  1271.     } else {
  1272.         fmt.Println("OK! " + message + " detail order query no issue")
  1273.     }
  1274.     return nil
  1275. }
  1276.  
  1277. func view_order() {
  1278.     scanner := bufio.NewScanner(os.Stdin)
  1279.     db := connectDb()
  1280.     rows, err := db.Query("select order_detail.order_id,customer_id,service_id,order_date,qty,received_by,created_at,updated_at,completion_date from public.order join order_detail on public.order.order_id = order_detail.order_id order by order_id asc")
  1281.     if err != nil {
  1282.         fmt.Println(err)
  1283.     } else {
  1284.         headers := []string{"Order ID", "Customer ID", "Service ID", "Order Date", "Quantity", "Received By", "Time Created", "Time Updated", "Completion Date"}
  1285.         scanDest := []interface{}{new(int), new(int), new(int), new(string), new(int), new(string), new(string), new(string), new(string)}
  1286.         tableWriter := tablewriter.NewWriter(os.Stdout)
  1287.         tableWriter.SetHeader(headers)
  1288.         for rows.Next() {
  1289.             err := rows.Scan(scanDest...)
  1290.             if err != nil {
  1291.                 panic(err)
  1292.             }
  1293.             rowValues := make([]string, len(scanDest))
  1294.             for i, dest := range scanDest {
  1295.                 switch v := dest.(type) {
  1296.                 case *int:
  1297.                     rowValues[i] = strconv.Itoa(*v)
  1298.                 case *string:
  1299.                     rowValues[i] = *v
  1300.                 default:
  1301.                     rowValues[i] = fmt.Sprintf("%v", v)
  1302.                 }
  1303.             }
  1304.             tableWriter.Append(rowValues)
  1305.         }
  1306.         tableWriter.Render()
  1307.     }
  1308.     fmt.Print("Back to previous menu?? (y/n)")
  1309.     scanner.Scan()
  1310.     answer := scanner.Text()
  1311.     if answer == "y" {
  1312.         order_menu()
  1313.     } else if answer == "n" {
  1314.         main_app()
  1315.     } else {
  1316.         fmt.Println("unrecognized input")
  1317.         os.Exit(0)
  1318.     }
  1319. }
  1320.  
  1321. func view_selectedOrder() error {
  1322.     db := connectDb()
  1323.     scanner := bufio.NewScanner(os.Stdin)
  1324.     fmt.Print("Input Order Detail Id to view : ")
  1325.     scanner.Scan()
  1326.     id, err := strconv.Atoi(scanner.Text())
  1327.     if err != nil {
  1328.         fmt.Println("Order detail id is invalid")
  1329.         fmt.Print("Try again?? (y/n) -> ")
  1330.         scanner.Scan()
  1331.         answer := scanner.Text()
  1332.         if answer == "y" {
  1333.             view_selectedOrder()
  1334.         } else if answer == "n" {
  1335.             order_menu()
  1336.         } else {
  1337.             fmt.Println("unrecognized input")
  1338.             os.Exit(0)
  1339.         }
  1340.     }
  1341.     id_ViewOrder, err := check_IDOrder(id)
  1342.     if err != nil {
  1343.         return err
  1344.     }
  1345.     if !id_ViewOrder {
  1346.         fmt.Printf("Order Detail id %d is not found\n", id)
  1347.         fmt.Print("Try again?? (y/n) -> ")
  1348.         scanner.Scan()
  1349.         answer := scanner.Text()
  1350.         if answer == "y" {
  1351.             view_selectedOrder()
  1352.         } else if answer == "n" {
  1353.             order_menu()
  1354.         } else {
  1355.             fmt.Println("unrecognized input")
  1356.             os.Exit(0)
  1357.         }
  1358.     }
  1359.     rows_Order, err := db.Query("select order_detail.order_id,customer_id,service_id,order_date,qty,received_by,created_at,updated_at,completion_date from public.order join order_detail on public.order.order_id = order_detail.order_id where order_detail_id = $1", id)
  1360.     if err != nil {
  1361.         panic(err)
  1362.     } else {
  1363.         headers := []string{"Order ID", "Customer ID", "Service ID", "Order Date", "Quantity", "Received By", "Time Created", "Time Updated", "Completion Date"}
  1364.         scanDest := []interface{}{new(int), new(int), new(int), new(string), new(int), new(string), new(string), new(string), new(string)}
  1365.         tableWriter := tablewriter.NewWriter(os.Stdout)
  1366.         tableWriter.SetHeader(headers)
  1367.         for rows_Order.Next() {
  1368.             err := rows_Order.Scan(scanDest...)
  1369.             if err != nil {
  1370.                 panic(err)
  1371.             }
  1372.             row_Values := make([]string, len(scanDest))
  1373.             for i, dest := range scanDest {
  1374.                 switch v := dest.(type) {
  1375.                 case *int:
  1376.                     row_Values[i] = strconv.Itoa(*v)
  1377.                 case *string:
  1378.                     row_Values[i] = *v
  1379.                 default:
  1380.                     row_Values[i] = fmt.Sprintf("%v", v)
  1381.                 }
  1382.             }
  1383.             tableWriter.Append(row_Values)
  1384.         }
  1385.         tableWriter.Render()
  1386.     }
  1387.     fmt.Print("Back to previous menu?? (y/n) -> ")
  1388.     scanner.Scan()
  1389.     answer := scanner.Text()
  1390.     if answer == "y" {
  1391.         order_menu()
  1392.     } else if answer == "n" {
  1393.         main_app()
  1394.     } else {
  1395.         fmt.Println("unrecognized input")
  1396.         os.Exit(0)
  1397.     }
  1398.     return nil
  1399. }
  1400.  
  1401. func complete_order(new_order order.Order) error {
  1402.     db := connectDb()
  1403.     scanner := bufio.NewScanner(os.Stdin)
  1404.     fmt.Print("Input Order id to complete : ")
  1405.     scanner.Scan()
  1406.     id, err := strconv.Atoi(scanner.Text())
  1407.     if err != nil {
  1408.         fmt.Println("Service id is invalid")
  1409.         fmt.Print("Try again?? (y/n) -> ")
  1410.         scanner.Scan()
  1411.         answer := scanner.Text()
  1412.         if answer == "y" {
  1413.             edit_service()
  1414.         } else if answer == "n" {
  1415.             service_menu()
  1416.         } else {
  1417.             fmt.Println("unrecognized input")
  1418.             os.Exit(0)
  1419.         }
  1420.     }
  1421.     id_Found, err := check_IDService(id)
  1422.     if err != nil {
  1423.         return err
  1424.     }
  1425.     if !id_Found {
  1426.         fmt.Printf("service id %d is not found\n", id)
  1427.         fmt.Print("Try again?? (y/n) -> ")
  1428.         scanner.Scan()
  1429.         answer := scanner.Text()
  1430.         if answer == "y" {
  1431.             edit_service()
  1432.         } else if answer == "n" {
  1433.             service_menu()
  1434.         } else {
  1435.             fmt.Println("unrecognized input")
  1436.             os.Exit(0)
  1437.         }
  1438.     }
  1439.     var data, layoutFormat string
  1440.     layoutFormat = "2006-01-02 15:04:05"
  1441.     fmt.Print("Input Completion Date (yyyy-mm-dd hh:mm:ss) : ")
  1442.     scanner.Scan()
  1443.     data = scanner.Text()
  1444.     new_order.Completion_date, _ = time.Parse(layoutFormat, data)
  1445.     fmt.Print("Are you sure to update this order?? (y/n) -> ")
  1446.     scanner.Scan()
  1447.     confirm := scanner.Text()
  1448.     if confirm == "y" {
  1449.         new_order = order.Order{
  1450.             Order_id:        id,
  1451.             Completion_date: new_order.Completion_date,
  1452.         }
  1453.         if _, err = db.Exec("update public.order set completion_date = $2, updated_at = now() where order_id = $1", new_order.Order_id, new_order.Completion_date); err != nil {
  1454.             return err
  1455.         } else {
  1456.             fmt.Println("Current Order has been updated to database successfully, order completed!")
  1457.             fmt.Print("Complete current order again?? (y/n) -> ")
  1458.             scanner.Scan()
  1459.             answer := scanner.Text()
  1460.             if answer == "y" {
  1461.                 complete_order(new_order)
  1462.             } else if answer == "n" {
  1463.                 order_menu()
  1464.             } else {
  1465.                 fmt.Println("unrecognized input")
  1466.                 os.Exit(0)
  1467.             }
  1468.         }
  1469.     } else {
  1470.         fmt.Println("failed to update order")
  1471.     }
  1472.     return nil
  1473. }
  1474.  
Tags: golang
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement