diff --git a/beginner/CRUD_API/go.mod b/beginner/CRUD_API/go.mod new file mode 100644 index 0000000..6d56b01 --- /dev/null +++ b/beginner/CRUD_API/go.mod @@ -0,0 +1,15 @@ +module golangexample/sampleapp + +go 1.19 + +require ( + github.com/gorilla/mux v1.8.0 + github.com/jinzhu/gorm v1.9.16 + gorm.io/gorm v1.24.0 +) + +require ( + github.com/jinzhu/inflection v1.0.0 // indirect + github.com/jinzhu/now v1.1.4 // indirect + github.com/mattn/go-sqlite3 v1.14.15 // indirect +) diff --git a/beginner/CRUD_API/main.go b/beginner/CRUD_API/main.go new file mode 100644 index 0000000..bf7276e --- /dev/null +++ b/beginner/CRUD_API/main.go @@ -0,0 +1,25 @@ +package main + +import ( + "log" + "net/http" + + "github.com/gorilla/mux" +) + +func initializeRouter() { + r := mux.NewRouter() + + r.HandleFunc("/users", GetUsers).Methods("GET") + r.HandleFunc("/users/{id}", GetUser).Methods("GET") + r.HandleFunc("/users", CreateUser).Methods("POST") + r.HandleFunc("/users/{id}", UpdateUser).Methods("PUT") + r.HandleFunc("/users/{id}", DeleteUser).Methods("DELETE") + + log.Fatal(http.ListenAndServe(":9001", r)) +} + +func main() { + IntialMigration() + initializeRouter() +} diff --git a/beginner/CRUD_API/user.go b/beginner/CRUD_API/user.go new file mode 100644 index 0000000..9bbfdd1 --- /dev/null +++ b/beginner/CRUD_API/user.go @@ -0,0 +1,95 @@ +package main + +import ( + "encoding/json" + "net/http" + + "github.com/gorilla/mux" + "github.com/jinzhu/gorm" + _ "github.com/jinzhu/gorm/dialects/sqlite" +) + + +var DB *gorm.DB +var err error + + +//Gorm is used to convert our struct into a model struct so that we can store date in db using ORM +type User struct{ + + gorm.Model + FirstName string `json:"first_name"` + LasName string `json:"last_name"` + Email string `json:"email"` + + +} + +func IntialMigration(){ + DB, err = gorm.Open("sqlite3", "user.db") + if err != nil { + panic("Failed to open the SQLite database.") + } + //defer DB.Close() + + // Create the table from our struct + DB.AutoMigrate(&User{}) + + +} + +//arguments includes the response writer and the a pointer pointing to the actual request that came in +func GetUsers(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + //slice + var users []User + DB.Find(&users) + json.NewEncoder(w).Encode(users) + +} + +func GetUser(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + var user User + params :=mux.Vars(r) + DB.First(&user,params["id"]) + json.NewEncoder(w).Encode(user) + +} + +func DeleteUser(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + var user User + params :=mux.Vars(r) + DB.Delete(&user,params["id"]) + json.NewEncoder(w).Encode("The user is deleted") + +} + +func CreateUser(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + var user User + err := json.NewDecoder(r.Body).Decode(&user) + if err!= nil { + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte(err.Error())) + return + } + DB.Create(&user) + json.NewEncoder(w).Encode(user) + + + +} + +func UpdateUser(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + var user User + params :=mux.Vars(r) + DB.First(&user,params["id"]) + json.NewDecoder(r.Body).Decode(&user) + DB.Save(&user) + json.NewEncoder(w).Encode(user) + + +} \ No newline at end of file