Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow project create calls to include project content #66

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 26 additions & 10 deletions app/concepts/project/operation/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,32 @@ module Operation
class Create
require 'operation_response'

def self.call(user_id:)
response = OperationResponse.new
response[:project] = Project.new(user_id: user_id, project_type: 'python')
response[:project].components.build(name: 'main', extension: 'py', default: true, index: 0)
response[:project].save!
response
rescue StandardError
# TODO: log error
response[:error] = 'Error creating project'
response
DEFAULT_COMPONENT = { name: 'main', extension: 'py', default: true, index: 0 }.freeze
DEFAULT_PROJECT = { type: 'python', name: 'Untitled project', components: [DEFAULT_COMPONENT],
image_list: [] }.freeze

class << self
def call(user_id:, params:)
response = OperationResponse.new

project = DEFAULT_PROJECT.merge(
params.deep_transform_keys do |key|
key.to_sym
rescue StandardError
key
end
)
new_project = Project.new(project_type: project[:type], user_id: user_id, name: project[:name])
new_project.components.build(project[:components])

response[:project] = new_project
response[:project].save!
response
rescue StandardError
# TODO: log error
response[:error] = 'Error creating project'
response
end
end
end
end
Expand Down
7 changes: 3 additions & 4 deletions app/controllers/api/projects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def show
end

def create
result = Project::Operation::Create.call(user_id: current_user)
result = Project::Operation::Create.call(params: project_params, user_id: current_user)

if result.success?
@project = result[:project]
Expand Down Expand Up @@ -53,9 +53,8 @@ def load_projects
end

def project_params
params.require(:project)
.permit(:name,
components: %i[id name extension content index])
params.permit(project: [:name, :type, :image_list,
{ components: %i[id name extension content index default] }]).fetch(:project, {})
end
end
end
15 changes: 7 additions & 8 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 30 additions & 1 deletion spec/concepts/project/operation/create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
require 'rails_helper'

RSpec.describe Project::Operation::Create, type: :unit do
subject(:create_project) { described_class.call(user_id: user_id) }
subject(:create_project) { described_class.call(user_id: user_id, params: project_params) }

let(:user_id) { 'e0675b6c-dc48-4cd6-8c04-0f7ac05af51a' }
let(:project_params) { {} }

before do
mock_phrase_generation
Expand Down Expand Up @@ -37,6 +38,34 @@
expect(attrs).to eq(expected)
end

context 'when initial project present' do
subject(:create_project_with_content) { described_class.call(user_id: user_id, params: project_params) }

let(:project_params) do
{
type: 'python',
components: [
{
name: 'main',
extension: 'py',
content: 'print("hello world")',
index: 0,
default: true
}
]
}
end

it 'returns success' do
expect(create_project_with_content.success?).to eq(true)
end

it 'returns project with correct component content' do
new_project = create_project_with_content[:project]
expect(new_project.components.first.content).to eq('print("hello world")')
end
end

context 'when creation fails' do
before do
mock_project = instance_double(Project)
Expand Down