Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func CreateComment(w http.ResponseWriter, r *http.Request) {
- comment := models.Comment{}
- user := models.User{}
- m := models.Message{}
- user, _ = r.Context().Value("user").(models.User)
- err := json.NewDecoder(r.Body).Decode(&comment)
- if err != nil {
- m.Code = http.StatusBadRequest
- m.Message = fmt.Sprintf("Error al leer el comentario: %s", err)
- commons.DisplayMessage(w, m)
- return
- }
- comment.UserID = user.ID
- db := configuration.GetConnection()
- defer db.Close()
- err = db.Create(&comment).Error
- if err != nil {
- m.Code = http.StatusBadRequest
- m.Message = fmt.Sprintf("Error al crear el comentario: %s", err)
- commons.DisplayMessage(w, m)
- return
- }
- db.Model(&comment).Related(&comment.User)
- /* comment.User[0].Password = "" */ /* El error está aquí */
- j, err := json.Marshal(&comment)
- if err != nil {
- m.Message = fmt.Sprintf("No se pudo convertir el comentario a json: %s", err)
- m.Code = http.StatusInternalServerError
- commons.DisplayMessage(w, m)
- return
- }
- origin := fmt.Sprintf("http://localhost:%d/", commons.Port)
- url := fmt.Sprintf("ws://localhost:%d/ws", commons.Port)
- ws, err := websocket.Dial(url, "", origin)
- if err != nil {
- log.Fatal(err)
- }
- if _, err := ws.Write(j); err != nil {
- log.Fatal(err)
- }
- m.Code = http.StatusCreated
- m.Message = "Comentario creado con éxito."
- commons.DisplayMessage(w, m)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement