DEV Community

Marcos Silva
Marcos Silva

Posted on

Building a Simple CRUD Application with Go: An Overview about the project

Introduction
In the start of this month, I assigned the task of developing the Age Viewer Go (Desktop) application. The purpose of this application is to provide a user-friendly interface for viewing and managing age-related data. With the use of Go programming language, we aimed to create a lightweight and efficient application that meets our specific requirements.
Functions

  • Create (C): The create operation involves adding new data to a system. It typically entails gathering user input, validating the data, and storing it in a database or another persistent storage medium. For instance, in a blog application, the create operation allows users to compose and publish new articles. Developers must ensure proper input validation, data integrity, and error handling to prevent erroneous or inconsistent data from being created.
func CreateUser(w http.ResponseWriter, r *http.Request) {
    // set the header to content type x-www-form-urlencoded
    // Allow all origin to handle cors issue
    w.Header().Set("Context-Type", "application/x-www-form-urlencoded")
    w.Header().Set("Access-Control-Allow-Origin", "*")
    w.Header().Set("Access-Control-Allow-Methods", "POST")
    w.Header().Set("Access-Control-Allow-Headers", "Content-Type")

    var user models.User

    err := json.NewDecoder(r.Body).Decode(&user)

    if err != nil {
        log.Fatalf("Unable to decode the request body.  %v", err)
    }

    insertID := insertUser(user)

    res := response{
        ID:      insertID,
        Message: "User created!!",
    }

    // send the response
    json.NewEncoder(w).Encode(res)
}
Enter fullscreen mode Exit fullscreen mode
  • Read(R) This function is an HTTP request handler that retrieves a user based on the provided user ID. It sets the necessary headers for the HTTP response, including the content type and access control. The function extracts the user ID from the request parameters, converts it to an integer, and retrieves the corresponding user using the getUser function. If an error occurs during the conversion or user retrieval, it logs an error message. Finally, it encodes the retrieved user as JSON and writes it as the response.
func GetUser(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Context-Type", "application/x-www-form-urlencoded")
    w.Header().Set("Access-Control-Allow-Origin", "*")
    // get the userid from the request params, key is "id"
    params := mux.Vars(r)

    id, err := strconv.Atoi(params["id"])

    if err != nil {
        log.Fatalf("Unable to convert the string.  %v", err)
    }

    user, err := getUser(int64(id))

    if err != nil {
        log.Fatalf("Cannot find the user. %v", err)
    }

    json.NewEncoder(w).Encode(user)
}
Enter fullscreen mode Exit fullscreen mode

This article will be divided into two or more parts for better comprehension. Thank you for reading this far.

References:

Top comments (0)