-
Notifications
You must be signed in to change notification settings - Fork 0
/
cms.ex
195 lines (145 loc) · 3.54 KB
/
cms.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
defmodule Hello.CMS do
@moduledoc """
The CMS context.
"""
import Ecto.Query, warn: false
alias Hello.Repo
alias Hello.CMS.{Page, Author}
alias Hello.Accounts
def list_pages do
Page
|> Repo.all()
|> Repo.preload(author: [user: :credential])
end
def get_page!(id) do
Page
|> Repo.get!(id)
|> Repo.preload(author: [user: :credential])
end
def get_author!(id) do
Author
|> Repo.get!(id)
|> Repo.preload(user: :credential)
end
def inc_page_views(%Page{} = page) do
{1, [%Page{views: views}]} =
Repo.update_all(
from(p in Page, where: p.id == ^page.id),
[inc: [views: 1]], returning: [:views])
put_in(page.views, views)
end
@doc """
Creates a page.
## Examples
iex> create_page(%{field: value})
{:ok, %Page{}}
iex> create_page(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_page(%Author{} = author, attrs \\ %{}) do
%Page{}
|> Page.changeset(attrs)
|> Ecto.Changeset.put_change(:author_id, author.id)
|> Repo.insert()
end
def ensure_author_exists(%Accounts.User{} = user) do
%Author{user_id: user.id}
|> Ecto.Changeset.change()
|> Ecto.Changeset.unique_constraint(:user_id)
|> Repo.insert()
|> handle_existing_author()
end
defp handle_existing_author({:ok, author}), do: author
defp handle_existing_author({:error, changeset}) do
Repo.get_by!(Author, user_id: changeset.data.user_id)
end
@doc """
Updates a page.
## Examples
iex> update_page(page, %{field: new_value})
{:ok, %Page{}}
iex> update_page(page, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_page(%Page{} = page, attrs) do
page
|> Page.changeset(attrs)
|> Repo.update()
end
@doc """
Deletes a Page.
## Examples
iex> delete_page(page)
{:ok, %Page{}}
iex> delete_page(page)
{:error, %Ecto.Changeset{}}
"""
def delete_page(%Page{} = page) do
Repo.delete(page)
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking page changes.
## Examples
iex> change_page(page)
%Ecto.Changeset{source: %Page{}}
"""
def change_page(%Page{} = page) do
Page.changeset(page, %{})
end
alias Hello.CMS.Author
@doc """
Returns the list of authors.
## Examples
iex> list_authors()
[%Author{}, ...]
"""
def list_authors do
Repo.all(Author)
end
@doc """
Creates a author.
## Examples
iex> create_author(%{field: value})
{:ok, %Author{}}
iex> create_author(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_author(attrs \\ %{}) do
%Author{}
|> Author.changeset(attrs)
|> Repo.insert()
end
@doc """
Updates a author.
## Examples
iex> update_author(author, %{field: new_value})
{:ok, %Author{}}
iex> update_author(author, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_author(%Author{} = author, attrs) do
author
|> Author.changeset(attrs)
|> Repo.update()
end
@doc """
Deletes a Author.
## Examples
iex> delete_author(author)
{:ok, %Author{}}
iex> delete_author(author)
{:error, %Ecto.Changeset{}}
"""
def delete_author(%Author{} = author) do
Repo.delete(author)
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking author changes.
## Examples
iex> change_author(author)
%Ecto.Changeset{source: %Author{}}
"""
def change_author(%Author{} = author) do
Author.changeset(author, %{})
end
end