Skip to content
This repository has been archived by the owner on Aug 28, 2019. It is now read-only.

Commit

Permalink
Implemented fetch for notifications, tried to work on write and ackno…
Browse files Browse the repository at this point in the history
…wledge as well
  • Loading branch information
alcen committed Jun 11, 2019
1 parent b3864cc commit 6393129
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions lib/cadet/accounts/notification.ex
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
defmodule Cadet.Accounts.Notification do
@moduledoc """
Provides the Notification schema as well as functions to
fetch, write and acknowledge notifications
"""
use Cadet, :model

import Ecto.Query

alias Cadet.Repo
alias Cadet.Accounts.{NotificationType, Role, User}
alias Cadet.Assessments.{Assessment, Question, Submission}

schema "notifications" do
field(:type, NotificationType)
field(:read, :boolean)
field(:role, Role)
field(:role, Role, virtual: true)

belongs_to(:user, User)
belongs_to(:assessment, Assessment)
Expand All @@ -19,7 +25,7 @@ defmodule Cadet.Accounts.Notification do
end

@required_fields ~w(type read role user_id)a
@optional_fields ~w(assessment_id submission_id question_id)
@optional_fields ~w(assessment_id submission_id question_id)a

def changeset(answer, params) do
answer
Expand Down Expand Up @@ -60,9 +66,10 @@ defmodule Cadet.Accounts.Notification do
def fetch(user = %User{}) do
IO.puts("Fetch called")
IO.inspect(user)

# Test
{:ok, []}
Cadet.Accounts.Notification
|> where(user_id: ^user.id)
|> Repo.all()
|> fn (array) -> {:ok, array} end.()
end

@doc """
Expand All @@ -71,6 +78,9 @@ defmodule Cadet.Accounts.Notification do
@spec write(:any) :: Ecto.Changeset.t()
def write(params) do
IO.puts("Write called")
%Cadet.Accounts.Notification{}
|> Cadet.Accounts.Notification.changeset(params)
|> Repo.insert!()
end

@doc """
Expand All @@ -79,11 +89,14 @@ defmodule Cadet.Accounts.Notification do
@spec acknowledge(:integer, %User{}) :: {:ok} | {:error, Ecto.Changeset.t()}
def acknowledge(notification_id, user = %User{}) do
IO.puts("Acknowledge called")
IO.puts("with notification id: ")
IO.inspect(notification_id)
IO.puts("with user: ")
IO.inspect(user)

IO.inspect(notification_id, label: "with notification_id")
IO.inspect(user, label: "with user")
Cadet.Accounts.Notification
|> where(user_id: ^user.id)
|> where(id: ^notification_id)
|> where(read: false)

This comment has been minimized.

Copy link
@geshuming

geshuming Jun 12, 2019

Owner

where clauses can be combined

|> Repo.one!()
|> fn (notif) -> %{notif | read: true} end.()
# Test
{:ok, nil}
end
Expand Down
Empty file modified lib/cadet/accounts/notification_type.ex
100644 → 100755
Empty file.

0 comments on commit 6393129

Please sign in to comment.