-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #88 from simaob/release/login-and-favorites
Release/login and favorites
- Loading branch information
Showing
16 changed files
with
257 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
module Api | ||
module V1 | ||
class FavoritesController < ApiController | ||
def create | ||
return if no_user | ||
return if no_store | ||
|
||
favorite = Favorite.new(store: @store, user: @user) | ||
if favorite.save | ||
render json: StoreSerializer.new(favorite.store).serialized_json, status: :created | ||
else | ||
render json: {errors: favorite.errors.full_messages.join(', ')}, status: :conflict | ||
end | ||
end | ||
|
||
def destroy | ||
return if no_user | ||
|
||
favorite = Favorite.find_by user_id: @user.id, store_id: params[:id] | ||
return render json: {errors: "Couldn't find favorite"}, status: :not_found unless favorite | ||
|
||
if favorite.destroy | ||
render json: {}, status: :no_content | ||
else | ||
render json: {errors: favorite.errors.full_messages.join(', ')}, status: :conflict | ||
end | ||
end | ||
|
||
def index | ||
favorites = context[:current_user].stores | ||
render json: StoreSerializer.new(favorites).serialized_json | ||
end | ||
|
||
private | ||
|
||
def no_user | ||
@user = context[:current_user] | ||
return false if @user&.confirmed? | ||
|
||
render json: {error: 'You must be authenticated'}, status: :forbidden | ||
true | ||
end | ||
|
||
def no_store | ||
@store = Store.find params.dig(:data, :attributes, :"store-id") | ||
false | ||
rescue ActiveRecord::RecordNotFound | ||
render json: {error: 'Store not found'} | ||
true | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# == Schema Information | ||
# | ||
# Table name: favorites | ||
# | ||
# id :bigint not null, primary key | ||
# store_id :bigint not null | ||
# user_id :bigint not null | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# | ||
class Favorite < ApplicationRecord | ||
belongs_to :store | ||
belongs_to :user | ||
|
||
validates :store_id, uniqueness: {scope: :user_id} | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module Api | ||
module V1 | ||
class FavoriteResource < ApplicationResource | ||
caching | ||
|
||
attributes :store_id, :created_at | ||
|
||
has_one :user | ||
has_one :store | ||
|
||
filters :user, :store | ||
|
||
def self.records(options = {}) | ||
current_user = options[:context][:current_user] | ||
current_user&.favorites | ||
end | ||
|
||
exclude_links :default | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class UserSerializer | ||
include FastJsonapi::ObjectSerializer | ||
set_key_transform :dash | ||
set_type :user | ||
|
||
attribute :email | ||
attribute :name | ||
attribute :reports_made do | ||
rand(1..100) | ||
end | ||
attribute :reporter_ranking do | ||
rand(1..100) | ||
end | ||
|
||
has_many :stores, type: :stores, serializer: StoreSerializer | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.