Browse Source

model: changement majeur des données de Task

Lou 3 years ago
parent
commit
eb92b35d19

+ 3
- 3
app/controllers/api/v1/tasks_controller.rb View File

22
       render json: TaskSerializer.new(task).serializable_hash.to_json,
22
       render json: TaskSerializer.new(task).serializable_hash.to_json,
23
       status: :created
23
       status: :created
24
     else
24
     else
25
-      render json: { errors: activity.errors }, status: :unprocessable_entity
25
+      render json: { errors: task.errors }, status: :unprocessable_entity
26
     end
26
     end
27
   end
27
   end
28
 
28
 
42
   private
42
   private
43
 
43
 
44
   def task_params
44
   def task_params
45
-    params.require(:task).permit(:name, :description, :duration, :activity_id, :created_at, :updated_at)
45
+    params.require(:task).permit(:name, :description, :start_time, :end_time, :is_paid, :paid_by, :owner_id, :activity_id)
46
   end
46
   end
47
 
47
 
48
   def set_task
48
   def set_task
50
   end
50
   end
51
 
51
 
52
   def check_owner
52
   def check_owner
53
-    head :forbidden unless @task.user_id === current_user.id
53
+    head :forbidden unless @task.owner_id === current_user.id
54
   end
54
   end
55
 end
55
 end

+ 4
- 2
app/models/task.rb View File

1
 class Task < ApplicationRecord
1
 class Task < ApplicationRecord
2
   validates :name, presence: true
2
   validates :name, presence: true
3
-  validates :user, presence: true
3
+  validates :owner, presence: true
4
+  validates :activity, presence: true
5
+  validates :start_time, presence: true
4
 
6
 
5
   belongs_to :activity
7
   belongs_to :activity
6
-  belongs_to :user
8
+  belongs_to :owner, class_name: "User"
7
 end
9
 end

+ 1
- 1
app/models/user.rb View File

9
   has_many :joined_user_activities
9
   has_many :joined_user_activities
10
   has_many :activities, :through => :joined_user_activities
10
   has_many :activities, :through => :joined_user_activities
11
 
11
 
12
-  has_many :tasks
12
+  has_many :tasks, foreign_key: 'owner_id', class_name: 'Task'
13
   
13
   
14
   has_many :memberships, foreign_key: :member_id
14
   has_many :memberships, foreign_key: :member_id
15
   has_many :teams, through: :memberships
15
   has_many :teams, through: :memberships

+ 1
- 1
app/serializers/task_serializer.rb View File

1
 class TaskSerializer
1
 class TaskSerializer
2
   include JSONAPI::Serializer
2
   include JSONAPI::Serializer
3
-  attributes :name, :description, :duration, :user_id, :activity_id, :created_at, :updated_at
3
+  attributes :name, :description, :start_time, :end_time, :is_paid, :paid_by_id, :owner_id, :activity_id
4
 
4
 
5
   cache_options store: Rails.cache, namespace: 'jsonapi-serializer', expires_in: 1.hour
5
   cache_options store: Rails.cache, namespace: 'jsonapi-serializer', expires_in: 1.hour
6
 end
6
 end

+ 5
- 2
db/migrate/20210510133939_create_tasks.rb View File

4
       t.string :name, null: false
4
       t.string :name, null: false
5
       t.index :name
5
       t.index :name
6
       t.text :description
6
       t.text :description
7
-      t.integer :duration
7
+      t.datetime :start_time, null: false
8
+      t.datetime :end_time, default: nil
9
+      t.boolean :is_paid, default: false
10
+      t.references :paid_by, default: nil
8
       t.references :activity, null: false
11
       t.references :activity, null: false
9
-      t.references :user, null: false
12
+      t.references :owner, null: false
10
 
13
 
11
       t.timestamps
14
       t.timestamps
12
     end
15
     end

+ 7
- 3
db/schema.rb View File

48
   create_table "tasks", force: :cascade do |t|
48
   create_table "tasks", force: :cascade do |t|
49
     t.string "name", null: false
49
     t.string "name", null: false
50
     t.text "description"
50
     t.text "description"
51
-    t.integer "duration"
51
+    t.datetime "start_time", null: false
52
+    t.datetime "end_time"
53
+    t.boolean "is_paid", default: false
54
+    t.bigint "paid_by_id"
52
     t.bigint "activity_id", null: false
55
     t.bigint "activity_id", null: false
53
-    t.bigint "user_id", null: false
56
+    t.bigint "owner_id", null: false
54
     t.datetime "created_at", precision: 6, null: false
57
     t.datetime "created_at", precision: 6, null: false
55
     t.datetime "updated_at", precision: 6, null: false
58
     t.datetime "updated_at", precision: 6, null: false
56
     t.index ["activity_id"], name: "index_tasks_on_activity_id"
59
     t.index ["activity_id"], name: "index_tasks_on_activity_id"
