I'm creating a REST api using Go and Mongo. I am still fairly new to the languages. So basically, I call this function to update existing data in a database. It is not actually updating anything in the database.
func UpdateCompanyEndpoint(response http.ResponseWriter, request *http.Request) {
response.Header().Set("content-type", "application/json")
params := mux.Vars(request)
name, _ := params["name"]
var company Company
_ = json.NewDecoder(request.Body).Decode(&company)
filter := bson.D{{"name", name}}
fmt.Println(name)
update := bson.D{{"$set", bson.D{{"application", company.Application}}}}
collection := client.Database("RESTful").Collection("companies")
doc := collection.FindOneAndUpdate(
context.Background(),
filter,
update,
nil)
fmt.Println(doc)
}
The database looks like this:
[
{
"name": "Test1",
"application": "Test1"
},
{
"name": "Test2",
"application": "Test2"
},
{
"name": "Test3",
"application": "Test3"
}
]
I call the put method on http://localhost:8080/update/Test2 with:
{
"name": "Test2",
"application": "Test2update"
}
However, it does not update anything in the database. Here is the code: https://github.com/jasonkim615/internship-db/blob/master/main.go