Esempio n. 1
0
func changeUserProfile(userdb *db.UserDB,
	w http.ResponseWriter, r *http.Request,
	user userView, u *pb.User) bool {
	log.Printf("hello changeUserProfile")

	if *u.Id != user.Id {
		http.Error(w, "Unauthorized", http.StatusUnauthorized)
		return false
	}

	if err := r.ParseForm(); err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return false
	}

	callsign := r.Form.Get("callsign")
	if len(callsign) > 10 {
		callsign = callsign[:10]
	}
	callsign = strings.ToUpper(callsign)
	if callsign != "" {
		u.Callsign = proto.String(callsign)
	} else {
		u.Callsign = nil
	}

	if err := userdb.Store(u); err != nil {
		log.Printf("Error storing user: %s", err.Error())
		http.Error(w, "", http.StatusInternalServerError)
		return false
	}

	return true
}
Esempio n. 2
0
func RestoreUserTable(input *db.RecordReader, output *db.UserDB) error {
	for {
		rec, err := input.ReadRecord()
		if err == io.EOF {
			break
		} else if err != nil {
			log.Printf("Error reading record: %s", err.Error())
			return err
		}

		u := &pb.User{}
		err = proto.Unmarshal(rec, u)
		if err != nil {
			log.Printf("Error parsing record: %s", err.Error())
			return err
		}

		err = output.Store(u)
		if err != nil {
			log.Printf("Error writing record: %s", err.Error())
			return err
		}

		fmt.Printf(".")
	}
	fmt.Printf("\n")

	log.Printf("User table restored.")
	return nil
}