Skip to content

Commit

Permalink
Added new field in models
Browse files Browse the repository at this point in the history
  • Loading branch information
DanAmador committed May 8, 2016
1 parent 040026f commit 280069b
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 7 deletions.
24 changes: 23 additions & 1 deletion lib/mockFill/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function fillSubjectModel(values) {
postDisShit("subjects", JSON.stringify(currentSubject));
});
}
};
}

function postDisShit(route, postBody) {
console.log(postBody);
Expand All @@ -62,4 +62,26 @@ function encodeImages(link) {
return deferred.promise
}

function fillDrawingModel(values) {
for (var i = 0; i < 200; i++) {
var currentChars = [];
for (var j = 0; j < 5; j++) {
currentChars.push(values[Math.floor(Math.random() * values.length)])
}

encodeImages(faker.image.image()).then(image => {
var currentSubject = {
drawing: {
name: faker.commerce.productName(),
order: Math.floor((Math.random()) * 6),
characteristics: currentChars,
img: image
}
};
postDisShit("drawings", JSON.stringify(currentSubject));
});
}

}
fillDrawingModel(characteristics);
fillSubjectModel(characteristics);
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
defmodule Habanero.Repo.Migrations.AddCharacteristicsToSubject do
use Ecto.Migration

def change do
alter table(:subjects) do
add :characteristics, {:array, :string}
end
alter table(:drawings) do
add :characteristics, {:array, :string}
end
end
end
5 changes: 5 additions & 0 deletions web/controllers/drawing_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ defmodule Habanero.DrawingController do
end
end

def get_all(conn) do
query = from d in Drawings,
select: d
json(conn, Repo.all(query))
end
defp parse_img(drawing_params) do
img = drawing_params["img"]
|> Base.decode64!
Expand Down
5 changes: 2 additions & 3 deletions web/models/drawing.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ defmodule Habanero.Drawing do
schema "drawings" do
belongs_to :subject, Subject
has_many :scores, Score

field :img_url, Habanero.DrawingImage.Type
field :name, :string
field :order, :integer
field :characteristics, {:array, :string}
timestamps
end

@required_fields ~w(name subject_id order)
@required_fields ~w(name order)
@optional_fields ~w()

@required_file_fields ~w(img_url)
Expand All @@ -36,7 +35,7 @@ defmodule Habanero.Drawing do
query = from(drawing in Drawing,
where: drawing.subject_id == ^subject_id,
order_by: [asc: drawing.order])

Repo.all(query)
end
end
1 change: 1 addition & 0 deletions web/models/subject.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ defmodule Habanero.Subject do
field :img_url, Habanero.SubjectImage.Type
# Complexity can be "easy", "mid", "hard"
field :complexity, :string
field :characteristics, {:array, :string}

timestamps
end
Expand Down
2 changes: 1 addition & 1 deletion web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ defmodule Habanero.Router do

get "/subjects/:subject_id/drawings", DrawingController, :by_subject
get "/drawings/:drawing_id/scores", ScoreController, :by_drawing

get "/drawings", DrawingController, :get_all
resources "/subjects", SubjectController, except: [:new, :edit]
resources "/drawings", DrawingController, except: [:new, :edit]
resources "/scores", ScoreController, except: [:new, :edit]
Expand Down
3 changes: 2 additions & 1 deletion web/views/drawing_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ defmodule Habanero.DrawingView do
subject_id: drawing.subject_id,
img_url: DrawingImage.url({drawing.img_url, drawing}),
name: drawing.name,
order: drawing.order
order: drawing.order,
characteristics: drawing.characteristics
}
end
end
4 changes: 3 additions & 1 deletion web/views/subject_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ defmodule Habanero.SubjectView do
%{id: subject.id,
name: subject.name,
img_url: SubjectImage.url({subject.img_url, subject}),
complexity: subject.complexity}
complexity: subject.complexity,
characteristics: subject.characteristics
}
end
end

0 comments on commit 280069b

Please sign in to comment.