From 3c5547f0d40a6333af8132c77834d3c72c2e2690 Mon Sep 17 00:00:00 2001 From: Dominic Grimm Date: Sun, 13 Mar 2022 11:54:42 +0100 Subject: [PATCH] Add config query --- .example.env | 2 +- docker-compose.yml | 2 +- docker/backend/shard.lock | 2 +- docker/backend/shard.yml | 2 +- .../backend/src/backend/api/schema/config.cr | 30 +++++++++++++++++++ .../src/backend/api/schema/mutation.cr | 2 +- .../backend/src/backend/api/schema/query.cr | 6 ++++ docker/backend/src/backend/config.cr | 2 +- 8 files changed, 42 insertions(+), 6 deletions(-) create mode 100644 docker/backend/src/backend/api/schema/config.cr diff --git a/.example.env b/.example.env index 90c7189..5591344 100644 --- a/.example.env +++ b/.example.env @@ -22,7 +22,7 @@ POSTGRES_USER="mw" POSTGRES_PASSWORD= # Backend -BACKEND_MIN_TEACHER_SELECTION_COUNT= +BACKEND_MINIMUM_TEACHER_SELECTION_COUNT= BACKEND_URL=URL # Backend - API BACKEND_API_GRAPHQL_PLAYGROUND=false diff --git a/docker-compose.yml b/docker-compose.yml index f5896ab..3632aa6 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -77,7 +77,7 @@ services: - redis environment: BACKEND_URL: ${URL} - BACKEND_MIN_TEACHER_SELECTION_COUNT: ${BACKEND_MIN_TEACHER_SELECTION_COUNT} + BACKEND_MINIMUM_TEACHER_SELECTION_COUNT: ${BACKEND_MINIMUM_TEACHER_SELECTION_COUNT} BACKEND_API_GRAPHQL_PLAYGROUND: ${BACKEND_API_GRAPHQL_PLAYGROUND} BACKEND_API_JWT_SECRET: ${BACKEND_API_JWT_SECRET} BACKEND_API_JWT_EXPIRATION: ${BACKEND_API_JWT_EXPIRATION} diff --git a/docker/backend/shard.lock b/docker/backend/shard.lock index dc8b1fd..3a3b79d 100644 --- a/docker/backend/shard.lock +++ b/docker/backend/shard.lock @@ -30,7 +30,7 @@ shards: graphql: git: https://github.com/graphql-crystal/graphql.git - version: 0.3.2+git.commit.8c6dc73c0c898ca511d9d12efefca7c837c25946 + version: 0.3.2+git.commit.75325d512bfa2f57a14b306aa9f7465fc0998fa5 habitat: git: https://github.com/luckyframework/habitat.git diff --git a/docker/backend/shard.yml b/docker/backend/shard.yml index d167d61..0f21b7c 100644 --- a/docker/backend/shard.yml +++ b/docker/backend/shard.yml @@ -37,7 +37,7 @@ dependencies: github: will/crystal-pg graphql: github: graphql-crystal/graphql - branch: master + branch: main jwt: github: crystal-community/jwt commander: diff --git a/docker/backend/src/backend/api/schema/config.cr b/docker/backend/src/backend/api/schema/config.cr new file mode 100644 index 0000000..9e1de34 --- /dev/null +++ b/docker/backend/src/backend/api/schema/config.cr @@ -0,0 +1,30 @@ +# Mentorenwahl: A fullstack application for assigning mentors to students based on their whishes. +# Copyright (C) 2022 Dominic Grimm +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +module Backend + module Api + module Schema + @[GraphQL::Object] + # Public configuration of the API for frontend pre-validation + class Config < GraphQL::BaseObject + @[GraphQL::Field] + def minimum_teacher_selection_count : Int32 + Backend.config.minimum_teacher_selection_count + end + end + end + end +end diff --git a/docker/backend/src/backend/api/schema/mutation.cr b/docker/backend/src/backend/api/schema/mutation.cr index 42edd62..f079ec7 100644 --- a/docker/backend/src/backend/api/schema/mutation.cr +++ b/docker/backend/src/backend/api/schema/mutation.cr @@ -143,7 +143,7 @@ module Backend def create_vote(context : Context, input : VoteCreateInput) : Vote context.student! - raise "Not enough teachers" if input.teacher_ids.size < Backend.config.min_teacher_selection_count + raise "Not enough teachers" if input.teacher_ids.size < Backend.config.minimum_teacher_selection_count teacher_role_count = Db::User.where(role: Db::UserRole::Teacher.to_s).count.run.as(Int64) raise "Teachers not registered" if teacher_role_count != Db::Teacher.count || teacher_role_count.zero? diff --git a/docker/backend/src/backend/api/schema/query.cr b/docker/backend/src/backend/api/schema/query.cr index 062e028..fec8e58 100644 --- a/docker/backend/src/backend/api/schema/query.cr +++ b/docker/backend/src/backend/api/schema/query.cr @@ -25,6 +25,12 @@ module Backend true end + @[GraphQL::Field] + # Public configuration of the API for frontend pre-validation + def config : Config + Config.new + end + @[GraphQL::Field] # Current authenticated user def me(context : Context) : User diff --git a/docker/backend/src/backend/config.cr b/docker/backend/src/backend/config.cr index c458f7e..77d9da9 100644 --- a/docker/backend/src/backend/config.cr +++ b/docker/backend/src/backend/config.cr @@ -59,7 +59,7 @@ module Backend getter url : String # Minimum teacher selection count - getter min_teacher_selection_count : Int32 + getter minimum_teacher_selection_count : Int32 @[EnvConfig::Setting(key: "api")] # Configuration for `Api`