Advertisement
Guest User

Untitled

a guest
Sep 27th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. type PostView struct {
  2. Id int64 `json:"userid"`
  3. PostId int64 `json:"postId"`
  4. Title string `json:"Title"`
  5. Content string `json:"Content"`
  6. Comment string `json:"comment"`
  7. CreatedAt *time.Time `json:"createdAt"`
  8. UpdatedAt *time.Time `json:"updatedAt"`
  9. }
  10.  
  11. type PostRequestBody struct {
  12. Title string
  13. Content string
  14. Comment string
  15. }
  16.  
  17. func (p *PostRequestBody) FieldMap(req *http.Request) binding.FieldMap {
  18. return binding.FieldMap{
  19. &p.Title: "Title",
  20. &p.Content: "Content",
  21. &p.Comment: "Comment",
  22.  
  23. }
  24.  
  25. func UpdatePost(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
  26. userId, err := strconv.ParseInt(ps.ByName("userId"), 10, 0)
  27. helpers.CheckErr(err, "Invalid userId")
  28.  
  29. postId, err := strconv.ParseInt(ps.ByName("postId"), 10, 0)
  30. helpers.CheckErr(err, "Invalid postId")
  31.  
  32. user := models.Getuser(userId)
  33. log.Println(user)
  34.  
  35. switch user.GroupId {
  36. case 1://日記
  37.  
  38. p := models.GetPost(postId)//指定したidのpostがストラクトで返ってくる
  39.  
  40. //p := new(models.PostRequestBody)//新しい投稿を作ることができる
  41.  
  42. errs := binding.Bind(req, p)//エラーが起きるアサインメントできないから
  43. if errs.Len() > 0 {
  44. log.Println(errs)
  45. R.JSON(w, 422, errs)
  46. return
  47. }
  48.  
  49. post := models.Post{
  50. Title: p.title,
  51. Content: p.content,
  52. Comment: p.comment,
  53. }
  54.  
  55.  
  56. log.Println("log:",post)
  57. R.JSON(w, http.StatusOK, models.UpdatePost(post))
  58. default:
  59. R.JSON(w, http.StatusBadRequest, map[string]interface{}{
  60. "code": http.StatusBadRequest,
  61. "message": http.StatusText(http.StatusBadRequest),
  62. })
  63.  
  64. }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement