-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
317 lines (267 loc) · 9.24 KB
/
main.go
File metadata and controls
317 lines (267 loc) · 9.24 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package main
import (
"database/sql"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"strconv"
_ "github.com/mattn/go-sqlite3"
)
// Person struct to represent a person in the 'persons' table
type Person struct {
ID *int `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
Mobile string `json:"mobile"`
}
var db *sql.DB // Declare the variable to hold the database connection
// Function to create the 'persons' table
func CreatePersonsTable(db *sql.DB) error {
const query = `
CREATE TABLE IF NOT EXISTS persons (
id INTEGER NOT NULL PRIMARY KEY,
name CHAR(40) NOT NULL,
email CHAR(50),
mobile CHAR(25) NOT NULL
);`
_, err := db.Exec(query)
if err != nil {
log.Printf("Error creating 'persons' table: %v", err)
}
return err
}
// Function to insert a person into the 'persons' table
// the id will be insert auto in sqlite without need to insert it
func InsertPerson(db *sql.DB, person Person) (sql.Result, error) {
insertDataQuery := `
INSERT INTO persons (name, email, mobile)
VALUES (?, ?, ?);`
result, err := db.Exec(insertDataQuery, person.Name, person.Email, person.Mobile)
if err != nil {
log.Printf("Error inserting person: %v", err)
}
return result, err
}
// Function to get all persons from the 'persons' table
func GetAllPersons(db *sql.DB) ([]Person, error) {
const query = `SELECT id, name, email, mobile FROM persons;`
rows, err := db.Query(query)
if err != nil {
log.Printf("Error getting all persons: %v", err)
return nil, err
}
defer rows.Close()
var persons []Person
for rows.Next() {
var p Person
if err := rows.Scan(&p.ID, &p.Name, &p.Email, &p.Mobile); err != nil {
log.Printf("Error scanning person: %v", err)
return nil, err
}
persons = append(persons, p)
}
return persons, nil
}
// Function to get a person by ID from the 'persons' table
func GetPersonByID(db *sql.DB, id int) (Person, error) {
const query = `SELECT id, name, email, mobile FROM persons WHERE id = ?;`
row := db.QueryRow(query, id)
var p Person
err := row.Scan(&p.ID, &p.Name, &p.Email, &p.Mobile)
if err != nil {
log.Printf("Error getting person by ID: %v", err)
return Person{}, err
}
return p, nil
}
// Function to update a person in the 'persons' table
func UpdatePerson(db *sql.DB, person Person) (sql.Result, error) {
updateDataQuery := `
UPDATE persons
SET name=?, email=?, mobile=?
WHERE id=?;`
// Check if ID is nil
if person.ID == nil {
return nil, fmt.Errorf("person ID is nil, cannot update")
}
fmt.Printf("Update Query: %s\n", updateDataQuery)
fmt.Printf("Person ID: %d\n", *person.ID)
result, err := db.Exec(updateDataQuery, person.Name, person.Email, person.Mobile, *person.ID)
if err != nil {
log.Printf("Error updating person: %v", err)
return nil, fmt.Errorf("failed to update person with ID %d: %v", *person.ID, err)
}
return result, nil
}
// Function to delete a person from the 'persons' table
func DeletePerson(db *sql.DB, id int) (sql.Result, error) {
deleteDataQuery := `
DELETE FROM persons
WHERE id=?;`
result, err := db.Exec(deleteDataQuery, id)
if err != nil {
log.Printf("Error deleting person: %v", err)
}
return result, err
}
// Function to check the status of the application
func checkHandler(w http.ResponseWriter, r *http.Request) {
// Respond with a success message
status := map[string]string{"status": "OK"}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(status)
// Print a message indicating that the "/check" endpoint is handled
fmt.Println("Check endpoint handled successfully.")
}
// Function to handle requests for getting a person by ID
func getPersonHandler(w http.ResponseWriter, r *http.Request) {
// Parse the request URL to get the person ID
personID, err := strconv.Atoi(r.URL.Query().Get("id"))
if err != nil {
http.Error(w, "Invalid person ID", http.StatusBadRequest)
return
}
// Get the person by ID from the 'persons' table
person, err := GetPersonByID(db, personID)
if err != nil {
http.Error(w, "Person not found", http.StatusNotFound)
return
}
// Respond with the person details
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(person)
}
// Function to handle requests for listing all persons
func listPersonsHandler(w http.ResponseWriter, r *http.Request) {
// Get all persons from the 'persons' table
persons, err := GetAllPersons(db)
if err != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
// Respond with the list of persons as JSON
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(persons)
}
// Function to handle requests for creating a new person
func createPersonHandler(w http.ResponseWriter, r *http.Request) {
// Parse the request body to get the new person data
var newPerson Person
err := json.NewDecoder(r.Body).Decode(&newPerson)
if err != nil {
http.Error(w, "Invalid JSON data", http.StatusBadRequest)
return
}
// Insert the new person into the 'persons' table
_, err = InsertPerson(db, newPerson)
if err != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
// Respond with a success message
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, `{"message": "Person created successfully"}`)
}
// Function to handle requests for updating a person
func updatePersonHandler(w http.ResponseWriter, r *http.Request) {
// Parse the request URL to get the person ID
personID, err := strconv.Atoi(r.URL.Query().Get("id"))
if err != nil {
http.Error(w, "Invalid person ID", http.StatusBadRequest)
return
}
fmt.Printf("Updating person with ID: %d\n", personID) // Add this print statement
// Get the existing person from the 'persons' table
existingPerson, err := GetPersonByID(db, personID)
if err != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
fmt.Printf("Existing person: %+v\n", existingPerson) // Add this print statement
// Parse the request body to get the updated person data
var updatedPerson Person
err = json.NewDecoder(r.Body).Decode(&updatedPerson)
if err != nil {
http.Error(w, "Invalid JSON data", http.StatusBadRequest)
return
}
fmt.Printf("Updated person data: %+v\n", updatedPerson) // Add this print statement
// Update the person in the 'persons' table
_, err = UpdatePerson(db, updatedPerson)
if err != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
// Respond with a success message
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, `{"message": "Person updated successfully"}`)
}
// Function to handle requests for deleting a person
func deletePersonHandler(w http.ResponseWriter, r *http.Request) {
// Parse the request URL to get the person ID
personID, err := strconv.Atoi(r.URL.Query().Get("id"))
if err != nil {
http.Error(w, "Invalid person ID", http.StatusBadRequest)
return
}
// Delete the person from the 'persons' table
_, err = DeletePerson(db, personID)
if err != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}
// Respond with a success message
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, `{"message": "Person deleted successfully"}`)
}
func main() {
// Open the database connection
var err error
db, err = sql.Open("sqlite3", "myDataBase.db")
if err != nil {
log.Fatalf("Error opening the database: %v", err)
}
defer db.Close()
// Create the 'persons' table
err = CreatePersonsTable(db)
if err != nil {
log.Fatalf("Error creating 'persons' table: %v", err)
}
// Define HTTP endpoints and corresponding handlers
http.HandleFunc("/check", checkHandler)
http.HandleFunc("/list-persons", listPersonsHandler)
http.HandleFunc("/get-person", getPersonHandler)
http.HandleFunc("/create-person", createPersonHandler)
http.HandleFunc("/update-person", updatePersonHandler)
http.HandleFunc("/delete-person", deletePersonHandler)
// Define default port
defaultPort := "8081"
// Check if a command-line argument is provided
if len(os.Args) == 3 && os.Args[1] == "serve" {
port := os.Args[2]
fmt.Printf("Starting HTTP server on port %s...\n", port)
log.Fatal(http.ListenAndServe(":"+port, nil))
} else {
fmt.Printf("Starting HTTP server on default port %s...\n", defaultPort)
log.Fatal(http.ListenAndServe(":"+defaultPort, nil))
}
}
/*
To run this code you should run without debbiging to make the server open the port and then you could
build an execusion file by go build in the terminal
After we test our code by this cmd :
for Check Endpoint:
curl http://localhost:8081/check // you could change the port by 8080 for exemple
List Persons Endpoint:
curl http://localhost:8081/list-persons
Get Person by ID (Replace 1 with the desired ID):
curl http://localhost:8081/get-person?id=1
Create Person:
curl -X POST -H "Content-Type: application/json" -d '{"name": "Rayene Amina", "email": "Rayene@gmail.com", "mobile": "05842154"}' http://localhost:8081/create-person
Update Person (Replace 1 with the desired ID):
curl -X POST -H "Content-Type: application/json" -d '{"id": 1, "name": "Rayene Amina", "email": "Brayeneamina18@gmail.com", "mobile": "05842154"}' http://localhost:8081/update-person?id=1
Delete Person (Replace 1 with the desired ID):
curl -X POST http://localhost:8081/delete-person?id=1
*/