Exemplo n.º 1
0
func CreateProfile(wr http.ResponseWriter, req *http.Request) {
	var (
		decoder *json.Decoder
		profile *data.Profile
		err     error
		encoder *json.Encoder
	)

	decoder = json.NewDecoder(req.Body)
	profile = data.NewProfile()
	encoder = json.NewEncoder(wr)

	if err = decoder.Decode(profile); err != nil {
		http.Error(wr, err.Error(), http.StatusBadRequest)
		return
	}

	if profile.Exists(profile.Email) {
		wr.WriteHeader(http.StatusConflict)
		encoder.Encode(NewErrorJSONExists())
		return
	}
	if _, err = profile.Save(); err != nil {
		log.Printf("error occurred saving profile %s ", err.Error())
		wr.WriteHeader(http.StatusInternalServerError)
		encoder.Encode(NewErrorJSONUnexpectedError(err.Error()))
		return
	}

	encoder.Encode(profile)
	//get handle to email channel and send email to user
	go service.SendMailTemplate(service.MAIL_TEMPLATE_INTEREST, service.NewMailSender(), service.MAIL_FROM, profile.Email)

}
Exemplo n.º 2
0
func UpdateProfile(wr http.ResponseWriter, req *http.Request) {
	var (
		decoder *json.Decoder
		profile *data.Profile
		err     error
		encoder *json.Encoder
	)

	decoder = json.NewDecoder(req.Body)
	profile = data.NewProfile()
	encoder = json.NewEncoder(wr)
	if err = decoder.Decode(profile); nil != err {
		wr.WriteHeader(http.StatusBadRequest)
		encoder.Encode(NewErrorJSONBadRequest())
		return
	}

	if err = profile.Update(); err != nil {
		log.Printf("error occurred saving profile %s ", err.Error())
		wr.WriteHeader(http.StatusInternalServerError)
		encoder.Encode(NewErrorJSONUnexpectedError(err.Error()))
		return
	}
	encoder.Encode(profile)
}
Exemplo n.º 3
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)
}
Exemplo n.º 4
0
func getMockProfile() (sqlmock.Sqlmock, *data.Profile, error) {
	db, mock, err := sqlmock.New()
	if err != nil {
		return nil, nil, err
	}

	var getDb data.DataBase = func() *sql.DB {
		return db
	}
	profile := data.NewProfile()
	profile.Store = getDb
	return mock, profile, err
}
Exemplo n.º 5
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)
}