File tree Expand file tree Collapse file tree 11 files changed +73
-19
lines changed
blog-frontend/src/components Expand file tree Collapse file tree 11 files changed +73
-19
lines changed Original file line number Diff line number Diff line change @@ -23,8 +23,8 @@ gem 'puma', '~> 3.11'
23
23
gem 'bootsnap' , '>= 1.4.2' , require : false
24
24
25
25
# Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible
26
+ gem 'active_model_serializers'
26
27
gem 'rack-cors'
27
-
28
28
group :development , :test do
29
29
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
30
30
gem 'byebug' , platforms : %i[ mri mingw x64_mingw ]
Original file line number Diff line number Diff line change 45
45
erubi (~> 1.4 )
46
46
rails-dom-testing (~> 2.0 )
47
47
rails-html-sanitizer (~> 1.1 , >= 1.2.0 )
48
+ active_model_serializers (0.10.10 )
49
+ actionpack (>= 4.1 , < 6.1 )
50
+ activemodel (>= 4.1 , < 6.1 )
51
+ case_transform (>= 0.2 )
52
+ jsonapi-renderer (>= 0.1.1.beta1 , < 0.3 )
48
53
activejob (6.0.2.1 )
49
54
activesupport (= 6.0.2.1 )
50
55
globalid (>= 0.3.6 )
70
75
msgpack (~> 1.0 )
71
76
builder (3.2.4 )
72
77
byebug (11.1.1 )
78
+ case_transform (0.2 )
79
+ activesupport
73
80
coderay (1.1.2 )
74
81
concurrent-ruby (1.1.6 )
75
82
crass (1.0.6 )
87
94
i18n (1.8.2 )
88
95
concurrent-ruby (~> 1.0 )
89
96
jaro_winkler (1.5.4 )
97
+ jsonapi-renderer (0.2.2 )
90
98
jwt (2.2.1 )
91
99
listen (3.1.5 )
92
100
rb-fsevent (~> 0.9 , >= 0.9.4 )
@@ -215,6 +223,7 @@ PLATFORMS
215
223
ruby
216
224
217
225
DEPENDENCIES
226
+ active_model_serializers
218
227
bcrypt (~> 3.1.7 )
219
228
bootsnap (>= 1.4.2 )
220
229
byebug
Original file line number Diff line number Diff line change @@ -8,7 +8,7 @@ def create
8
8
if BlogAccessLevel . can_create? ( @current_user , blog )
9
9
blog . save!
10
10
BlogNotificationService . on_blog_created ( blog )
11
- render_created ( blog )
11
+ render_created ( BlogSerializer . new ( blog ) )
12
12
else
13
13
render_unauthorized
14
14
end
@@ -25,16 +25,16 @@ def destroy
25
25
end
26
26
27
27
def index
28
- return render_ok ( Blog . only_public ) if @current_user . nil?
28
+ return render_ok ( Blog . only_public . map { | blog | BlogSerializer . new ( blog ) } ) if @current_user . nil?
29
29
30
- render_ok ( Blog . all )
30
+ render_ok ( Blog . all . map { | blog | BlogSerializer . new ( blog ) } )
31
31
end
32
32
33
33
def show
34
34
blog = Blog . find_by! ( id : search_params [ :id ] )
35
35
36
36
if BlogAccessLevel . can_show? ( @current_user , blog )
37
- render_ok ( blog . to_json ( include : :posts ) )
37
+ render_ok ( BlogSerializer . new ( blog ) )
38
38
else
39
39
render_unauthorized
40
40
end
Original file line number Diff line number Diff line change @@ -7,7 +7,7 @@ def create
7
7
post . user_id = @current_user . id
8
8
9
9
if PostAccessLevel . can_create? ( @current_user , post )
10
- render_created ( post ) if post . save!
10
+ render_created ( PostSerializer . new ( post ) ) if post . save!
11
11
else
12
12
render_unauthorized
13
13
end
@@ -30,7 +30,7 @@ def show
30
30
post = Post . find_by! ( id : search_params [ :id ] , blog_id : search_params [ :blog_id ] )
31
31
32
32
if PostAccessLevel . can_show? ( @current_user , post )
33
- render_ok ( post )
33
+ render_ok ( PostSerializer . new ( post ) )
34
34
else
35
35
render_unauthorized
36
36
end
Original file line number Diff line number Diff line change
1
+ class BlogSerializer < ActiveModel ::Serializer
2
+ attributes :id
3
+ attributes :name
4
+ attributes :owner
5
+ attributes :is_private
6
+
7
+ has_many :posts
8
+
9
+ def owner
10
+ object . user . username
11
+ end
12
+ end
Original file line number Diff line number Diff line change
1
+ class PostSerializer < ActiveModel ::Serializer
2
+ attributes :id
3
+ attributes :title
4
+ attributes :owner
5
+ attributes :is_private
6
+ attributes :blog_id
7
+ attributes :user_id
8
+
9
+ def owner
10
+ object . user . username
11
+ end
12
+
13
+ def is_private
14
+ object . blog . is_private
15
+ end
16
+ end
Original file line number Diff line number Diff line change @@ -45,8 +45,8 @@ export default {
45
45
.then (this .redirectToShowBlog )
46
46
.catch (this .showErrorMessage );
47
47
},
48
- redirectToShowBlog (response ) {
49
- console . log (response );
48
+ redirectToShowBlog ({ data : { data } } ) {
49
+ this . $router . push ({ name : " showBlog " , params : { id : data . id } } );
50
50
},
51
51
52
52
showErrorMessage ({
Original file line number Diff line number Diff line change 8
8
<th >id</th >
9
9
<th >name</th >
10
10
<th >private</th >
11
- <th >show</th >
12
- <th >Delete</th >
13
-
11
+ <th >owner</th >
14
12
</tr >
15
13
</thead >
16
14
<tbody >
17
15
<tr v-for =" blog in blogs" v-bind:key =" blog.id" >
18
16
<td >{{blog.id}}</td >
19
17
<td >{{blog.name}}</td >
20
18
<td >{{blog.is_private}}</td >
19
+ <td >{{blog.owner}}</td >
21
20
<td ><button @click =" redirectToShowBlog(blog.id)" class =" ListBlog__showBlog-button-js" >Show</button ></td >
22
21
<td ><button @click =" deleteBlog(blog.id)" class =" ListBlog__deleteBlog-button-js" >Delete</button ></td >
23
22
</tr >
Original file line number Diff line number Diff line change 1
1
<template lang="">
2
2
<div >
3
- <h3 >Show Blog</h3 >
4
-
5
3
<div >
6
4
<h3 > Blog Name: {{blog.name}} </h3 >
7
5
</div >
11
9
<tr >
12
10
<th >id</th >
13
11
<th >title</th >
14
- <th >show post </th >
15
- <th >delete </th >
12
+ <th >owner </th >
13
+ <th >private </th >
16
14
</tr >
17
15
</thead >
18
16
<tbody >
19
17
<tr v-for =" post in blog.posts" v-bind:key =" post.id" >
20
18
<td >{{post.id}}</td >
21
19
<td >{{post.title}}</td >
20
+ <td >{{post.owner}}</td >
21
+ <td >{{post.is_private}}</td >
22
+
22
23
<td ><button @click =" redirectToShowPost(post.id)" >Show post</button ></td >
23
24
<td ><button @click =" deletePost(post.id)" >Delete post</button ></td >
24
25
</tr >
@@ -76,7 +77,9 @@ export default {
76
77
},
77
78
78
79
updateData ({ data: { data } }) {
79
- this .blog = JSON .parse (data);
80
+ // debugger; // eslint-disable-line
81
+
82
+ this .blog = data;
80
83
},
81
84
82
85
showErrorMessage ({
Original file line number Diff line number Diff line change 3
3
<h3 >Show Post</h3 >
4
4
5
5
<div >
6
- <h3 > Post Title: {{ post.title }} </h3 >
6
+ <p > Post Title: {{ post.title }} </p >
7
+ <p > Post Owner: {{ post.owner }} </p >
8
+
7
9
</div >
10
+
11
+ <button @click =" redirectToShowBlog" >Show posts</button >
12
+ <button @click =" redirectToCreatePost" >Create post</button >
13
+
8
14
</div >
9
15
</template >
10
16
@@ -42,6 +48,15 @@ export default {
42
48
.catch (this .showErrorMessage );
43
49
},
44
50
51
+ redirectToShowBlog () {
52
+ this .$router .push ({ name: " showBlog" , params: { id: this .blogId } });
53
+ },
54
+ redirectToCreatePost () {
55
+ this .$router .push ({
56
+ name: " createPost" ,
57
+ params: { blogId: this .blogId }
58
+ });
59
+ },
45
60
updateData ({ data: { data } }) {
46
61
this .post = data;
47
62
},
Original file line number Diff line number Diff line change @@ -18,7 +18,7 @@ export default {
18
18
return " not registred" ;
19
19
}
20
20
const user = this .$store .getters .user ;
21
- return ` username: ${ user .username } , accessLevel: ${ user .accessLevel } ` ;
21
+ return ` username: ${ user .username } , accessLevel: ${ user .accessLevel } , id: ${ user } ` ;
22
22
},
23
23
isLoged : function () {
24
24
return this .$store .getters .isLoged ;
You can’t perform that action at this time.
0 commit comments