Exemplo n.º 1
0
func LinkHandler(w http.ResponseWriter, r *UserRequest) {
	switch r.Method {
	case "GET":
		groupId := util.ParseInt64FromQuery(r.URL, "groupId")

		if service.HasAccessToGroup(r.User, groupId) {
			links := service.GetLinksForGroup(groupId)

			util.MarshalToResponseWriter(links, w)
		} else {
			w.WriteHeader(http.StatusForbidden)
		}
	case "POST":
		var link model.Link

		decoder := json.NewDecoder(r.Body)
		decoder.Decode(&link)

		service.SaveLink(&link)

		w.WriteHeader(http.StatusOK)
	case "DELETE":
		linkId := util.ParseInt64FromQuery(r.URL, "id")

		service.DeleteLink(linkId)
		w.WriteHeader(http.StatusOK)
	default:
		w.WriteHeader(http.StatusMethodNotAllowed)
	}
}
Exemplo n.º 2
0
func UserHandler(w http.ResponseWriter, r *UserRequest) {
	switch r.Method {
	case "GET":
		util.MarshalToResponseWriter(r.User, w)
	case "POST":
		var user model.User

		decoder := json.NewDecoder(r.Body)
		decoder.Decode(&user)

		valErr := validator.ValidateUser(&user)

		if !util.HandleBadRequest(valErr, w) {
			user = *service.SaveUser(&user)

			w.WriteHeader(http.StatusCreated)
			util.MarshalToResponseWriter(user, w)
		}
	default:
		w.WriteHeader(http.StatusMethodNotAllowed)
	}
}
func LinkGroupHandler(w http.ResponseWriter, r *UserRequest) {
	switch r.Method {
	case "GET":
		groups := service.GetLinkGroupsForUser(r.User)

		util.MarshalToResponseWriter(groups, w)
	case "POST":
		var group model.LinkGroup

		decoder := json.NewDecoder(r.Body)
		decoder.Decode(&group)
		group.User = r.User

		service.SaveLinkGroup(&group)
		w.WriteHeader(http.StatusOK)
	default:
		w.WriteHeader(http.StatusMethodNotAllowed)
	}
}