當前位置: 首頁>>代碼示例>>Golang>>正文


Golang pgmapper.Mapper類代碼示例

本文整理匯總了Golang中github.com/InteractiveLecture/pgmapper.Mapper的典型用法代碼示例。如果您正苦於以下問題:Golang Mapper類的具體用法?Golang Mapper怎麽用?Golang Mapper使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了Mapper類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: ModulesPatchHandler

func ModulesPatchHandler(mapper *pgmapper.Mapper, extractor idextractor.Extractor) http.Handler {
	handlerFunc := func(w http.ResponseWriter, r *http.Request) int {
		id, err := extractor(r)
		if err != nil {
			return http.StatusInternalServerError
		}
		log.Println("got patch request for module")
		patch, err := jsonpatch.Decode(r.Body)
		if err != nil {
			log.Println("error while decoding json: ", err)
			return http.StatusBadRequest
		}
		userId := context.Get(r, "user").(*jwt.Token).Claims["id"].(string)
		options := map[string]interface{}{
			"userId": userId,
			"id":     id,
			"jwt":    r.Header.Get("Authorization"),
		}
		compiler := lecturepatch.ForModules()
		err = mapper.ApplyPatch(patch, compiler, options)
		if err != nil {
			if err == lecturepatch.PermissionDeniedError {
				log.Println(err)
				return http.StatusUnauthorized
			} else {
				log.Println("error while applying module patch: ", err)
				return http.StatusBadRequest
			}
		}
		return -1
	}
	return jwtware.New(createHandler(handlerFunc))
}
開發者ID:InteractiveLecture,項目名稱:lecture-service,代碼行數:33,代碼來源:moduleshandler.go

示例2: TopicCreateHandler

func TopicCreateHandler(mapper *pgmapper.Mapper) http.Handler {
	handlerFunc := func(w http.ResponseWriter, r *http.Request) int {
		var topic = make(map[string]interface{})
		err := json.NewDecoder(r.Body).Decode(&topic)
		if err != nil {
			log.Println("error while decoding new topic json: ", err)
			return http.StatusBadRequest
		}
		err = mapper.Execute("SELECT add_topic(%v)", topic["id"], topic["name"], topic["description"], topic["officers"])
		if err != nil {
			log.Println("error while inserting new topic into database")
			return http.StatusBadRequest // TODO it could be an internal server error as well. need distinction
		}
		client := serviceclient.New("acl-service")
		aclEntity, _ := json.Marshal(topic)
		resp, err := client.Post("/objects", "application/json", bytes.NewReader(aclEntity), "Authorization", r.Header.Get("Authorization"))
		if err != nil {
			log.Println("error while creating acl-object: ", err)
			return http.StatusInternalServerError
		}
		if resp.StatusCode >= 300 {
			log.Println("got unexpected statuscode from acl-service while creating object: ", resp.StatusCode)
			return http.StatusInternalServerError
		}
		return http.StatusCreated
	}
	return jwtware.New(createHandler(handlerFunc))
}
開發者ID:InteractiveLecture,項目名稱:lecture-service,代碼行數:28,代碼來源:topicshandler.go

示例3: upsertPermissionsHandler

