Golang serialize form-urlencoded to object - postgresql

I've made a post request where I'm sending data as JSON and this code creates a new row in the DB.
json.NewDecoder(r.Body).Decode(&user)
DB.Create(&user)
json.NewEncoder(w).Encode(user)
But parsing the form data shows this error
I figured this is how I would read every individual value
for key, value := range r.PostForm {
fmt.Printf("Key:%s, Value:%s\n", key, value)
}
My model looks like this
type User struct {
gorm.Model
FirstName string `json:"firstname"`
LastName string `json:"lastname"`
Email string `json:"email"`
}
How would I convert this to user and insert to DB?

Another way to do it (a bit overengineered, better to use r.FormValue), without having to type the fields again, such as r.FormValue("firstname"), etc if you already know the data, types etc.
func handleUser(w http.ResponseWriter, r *http.Request) {
user := User{}
userType := reflect.TypeOf(user)
for i := 0; i < userType.NumField(); i++ {
// Get the JSON tag of the field, use it for the r.FormValue
f := userType.Field(i).Tag.Get("json")
if f != "" {
reflect.ValueOf(&user).Elem().FieldByName(
userType.Field(i).Name).SetString(r.FormValue(f))
}
}
// DB.Create(&user) need a reference to DB here.
json.NewEncoder(w).Encode(user)
}
Note: This is just an example that will work for string fields, may panic otherwise because it's calling SetString. Additionally, a field may not exist and could panic when trying to access it in the FieldByName(..., but I think your question was geared to this direction of not having to type each field, so leaving it as an example.
The right way to do it, as Cerise Limón pointed out, would be
func handleUser(w http.ResponseWriter, r *http.Request) {
user := User{}
user.FirstName = r.FormValue("firstname")
user.LastName = r.FormValue("lastname")
user.Email = r.FormValue("email")
// DB.Create(&user) need a reference to DB here.
json.NewEncoder(w).Encode(user)
}
Note that you can also add a method for populating the user with the form if you do this in several places, for example
func (u *User) setFormData(r *http.Request) {
u.FirstName = r.FormValue("firstname")
u.LastName = r.FormValue("lastname")
u.Email = r.FormValue("email")
}
And then use it like this
func handleUser(w http.ResponseWriter, r *http.Request) {
user := User{}
user.setFormData(r)
// DB.Create(&user)
json.NewEncoder(w).Encode(user)
}

Related

How can I read/write an array of objects inside a PostgreSQL row? (GoLang)

I defined this types in GoLang:
type Comment struct {
Id int
User string
Email string
Date string
Comment string
}
type Post struct {
Id int
Special bool
Title string
Date string
Content string
Image string
Comments []Comment
}
I need to know how to modify this code:
OpenDB()
rows, _ := cn.Query(`SELECT id, date, title, special, content, image
FROM posts ORDER BY date DESC LIMIT $1
OFFSET $2`, fmt.Sprint(limit), fmt.Sprint(offset))
posts := []Post{}
for rows.Next() {
post := Post{}
e := rows.Scan(&post.Id, &post.Date, &post.Title,
&post.Special, &post.Content, &post.Image)
if e != nil {
panic(e)
}
posts = append(posts, post)
}
To allow reading comments. And also, how I can modify:
OpenDB()
_, e = cn.Exec(`INSERT INTO
posts(date, title, special, content, image)
VALUES ($1, $2, $3, $4, $5)`, date, title, special, content, image)
if e != nil {
panic(e)
}
defer CloseDB()
To allow writting an empty array of comments.
Finally I would be grateful if someone tell me how can I write single comments into an existing post.
This depends on your database schema.
If Comment has its own table you will have to loop over all comments in a Post and insert them after you have have inserted the Post. cn.Exec should return a Result which can be used to get the last inserted ID like:
result, err := cn.Exec(...)
if err != nil {
panic(err)
}
id, err := result.LastInsertId()
if err != nil {
panic(err)
}
You can now use the ID of your post as a foreign key.
If you are using a JSON column to store your comments you should define a custom type as alias for []Comment and make the type implement sql.Scanner and driver.Valuer.
type Comment struct {
Id int
User string
Email string
Date string
Comment string
}
type Comments []Comment
// Make the Comments type implement the driver.Valuer interface. This method
// simply returns the JSON-encoded representation of the struct.
func (c Comments) Value() (driver.Value, error) {
return json.Marshal(c)
}
// Make the Comments type implement the sql.Scanner interface. This method
// simply decodes a JSON-encoded value into the struct fields.
func (c *Comments) Scan(value interface{}) error {
var b []byte
switch t := value.(type) {
case []byte:
b = t
case string:
b = string(t)
default:
return errors.New("unknown type")
}
return json.Unmarshal(b, &c)
}

