Exemple #1
0
func GetProfile(wr http.ResponseWriter, req *http.Request) {
	if err := Authenticate(wr, req); nil != err {
		wr.WriteHeader(http.StatusUnauthorized)
		return
	}
	enc := json.NewEncoder(wr)
	profile := data.NewProfile()
	params := mux.Vars(req)
	log.Println(params)
	log.Println(req.URL.String())
	id, err := strconv.ParseInt(params["id"], 10, 64)
	p, err := profile.FindById(id)
	if _, ok := err.(*data.NoResult); ok { // if it is a NoResult
		wr.WriteHeader(http.StatusNotFound)
		enc.Encode(NewErrorJSONNotFound())
		return
	}
	if nil != err {
		wr.WriteHeader(http.StatusInternalServerError)
		log.Println("faild to get profile " + err.Error())
		return
	}

	enc.Encode(p)
}
Exemple #2
0
func GetProject(wr http.ResponseWriter, req *http.Request) {
	var (
		encoder *json.Encoder
		proj    *data.Project
		err     error
	)

	encoder = json.NewEncoder(wr)
	proj = data.NewProject()
	params := mux.Vars(req)
	log.Println(params)
	log.Println(req.URL.String())
	id, err := strconv.ParseInt(params["id"], 10, 64)
	if err != nil {
		wr.WriteHeader(http.StatusBadRequest)
		encoder.Encode(NewErrorJSONBadRequest())
		return
	}

	proj, err = proj.FindById(id)
	if _, ok := err.(*data.NoResult); ok {
		wr.WriteHeader(http.StatusNotFound)
		encoder.Encode(NewErrorJSONNotFound())
		return
	}
	if nil != err {
		wr.WriteHeader(http.StatusInternalServerError)
		log.Println("faild to get profile " + err.Error())
		return
	}

	encoder.Encode(proj)
}
Exemple #3
0
func UploadProfilePic(wr http.ResponseWriter, req *http.Request) {
	var (
		profileImgLoc string
		id            int64
		err           error
		p             *data.Profile
		file          multipart.File
		header        *multipart.FileHeader
		enc           *json.Encoder
	)

	enc = json.NewEncoder(wr)
	params := mux.Vars(req)
	id, err = strconv.ParseInt(params["id"], 10, 64)
	p, err = data.FindProfileById(id)
	req.ParseMultipartForm(10 << 20) //approx 10MB
	file, header, err = req.FormFile("file")

	handleUploadErr := func(err error, status int) {
		if nil != err {
			wr.WriteHeader(status)
			enc.Encode(NewErrorJSON(err.Error(), status))
		}
	}

	if err != nil {
		log.Println("error upload pic " + err.Error())
		handleUploadErr(err, http.StatusBadRequest)
		return
	}
	defer file.Close()
	uploadedFilePath, err := service.SaveUploadedFile(file, header.Filename)
	if err != nil {
		log.Println("failed to create thumbnail file  " + err.Error())
		handleUploadErr(err, http.StatusInternalServerError)
		return
	}

	uploadedFilePath, err = service.ThumbnailMultipart(file, header.Filename)
	if err != nil {
		log.Println("failed to create thumbnail file  " + err.Error())
		handleUploadErr(err, http.StatusInternalServerError)
		return
	}

	profileImgLoc, err = data.PutInBucket(uploadedFilePath, header.Filename)

	if err != nil {
		log.Println("failed up upload to s3 " + err.Error())
		handleUploadErr(err, http.StatusInternalServerError)
		return
	}

	p.UpdateProfilePic(profileImgLoc)
	enc.Encode(p)

}
Exemple #4
0
func ListProfiles(wr http.ResponseWriter, req *http.Request) {
	err := Authenticate(wr, req)
	if nil != err {
		wr.WriteHeader(401)
		return
	}
	params := mux.Vars(req)
	profile := data.NewProfile()
	log.Printf("listing by county %s ", params)
	profiles, err := profile.FindByCounty(params["county"])

	if nil != err {
		log.Print("error getting list " + err.Error())
		wr.WriteHeader(http.StatusInternalServerError)
		return
	}
	log.Printf("encoding profiles %i", len(profiles))
	enc := json.NewEncoder(wr)
	enc.Encode(profiles)
}