至急です。お願いします!
言葉で説明するのが難しいですが
structが2つあり1つは全体としてのstructで、もう一つが変更等を加えるためだけのstructとして2つ存在します。今書いているコードで書けないので、とても適当ではありますが,,,,わかる方いましたらお願いします.
全体
type Post struct{ Id: int64 `db:"id" json:"id"` UserId: int64 `db:"userid" json:"userid"` Title: string `db:"title" json:"title"` Content: string `db:"content" json:"content"` DeletedAt: *time.Time `db:"deleted_at" json:"deltedAt"` CreatedAt: *time.Time `db:"created_at" json:"createdAt"` UpdatedAt: *time.Time `db:"updated_at" json:"updatedAt"` }
アップデート項目(bindingしている方)
type PostRequestBody struct{ Title: string Content: string }
binding
func(p *PostRequestBoby)FieldMap(req *http.Request) binding.fieldMap{ return binding.FieldMap{ &p.Title: "title", &p.Content: "content", } }
func UpdatPost(w http.ResponseWriter, req *http.Request, ps httprouter.Params) { userId, err := strconv.ParseInt(ps.ByName("userId"), 10, 0) helpers.CheckErr(err, "Invalid userId") postId, err = strconv.ParseInt(ps.ByName("postId"), 10, 0) helpers.CheckErr(err, "Invalid postId") user := models.GetUser(userId) switch user.GroupeId { case 4: s := models.Post(PostId)// //s := new(models.PostRequestBody)で新しく作るのはわかります。 errs := binding.Bind(req, s) if errs.Len() > 0 { log.Println(errs) R.JSON(w, 422, errs) return } post := models.Post{ Title: title, Content: content, } R.JSON(w, httpStatusOK, models.UpdatePost(post))
今やっているコードで書けないので、このコードはとてつもなく適当ですが、ご容赦ください...
やりたいことは、アップデートしたいものを取得し(UpdatePostファンクションのs := の部分)てから
最終的には、変更を加えたものを保存したいと思います。
わかる方いましたらご教授ください。
また例えが書けそうであれば更新します。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。