How to read list of query params

I have a method that takes a list of userIDs and return a list of users' details.
func ListUser(userIDs []interface{}) (users []User, err error) {
// query on DB based on userID and return list of users
return users, nil
}
Now I want to expose an API endpoint for it. So, I am trying to get the userIDs from my URL.
func ListUserProfile(w http.ResponseWriter, r *http.Request) {
// I know I can get the single value using
//r.URL.Query().Get("user-id")
// but here List user takes []interface{} as a argument
users, err := users.ListUser(userIDs)
}
Is there have any way I can get the list of userIDS from my URL.
Parse the form and copy the []string in the form to an []interface{}. The Go FAQ explains why copy is required.
r.ParseForm()
userIDs := make([]interface{}, len(r.Form["user-id"]))
for i, s := range r.Form["user-id"] {
userIDs[i] = s
}
users, err := users.ListUser(userIDs)
(The question and a comment disagree on the name of the query parameter. Adjust the code in the answer to match the actual name.)

Golang and MongoDB: DeleteMany with filter

I try to read and write and delete data from a Go application with the official mongodb driver for go (go.mongodb.org/mongo-driver).
Here is my struct I want to use:
Contact struct {
ID xid.ID `json:"contact_id" bson:"contact_id"`
SurName string `json:"surname" bson:"surname"`
PreName string `json:"prename" bson:"prename"`
}
// xid is https://github.com/rs/xid
I omit code to add to the collection as this is working find.
I can get a list of contacts with a specific contact_id using the following code (abbreviated):
filter := bson.D{}
cursor, err := contactCollection.Find(nil, filter)
for cur.Next(context.TODO()) {
...
}
This works and returns the documents. I thought about doing the same for delete or a matched get:
// delete - abbreviated
filter := bson.M{"contact_id": id}
result, _ := contactCollection.DeleteMany(nil, filter)
// result.DeletedCount is always 0, err is nil
if err != nil {
sendError(c, err) // helper function
return
}
c.JSON(200, gin.H{
"ok": true,
"message": fmt.Sprintf("deleted %d patients", result.DeletedCount),
}) // will be called, it is part of a webservice done with gin
// get complete
func Get(c *gin.Context) {
defer c.Done()
id := c.Param("id")
filter := bson.M{"contact_id": id}
cur, err := contactCollection.Find(nil, filter)
if err != nil {
sendError(c, err) // helper function
return
} // no error
contacts := make([]types.Contact, 0)
for cur.Next(context.TODO()) { // nothing returned
// create a value into which the single document can be decoded
var elem types.Contact
err := cur.Decode(&elem)
if err != nil {
sendError(c, err) // helper function
return
}
contacts = append(contacts, elem)
}
c.JSON(200, contacts)
}
Why does the same filter does not work on delete?
Edit: Insert code looks like this:
_, _ = contactCollection.InsertOne(context.TODO(), Contact{
ID: "abcdefg",
SurName: "Demo",
PreName: "on stackoverflow",
})
Contact.ID is of type xid.ID, which is a byte array:
type ID [rawLen]byte
So the insert code you provided where you use a string literal to specify the value for the ID field would be a compile-time error:
_, _ = contactCollection.InsertOne(context.TODO(), Contact{
ID: "abcdefg",
SurName: "Demo",
PreName: "on stackoverflow",
})
Later in your comments you clarified that the above insert code was just an example, and not how you actually do it. In your real code you unmarshal the contact (or its ID field) from a request.
xid.ID has its own unmarshaling logic, which might interpret the input data differently, and might result in an ID representing a different string value than your input. ID.UnmarshalJSON() defines how the string ID will be converted to xid.ID:
func (id *ID) UnmarshalJSON(b []byte) error {
s := string(b)
if s == "null" {
*id = nilID
return nil
}
return id.UnmarshalText(b[1 : len(b)-1])
}
As you can see, the first byte is cut off, and ID.UnmarshalText() does even more "magic" on it (check the source if you're interested).
All-in-all, to avoid such "transformations" happen in the background without your knowledge, use a simple string type for your ID, and do necessary conversions yourself wherever you need to store / transmit your ID.
For the ID Field, you should use the primitive.ObjectID provided by the bson package.
"go.mongodb.org/mongo-driver/bson/primitive"
ID primitive.ObjectID `json:"_id" bson:"_id"`

Go optional fields with SQLX

I'm learning Go and am trying to create an api endpoint that has an 'fields' parameter. When I try to scan the sqlx resulting rows it into a struct,however the fields omitted by the user are being returned as as an empty string. Is there a way that I can change the struct to reflect only the fields that the user passed? I don't think I want to use omitempty in case for example user_name is an empty string.
type User struct {
Id int `db:"id"`
UserName string `db:"user_name"`
}
func GetUsers(w http.ResponseWriter,r *http.Request,db *sqlx.DB) {
acceptedFields := map[string]bool {
"id":true,
"user_name":true,
}
var requestedFields string = "id"
if r.URL.Query().Get("fields") != ""{
requestedFields = r.URL.Query().Get("fields");
}
for _, requestedField := range strings.Split(requestedFields,",") {
if !acceptedFields[requestedField] {
http.Error(w, fmt.Sprintf("Unknown Field '%s'",requestedField), http.StatusBadRequest)
}
}
users := []User{}
err := db.Select(&users,fmt.Sprintf("SELECT %s FROM users",requestedFields));
if err != nil {
log.Fatal(err)
}
response, _ := json.Marshal(users)
fmt.Fprintf(w,string(response))
}
Resulting Endpoint Output
/users?fields=id => [{"Id":12,"UserName":""}]
Desired Endpoint Output
/users?fields=id => [{"Id":12}]
Also using sql.NullString results in this:
[{"Id":12,"UserName":{"String":"","Valid":false}}]
Thanks to mkorpriva here is a solution
type User struct {
Id int `db:"id"`
UserName *string `db:"user_name" json:",omitempty"`
}

Clearing out Previous Request RESTFul with Go(Golang)?

I've created a RESTFul API via golang.
The Issue is that when I send a /product request I will be given the result in json and when I repeat this request the result will append prevoius.
I want to clear the REST data buffer and whenever I send a request, API send me fresh data, not with prevoius. What should I do?
Route Handler
func main() {
router := mux.NewRouter()
router.HandleFunc("/product", GetProductInfo).Methods("GET")
log.Printf("Listenning on Port %s ...\n", PORT)
log.Fatal(http.ListenAndServe(PORT, router))
}
Request Handler
type ProductOut struct {
ID int `json:"id,omitempty"`
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
Price string `json:"price,omitempty"`
Location string `json:"location,omitempty"`
Created_at string `json:"created_at,omitempty"`
Images []string `json:"images,omitempty"`
}
var product_out []ProductOut
func GetProductInfo(w http.ResponseWriter, r *http.Request) {
db_select_products := db.SelectProducts() // Gets data from database
var out ProductOut
for _, ele := range db_select_products {
out.ID = contentGetFieldInteger(ele, "out_id") // A method for getting integer from struct field
out.Title = contentGetFieldString(ele, "out_title") // A method for getting string from struct field
out.Description = contentGetFieldString(ele, "out_description")
out.Price = contentGetFieldString(ele, "out_price")
out.Location = contentGetFieldString(ele, "out_location")
out.Created_at = contentGetFieldString(ele, "out_created_at")
db_select_image := db.SelectImages(out.ID) // Gets another data from database
for _, imele := range db_select_image {
out.Images = append(out.Images, imageGetFieldString(imele, "out_path"))
fmt.Println(out.Images)
}
product_out = append(product_out, out)
out.Images = nil
}
json.NewEncoder(w).Encode(product_out)
}
You are declaring this as a global variable in your package:
var product_out []ProductOut
That way, the slice is just created once, and you are sharing it between requests.
If you want to declare a new slice for each request, you should move that line inside your GetProductInfo function.