コード例 #1
0
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))
}
コード例 #2
0
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))
}
コード例 #3
0
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))
}
コード例 #4
0
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))
}
コード例 #5
0
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))
}
コード例 #6
0
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))
}
コード例 #7
0
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)
}
コード例 #8
0
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))
}
コード例 #9
0
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))
}
コード例 #10
0
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))
}
コード例 #11
0
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)
}
コード例 #12
0
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))
}
コード例 #13
0
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))
}
コード例 #14
0
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)
}
コード例 #15
0
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)
}
コード例 #16
0
func TopicAddOfficerHandler(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 officer string
		err = json.NewDecoder(r.Body).Decode(officer)
		if err != nil {
			return http.StatusBadRequest
		}
		err = mapper.Execute(`SELECT add_officer($1,$2)`, id, officer)
		if err != nil {
			return http.StatusInternalServerError
		}
		return -1
	}
	return createHandler(handlerFunc)
}
コード例 #17
0
func TopicRemoveOfficerHandler(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 officer string
		err = json.NewDecoder(r.Body).Decode(officer)
		if err != nil {
			return http.StatusBadRequest
		}
		err = mapper.Execute("SELECT remove_officer(%v)", id, officer)
		if err != nil {
			return http.StatusInternalServerError
		}
		return -1
	}
	return jwtware.New(createHandler(handlerFunc))
}
コード例 #18
0
func TopicBalanceHandler(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_balances(%v)", 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 jwtware.New(createHandler(handlerFunc))
}
コード例 #19
0
func NextModulesForUserHandler(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_next_modules_for_user(%v)", 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)
}
コード例 #20
0
func TopicPatchHandler(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
		}
		patch, err := jsonpatch.Decode(r.Body)
		if err != nil {
			return http.StatusBadRequest
		}
		userId := context.Get(r, "user").(*jwt.Token).Claims["id"].(string)
		compiler := lecturepatch.ForTopics()
		err = mapper.ApplyPatch(id, userId, patch, compiler)
		if err != nil {
			return http.StatusBadRequest
		}
		return -1
	}
	return createHandler(handlerFunc)
}
コード例 #21
0
func TopicCollectionHandler(mapper *pgmapper.Mapper) http.Handler {
	handlerFunc := func(w http.ResponseWriter, r *http.Request) int {
		pageRequest, err := paginator.ParsePages(r.URL)
		if err != nil {
			return http.StatusInternalServerError
		}
		result, err := mapper.PreparedQueryIntoBytes("SELECT * from query_topics(%v)", pageRequest.Number*pageRequest.Size, pageRequest.Size)
		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))
}
コード例 #22
0
func ModulesGetHandler(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 ModulesGetHandler")
			return http.StatusInternalServerError
		}
		result, err := mapper.QueryIntoBytes(`SELECT details from module_details where id = $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 jwtware.New(createHandler(handlerFunc))
}
コード例 #23
0
func GetHintHandler(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 get_hint(%v)", userId, id)
		switch {
		case err != nil:
			return http.StatusInternalServerError
		case len(result) == 0:
			return http.StatusPaymentRequired
		}
		reader := bytes.NewReader(result)
		_, err = io.Copy(w, reader)
		if err != nil {
			log.Println(err)
		}
		return -1
	}
	return jwtware.New(createHandler(handlerFunc))
}
コード例 #24
0
func getPermissionsHandler(mapper *pgmapper.Mapper, objectIdExtractor idextractor.Extractor, userIdExtractor idextractor.Extractor) http.Handler {
	result := func(w http.ResponseWriter, r *http.Request) {
		objectId, err := objectIdExtractor(r)
		if err != nil {
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
		userId, err := userIdExtractor(r)
		if err != nil {
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
		result, err := mapper.QueryIntoBytes("SELECT get_permissions(%v)", objectId, userId)
		if err != nil {
			w.WriteHeader(http.StatusNotFound)
			return
		}
		_, err = io.Copy(w, bytes.NewReader(result))
		if err != nil {
			log.Println(err)
		}
	}
	return http.Handler(http.HandlerFunc(result))
}
コード例 #25
0
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)
		purchaseResult, _ := binary.Varint(result) //TODO check function...something is fishy
		switch {
		case purchaseResult == 0:
			return -1
		case purchaseResult == 1:
			return 420
		case purchaseResult == 2:
			return http.StatusConflict
		case purchaseResult == 3:
			return http.StatusNotFound
		default:
			return http.StatusInternalServerError
		}
	}
	return createHandler(handlerFunc)
}