-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathdocument.go
115 lines (95 loc) · 2.41 KB
/
document.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
/*
The package works on 2 tables on a PostgreSQL data base server.
The names of the tables are:
* Users
* Userdata
The definitions of the tables in the PostgreSQL server are:
CREATE TABLE Users (
ID SERIAL,
Username VARCHAR(100) PRIMARY KEY
);
CREATE TABLE Userdata (
UserID Int NOT NULL,
Name VARCHAR(100),
Surname VARCHAR(100),
Description VARCHAR(200)
);
This is rendered as code
This is not rendered as code
*/
package document
// BUG(1): Function ListUsers() not working as expected
// BUG(2): Function AddUser() is too slow
import (
"database/sql"
"fmt"
"strings"
)
/*
This block of global variables holds the connection details to the Postgres server
Hostname: is the IP or the hostname of the server
Port: is the TCP port the DB server listens to
Username: is the username of the database user
Password: is the password of the database user
Database: is the name of the Database in PostgreSQL
*/
var (
Hostname = ""
Port = 2345
Username = ""
Password = ""
Database = ""
)
// The Userdata structure is for holding full user data
// from the Userdata table and the Username from the
// Users table
type Userdata struct {
ID int
Username string
Name string
Surname string
Description string
}
// openConnection() is for opening the Postgres connection
// in order to be used by the other functions of the package.
func openConnection() (*sql.DB, error) {
var db *sql.DB
return db, nil
}
// The function returns the User ID of the username
// -1 if the user does not exist
func exists(username string) int {
fmt.Println("Searching user", username)
return 0
}
// AddUser adds a new user to the database
//
// Returns new User ID
// -1 if there was an error
func AddUser(d Userdata) int {
d.Username = strings.ToLower(d.Username)
return -1
}
/*
DeleteUser deletes an existing user if the user exists.
It requires the User ID of the user to be deleted.
*/
func DeleteUser(id int) error {
fmt.Println(id)
return nil
}
// ListUsers lists all users in the database
// and returns a slice of Userdata.
func ListUsers() ([]Userdata, error) {
// Data holds the records returned by the SQL query
Data := []Userdata{}
return Data, nil
}
// UpdateUser is for updating an existing user
// given a Userdata structure.
// The user ID of the user to be updated is found
// inside the function.
func UpdateUser(d Userdata) error {
fmt.Println(d)
return nil
}