-
Notifications
You must be signed in to change notification settings - Fork 0
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
create tags and captures_tags tables #16
base: master
Are you sure you want to change the base?
Changes from all commits
4a9f880
59f4e65
e2ee3aa
14dc9b4
17a1523
14bff08
f210730
90bd161
e07d853
e4d440b
d9e7c6c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
language: elixir | ||
elixir: | ||
- 1.9 | ||
otp_release: | ||
- 22.1.8 | ||
env: | ||
- MIX_ENV=test | ||
cache: | ||
directories: | ||
- _build | ||
- deps | ||
services: | ||
- postgresql | ||
env: | ||
global: | ||
- MIX_ENV=test | ||
before_script: | ||
- mix do ecto.create, ecto.migrate | ||
script: | ||
- mix cover | ||
after_success: | ||
- bash <(curl -s https://codecov.io/bash) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"coverage_options": { | ||
"minimum_coverage": 80 | ||
}, | ||
"skip_files": [ | ||
"test/", | ||
"lib/app_api.ex" | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
defmodule AppApi.Tags do | ||
@moduledoc """ | ||
The Tags context. | ||
""" | ||
|
||
import Ecto.Query, warn: false | ||
alias AppApi.Repo | ||
|
||
alias AppApi.Tags.Tag | ||
|
||
@doc """ | ||
Returns the list of tags created by a person | ||
i.e. default tags | ||
""" | ||
def get_tags_by_id_person(id_person) do | ||
query = | ||
from t in Tag, | ||
where: t.id_person == ^id_person, | ||
order_by: [asc: t.text] | ||
|
||
Repo.all(query) | ||
end | ||
|
||
@doc """ | ||
Returns the list of tags where the person is not defined | ||
i.e. default tags | ||
""" | ||
def get_default_tags() do | ||
query = | ||
from t in Tag, | ||
where: is_nil(t.id_person), | ||
order_by: [asc: t.text] | ||
|
||
Repo.all(query) | ||
end | ||
|
||
@doc """ | ||
Creates a tag. | ||
""" | ||
def create_tag(attrs \\ %{}) do | ||
%Tag{} | ||
|> Tag.changeset(attrs) | ||
|> Repo.insert!() | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
defmodule AppApi.Tags.Tag do | ||
use Ecto.Schema | ||
import Ecto.Changeset | ||
|
||
schema "tags" do | ||
field :text, :string | ||
field :id_person, :integer | ||
|
||
timestamps() | ||
end | ||
|
||
@doc false | ||
def changeset(tag, attrs) do | ||
tag | ||
|> cast(attrs, [:text, :id_person]) | ||
|> validate_required([:text]) | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
defmodule AppApiWeb.TagController do | ||
use AppApiWeb, :controller | ||
alias AppApi.Tags | ||
|
||
def index(conn, _params) do | ||
tags = Tags.get_tags_by_id_person(conn.assigns.person.id_person) | ||
default_tags = Tags.get_default_tags() | ||
render(conn, "index.json", tags: default_tags ++ tags) | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
defmodule AppApiWeb.TagView do | ||
use AppApiWeb, :view | ||
|
||
def render("index.json", %{tags: tags}) do | ||
%{data: Enum.map(tags, &tag_to_json/1)} | ||
end | ||
|
||
def tag_to_json(tag) do | ||
%{ | ||
text: tag.text, | ||
id_person: tag.id_person | ||
} | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
defmodule AppApi.Repo.Migrations.CreateTags do | ||
use Ecto.Migration | ||
|
||
def change do | ||
create table(:tags) do | ||
add :text, :string | ||
add :id_person, :integer | ||
|
||
timestamps() | ||
end | ||
|
||
create unique_index(:tags, [:text]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This index makes sure the database doesn't contain tags with the same name There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @SimonLab which issue requirement or acceptance criteria is this for? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've tried to follow the requirements from dwyl/app#245 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @SimonLab thanks for clarifying. In which case it could be worth having that issue linked in the PR description and having it "in-progress" on the Kanban board (which makes it easier for the reviewer) 😉 |
||
end | ||
|
||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tags are passed as a string separated by a comma