diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rb new file mode 100644 index 0000000..e254ef8 --- /dev/null +++ b/app/controllers/topics_controller.rb @@ -0,0 +1,72 @@ +class TopicsController < ApplicationController + include Authorization + + before_action :set_topic, only: %i[ show edit update destroy ] + + # GET /topics or /topics.json + def index + @topics = Topic.all + end + + # GET /topics/1 or /topics/1.json + def show + end + + # GET /topics/new + def new + @topic = Topic.new + end + + # GET /topics/1/edit + def edit + end + + # POST /topics or /topics.json + def create + @topic = Topic.new(topic_params) + + respond_to do |format| + if @topic.save + format.html { redirect_to @topic, notice: "Topic was successfully created." } + format.json { render :show, status: :created, location: @topic } + else + format.html { render :new, status: :unprocessable_entity } + format.json { render json: @topic.errors, status: :unprocessable_entity } + end + end + end + + # PATCH/PUT /topics/1 or /topics/1.json + def update + respond_to do |format| + if @topic.update(topic_params) + format.html { redirect_to @topic, notice: "Topic was successfully updated." } + format.json { render :show, status: :ok, location: @topic } + else + format.html { render :edit, status: :unprocessable_entity } + format.json { render json: @topic.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /topics/1 or /topics/1.json + def destroy + @topic.destroy! + + respond_to do |format| + format.html { redirect_to topics_path, status: :see_other, notice: "Topic was successfully destroyed." } + format.json { head :no_content } + end + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_topic + @topic = Topic.find(params.expect(:id)) + end + + # Only allow a list of trusted parameters through. + def topic_params + params.expect(topic: [ :title, :description, :uid, :state, :language_id, :provider_id ]) + end +end diff --git a/app/helpers/topics_helper.rb b/app/helpers/topics_helper.rb new file mode 100644 index 0000000..488eed5 --- /dev/null +++ b/app/helpers/topics_helper.rb @@ -0,0 +1,2 @@ +module TopicsHelper +end diff --git a/app/models/language.rb b/app/models/language.rb index 9625d5a..7dbf1aa 100644 --- a/app/models/language.rb +++ b/app/models/language.rb @@ -2,4 +2,6 @@ class Language < ApplicationRecord validates :name, presence: true validates :name, uniqueness: true validates :file_share_folder, presence: true + + has_many :topics end diff --git a/app/models/provider.rb b/app/models/provider.rb index 9bde6c2..c359cc3 100644 --- a/app/models/provider.rb +++ b/app/models/provider.rb @@ -3,4 +3,5 @@ class Provider < ApplicationRecord validates :name, uniqueness: true has_many :users + has_many :topics end diff --git a/app/models/topic.rb b/app/models/topic.rb new file mode 100644 index 0000000..4c753b9 --- /dev/null +++ b/app/models/topic.rb @@ -0,0 +1,10 @@ +class Topic < ApplicationRecord + validates :description, presence: true + validates :title, presence: true + validates :title, uniqueness: true + + belongs_to :language + belongs_to :provider + + enum :state, [ :active, :archived ] +end diff --git a/app/views/layouts/_sidebar.html.erb b/app/views/layouts/_sidebar.html.erb index 3da6552..40d8777 100644 --- a/app/views/layouts/_sidebar.html.erb +++ b/app/views/layouts/_sidebar.html.erb @@ -16,8 +16,11 @@ Dashboard + - - + %> <% if current_user_is_admin? %>