func upsertPermissionsHandler(mapper *pgmapper.Mapper, objectIdExtractor idextractor.Extractor) http.Handler {
	result := func(w http.ResponseWriter, r *http.Request) {
		objectId, err := objectIdExtractor(r)
		if err != nil {
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
		ids, ok := r.URL.Query()["sid"]
		entity := make(map[string]interface{})
		err = json.NewDecoder(r.Body).Decode(&entity)
		if err != nil {
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
		if ok {
			err = mapper.Execute("SELECT insert_bulk_permissions(%v)", objectId, entity["create_permission"], entity["read_permission"], entity["update_permission"], entity["delete_permission"], ids)
		} else {
			_, err = mapper.ExecuteRaw("insert into acl_entries(object_id,sid,create_permission,read_permission,update_permission,delete_permission) values($1,$2,$3,$4,$5,$6) ON CONFLICT (object_id,sid) DO UPDATE SET create_permission = $3, read_permission = $4, update_permission = $5, delete_permission = $6 where acl_entries.sid = $2 AND acl_entries.object_id = $1", objectId, entity["sid"], entity["create_permission"], entity["read_permission"], entity["update_permission"], entity["delete_permission"])
		}
		if err != nil {
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
	}
	return http.Handler(http.HandlerFunc(result))
}
開發者ID:InteractiveLecture,項目名稱:acl-service,代碼行數:26,代碼來源:main.go

示例4: ExerciseHistoryHandler

func ExerciseHistoryHandler(mapper *pgmapper.Mapper, extractor idextractor.Extractor) http.Handler {
	handlerFunc := func(w http.ResponseWriter, r *http.Request) int {
		pr, err := paginator.ParsePages(r.URL)
		if err != nil {
			return http.StatusInternalServerError
		}
		id, err := extractor(r)
		if err != nil {
			return http.StatusInternalServerError
		}
		limit := pr.Size
		skip := pr.Size * pr.Number
		if pr.Number == -1 || pr.Size == -1 {
			skip = -1
		}
		var result []byte
		if ids, ok := r.URL.Query()["module_id"]; ok {
			result, err = mapper.PreparedQueryIntoBytes("SELECT get_exercise_history(%v)", id, limit, skip, ids[0])
		} else {
			result, err = mapper.PreparedQueryIntoBytes("SELECT get_exercise_history(%v)", id, limit, skip)
		}
		if err != nil {
			return http.StatusNotFound
		}
		reader := bytes.NewReader(result)
		_, err = io.Copy(w, reader)
		if err != nil {
			log.Println(err)
		}
		return -1
	}
	return jwtware.New(createHandler(handlerFunc))
}
開發者ID:InteractiveLecture,項目名稱:lecture-service,代碼行數:33,代碼來源:historyhandler.go

示例5: PurchaseHintHandler

func PurchaseHintHandler(mapper *pgmapper.Mapper, extractor idextractor.Extractor) http.Handler {
	handlerFunc := func(w http.ResponseWriter, r *http.Request) int {
		id, err := extractor(r)
		if err != nil {
			return http.StatusBadRequest
		}
		userId := context.Get(r, "user").(*jwt.Token).Claims["id"].(string)
		result, err := mapper.PreparedQueryIntoBytes("SELECT purchase_hint(%v)", id, userId)
		if err != nil {
			log.Println(err)
			return http.StatusInternalServerError
		}
		purchaseResult, _ := strconv.Atoi(string(result))
		log.Println("hint purchase achieved the following result: ", purchaseResult)
		switch {
		case purchaseResult == 0:
			//hint purchase without errors
			return -1
		case purchaseResult == 1:
			//balance not sufficient
			return 420
		case purchaseResult == 2:
			//already purchased
			return http.StatusConflict
		case purchaseResult == 3:
			//hint not found
			return http.StatusNotFound
		default:
			//something different went wrong
			return http.StatusInternalServerError
		}
	}
	return jwtware.New(createHandler(handlerFunc))
}
開發者ID:InteractiveLecture,項目名稱:lecture-service,代碼行數:34,代碼來源:exercisehandler.go

示例6: ModulesTreeHandler

func ModulesTreeHandler(mapper *pgmapper.Mapper, extractor idextractor.Extractor) http.Handler {
	handlerFunc := func(w http.ResponseWriter, r *http.Request) int {
		id, err := extractor(r)
		if err != nil {
			log.Println("error with extractor in ModulesTreeHandler")
			return http.StatusBadRequest
		}
		dr, err := paginator.ParseDepth(r.URL)
		if err != nil {
			log.Println("error parsing depth request in ModulesTreeHandler")
			return http.StatusInternalServerError
		}
		result, err := mapper.PreparedQueryIntoBytes("SELECT get_module_tree(%v)", id, dr.Descendants, dr.Ancestors)
		if err != nil {
			log.Println(err)
			return http.StatusInternalServerError
		}
		reader := bytes.NewReader(result)
		_, err = io.Copy(w, reader)
		if err != nil {
			log.Println(err)
		}
		return -1
	}
	return jwtware.New(createHandler(handlerFunc))
}
開發者ID:InteractiveLecture,項目名稱:lecture-service,代碼行數:26,代碼來源:moduleshandler.go

示例7: HintHistoryHandler

func HintHistoryHandler(mapper *pgmapper.Mapper, extractor idextractor.Extractor) http.Handler {
	handlerFunc := func(w http.ResponseWriter, r *http.Request) int {
		pr, err := paginator.ParsePages(r.URL)
		if err != nil {
			return http.StatusInternalServerError
		}
		id, err := extractor(r)
		if err != nil {
			return http.StatusInternalServerError
		}
		var result []byte
		ids, ok := r.URL.Query()["exercise_id"]
		if ok {
			result, err = mapper.PreparedQueryIntoBytes("SELECT get_hint_purchase_history(%v)", id, pr.Size, pr.Size*pr.Number, ids[0])
		} else {
			result, err = mapper.PreparedQueryIntoBytes("SELECT get_hint_purchase_history(%v)", id, pr.Size, pr.Size*pr.Number)
		}
		if err != nil {
			log.Println(err)
			return http.StatusNotFound
		}
		reader := bytes.NewReader(result)
		_, err = io.Copy(w, reader)
		if err != nil {
			log.Println(err)
		}
		return -1
	}
	return createHandler(handlerFunc)
}
開發者ID:Richterrettich,項目名稱:lecture-service,代碼行數:30,代碼來源:historyhandler.go

示例8: upsertMultiplePermissionsHandler

func upsertMultiplePermissionsHandler(mapper *pgmapper.Mapper, sidIdExtractor idextractor.Extractor) http.Handler {
	result := func(w http.ResponseWriter, r *http.Request) {
		sid, err := sidIdExtractor(r)
		if err != nil {
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
		ids, ok := r.URL.Query()["oid"]
		if !ok {
			w.WriteHeader(http.StatusBadRequest)
			return
		}
		permissions := make(map[string]interface{})
		err = json.NewDecoder(r.Body).Decode(&permissions)
		if err != nil {
			w.WriteHeader(http.StatusBadRequest)
			return
		}
		err = mapper.Execute("SELECT insert_bulk_sid_permissions(%v)", sid, permissions["create_permission"], permissions["read_permission"], permissions["update_permission"], permissions["delete_permission"], ids)
		if err != nil {
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
		w.WriteHeader(http.StatusOK)
		return
	}
	return http.Handler(http.HandlerFunc(result))
}
開發者ID:InteractiveLecture,項目名稱:acl-service,代碼行數:28,代碼來源:main.go

示例9: deleteMultipleObjectsHandler

func deleteMultipleObjectsHandler(mapper *pgmapper.Mapper) http.Handler {
	result := func(w http.ResponseWriter, r *http.Request) {
		ids, ok := r.URL.Query()["oid"]
		if !ok {
			w.WriteHeader(http.StatusBadRequest)
		}
		err := mapper.Execute("SELECT delete_objects(%v)", ids)
		if err != nil {
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
		w.WriteHeader(http.StatusOK)
	}
	return http.Handler(http.HandlerFunc(result))
}
開發者ID:InteractiveLecture,項目名稱:acl-service,代碼行數:15,代碼來源:main.go

示例10: deleteObjectHandler

func deleteObjectHandler(mapper *pgmapper.Mapper, extractor idextractor.Extractor) http.Handler {
	result := func(w http.ResponseWriter, r *http.Request) {
		objectId, err := extractor(r)
		if err != nil {
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
		_, err = mapper.ExecuteRaw("delete from object_identities where id = $1", objectId)
		if err != nil {
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
	}
	return http.Handler(http.HandlerFunc(result))
}
開發者ID:InteractiveLecture,項目名稱:acl-service,代碼行數:15,代碼來源:main.go

示例11: TopicCreateHandler

func TopicCreateHandler(mapper *pgmapper.Mapper) http.Handler {
	handlerFunc := func(w http.ResponseWriter, r *http.Request) int {
		var topic = make(map[string]interface{})
		err := json.NewDecoder(r.Body).Decode(topic)
		if err != nil {
			return http.StatusBadRequest
		}
		err = mapper.Execute("SELECT add_topic(%v)", topic["id"], topic["name"], topic["description"], topic["officers"])
		if err != nil {
			return http.StatusBadRequest // TODO it could be an internal server error as well. need distinction
		}
		w.WriteHeader(http.StatusCreated)
		return -1
	}
	return createHandler(handlerFunc)
}
開發者ID:Richterrettich,項目名稱:lecture-service,代碼行數:16,代碼來源:topicshandler.go

示例12: ExerciseStartHandler

func ExerciseStartHandler(mapper *pgmapper.Mapper, extractor idextractor.Extractor) http.Handler {
	handlerFunc := func(w http.ResponseWriter, r *http.Request) int {
		user := context.Get(r, "user")
		id := user.(*jwt.Token).Claims["id"]
		exerciseId, err := extractor(r)
		if err != nil {
			return http.StatusInternalServerError
		}
		err = mapper.Execute("select start_exercise(%v)", exerciseId, id)
		if err != nil {
			return http.StatusNotFound
		}
		return -1
	}
	return jwtware.New(createHandler(handlerFunc))
}
開發者ID:InteractiveLecture,項目名稱:lecture-service,代碼行數:16,代碼來源:historyhandler.go

示例13: addObjectHandler

func addObjectHandler(mapper *pgmapper.Mapper) http.Handler {
	result := func(w http.ResponseWriter, r *http.Request) {
		entity := make(map[string]interface{})
		err := json.NewDecoder(r.Body).Decode(&entity)
		if err != nil {
			log.Println("error while decoding json: ", err)
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
		err = mapper.Execute("insert into object_identities(id,parent_object) values(%v)", entity["id"], entity["parent"])
		if err != nil {
			log.Println("error while insertiing object into database: ", err)
			w.WriteHeader(http.StatusBadRequest)
		}
	}
	return http.Handler(http.HandlerFunc(result))
}
開發者ID:InteractiveLecture,項目名稱:acl-service,代碼行數:17,代碼來源:main.go

示例14: ExerciseStartHandler

func ExerciseStartHandler(mapper *pgmapper.Mapper, extractor idextractor.Extractor) http.Handler {
	handlerFunc := func(w http.ResponseWriter, r *http.Request) int {
		id, err := extractor(r)
		if err != nil {
			return http.StatusInternalServerError
		}
		var exerciseId string
		err = json.NewDecoder(r.Body).Decode(exerciseId)
		if err != nil {
			return http.StatusBadRequest
		}
		err = mapper.Execute("insert into exercise_progress_histories(user_id,exercise_id,amount,time,state) values(%v)", id, exerciseId, 0, time.Now(), 1)
		if err != nil {
			return http.StatusNotFound
		}
		return -1
	}
	return createHandler(handlerFunc)
}
開發者ID:Richterrettich,項目名稱:lecture-service,代碼行數:19,代碼來源:historyhandler.go

示例15: TopicFindHandler

func TopicFindHandler(mapper *pgmapper.Mapper, extractor idextractor.Extractor) http.Handler {
	handlerFunc := func(w http.ResponseWriter, r *http.Request) int {
		id, err := extractor(r)
		if err != nil {
			return http.StatusInternalServerError
		}
		result, err := mapper.PreparedQueryIntoBytes(`SELECT get_topic($1)`, id)
		if err != nil {
			return http.StatusNotFound
		}
		reader := bytes.NewReader(result)
		_, err = io.Copy(w, reader)
		if err != nil {
			log.Println(err)
		}
		return -1
	}
	return createHandler(handlerFunc)
}
開發者ID:Richterrettich,項目名稱:lecture-service,代碼行數:19,代碼來源:topicshandler.go


注:本文中的github.com/InteractiveLecture/pgmapper.Mapper類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。