Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 9 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
# README
##### Prerequisites

This README would normally document whatever steps are necessary to get the
application up and running.
The setups steps expect following tools installed on the system.

Things you may want to cover:
- Github
- Ruby [3.2.2](https://github.com/organization/project-name/blob/master/.ruby-version#L1)
- Rails [7.0.5](https://github.com/organization/project-name/blob/master/Gemfile#L12)

* Ruby version
##### 1. Check out the repository

* System dependencies

* Configuration

* Database creation

* Database initialization

* How to run the test suite

* Services (job queues, cache servers, search engines, etc.)

* Deployment instructions

* ...
```bash
git clone git@github.com:organization/project-name.git
```
20 changes: 20 additions & 0 deletions app/controllers/api/v1/friends_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module Api
module V1
class FriendsController < AuthentificateController
def index
result = Api::V1::Friends::Index.call(current_user:, serializer: FriendSerializer)
default_handler(result)
end

def create
result = Api::V1::Friends::AddFriend.call(params:, current_user:, serializer: FriendSerializer)
default_handler(result)
end

def destroy
result = Api::V1::Friends::RemoveFriend.call(params:, current_user:)
default_handler(result)
end
end
end
end
48 changes: 48 additions & 0 deletions app/interactors/api/v1/friends/add_friend.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
module Api
module V1
module Friends
class AddFriend < BaseInteractor
def call
return not_found unless current_friend
return good_outcome if friend_excist?

current_form.save
result
end

private

def good_outcome
context.status = :ok
context.success_message = { friend: :already_added }
end

def friend_excist?
context.current_user.friends.each do |friend|
return true if friend.user == current_friend
end

false
end

def current_form
@current_form ||= UserFriend.new(user_id: context.current_user.id, friend_id: friend_form.id)
end

def current_friend
@current_friend ||= User.find_by(id: context.params[:user_id])
end

def friend_form
@friend_form ||= Friend.create(user_id: current_friend.id)
end

def result
context.form = current_form
context.status = :created
context.success_message = { friend: :added }
end
end
end
end
end
13 changes: 13 additions & 0 deletions app/interactors/api/v1/friends/index.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Api
module V1
module Friends
class Index < BaseInteractor
def call
context.form = context.current_user.friends
context.status = :ok
context.success_message = { operation: :success }
end
end
end
end
end
25 changes: 25 additions & 0 deletions app/interactors/api/v1/friends/remove_friend.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module Api
module V1
module Friends
class RemoveFriend < BaseInteractor
def call
return not_found unless current_friend
return access_denied unless belong_to_user?(Api::V1::FriendPolicy, current_friend)

result
end

private

def current_friend
@current_friend ||= Friend.find_by(id: context.params[:id])
end

def result
context.status = :no_content
current_friend.destroy
end
end
end
end
end
6 changes: 6 additions & 0 deletions app/models/friend.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Friend < ApplicationRecord
belongs_to :user

has_many :user_friends, dependent: :destroy
has_many :users, through: :user_friends
end
2 changes: 2 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ class User < ApplicationRecord

has_many :messages, dependent: :destroy
has_many :news, dependent: :destroy
has_many :user_friends, dependent: :destroy
has_many :friends, through: :user_friends
end
4 changes: 4 additions & 0 deletions app/models/user_friend.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class UserFriend < ApplicationRecord
belongs_to :user
belongs_to :friend
end
9 changes: 9 additions & 0 deletions app/policies/api/v1/friend_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Api
module V1
class FriendPolicy < ApplicationPolicy
def belong_to_user?
@subject.friends.include?(@model)
end
end
end
end
3 changes: 3 additions & 0 deletions app/serializers/friend_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class FriendSerializer < ApplicationSerializer
attributes :user_id, :created_at, :updated_at
end
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
resources :messages
resources :news
resource :newsline, only: :show
resources :friends, except: %i[update show]
end
end
end
9 changes: 9 additions & 0 deletions db/migrate/20230630084051_create_friends.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateFriends < ActiveRecord::Migration[7.0]
def change
create_table :friends do |t|
t.references :user, null: false, foreign_key: true

t.timestamps
end
end
end
10 changes: 10 additions & 0 deletions db/migrate/20230704074345_create_user_friends.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateUserFriends < ActiveRecord::Migration[7.0]
def change
create_table :user_friends do |t|
t.references :user, null: false, foreign_key: true
t.references :friend, null: false, foreign_key: true

t.timestamps
end
end
end
21 changes: 20 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions spec/factories/friends.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FactoryBot.define do
factory :friend do
user { create(:user) }
end
end
6 changes: 6 additions & 0 deletions spec/factories/user_friends.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FactoryBot.define do
factory :user_friend do
user { create(:user) }
friend { create(:friend) }
end
end
28 changes: 28 additions & 0 deletions spec/interactors/api/v1/friends/add_friend_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
RSpec.describe Api::V1::Friends::AddFriend do
subject(:result) { described_class.call(params:, current_user: user) }

let!(:user) { create(:user) }

describe 'Success' do
let!(:friend_user) { create(:user) }
let(:params) { { user_id: friend_user.id } }

it 'is create message' do
expect { result }.to change(Friend, :count).from(0).to(1)
end

it 'is result success' do
expect(result).to be_success
end
end

describe 'Wrong' do
context 'when not found user' do
let(:params) { { user_id: 0 } }

it 'is result not success' do
expect(result).not_to be_success
end
end
end
end
5 changes: 5 additions & 0 deletions spec/models/friend_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
RSpec.describe Friend, type: :model do
describe 'associations' do
it { is_expected.to belong_to(:user) }
end
end
6 changes: 6 additions & 0 deletions spec/models/user_friend_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
RSpec.describe UserFriend, type: :model do
describe 'associations' do
it { is_expected.to belong_to(:user) }
it { is_expected.to belong_to(:friend) }
end
end
Loading