Skip to content

Commit 7517faf

Browse files
adds karma update for users
1 parent 48ec4a7 commit 7517faf

File tree

4 files changed

+11
-1
lines changed

4 files changed

+11
-1
lines changed

lib/reddit/category/community.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,5 @@ defmodule Reddit.Category.Community do
3434
defp clean_name(name) do
3535
name |> String.split |> Enum.join
3636
end
37+
3738
end

lib/reddit/users/user.ex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ defmodule Reddit.Users.User do
55
extensions: [PowResetPassword]
66

77
import Ecto.Changeset
8+
import Ecto.Query, only: [from: 2]
89

910
alias Reddit.Users.User
1011
alias Reddit.Repo
@@ -41,4 +42,9 @@ defmodule Reddit.Users.User do
4142
def profile(param) do
4243
Repo.get_by(User, username: param)
4344
end
45+
46+
def karma_update(user_id, k) do
47+
from(u in User, update: [inc: [karma: ^k]], where: u.id == ^user_id)
48+
|> Repo.update_all([])
49+
end
4450
end

lib/reddit_web/controllers/community_controller.ex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ defmodule RedditWeb.CommunityController do
33

44
alias Reddit.Category
55
alias Reddit.Category.Community
6+
alias Reddit.Users.User
67

78
def index(conn, _params) do
89
communities = Category.list_communities()
@@ -20,6 +21,8 @@ defmodule RedditWeb.CommunityController do
2021

2122
case Category.create_community(conn.assigns.current_user, community) do
2223
{:ok, community} ->
24+
User.karma_update(conn.assigns.current_user.id, 1)
25+
2326
conn
2427
|> put_flash(:info, "Community created successfully.")
2528
|> redirect(to: Routes.community_path(conn, :show, community.name))

lib/reddit_web/templates/layout/root.html.eex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
</div>
3131

3232
<div class="karma float-left mt-2 mr-2">
33-
0
33+
<%= @current_user.karma %>
3434
<span class="fa fa-star text-warning"></span>
3535
</div>
3636
<% else %>

0 commit comments

Comments
 (0)