Skip to content

Commit 3058d58

Browse files
committed
inproving usability
1 parent b137fd2 commit 3058d58

File tree

11 files changed

+73
-19
lines changed

11 files changed

+73
-19
lines changed

volume/blog-backend/Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ gem 'puma', '~> 3.11'
2323
gem 'bootsnap', '>= 1.4.2', require: false
2424

2525
# Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible
26+
gem 'active_model_serializers'
2627
gem 'rack-cors'
27-
2828
group :development, :test do
2929
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
3030
gem 'byebug', platforms: %i[mri mingw x64_mingw]

volume/blog-backend/Gemfile.lock

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ GEM
4545
erubi (~> 1.4)
4646
rails-dom-testing (~> 2.0)
4747
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)
4853
activejob (6.0.2.1)
4954
activesupport (= 6.0.2.1)
5055
globalid (>= 0.3.6)
@@ -70,6 +75,8 @@ GEM
7075
msgpack (~> 1.0)
7176
builder (3.2.4)
7277
byebug (11.1.1)
78+
case_transform (0.2)
79+
activesupport
7380
coderay (1.1.2)
7481
concurrent-ruby (1.1.6)
7582
crass (1.0.6)
@@ -87,6 +94,7 @@ GEM
8794
i18n (1.8.2)
8895
concurrent-ruby (~> 1.0)
8996
jaro_winkler (1.5.4)
97+
jsonapi-renderer (0.2.2)
9098
jwt (2.2.1)
9199
listen (3.1.5)
92100
rb-fsevent (~> 0.9, >= 0.9.4)
@@ -215,6 +223,7 @@ PLATFORMS
215223
ruby
216224

217225
DEPENDENCIES
226+
active_model_serializers
218227
bcrypt (~> 3.1.7)
219228
bootsnap (>= 1.4.2)
220229
byebug

volume/blog-backend/app/controllers/blogs_controller.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def create
88
if BlogAccessLevel.can_create?(@current_user, blog)
99
blog.save!
1010
BlogNotificationService.on_blog_created(blog)
11-
render_created(blog)
11+
render_created(BlogSerializer.new(blog))
1212
else
1313
render_unauthorized
1414
end
@@ -25,16 +25,16 @@ def destroy
2525
end
2626

2727
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?
2929

30-
render_ok(Blog.all)
30+
render_ok(Blog.all.map { |blog| BlogSerializer.new(blog) })
3131
end
3232

3333
def show
3434
blog = Blog.find_by!(id: search_params[:id])
3535

3636
if BlogAccessLevel.can_show?(@current_user, blog)
37-
render_ok(blog.to_json(include: :posts))
37+
render_ok(BlogSerializer.new(blog))
3838
else
3939
render_unauthorized
4040
end

volume/blog-backend/app/controllers/posts_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def create
77
post.user_id = @current_user.id
88

99
if PostAccessLevel.can_create?(@current_user, post)
10-
render_created(post) if post.save!
10+
render_created(PostSerializer.new(post)) if post.save!
1111
else
1212
render_unauthorized
1313
end
@@ -30,7 +30,7 @@ def show
3030
post = Post.find_by!(id: search_params[:id], blog_id: search_params[:blog_id])
3131

3232
if PostAccessLevel.can_show?(@current_user, post)
33-
render_ok(post)
33+
render_ok(PostSerializer.new(post))
3434
else
3535
render_unauthorized
3636
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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

volume/blog-frontend/src/components/blog/create-blog/CreateBlog.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ export default {
4545
.then(this.redirectToShowBlog)
4646
.catch(this.showErrorMessage);
4747
},
48-
redirectToShowBlog(response) {
49-
console.log(response);
48+
redirectToShowBlog({ data: { data } }) {
49+
this.$router.push({ name: "showBlog", params: { id: data.id } });
5050
},
5151
5252
showErrorMessage({

volume/blog-frontend/src/components/blog/list-blog/ListBlog.vue

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,15 @@
88
<th>id</th>
99
<th>name</th>
1010
<th>private</th>
11-
<th>show</th>
12-
<th>Delete</th>
13-
11+
<th>owner</th>
1412
</tr>
1513
</thead>
1614
<tbody>
1715
<tr v-for="blog in blogs" v-bind:key="blog.id">
1816
<td>{{blog.id}}</td>
1917
<td>{{blog.name}}</td>
2018
<td>{{blog.is_private}}</td>
19+
<td>{{blog.owner}}</td>
2120
<td><button @click="redirectToShowBlog(blog.id)" class="ListBlog__showBlog-button-js">Show</button></td>
2221
<td><button @click="deleteBlog(blog.id)" class="ListBlog__deleteBlog-button-js">Delete</button></td>
2322
</tr>

volume/blog-frontend/src/components/blog/show-blog/ShowBlog.vue

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<template lang="">
22
<div>
3-
<h3>Show Blog</h3>
4-
53
<div>
64
<h3> Blog Name: {{blog.name}} </h3>
75
</div>
@@ -11,14 +9,17 @@
119
<tr>
1210
<th>id</th>
1311
<th>title</th>
14-
<th>show post</th>
15-
<th>delete</th>
12+
<th>owner</th>
13+
<th>private</th>
1614
</tr>
1715
</thead>
1816
<tbody>
1917
<tr v-for="post in blog.posts" v-bind:key="post.id">
2018
<td>{{post.id}}</td>
2119
<td>{{post.title}}</td>
20+
<td>{{post.owner}}</td>
21+
<td>{{post.is_private}}</td>
22+
2223
<td><button @click="redirectToShowPost(post.id)">Show post</button></td>
2324
<td><button @click="deletePost(post.id)">Delete post</button></td>
2425
</tr>
@@ -76,7 +77,9 @@ export default {
7677
},
7778
7879
updateData({ data: { data } }) {
79-
this.blog = JSON.parse(data);
80+
// debugger; // eslint-disable-line
81+
82+
this.blog = data;
8083
},
8184
8285
showErrorMessage({

volume/blog-frontend/src/components/post/show-post/ShowPost.vue

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@
33
<h3>Show Post</h3>
44

55
<div>
6-
<h3> Post Title: {{ post.title }} </h3>
6+
<p> Post Title: {{ post.title }} </p>
7+
<p> Post Owner: {{ post.owner }} </p>
8+
79
</div>
10+
11+
<button @click="redirectToShowBlog">Show posts</button>
12+
<button @click="redirectToCreatePost">Create post</button>
13+
814
</div>
915
</template>
1016

@@ -42,6 +48,15 @@ export default {
4248
.catch(this.showErrorMessage);
4349
},
4450
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+
},
4560
updateData({ data: { data } }) {
4661
this.post = data;
4762
},

volume/blog-frontend/src/components/user-header/UserHeader.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export default {
1818
return "not registred";
1919
}
2020
const user = this.$store.getters.user;
21-
return `username: ${user.username}, accessLevel: ${user.accessLevel}`;
21+
return `username: ${user.username}, accessLevel: ${user.accessLevel}, id: ${user}`;
2222
},
2323
isLoged: function() {
2424
return this.$store.getters.isLoged;

0 commit comments

Comments
 (0)