func (s *WalkthroughService) walkthroughPutHandler() http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		id := hitch.Params(r).ByName("id")

		entity := &Walkthrough{}
		ab.MustDecode(r, entity)

		if err := entity.Validate(); entity.UUID != id || err != nil {
			ab.Fail(r, http.StatusBadRequest, err)
		}

		db := ab.GetDB(r)
		abort := false

		beforeWalkthroughPutUpdateHandler(r, entity, db)

		if abort {
			return
		}

		err := entity.Update(db)
		ab.MaybeFail(r, http.StatusInternalServerError, ab.ConvertDBError(err, walkthroughDBErrorConverter))

		afterWalkthroughPutUpdateHandler(s, entity)

		if abort {
			return
		}

		ab.Render(r).JSON(entity)
	})
}
func (s *WalkthroughService) walkthroughPostHandler() http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		entity := &Walkthrough{}
		ab.MustDecode(r, entity)

		abort := false

		walkthroughPostValidation(entity, r)

		if abort {
			return
		}

		if err := entity.Validate(); err != nil {
			ab.Fail(r, http.StatusBadRequest, err)
		}

		db := ab.GetDB(r)

		err := entity.Insert(db)
		ab.MaybeFail(r, http.StatusInternalServerError, ab.ConvertDBError(err, walkthroughDBErrorConverter))

		afterWalkthroughPostInsertHandler(db, s, entity)

		if abort {
			return
		}

		ab.Render(r).SetCode(http.StatusCreated).JSON(entity)
	})
}
Exemplo n.º 3
0
func (s *UserService) userPutHandler() http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		id := hitch.Params(r).ByName("id")

		entity := &User{}
		ab.MustDecode(r, entity)

		if err := entity.Validate(); entity.UUID != id || err != nil {
			ab.Fail(r, http.StatusBadRequest, err)
		}

		db := ab.GetDB(r)
		abort := false

		// HOOK: beforeUserPutUpdateHandler()

		if abort {
			return
		}

		err := entity.Update(db)
		ab.MaybeFail(r, http.StatusInternalServerError, ab.ConvertDBError(err, userDBErrorConverter))

		// HOOK: afterUserPutUpdateHandler()

		if abort {
			return
		}

		ab.Render(r).JSON(entity)
	})
}
Exemplo n.º 4
0
func (s *UserService) userPostHandler() http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		entity := &User{}
		ab.MustDecode(r, entity)

		abort := false

		// HOOK: userPostValidation()

		if abort {
			return
		}

		if err := entity.Validate(); err != nil {
			ab.Fail(r, http.StatusBadRequest, err)
		}

		db := ab.GetDB(r)

		err := entity.Insert(db)
		ab.MaybeFail(r, http.StatusInternalServerError, ab.ConvertDBError(err, userDBErrorConverter))

		// HOOK: afterUserPostInsertHandler()

		if abort {
			return
		}

		ab.Render(r).SetCode(http.StatusCreated).JSON(entity)
	})
}
Exemplo n.º 5
0
func (s *LogService) Register(h *hitch.Hitch) error {
	walkthroughPlayed := prometheus.NewCounter(stdprometheus.CounterOpts{
		Namespace: "walkhub",
		Subsystem: "metrics",
		Name:      "walkthrough_played",
		Help:      "Number of walkthrough plays",
	}, []string{"uuid", "embedorigin"})

	walkthroughVisited := prometheus.NewCounter(stdprometheus.CounterOpts{
		Namespace: "walkhub",
		Subsystem: "metrics",
		Name:      "walkthrough_visited",
		Help:      "Number of walkthrough visits",
	}, []string{"uuid", "embedorigin"})

	h.Post("/api/log/helpcenteropened", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		l := helpCenterOpenedLog{}
		ab.MustDecode(r, &l)

		db := ab.GetDB(r)
		userid := getLogUserID(r)

		message := fmt.Sprintf("%s has opened the help center on %s", userid, l.URL)
		ab.MaybeFail(r, http.StatusInternalServerError, DBLog(db, "helpcenteropened", message))
	}))

	h.Post("/api/log/walkthroughplayed", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		l := walkthroughPlayedLog{}
		ab.MustDecode(r, &l)

		db := ab.GetDB(r)
		userid := getLogUserID(r)
		wt, err := LoadActualRevision(db, l.UUID)
		ab.MaybeFail(r, http.StatusBadRequest, err)
		if wt == nil {
			ab.Fail(r, http.StatusNotFound, nil)
		}

		message := ""

		embedPart := ""
		if l.EmbedOrigin != "" {
			embedPart = "from the help center on " + l.EmbedOrigin + " "
		}

		wturl := s.BaseURL + "walkthrough/" + wt.UUID

		if l.ErrorMessage == "" {
			message = fmt.Sprintf("%s has played the walkthrough %s<%s|%s>", userid, embedPart, wturl, wt.Name)
		} else {
			message = fmt.Sprintf("%s has failed to play the walkthrough %s<%s|%s> with the error message %s", userid, embedPart, wturl, wt.Name, l.ErrorMessage)
		}

		ab.MaybeFail(r, http.StatusInternalServerError, DBLog(db, "walkthroughplayed", message))

		walkthroughPlayed.
			With(metrics.Field{Key: "uuid", Value: l.UUID}).
			With(metrics.Field{Key: "embedorigin", Value: l.EmbedOrigin}).
			Add(1)
	}))

	h.Post("/api/log/walkthroughpagevisited", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		l := walkthroughPageVisitedLog{}
		ab.MustDecode(r, &l)

		db := ab.GetDB(r)
		userid := getLogUserID(r)
		wt, err := LoadActualRevision(db, l.UUID)
		ab.MaybeFail(r, http.StatusBadRequest, err)
		if wt == nil {
			ab.Fail(r, http.StatusNotFound, nil)
		}

		embedPart := ""
		if l.EmbedOrigin != "" {
			embedPart = "embedded on " + l.EmbedOrigin + " "
		}

		wturl := s.BaseURL + "walkthrough/" + wt.UUID

		message := fmt.Sprintf("%s has visited the walkthrough page %s<%s|%s>", userid, embedPart, wturl, wt.Name)

		ab.MaybeFail(r, http.StatusInternalServerError, DBLog(db, "walkthroughvisited", message))

		walkthroughVisited.
			With(metrics.Field{Key: "uuid", Value: l.UUID}).
			With(metrics.Field{Key: "embedorigin", Value: l.EmbedOrigin}).
			Add(1)
	}))

	return nil
}