Skip to content

Commit 73d5739

Browse files
authored
Release v21.12.2023
Release v21.12.2023
2 parents fcb69f4 + 8996c42 commit 73d5739

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

app/api/web/users_api.rb

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
module API
2+
class Web::UsersAPI < Grape::API
3+
4+
resource :users do
5+
6+
desc 'Get a user',
7+
security: [access_token: {}]
8+
get do
9+
authenticate_user!
10+
status 200
11+
{
12+
success: true,
13+
user: {
14+
id: current_user.id,
15+
email: current_user.email,
16+
name: current_user.name,
17+
role: current_user.role,
18+
last_name: current_user.last_name,
19+
first_name: current_user.first_name,
20+
avatar_url: current_user.avatar_url,
21+
created_at: current_user.created_at.to_fs(:ymd_hms),
22+
},
23+
user_feedbacks: current_user.feedbacks.map do |feedback|
24+
{
25+
id: feedback.id,
26+
post_id: feedback.post_id,
27+
content: feedback.content,
28+
created_at: feedback.created_at.to_fs(:ymd_hms),
29+
}
30+
end,
31+
user_posts: current_user.posts.map do |post|
32+
{
33+
id: post.id,
34+
title: post.title,
35+
total_views: post.total_view,
36+
created_at: post.created_at.to_fs(:ymd_hms),
37+
}
38+
end,
39+
user_comments: current_user.comments.map do |comment|
40+
{
41+
id: comment.id,
42+
linked_object_id: comment.linked_object_id,
43+
linked_object_type: comment.linked_object_type,
44+
content: comment.content,
45+
created_at: comment.created_at.to_fs(:ymd_hms),
46+
}
47+
end,
48+
user_discussions: current_user.discussions.map do |discussion|
49+
{
50+
id: discussion.id,
51+
content: discussion.content,
52+
created_at: discussion.created_at.to_fs(:ymd_hms),
53+
}
54+
end
55+
}
56+
end
57+
58+
desc 'Edit profile',
59+
security: [access_token: {}]
60+
params do
61+
optional :last_name, type: String, desc: 'Last name'
62+
optional :first_name, type: String, desc: 'First name'
63+
optional :avatar_url, type: String, desc: 'Avatar url'
64+
optional :old_password, type: String, desc: 'Old password'
65+
optional :new_password, type: String, desc: 'New password'
66+
optional :new_password_confirmation, type: String, desc: 'New password confirmation'
67+
end
68+
put do
69+
authenticate_user!
70+
if params[:old_password].present? && params[:new_password].present? && params[:new_password_confirmation].present?
71+
if params[:new_password] != params[:new_password_confirmation]
72+
error!({ success: false, message: 'Password confirmation does not match' }, 400)
73+
end
74+
unless current_user.valid_password?(params[:old_password])
75+
error!({ success: false, message: 'Old password is incorrect' }, 400)
76+
end
77+
current_user.password = params[:new_password]
78+
current_user.password_confirmation = params[:new_password_confirmation]
79+
end
80+
81+
if params[:old_password].present? && params[:new_password].blank? && params[:new_password_confirmation].blank?
82+
error!({ success: false, message: 'New password is required' }, 400)
83+
end
84+
85+
if params[:old_password].blank? && params[:new_password].present? && params[:new_password_confirmation].present?
86+
error!({ success: false, message: 'Old password is required' }, 400)
87+
end
88+
89+
current_user.last_name = params[:last_name] if params[:last_name].present?
90+
current_user.first_name = params[:first_name] if params[:first_name].present?
91+
current_user.avatar_url = params[:avatar_url] if params[:avatar_url].present?
92+
93+
if current_user.save
94+
status 200
95+
{
96+
success: true,
97+
message: 'Profile updated successfully',
98+
}
99+
else
100+
error!({ success: false, message: current_user.errors.full_messages.join(', ') }, 400)
101+
end
102+
end
103+
end
104+
end
105+
end

app/api/web_api.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class WebAPI < Grape::API
66

77
mount Web::SessionsAPI
88
mount Web::RegistrationsAPI
9+
mount Web::UsersAPI
910
mount Web::FeedbacksAPI
1011
mount Web::DiscussionsAPI
1112
mount Web::PostsAPI

0 commit comments

Comments
 (0)