#19 Wiki 新規作成完成
This commit is contained in:
@@ -1,19 +1,24 @@
|
||||
class WikiPagesController < ApplicationController
|
||||
def show
|
||||
wiki_page = WikiPage.find(params[:id])
|
||||
render json: wiki_page.as_json
|
||||
end
|
||||
|
||||
def show_by_title
|
||||
wiki_page = WikiPage.find_by(title: params[:title])
|
||||
if wiki_page
|
||||
render plain: wiki_page.markdown
|
||||
else
|
||||
head :not_found
|
||||
end
|
||||
body = wiki_page&.body
|
||||
return head :not_found unless body
|
||||
|
||||
render plain: body
|
||||
end
|
||||
|
||||
def create
|
||||
return head :unauthorized unless current_user
|
||||
return head :forbidden unless ['admin', 'member'].include?(current_user.role)
|
||||
|
||||
wiki_page = WikiPage.new(title: params[:title], tag_id: params[:tag_id], created_user: current_user, updated_user: current_user)
|
||||
wiki_page.markdown = params[:markdown], user: current_user
|
||||
wiki_page = WikiPage.new(title: params[:title], created_user: current_user, updated_user: current_user)
|
||||
if wiki_page.save
|
||||
wiki_page.set_body params[:body], user: current_user
|
||||
render json: wiki_page, status: :created
|
||||
else
|
||||
render json: { errors: wiki_page.errors.full_messages }, status: :unprocessable_entity
|
||||
@@ -27,7 +32,7 @@ class WikiPagesController < ApplicationController
|
||||
return head :not_found unless wiki_pages
|
||||
|
||||
wiki_page.updated_user = current_user
|
||||
wiki_page.markdown = params[:markdown], user: current_user
|
||||
wiki_page.set_body params[:body], user: current_user
|
||||
wiki_page.save!
|
||||
head :ok
|
||||
end
|
||||
|
||||
@@ -13,17 +13,17 @@ class WikiPage < ApplicationRecord
|
||||
page&.raw_data
|
||||
end
|
||||
|
||||
def body= content, user:
|
||||
def set_body content, user:
|
||||
page = wiki.page("#{ id }.md")
|
||||
|
||||
commit_info = { message: "Update #{ title }",
|
||||
name: user.id,
|
||||
name: user.id.to_s,
|
||||
email: 'dummy@example.com' }
|
||||
|
||||
if page
|
||||
page.update(content, commit: commit_info)
|
||||
else
|
||||
wiki.write_page("#{ id }.md", :markdown, content, commit_info)
|
||||
wiki.write_page(id.to_s, :markdown, content, commit_info)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -20,7 +20,8 @@ Rails.application.routes.draw do
|
||||
delete 'posts/:id/viewed', to: 'posts#unviewed'
|
||||
get 'preview/title', to: 'preview#title'
|
||||
get 'preview/thumbnail', to: 'preview#thumbnail'
|
||||
get 'wiki/:title', to: 'wiki_pages#show'
|
||||
get 'wiki/title/:title', to: 'wiki_pages#show_by_title'
|
||||
get 'wiki/:id', to: 'wiki_pages#show'
|
||||
post 'wiki', to: 'wiki_pages#create'
|
||||
put 'wiki/:id', to: 'wiki_pages#update'
|
||||
|
||||
|
||||
Reference in New Issue
Block a user