-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.ex
240 lines (173 loc) · 4.23 KB
/
auth.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
defmodule HabiticaBackend.Auth do
@moduledoc """
The Auth context.
"""
import Ecto.Query, warn: false
alias HabiticaBackend.Repo
alias HabiticaBackend.Auth.User
alias HabiticaBackend.Player
alias HabiticaBackend.Rewards
alias HabiticaBackend.Completedtasks
alias HabiticaBackend.Habittasks
alias HabiticaBackend.Todotasks
alias HabiticaBackend.Dailytasks
@doc """
Returns the list of users.
## Examples
iex> list_users()
[%User{}, ...]
"""
def list_users do
Repo.all(User)
end
@doc """
Gets a single user.
Raises `Ecto.NoResultsError` if the User does not exist.
## Examples
iex> get_user!(123)
%User{}
iex> get_user!(456)
** (Ecto.NoResultsError)
"""
def get_user!(id), do: Repo.get!(User, id)
@doc """
Creates a user.
## Examples
iex> create_user(%{field: value})
{:ok, %User{}}
iex> create_user(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_user(attrs \\ %{}) do
%User{}
|> User.changeset(attrs)
|> Repo.insert()
end
@doc """
Updates a user.
## Examples
iex> update_user(user, %{field: new_value})
{:ok, %User{}}
iex> update_user(user, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_user(%User{} = user, attrs) do
user
|> User.changeset(attrs)
|> Repo.update()
end
@doc """
Deletes a User.
## Examples
iex> delete_user(user)
{:ok, %User{}}
iex> delete_user(user)
{:error, %Ecto.Changeset{}}
"""
def delete_user(%User{} = user) do
Repo.delete(user)
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking user changes.
## Examples
iex> change_user(user)
%Ecto.Changeset{source: %User{}}
"""
def change_user(%User{} = user) do
User.changeset(user, %{})
end
def authenticate_user(email, password) do
query = from(u in User, where: u.email == ^email)
query |> Repo.one() |> verify_password(password)
end
defp verify_password(nil, _) do
Bcrypt.no_user_verify()
{:error, "Wrong email or password"}
end
defp verify_password(user, password) do
if Bcrypt.verify_pass(password, user.password_hash) do
{:ok, user}
else
{:error, "Wrong email or password"}
end
end
#Start Todo Task Section #
def create_todotasks(attrs \\ %{}) do
%Todotasks{}
|> Todotasks.changeset(attrs)
|> Repo.insert()
end
def get_todotasks(id), do: Repo.get(Todotasks, id)
def delete_todotasks(%Todotasks{} = task) do
Repo.delete(task)
end
def change_todotasks(%Todotasks{} = task, changes) do
changeset = Todotasks.changeset(task, changes)
changeset
|> Repo.update()
end
def list_todotasks do
Repo.all(Todotasks)
end
#End Todo Task Section #
#Start Completed Task Section #
def create_completedtask(attrs \\ %{}) do
%Completedtasks{}
|> Completedtasks.changeset(attrs)
|> Repo.insert()
end
def list_completedtasks do
Repo.all(Completedtasks)
end
def get_completedtasks(id), do: Repo.get(Completedtasks, id)
def delete_completedtasks(%Completedtasks{} = task) do
Repo.delete(task)
end
#End Completed Task Section #
#Start Habit tasks Section#
def list_habittasks do
Repo.all(Habittasks)
end
def create_habittasks(attrs \\ %{}) do
%Habittasks{}
|> Habittasks.changeset(attrs)
|> Repo.insert()
end
#End Habit tasks Section#
#Start Daily tasks Section#
def list_dailytasks do
Repo.all(Dailytasks)
end
def create_dailytasks(attrs \\ %{}) do
%Dailytasks{}
|> Dailytasks.changeset(attrs)
|> Repo.insert()
end
#End Daily tasks Section#
#Start Player Section#
def create_player(attrs \\ %{}) do
%Player{}
|> Player.changeset(attrs)
|> Repo.insert()
end
def get_player!(id), do: Repo.get!(Player, id)
def list_player do
Repo.all(Player)
end
def update_player(%Player{} = player, attrs) do
player
|> Player.changeset(attrs)
|> Repo.update()
end
#End Player Section#
#Start Reward Section#
def list_rewards do
Repo.all(Rewards)
end
def create_rewards(attrs \\ %{}) do
%Rewards{}
|> Rewards.changeset(attrs)
|> Repo.insert()
end
#End Reward Section#
end