-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
158 lines (124 loc) · 3.85 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"bufio"
"context"
"fmt"
"log"
"os"
"strings"
"github.com/i1d9/postgres_connection-go/models"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/spf13/viper"
"strconv"
)
func createDatabaseConnectionPool() (*pgxpool.Pool, error) {
// Create a new connection pool
database_url := viper.GetString("DATABASE_URL")
dbpool, err := pgxpool.New(context.Background(), database_url)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to create connection pool: %v\n", err)
os.Exit(1)
}
return dbpool, err
}
func loadConfig() {
viper.SetConfigName(".env")
viper.AddConfigPath(".")
viper.AutomaticEnv()
viper.SetConfigType("env")
err := viper.ReadInConfig()
if err != nil {
log.Fatalf("Error reading config file, %s", err)
}
}
func getConsoleInput(message string) string {
reader := bufio.NewReader(os.Stdin)
fmt.Print(message)
text1, _ := reader.ReadString('\n')
text1 = strings.Replace(text1, "\n", "", -1)
return text1
}
func renderUsers(users []models.User) {
for i, user := range users {
fmt.Printf("User %d:\n", i+1)
fmt.Printf(" ID: %d\n", user.ID)
fmt.Printf(" Email: %s\n", models.NullableString(user.Email))
fmt.Printf(" Verified: %s\n", models.NullableBool(user.Verified))
fmt.Printf(" Active: %s\n", models.NullableBool(user.Active))
fmt.Printf(" Name: %s %s %s\n", models.NullableString(user.FirstName), models.NullableString(user.LastName), models.NullableString(user.Surname))
fmt.Printf(" Mobile: %s (Verified: %s)\n", models.NullableString(user.MobileNumber), models.NullableBool(user.MobileVerified))
fmt.Printf(" Gender: %s\n", models.NullableString(user.Gender))
fmt.Printf(" Created At: %s\n", models.NullableTime(user.CreatedAt))
fmt.Printf(" Updated At: %s\n", models.NullableTime(user.UpdatedAt))
fmt.Printf(" Last Login At: %s\n", models.NullableTime(user.LastLoginAt))
fmt.Printf(" Deleted At: %s\n", models.NullableTime(user.DeletedAt))
fmt.Println(" ---\n")
}
}
func listUsers(dbpool *pgxpool.Pool) {
users, err := models.GetAllUsers(dbpool)
if err != nil {
log.Fatalf("Error fetching users: %v\n", err)
}
renderUsers(users)
}
func main() {
loadConfig()
dbpool, _ := createDatabaseConnectionPool()
defer dbpool.Close()
for {
fmt.Println("\nUser Management CLI")
fmt.Println("---------------------")
mainInput := getConsoleInput("1. List\n2. Create\n3. Delete\n4. Search\n5. Exit\n-> ")
switch mainInput {
case "1":
listUsers(dbpool)
case "2":
firstName := getConsoleInput("Enter First Name: ")
lastName := getConsoleInput("Enter Last Name: ")
surname := getConsoleInput("Enter Surname: ")
email := getConsoleInput("Enter Email: ")
mobileNumber := getConsoleInput("Enter Mobile Number: ")
gender := getConsoleInput("Enter Gender: ")
user := models.User{
FirstName: &firstName,
LastName: &lastName,
Surname: &surname,
Email: &email,
MobileNumber: &mobileNumber,
Gender: &gender,
}
err := models.CreateUser(dbpool, user)
if err != nil {
log.Fatalf("Error creating user: %v\n", err)
} else {
fmt.Println("User created successfully")
}
case "3":
userID := getConsoleInput("\nEnter the User ID to be deleted\n-> ")
user_id, err := strconv.Atoi(userID)
if err != nil {
log.Fatalf("Error searching for users: %v\n", err)
}
result := models.DeleteUser(dbpool, user_id)
if result != nil {
log.Fatalf("Error deleting the specified user: %v\n", result)
}else
{
fmt.Println("User Deleted Successfully")
}
case "4":
searchTerm := getConsoleInput("\nSearch.\nWho are you looking for?\n-> ")
users, err := models.SearchUsers(dbpool, searchTerm)
if err != nil {
log.Fatalf("Error searching for users: %v\n", err)
}
fmt.Printf("\nFound %d users\n", len(users))
renderUsers(users)
case "5":
os.Exit(0)
case "q":
os.Exit(0)
}
}
}