57
     t.index ["name"], name: "index_tasks_on_name"
60
     t.index ["name"], name: "index_tasks_on_name"
58
-    t.index ["user_id"], name: "index_tasks_on_user_id"
61
+    t.index ["owner_id"], name: "index_tasks_on_owner_id"
62
+    t.index ["paid_by_id"], name: "index_tasks_on_paid_by_id"
59
   end
63
   end
60
 
64
 
61
   create_table "teams", force: :cascade do |t|
65
   create_table "teams", force: :cascade do |t|

+ 5
- 4
db/seeds.rb View File

48
 User.create(username: "admin", email: "admin@email.com", password: "azerty", is_admin: true)
48
 User.create(username: "admin", email: "admin@email.com", password: "azerty", is_admin: true)
49
 puts "Created Admin"
49
 puts "Created Admin"
50
 
50
 
51
-30.times do |i|
51
+60.times do |i|
52
   task = Task.create(
52
   task = Task.create(
53
-    name: Faker::Fantasy::Tolkien.character,
53
+    name: Faker::Verb.ing_form,
54
     description: Faker::Fantasy::Tolkien.poem,
54
     description: Faker::Fantasy::Tolkien.poem,
55
-    duration: rand(1..120),
56
-    user_id: User.all.sample.id,
55
+    start_time: Faker::Time.between(from: DateTime.now - 1, to: DateTime.now),
56
+    end_time: Faker::Time.between(from: DateTime.now + 1, to: DateTime.now + 5),
57
+    owner_id: User.all.sample.id,
57
     activity_id: Activity.all.sample.id
58
     activity_id: Activity.all.sample.id
58
   )
59
   )
59
   puts "Created TASK ##{i+1} - #{task.name}"
60
   puts "Created TASK ##{i+1} - #{task.name}"

+ 3
- 3
test/controllers/api/v1/tasks_controller_test.rb View File

30
 
30
 
31
     json_response = JSON.parse(self.response.body)
31
     json_response = JSON.parse(self.response.body)
32
     assert_equal @task.name, json_response['data']['attributes']['name']
32
     assert_equal @task.name, json_response['data']['attributes']['name']
33
-    assert_equal @task.user_id, json_response['data']['attributes']['user_id']
33
+    assert_equal @task.owner_id, json_response['data']['attributes']['owner_id']
34
     assert_equal @task.activity_id, json_response['data']['attributes']['activity_id']
34
     assert_equal @task.activity_id, json_response['data']['attributes']['activity_id']
35
   end
35
   end
36
 
36
 
44
   test "should create task" do
44
   test "should create task" do
45
     assert_difference("Task.count") do
45
     assert_difference("Task.count") do
46
       post api_v1_activity_tasks_url(@activity),
46
       post api_v1_activity_tasks_url(@activity),
47
-      params: { task: { name: @task.name, description: @task.description, user_id: @user, activity_id: @activity } },
47
+      params: { task: { name: @task.name, description: @task.description, owner_id: @user, activity_id: @activity, start_time: DateTime.now } },
48
       headers: { Authorization: JsonWebToken.encode(user_id: @user.id) },
48
       headers: { Authorization: JsonWebToken.encode(user_id: @user.id) },
49
       as: :json
49
       as: :json
50
     end
50
     end
54
   test "should not create task" do
54
   test "should not create task" do
55
     assert_no_difference("Task.count") do
55
     assert_no_difference("Task.count") do
56
       post api_v1_activity_tasks_url(@activity),
56
       post api_v1_activity_tasks_url(@activity),
57
-      params: { task: { name: @task.name, description: @task.description, user_id: @user, activity_id: @activity } },
57
+      params: { task: { name: @task.name, description: @task.description, owner_id: @user, activity_id: @activity } },
58
       as: :json
58
       as: :json
59
     end
59
     end
60
     assert_response :forbidden
60
     assert_response :forbidden

+ 5
- 3
test/fixtures/tasks.yml View File

3
 one:
3
 one:
4
   name: Do something
4
   name: Do something
5
   description: Description of a task
5
   description: Description of a task
6
-  duration: 60
7
-  user: one
8
-  activity: one
6
+  start_time: <%= DateTime.now - 1%>
7
+  end_time: <%= DateTime.now +1 %>
8
+  is_paid: false
9
+  owner: one
10
+  activity: one

+ 1
- 1
test/models/task_test.rb View File

7
   end
7
   end
8
 
8
 
9
   test "task should be valid" do
9
   test "task should be valid" do
10
-    task = Task.new(name: "Write", description: "Write a new line", user_id: @user.id, activity_id: @activity.id)
10
+    task = Task.new(name: "Write", description: "Write a new line", owner_id: @user.id, activity_id: @activity.id, start_time: DateTime.now)
11
     assert task.valid?
11
     assert task.valid?
12
   end
12
   end
13
 
13
 

Loading…
Cancel
Save