Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "bufio"
- "challenge-godb/customer"
- "challenge-godb/order"
- "challenge-godb/order_detail"
- "challenge-godb/service"
- "database/sql"
- "fmt"
- "os"
- "strconv"
- "time"
- _ "github.com/lib/pq"
- "github.com/olekukonko/tablewriter"
- )
- // simplified function main
- func main() {
- main_app()
- }
- //setup_database
- const (
- host = "localhost"
- port = 5432
- user = "postgres"
- password = "root"
- dbname = "enigma_laundry"
- )
- var psqlInfo = fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", host, port, user, password, dbname)
- func connectDb() *sql.DB {
- db, err := sql.Open("postgres", psqlInfo)
- if err != nil {
- panic(err)
- }
- err = db.Ping()
- if err != nil {
- panic(err)
- } else {
- fmt.Println("OK")
- }
- return db
- }
- //detail dari function main diatas
- func main_app() {
- fmt.Println("|---------------------------------|")
- fmt.Println("| ~ Welcome to Enigma Laundry ~ |")
- fmt.Println("|---------------------------------|")
- fmt.Println("|\t Choices Menu :\t |")
- fmt.Println("|1. Customer\t\t\t |")
- fmt.Println("|2. Service\t\t\t |")
- fmt.Println("|3. Order\t\t\t |")
- fmt.Println("|---------------------------------|")
- fmt.Print("==> Input your Choice : ")
- scanner := bufio.NewScanner(os.Stdin)
- scanner.Scan()
- confirm, _ := strconv.Atoi(scanner.Text())
- if confirm == 1 {
- //simplified dari menu customer untuk dibuat function terpisah
- customer_menu()
- } else if confirm == 2 {
- //simplified dari menu service untuk dibuat function terpisah
- service_menu()
- } else if confirm == 3 {
- //simplified dari menu order untuk dibuat function terpisah
- order_menu()
- } else {
- fmt.Println("unrecognized input")
- os.Exit(0)
- }
- }
- //detail dari function customer_menu
- func customer_menu() error {
- fmt.Println("|---------------------------------|")
- fmt.Println("| ~ Customer Dashboard Menu ~ |")
- fmt.Println("|---------------------------------|")
- fmt.Println("|\t Choices :\t\t |")
- fmt.Println("|1. Create New Customer\t\t |")
- fmt.Println("|2. View All Customers\t\t |")
- fmt.Println("|3. View Selected Customer\t |")
- fmt.Println("|4. Update Customer\t\t |")
- fmt.Println("|5. Delete Customer\t\t |")
- fmt.Println("|6. Back to Main Menu\t\t |")
- fmt.Println("|---------------------------------|")
- fmt.Print("==> Input your Choice : ")
- scanner := bufio.NewScanner(os.Stdin)
- scanner.Scan()
- confirm, _ := strconv.Atoi(scanner.Text())
- if confirm == 1 {
- //simplified dari proses menambah customer untuk dibuat function terpisah
- add_customer()
- } else if confirm == 2 {
- //simplified dari proses melihat customer untuk dibuat function terpisah
- view_customer()
- } else if confirm == 3 {
- //simplified dari proses melihat customer tertentu untuk dibuat function terpisah
- view_selectedCustomer()
- } else if confirm == 4 {
- //simplified dari proses mengubah customer untuk dibuat function terpisah
- edit_customer()
- } else if confirm == 5 {
- //simplified dari proses menghapus customer untuk dibuat function terpisah
- delete_customer(order.Order{})
- } else if confirm == 6 {
- main_app()
- } else {
- fmt.Println("unrecognized input")
- os.Exit(0)
- }
- return nil
- }
- //detail dari function service_menu
- func service_menu() {
- fmt.Println("|---------------------------------|")
- fmt.Println("| ~ Service Dashboard Menu ~ |")
- fmt.Println("|---------------------------------|")
- fmt.Println("|\t Choices :\t\t |")
- fmt.Println("|1. Create New Service\t\t |")
- fmt.Println("|2. View All Services\t\t |")
- fmt.Println("|3. View Selected Service\t |")
- fmt.Println("|4. Update Service\t\t |")
- fmt.Println("|5. Delete Service\t\t |")
- fmt.Println("|6. Back to Main Menu\t\t |")
- fmt.Println("|---------------------------------|")
- fmt.Print("==> Input your Choice : ")
- scanner := bufio.NewScanner(os.Stdin)
- scanner.Scan()
- confirm, _ := strconv.Atoi(scanner.Text())
- if confirm == 1 {
- //simplified dari proses menambah service untuk dibuat function terpisah
- add_service()
- } else if confirm == 2 {
- //simplified dari proses melihat service untuk dibuat function terpisah
- view_service()
- } else if confirm == 3 {
- //simplified dari proses melihat service untuk dibuat function terpisah
- view_selectedService()
- } else if confirm == 4 {
- //simplified dari proses mengubah service untuk dibuat function terpisah
- edit_service()
- } else if confirm == 5 {
- //simplified dari proses menghapus service untuk dibuat function terpisah
- delete_service(order.Order{}, order_detail.Order_detail{})
- } else if confirm == 6 {
- main_app()
- } else {
- fmt.Println("unrecognized input")
- os.Exit(0)
- }
- }
- //detail dari function order_menu
- func order_menu() {
- fmt.Println("|---------------------------------|")
- fmt.Println("| ~ Order Dashboard Menu ~ |")
- fmt.Println("|---------------------------------|")
- fmt.Println("|\t Choices :\t\t |")
- fmt.Println("|1. Create New Order\t\t |")
- fmt.Println("|2. View All Orders\t\t |")
- fmt.Println("|3. View Selected Order\t\t |")
- fmt.Println("|4. Complete Order\t\t |")
- fmt.Println("|5. Back to Main Menu\t\t |")
- fmt.Println("|---------------------------------|")
- fmt.Print("==> Input your Choice : ")
- scanner := bufio.NewScanner(os.Stdin)
- scanner.Scan()
- confirm, _ := strconv.Atoi(scanner.Text())
- if confirm == 1 {
- //simplified dari proses menambah order untuk dibuat function terpisah
- add_order(customer.Customer{}, service.Service{})
- } else if confirm == 2 {
- //simplified dari proses melihat order untuk dibuat function terpisah
- view_order()
- } else if confirm == 3 {
- //simplified dari proses melihat order tertentu untuk dibuat function terpisah
- view_selectedOrder()
- } else if confirm == 4 {
- //simplified dari proses menyelesaikan order untuk dibuat function terpisah
- complete_order(order.Order{})
- } else if confirm == 5 {
- main_app()
- } else {
- fmt.Println("unrecognized input")
- os.Exit(0)
- }
- }
- //detail proses menambah customer
- func add_customer() error {
- db := connectDb()
- scanner := bufio.NewScanner(os.Stdin)
- new_Customer := customer.Customer{}
- fmt.Print("Input Customer id : ")
- scanner.Scan()
- new_Customer.Customer_id, _ = strconv.Atoi(scanner.Text())
- id_customerExist, err := check_IDCustomer(new_Customer.Customer_id)
- if err != nil {
- return err
- }
- if id_customerExist {
- fmt.Printf("customer id %d already exist\n", new_Customer.Customer_id)
- fmt.Print("Try again?? (y/n) -> ")
- scanner := bufio.NewScanner(os.Stdin)
- scanner.Scan()
- answer := scanner.Text()
- if answer == "y" {
- add_customer()
- } else if answer == "n" {
- customer_menu()
- } else {
- fmt.Println("unrecognized input")
- os.Exit(0)
- }
- }
- if new_Customer.Customer_id == 0 {
- fmt.Println("customer id is invalid")
- fmt.Print("Try again?? (y/n) -> ")
- scanner.Scan()
- answer := scanner.Text()
- if answer == "y" {
- add_customer()
- } else if answer == "n" {
- customer_menu()
- } else {
- fmt.Println("unrecognized input")
- os.Exit(0)
- }
- }
- fmt.Print("Input Customer name : ")
- scanner.Scan()
- new_Customer.Name = scanner.Text()
- fmt.Print("Input Customer phone : ")
- scanner.Scan()
- new_Customer.Phone = scanner.Text()
- fmt.Print("Input Customer address : ")
- scanner.Scan()
- new_Customer.Address = scanner.Text()
- fmt.Print("Are you sure to add this customer?? (y/n) -> ")
- scanner.Scan()
- confirm := scanner.Text()
- if confirm == "y" {
- new_Customer = customer.Customer{
- Customer_id: new_Customer.Customer_id,
- Name: new_Customer.Name,
- Phone: new_Customer.Phone,
- Address: new_Customer.Address,
- }
- 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 {
- } else {
- fmt.Println("New Customer has been added to database successfully")
- }
- } else {
- fmt.Println("failed to add new customer")
- }
- fmt.Print("Input new customer again?? (y/n) -> ")
- scanner.Scan()
- answer := scanner.Text()
- if answer == "y" {
- add_customer()
- } else if answer == "n" {
- customer_menu()
- } else {
- fmt.Println("unrecognized input")
- os.Exit(0)
- }
- return nil
- }
- //cek customer by id
- func check_IDCustomer(id int) (bool, error) {
- db := connectDb()
- rows_Customer, err := db.Query("select customer_id from customer where customer_id = $1", id)
- if err != nil {
- return false, err
- }
- defer rows_Customer.Close()
- if rows_Customer.Next() {
- return true, nil
- }
- return false, nil
- }
- //cek service by id
- func check_IDService(id int) (bool, error) {
- db := connectDb()
- rows_Service, err := db.Query("select service_id from service where service_id = $1", id)
- if err != nil {
- return false, err
- }
- defer rows_Service.Close()
- if rows_Service.Next() {
- return true, nil
- }
- return false, nil
- }
- func check_IDOrder(id int) (bool, error) {
- db := connectDb()
- rows_Order, err := db.Query("select order_id from public.order where order_id = $1", id)
- if err != nil {
- return false, err
- }
- defer rows_Order.Close()
- if rows_Order.Next() {
- return true, nil
- }
- return false, nil
- }
- func check_IDDetailOrder(id int) (bool, error) {
- db := connectDb()
- rows_DetailOrder, err := db.Query("select order_detail_id from public.order where order_detail_id = $1", id)
- if err != nil {
- return false, err
- }
- defer rows_DetailOrder.Close()
- if rows_DetailOrder.Next() {
- return true, nil
- }
- return false, nil
- }
- //melihat semua data customer yang ada di db
- func view_customer() {
- db := connectDb()
- scanner := bufio.NewScanner(os.Stdin)
- rows_viewCustomer, err := db.Query("select * from customer order by customer_id asc")
- if err != nil {
- fmt.Println(err)
- } else {
- headers := []string{"Customer ID", "Customer Name", "Phone Number", "Customer Address", "Time Created", "Time Updated"}
- scanDest := []interface{}{new(int), new(string), new(string), new(string), new(string), new(string)}
- tableWriter := tablewriter.NewWriter(os.Stdout)
- tableWriter.SetHeader(headers)
- for rows_viewCustomer.Next() {
- err := rows_viewCustomer.Scan(scanDest...)
- if err != nil {
- panic(err)
- }
- row_Values := make([]string, len(scanDest))
- for i, dest := range scanDest {
- switch v := dest.(type) {
- case *int:
- row_Values[i] = strconv.Itoa(*v)
- case *string:
- row_Values[i] = *v
- default:
- row_Values[i] = fmt.Sprintf("%v", v)
- }
- }
- tableWriter.Append(row_Values)
- }
- tableWriter.Render()
- }
- fmt.Print("Back to previous menu?? (y/n) -> ")
- scanner.Scan()
- answer := scanner.Text()
- if answer == "y" {
- customer_menu()
- } else if answer == "n" {
- main_app()
- } else {
- fmt.Println("unrecognized input")
- os.Exit(0)
- }
- }
- //melihat data customer tertentu dari db
- func view_selectedCustomer() error {
- db := connectDb()
- scanner := bufio.NewScanner(os.Stdin)
- fmt.Print("Input Customer id to view : ")
- scanner.Scan()
- id, err := strconv.Atoi(scanner.Text())
- if err != nil {
- fmt.Println("customer id is invalid")
- fmt.Print("Try again?? (y/n) -> ")
- scanner.Scan()
- answer := scanner.Text()
- if answer == "y" {
- view_selectedCustomer()
- } else if answer == "n" {
- customer_menu()
- } else {
- fmt.Println("unrecognized input")
- os.Exit(0)
- }
- }
- id_viewCustomer, err := check_IDCustomer(id)
- if err != nil {
- return err
- }
- if !id_viewCustomer {
- fmt.Printf("customer id %d is not found\n", id)
- fmt.Print("Try again?? (y/n) -> ")
- scanner.Scan()
- answer := scanner.Text()
- if answer == "y" {
- view_selectedCustomer()
- } else if answer == "n" {
- customer_menu()
- } else {
- fmt.Println("unrecognized input")
- os.Exit(0)
- }
- }
- rows_Customer, err := db.Query("select * from customer where customer_id = $1", id)
- if err != nil {
- panic(err)
- } else {
- headers := []string{"Customer ID", "Customer Name", "Phone Number", "Customer Address", "Time Created", "Time Updated"}
- scanDest := []interface{}{new(int), new(string), new(string), new(string), new(string), new(string)}
- tableWriter := tablewriter.NewWriter(os.Stdout)
- tableWriter.SetHeader(headers)
- for rows_Customer.Next() {
- err := rows_Customer.Scan(scanDest...)
- if err != nil {
- panic(err)
- }
- row_Values := make([]string, len(scanDest))
- for i, dest := range scanDest {
- switch v := dest.(type) {
- case *int:
- row_Values[i] = strconv.Itoa(*v)
- case *string:
- row_Values[i] = *v
- default:
- row_Values[i] = fmt.Sprintf("%v", v)
- }
- }
- tableWriter.Append(row_Values)
- }
- tableWriter.Render()
- }
- fmt.Print("Back to previous menu?? (y/n) -> ")
- scanner.Scan()
- answer := scanner.Text()
- if answer == "y" {
- customer_menu()
- } else if answer == "n" {
- main_app()
- } else {
- fmt.Println("unrecognized input")
- os.Exit(0)
- }
- return nil
- }
- //detail proses mengedit customer
- func edit_customer() error {
- db := connectDb()
- scanner := bufio.NewScanner(os.Stdin)
- fmt.Print("Input Customer id to update : ")
- scanner.Scan()
- id, err := strconv.Atoi(scanner.Text())
- if err != nil {
- fmt.Println("customer id is invalid")
- fmt.Print("Try again?? (y/n) -> ")
- scanner.Scan()
- answer := scanner.Text()
- if answer == "y" {
- edit_customer()
- } else if answer == "n" {
- customer_menu()
- } else {
- fmt.Println("unrecognized input")
- os.Exit(0)
- }
- }
- id_Found, err := check_IDCustomer(id)
- if err != nil {
- return err
- }
- if !id_Found {
- fmt.Printf("customer id %d is not found\n", id)
- fmt.Print("Try again?? (y/n) -> ")
- scanner.Scan()
- answer := scanner.Text()
- if answer == "y" {
- edit_customer()
- } else if answer == "n" {
- customer_menu()
- } else {
- fmt.Println("unrecognized input")
- os.Exit(0)
- }
- }
- update_Customer := customer.Customer{
- Customer_id: id,
- }
- fmt.Print("Input New Customer name : ")
- scanner.Scan()
- update_Customer.Name = scanner.Text()
- fmt.Print("Input New Customer phone : ")
- scanner.Scan()
- update_Customer.Phone = scanner.Text()
- fmt.Print("Input New Customer address : ")
- scanner.Scan()
- update_Customer.Address = scanner.Text()
- fmt.Print("Are you sure to update this customer?? (y/n) -> ")
- scanner.Scan()
- confirm := scanner.Text()
- if confirm == "y" {
- new_Name := update_Customer.Name != ""
- new_Phone := update_Customer.Phone != ""
- new_Address := update_Customer.Address != ""
- process_Update := new_Name || new_Phone || new_Address
- if !process_Update {
- return nil
- }
- i := 2
- q := "update customer set "
- if new_Name {
- q += fmt.Sprintf("name = $%d", i)
- i++
- }
- if new_Phone {
- if i > 2 {
- q += fmt.Sprintf(",phone = $%d", i)
- } else {
- q += fmt.Sprintf("phone = $%d", i)
- }
- i++
- }
- if new_Address {
- if i > 2 {
- q += fmt.Sprintf(",address = $%d", i)
- } else {
- q += fmt.Sprintf("address = $%d", i)
- }
- i++
- }
- q += " ,updated_at = now() where customer_id = $1"
- arr := []any{}
- arr = append(arr, update_Customer.Customer_id)
- if new_Name {
- arr = append(arr, update_Customer.Name)
- }
- if new_Phone {
- arr = append(arr, update_Customer.Phone)
- }
- if new_Address {
- arr = append(arr, update_Customer.Address)
- }
- if _, err := db.Exec(q, arr...); err != nil {
- return err
- } else {
- fmt.Println("Current Customer has been updated to database successfully")
- fmt.Print("Edit current customer again?? (y/n) -> ")
- scanner.Scan()
- answer := scanner.Text()
- if answer == "y" {
- edit_customer()
- } else if answer == "n" {
- customer_menu()
- } else {
- fmt.Println("unrecognized input")
- os.Exit(0)
- }
- }
- } else {
- fmt.Println("failed to update customer")
- }
- return nil
- }
- //detail proses menghapus customer
- func delete_customer(new_order order.Order) error {
- db := connectDb()
- scanner := bufio.NewScanner(os.Stdin)
- fmt.Print("Input Customer id to delete : ")
- scanner.Scan()
- id, err := strconv.Atoi(scanner.Text())
- if err != nil {
- fmt.Println("Customer id is invalid")
- fmt.Print("Try again?? (y/n) -> ")
- scanner.Scan()
- answer := scanner.Text()
- if answer == "y" {
- delete_customer(new_order)
- } else if answer == "n" {
- customer_menu()
- } else {
- fmt.Println("unrecognized input")
- os.Exit(0)
- }
- }
- id_Found, err := check_IDCustomer(id)
- if err != nil {
- return err
- }
- if !id_Found {
- fmt.Printf("customer id %d is not found\n", id)
- fmt.Print("Try again?? (y/n) -> ")
- scanner.Scan()
- answer := scanner.Text()
- if answer == "y" {
- delete_customer(new_order)
- } else if answer == "n" {
- customer_menu()
- } else {
- fmt.Println("unrecognized input")
- os.Exit(0)
- }
- }
- fmt.Print("Are you sure to delete this customer?? (y/n) -> ")
- scanner.Scan()
- confirm := scanner.Text()
- if confirm == "y" {
- check_IDOrder(new_order.Order_id)
- if new_order.Completion_date.String() != "0001-01-01 00:00:00 +0000 UTC" {
- if _, err := db.Exec("delete from customer where customer_id = $1", id); err != nil {
- return err
- } else {
- fmt.Println("Current Customer has been deleted from database successfully")
- }
- } else {
- fmt.Printf("customer id %d is have order transaction, please delete order first\n", id)
- fmt.Print("Try again?? (y/n) -> ")
- scanner.Scan()
- answer := scanner.Text()
- if answer == "y" {
- delete_customer(new_order)
- } else if answer == "n" {
- customer_menu()
- } else {
- fmt.Println("unrecognized input")
- os.Exit(0)
- }
- }
- } else {
- fmt.Println("failed to delete customer")
- }
- fmt.Print("Delete current customer again?? (y/n) -> ")
- scanner.Scan()
- answer := scanner.Text()
- if answer == "y" {
- delete_customer(new_order)
- } else if answer == "n" {
- customer_menu()
- } else {
- fmt.Println("unrecognized input")
- os.Exit(0)
- }
- return nil
- }
- func add_service() error {
- db := connectDb()
- scanner := bufio.NewScanner(os.Stdin)
- new_Service := service.Service{}
- fmt.Print("Input Service id : ")
- scanner.Scan()
- new_Service.Service_id, _ = strconv.Atoi(scanner.Text())
- id_serviceExist, err := check_IDService(new_Service.Service_id)
- if err != nil {
- return err
- }
- if id_serviceExist {
- fmt.Printf("service id %d already exist\n", new_Service.Service_id)
- fmt.Print("Try again?? (y/n) -> ")
- scanner := bufio.NewScanner(os.Stdin)
- scanner.Scan()
- answer := scanner.Text()
- if answer == "y" {
- add_service()
- } else if answer == "n" {
- service_menu()
- } else {
- fmt.Println("unrecognized input")
- os.Exit(0)
- }
- }
- if new_Service.Service_id == 0 {
- fmt.Println("service id is invalid")
- fmt.Print("Try again?? (y/n) -> ")
- scanner.Scan()
- answer := scanner.Text()
- if answer == "y" {
- add_service()
- } else if answer == "n" {
- service_menu()
- } else {
- fmt.Println("unrecognized input")
- os.Exit(0)
- }
- }
- fmt.Print("Input Service name : ")
- scanner.Scan()
- new_Service.Service_name = scanner.Text()
- fmt.Print("Input Service Unit : ")
- scanner.Scan()
- new_Service.Unit = scanner.Text()
- fmt.Print("Input Service Price : ")
- scanner.Scan()
- new_Service.Price, _ = strconv.Atoi(scanner.Text())
- fmt.Print("Are you sure to add this service?? (y/n) -> ")
- scanner.Scan()
- confirm := scanner.Text()
- if confirm == "y" {
- new_Service = service.Service{
- Service_id: new_Service.Service_id,
- Service_name: new_Service.Service_name,
- Unit: new_Service.Unit,
- Price: new_Service.Price,
- }
- 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 {
- } else {
- fmt.Println("New Service has been added to database successfully")
- }
- } else {
- fmt.Println("failed to add new service")
- }
- fmt.Print("Input new service again?? (y/n) -> ")
- scanner.Scan()
- answer := scanner.Text()
- if answer == "y" {
- add_service()
- } else if answer == "n" {
- service_menu()
- } else {
- fmt.Println("unrecognized input")
- os.Exit(0)
- }
- return nil
- }
- func view_service() {
- scanner := bufio.NewScanner(os.Stdin)
- db := connectDb()
- rows_Service, err := db.Query("select * from service order by service_id asc")
- if err != nil {
- fmt.Println(err)
- } else {
- headers := []string{"Service ID", "Service Name", "Service Unit", "Service Price", "Time Created", "Time Updated"}
- scanDest := []interface{}{new(int), new(string), new(string), new(int), new(string), new(string)}
- tableWriter := tablewriter.NewWriter(os.Stdout)
- tableWriter.SetHeader(headers)
- for rows_Service.Next() {
- err := rows_Service.Scan(scanDest...)
- if err != nil {
- panic(err)
- }
- row_Values := make([]string, len(scanDest))
- for i, dest := range scanDest {
- switch v := dest.(type) {
- case *int:
- row_Values[i] = strconv.Itoa(*v)
- case *string:
- row_Values[i] = *v
- default:
- row_Values[i] = fmt.Sprintf("%v", v)
- }
- }
- tableWriter.Append(row_Values)
- }
- tableWriter.Render()
- }
- fmt.Print("Back to previous menu?? (y/n)")
- scanner.Scan()
- answer := scanner.Text()
- if answer == "y" {
- service_menu()
- } else if answer == "n" {
- main_app()
- } else {
- fmt.Println("unrecognized input")
- os.Exit(0)
- }
- }
- func view_selectedService() error {
- db := connectDb()
- scanner := bufio.NewScanner(os.Stdin)
- fmt.Print("Input Service Id to view : ")
- scanner.Scan()
- id, err := strconv.Atoi(scanner.Text())
- if err != nil {
- fmt.Println("Service id is invalid")
- fmt.Print("Try again?? (y/n) -> ")
- scanner.Scan()
- answer := scanner.Text()
- if answer == "y" {
- view_selectedService()
- } else if answer == "n" {
- service_menu()
- } else {
- fmt.Println("unrecognized input")
- os.Exit(0)
- }
- }
- id_ViewService, err := check_IDService(id)
- if err != nil {
- return err
- }
- if !id_ViewService {
- fmt.Printf("service id %d is not found\n", id)
- fmt.Print("Try again?? (y/n) -> ")
- scanner.Scan()
- answer := scanner.Text()
- if answer == "y" {
- view_selectedService()
- } else if answer == "n" {
- service_menu()
- } else {
- fmt.Println("unrecognized input")
- os.Exit(0)
- }
- }
- rows_Service, err := db.Query("select * from service where service_id = $1", id)
- if err != nil {
- panic(err)
- } else {
- headers := []string{"Service ID", "Service Name", "Service Unit", "Service Price", "Time Created", "Time Updated"}
- scanDest := []interface{}{new(int), new(string), new(string), new(int), new(string), new(string)}
- tableWriter := tablewriter.NewWriter(os.Stdout)
- tableWriter.SetHeader(headers)
- for rows_Service.Next() {
- err := rows_Service.Scan(scanDest...)
- if err != nil {
- panic(err)
- }
- row_Values := make([]string, len(scanDest))
- for i, dest := range scanDest {
- switch v := dest.(type) {
- case *int:
- row_Values[i] = strconv.Itoa(*v)
- case *string:
- row_Values[i] = *v
- default:
- row_Values[i] = fmt.Sprintf("%v", v)
- }
- }
- tableWriter.Append(row_Values)
- }
- tableWriter.Render()
- }
- fmt.Print("Back to previous menu?? (y/n) -> ")
- scanner.Scan()
- answer := scanner.Text()
- if answer == "y" {
- service_menu()
- } else if answer == "n" {
- main_app()
- } else {
- fmt.Println("unrecognized input")
- os.Exit(0)
- }
- return nil
- }
- func edit_service() error {
- db := connectDb()
- scanner := bufio.NewScanner(os.Stdin)
- fmt.Print("Input Service id to update : ")
- scanner.Scan()
- id, err := strconv.Atoi(scanner.Text())
- if err != nil {
- fmt.Println("Service id is invalid")
- fmt.Print("Try again?? (y/n) -> ")
- scanner.Scan()
- answer := scanner.Text()
- if answer == "y" {
- edit_service()
- } else if answer == "n" {
- service_menu()
- } else {
- fmt.Println("unrecognized input")
- os.Exit(0)
- }
- }
- id_Found, err := check_IDService(id)
- if err != nil {
- return err
- }
- if !id_Found {
- fmt.Printf("service id %d is not found\n", id)
- fmt.Print("Try again?? (y/n) -> ")
- scanner.Scan()
- answer := scanner.Text()
- if answer == "y" {
- edit_service()
- } else if answer == "n" {
- service_menu()
- } else {
- fmt.Println("unrecognized input")
- os.Exit(0)
- }
- }
- update_Service := service.Service{
- Service_id: id,
- }
- fmt.Print("Input New Service Name : ")
- scanner.Scan()
- update_Service.Service_name = scanner.Text()
- fmt.Print("Input New Service Unit : ")
- scanner.Scan()
- update_Service.Unit = scanner.Text()
- fmt.Print("Input New Service Price : ")
- scanner.Scan()
- price := scanner.Text()
- fmt.Print("Are you sure to update this service?? (y/n) -> ")
- scanner.Scan()
- confirm := scanner.Text()
- if confirm == "y" {
- new_ServiceName := update_Service.Service_name != ""
- new_Unit := update_Service.Unit != ""
- new_Price := price != ""
- process_Update := new_ServiceName || new_Unit || new_Price
- if !process_Update {
- return nil
- }
- i := 2
- q := "update service set "
- if new_ServiceName {
- q += fmt.Sprintf("service_name = $%d", i)
- i++
- }
- if new_Unit {
- if i > 2 {
- q += fmt.Sprintf(",unit = $%d", i)
- } else {
- q += fmt.Sprintf("unit = $%d", i)
- }
- i++
- }
- if new_Price {
- p, err := strconv.Atoi(price)
- if err != nil {
- return fmt.Errorf("invalid price %w", err)
- }
- update_Service.Price = p
- if i > 2 {
- q += fmt.Sprintf(",price = $%d", i)
- } else {
- q += fmt.Sprintf("price = $%d", i)
- }
- i++
- }
- q += " ,updated_at = now() where service_id = $1"
- arr := []any{}
- arr = append(arr, update_Service.Service_id)
- if new_ServiceName {
- arr = append(arr, update_Service.Service_name)
- }
- if new_Unit {
- arr = append(arr, update_Service.Unit)
- }
- if new_Price {
- arr = append(arr, update_Service.Price)
- }
- if _, err := db.Exec(q, arr...); err != nil {
- return err
- } else {
- fmt.Println("Current Service has been updated to database successfully")
- fmt.Print("Edit current service again?? (y/n) -> ")
- scanner.Scan()
- answer := scanner.Text()
- if answer == "y" {
- edit_service()
- } else if answer == "n" {
- service_menu()
- } else {
- fmt.Println("unrecognized input")
- os.Exit(0)
- }
- }
- } else {
- fmt.Println("failed to update service")
- }
- return nil
- }
- func delete_service(new_order order.Order, new_detailOrder order_detail.Order_detail) error {
- db := connectDb()
- scanner := bufio.NewScanner(os.Stdin)
- fmt.Print("Input Service id to delete : ")
- scanner.Scan()
- id, err := strconv.Atoi(scanner.Text())
- if err != nil {
- fmt.Println("Service id is invalid")
- fmt.Print("Try again?? (y/n) -> ")
- scanner.Scan()
- answer := scanner.Text()
- if answer == "y" {
- delete_service(new_order, new_detailOrder)
- } else if answer == "n" {
- service_menu()
- } else {
- fmt.Println("unrecognized input")
- os.Exit(0)
- }
- }
- id_Found, err := check_IDService(id)
- if err != nil {
- return err
- }
- if !id_Found {
- fmt.Printf("service id %d is not found\n", id)
- fmt.Print("Try again?? (y/n) -> ")
- scanner.Scan()
- answer := scanner.Text()
- if answer == "y" {
- delete_service(new_order, new_detailOrder)
- } else if answer == "n" {
- service_menu()
- } else {
- fmt.Println("unrecognized input")
- os.Exit(0)
- }
- }
- fmt.Print("Are you sure to delete this service?? (y/n) -> ")
- scanner.Scan()
- confirm := scanner.Text()
- if confirm == "y" {
- check_IDDetailOrder(id)
- if new_order.Completion_date.String() == "0001-01-01 00:00:00 +0000 UTC" {
- fmt.Printf("service id %d is used in order transaction, please delete order first\n", id)
- fmt.Print("Try again?? (y/n) -> ")
- scanner.Scan()
- answer := scanner.Text()
- if answer == "y" {
- delete_service(new_order, new_detailOrder)
- } else if answer == "n" {
- service_menu()
- } else {
- fmt.Println("unrecognized input")
- os.Exit(0)
- }
- } else {
- if _, err := db.Exec("delete from service where service_id = $1", id); err != nil {
- return err
- } else {
- fmt.Println("Current Service has been deleted from database successfully")
- }
- }
- } else {
- fmt.Println("failed to delete service")
- }
- fmt.Print("Delete current service again?? (y/n) -> ")
- scanner.Scan()
- answer := scanner.Text()
- if answer == "y" {
- delete_service(new_order, new_detailOrder)
- } else if answer == "n" {
- service_menu()
- } else {
- fmt.Println("unrecognized input")
- os.Exit(0)
- }
- return nil
- }
- func add_order(new_Customer customer.Customer, new_Service service.Service) error {
- scanner := bufio.NewScanner(os.Stdin)
- new_order := order.Order{}
- new_detailOrder := order_detail.Order_detail{}
- fmt.Print("Input Order id : ")
- scanner.Scan()
- new_order.Order_id, _ = strconv.Atoi(scanner.Text())
- id_orderExist, err := check_IDOrder(new_order.Order_id)
- if err != nil {
- return err
- }
- if id_orderExist {
- fmt.Printf("order id %d already exist\n", new_order.Order_id)
- fmt.Print("Try again?? (y/n) -> ")
- scanner := bufio.NewScanner(os.Stdin)
- scanner.Scan()
- answer := scanner.Text()
- if answer == "y" {
- add_order(new_Customer, new_Service)
- } else if answer == "n" {
- order_menu()
- } else {
- fmt.Println("unrecognized input")
- os.Exit(0)
- }
- }
- if new_order.Order_id == 0 {
- fmt.Println("order id is invalid")
- fmt.Print("Try again?? (y/n) -> ")
- scanner.Scan()
- answer := scanner.Text()
- if answer == "y" {
- add_order(new_Customer, new_Service)
- } else if answer == "n" {
- order_menu()
- } else {
- fmt.Println("unrecognized input")
- os.Exit(0)
- }
- }
- fmt.Print("Input Customer id : ")
- scanner.Scan()
- new_Customer.Customer_id, _ = strconv.Atoi(scanner.Text())
- id_Found, err := check_IDCustomer(new_Customer.Customer_id)
- if err != nil {
- return err
- }
- if !id_Found {
- fmt.Printf("customer id %d is not found\n", new_Customer.Customer_id)
- fmt.Print("Try again?? (y/n) -> ")
- scanner.Scan()
- answer := scanner.Text()
- if answer == "y" {
- add_order(new_Customer, new_Service)
- } else if answer == "n" {
- order_menu()
- } else {
- fmt.Println("unrecognized input")
- os.Exit(0)
- }
- }
- if new_Customer.Customer_id == 0 {
- fmt.Println("customer id is invalid")
- fmt.Print("Try again?? (y/n) -> ")
- scanner.Scan()
- answer := scanner.Text()
- if answer == "y" {
- add_order(new_Customer, new_Service)
- } else if answer == "n" {
- order_menu()
- } else {
- fmt.Println("unrecognized input")
- os.Exit(0)
- }
- }
- fmt.Print("Input Service id : ")
- scanner.Scan()
- new_Service.Service_id, _ = strconv.Atoi(scanner.Text())
- id_Found, err = check_IDService(new_Service.Service_id)
- if err != nil {
- return err
- }
- if !id_Found {
- fmt.Printf("service id %d is not found\n", new_Service.Service_id)
- fmt.Print("Try again?? (y/n) -> ")
- scanner.Scan()
- answer := scanner.Text()
- if answer == "y" {
- add_order(new_Customer, new_Service)
- } else if answer == "n" {
- order_menu()
- } else {
- fmt.Println("unrecognized input")
- os.Exit(0)
- }
- }
- if new_Service.Service_id == 0 {
- fmt.Println("service id is invalid")
- fmt.Print("Try again?? (y/n) -> ")
- scanner.Scan()
- answer := scanner.Text()
- if answer == "y" {
- add_order(new_Customer, new_Service)
- } else if answer == "n" {
- order_menu()
- } else {
- fmt.Println("unrecognized input")
- os.Exit(0)
- }
- }
- fmt.Print("Input Quantity : ")
- scanner.Scan()
- new_detailOrder.Qty, _ = strconv.Atoi(scanner.Text())
- var data, layoutFormat string
- layoutFormat = "2006-01-02 15:04:05"
- fmt.Print("Input Order Date (yyyy-mm-dd hh:mm:ss) : ")
- scanner.Scan()
- data = scanner.Text()
- new_order.Order_date, _ = time.Parse(layoutFormat, data)
- fmt.Print("Input Received by : ")
- scanner.Scan()
- new_order.Received_by = scanner.Text()
- new_order = order.Order{
- Order_id: new_order.Order_id,
- Customer_id: new_Customer.Customer_id,
- Order_date: new_order.Order_date,
- Received_by: new_order.Received_by,
- }
- new_detailOrder = order_detail.Order_detail{
- Order_id: new_order.Order_id,
- Service_id: new_Service.Service_id,
- Qty: new_detailOrder.Qty,
- }
- fmt.Print("Are you sure to add this order?? (y/n) -> ")
- scanner.Scan()
- confirm := scanner.Text()
- if confirm == "y" {
- exec(new_order, new_Customer, new_Service, new_detailOrder)
- } else {
- fmt.Println("failed to add new order")
- }
- fmt.Print("Input new order again?? (y/n)")
- scanner.Scan()
- answer := scanner.Text()
- if answer == "y" {
- add_order(new_Customer, new_Service)
- } else if answer == "n" {
- order_menu()
- } else {
- fmt.Println("unrecognized input")
- os.Exit(0)
- }
- return nil
- }
- func exec(new_order order.Order, new_Customer customer.Customer, new_Service service.Service, new_detailOrder order_detail.Order_detail) {
- db := connectDb()
- defer db.Close()
- tx, err := db.Begin()
- if err != nil {
- fmt.Println(err)
- }
- insert_Order(new_order, new_Customer, tx)
- insert_DetailOrder(new_detailOrder, new_Service, tx)
- err = tx.Commit()
- if err != nil {
- fmt.Println(err)
- } else {
- fmt.Println("OK! Committed!")
- fmt.Println("New Order has been added to database successfully :)")
- }
- }
- func insert_Order(new_order order.Order, new_Customer customer.Customer, tx *sql.Tx) {
- 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')"
- _, err := tx.Exec(sql_statement, new_order.Order_id, new_Customer.Customer_id, new_order.Order_date, new_order.Received_by)
- validate_Orders(err, "insert", tx)
- }
- func insert_DetailOrder(new_detailOrder order_detail.Order_detail, new_Service service.Service, tx *sql.Tx) {
- sql_statement := "insert into order_detail (order_id,service_id,qty) values ($1,$2,$3)"
- _, err := tx.Exec(sql_statement, new_detailOrder.Order_id, new_Service.Service_id, new_detailOrder.Qty)
- validate_detailOrders(err, "insert", tx)
- }
- func validate_Orders(err error, message string, tx *sql.Tx) error {
- if err != nil {
- tx.Rollback()
- fmt.Println(err, "Failed! Rollback!")
- } else {
- fmt.Println("OK! " + message + " order query no issue")
- }
- return nil
- }
- func validate_detailOrders(err error, message string, tx *sql.Tx) error {
- if err != nil {
- tx.Rollback()
- fmt.Println(err, "Failed! Rollback!")
- } else {
- fmt.Println("OK! " + message + " detail order query no issue")
- }
- return nil
- }
- func view_order() {
- scanner := bufio.NewScanner(os.Stdin)
- db := connectDb()
- 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")
- if err != nil {
- fmt.Println(err)
- } else {
- headers := []string{"Order ID", "Customer ID", "Service ID", "Order Date", "Quantity", "Received By", "Time Created", "Time Updated", "Completion Date"}
- scanDest := []interface{}{new(int), new(int), new(int), new(string), new(int), new(string), new(string), new(string), new(string)}
- tableWriter := tablewriter.NewWriter(os.Stdout)
- tableWriter.SetHeader(headers)
- for rows.Next() {
- err := rows.Scan(scanDest...)
- if err != nil {
- panic(err)
- }
- rowValues := make([]string, len(scanDest))
- for i, dest := range scanDest {
- switch v := dest.(type) {
- case *int:
- rowValues[i] = strconv.Itoa(*v)
- case *string:
- rowValues[i] = *v
- default:
- rowValues[i] = fmt.Sprintf("%v", v)
- }
- }
- tableWriter.Append(rowValues)
- }
- tableWriter.Render()
- }
- fmt.Print("Back to previous menu?? (y/n)")
- scanner.Scan()
- answer := scanner.Text()
- if answer == "y" {
- order_menu()
- } else if answer == "n" {
- main_app()
- } else {
- fmt.Println("unrecognized input")
- os.Exit(0)
- }
- }
- func view_selectedOrder() error {
- db := connectDb()
- scanner := bufio.NewScanner(os.Stdin)
- fmt.Print("Input Order Detail Id to view : ")
- scanner.Scan()
- id, err := strconv.Atoi(scanner.Text())
- if err != nil {
- fmt.Println("Order detail id is invalid")
- fmt.Print("Try again?? (y/n) -> ")
- scanner.Scan()
- answer := scanner.Text()
- if answer == "y" {
- view_selectedOrder()
- } else if answer == "n" {
- order_menu()
- } else {
- fmt.Println("unrecognized input")
- os.Exit(0)
- }
- }
- id_ViewOrder, err := check_IDOrder(id)
- if err != nil {
- return err
- }
- if !id_ViewOrder {
- fmt.Printf("Order Detail id %d is not found\n", id)
- fmt.Print("Try again?? (y/n) -> ")
- scanner.Scan()
- answer := scanner.Text()
- if answer == "y" {
- view_selectedOrder()
- } else if answer == "n" {
- order_menu()
- } else {
- fmt.Println("unrecognized input")
- os.Exit(0)
- }
- }
- 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)
- if err != nil {
- panic(err)
- } else {
- headers := []string{"Order ID", "Customer ID", "Service ID", "Order Date", "Quantity", "Received By", "Time Created", "Time Updated", "Completion Date"}
- scanDest := []interface{}{new(int), new(int), new(int), new(string), new(int), new(string), new(string), new(string), new(string)}
- tableWriter := tablewriter.NewWriter(os.Stdout)
- tableWriter.SetHeader(headers)
- for rows_Order.Next() {
- err := rows_Order.Scan(scanDest...)
- if err != nil {
- panic(err)
- }
- row_Values := make([]string, len(scanDest))
- for i, dest := range scanDest {
- switch v := dest.(type) {
- case *int:
- row_Values[i] = strconv.Itoa(*v)
- case *string:
- row_Values[i] = *v
- default:
- row_Values[i] = fmt.Sprintf("%v", v)
- }
- }
- tableWriter.Append(row_Values)
- }
- tableWriter.Render()
- }
- fmt.Print("Back to previous menu?? (y/n) -> ")
- scanner.Scan()
- answer := scanner.Text()
- if answer == "y" {
- order_menu()
- } else if answer == "n" {
- main_app()
- } else {
- fmt.Println("unrecognized input")
- os.Exit(0)
- }
- return nil
- }
- func complete_order(new_order order.Order) error {
- db := connectDb()
- scanner := bufio.NewScanner(os.Stdin)
- fmt.Print("Input Order id to complete : ")
- scanner.Scan()
- id, err := strconv.Atoi(scanner.Text())
- if err != nil {
- fmt.Println("Service id is invalid")
- fmt.Print("Try again?? (y/n) -> ")
- scanner.Scan()
- answer := scanner.Text()
- if answer == "y" {
- edit_service()
- } else if answer == "n" {
- service_menu()
- } else {
- fmt.Println("unrecognized input")
- os.Exit(0)
- }
- }
- id_Found, err := check_IDService(id)
- if err != nil {
- return err
- }
- if !id_Found {
- fmt.Printf("service id %d is not found\n", id)
- fmt.Print("Try again?? (y/n) -> ")
- scanner.Scan()
- answer := scanner.Text()
- if answer == "y" {
- edit_service()
- } else if answer == "n" {
- service_menu()
- } else {
- fmt.Println("unrecognized input")
- os.Exit(0)
- }
- }
- var data, layoutFormat string
- layoutFormat = "2006-01-02 15:04:05"
- fmt.Print("Input Completion Date (yyyy-mm-dd hh:mm:ss) : ")
- scanner.Scan()
- data = scanner.Text()
- new_order.Completion_date, _ = time.Parse(layoutFormat, data)
- fmt.Print("Are you sure to update this order?? (y/n) -> ")
- scanner.Scan()
- confirm := scanner.Text()
- if confirm == "y" {
- new_order = order.Order{
- Order_id: id,
- Completion_date: new_order.Completion_date,
- }
- 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 {
- return err
- } else {
- fmt.Println("Current Order has been updated to database successfully, order completed!")
- fmt.Print("Complete current order again?? (y/n) -> ")
- scanner.Scan()
- answer := scanner.Text()
- if answer == "y" {
- complete_order(new_order)
- } else if answer == "n" {
- order_menu()
- } else {
- fmt.Println("unrecognized input")
- os.Exit(0)
- }
- }
- } else {
- fmt.Println("failed to update order")
- }
- return nil
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement