func PostPhoto(w http.ResponseWriter, r *http.Request) { // the FormFile function takes in the POST input id file file, header, _ := r.FormFile("file") bits := strings.Split(header.Filename, ".") fileExt := bits[len(bits)-1] fileName := strconv.Itoa(int(time.Now().Unix())) + "." + fileExt uploadResult := s3s.Upload(file, fileName) if !uploadResult { log.Fatalln("Failed to upload") } else { log.Println("Uploaded") } // Get user u := repository.GetUserByToken(r.FormValue("token")) url := "http://" + os.Getenv("BUCKET") + ".s3-website-" + os.Getenv("AWS_REGION") + ".amazonaws.com/" + fileName // Save photo photo := models.Photo{ Url: url, CreatedAt: time.Now(), UserId: u.Id, User: u, } repository.CreatePhoto(&photo) json.NewEncoder(w).Encode(photo) }
func CreateComment(w http.ResponseWriter, r *http.Request) { // Verify user u := repository.GetUserByToken(r.FormValue("token")) // Create vars := mux.Vars(r) _photoId, _ := strconv.Atoi(vars["photoId"]) c := models.Comment{} c.UserId, _ = strconv.Atoi(r.FormValue("userId")) c.PhotoId = _photoId c.Content = r.FormValue("content") c.UserId = u.Id c.User = u _processTags(c.Content, _photoId) repository.CreateComment(&c) json.NewEncoder(w).Encode(c) }