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
46 changes: 46 additions & 0 deletions app/controllers/api/v1/brand_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
class Api::V1::BrandController < ApplicationController
def index
brand = Brand.all
render json: brand, status: :ok
end

def show
brand= Brand.find(params[:id])
render json: brand, status: :ok
rescue StandardError => e
render json: e, status: :not_found
end

def create
brand = Brand.new(brand_params)
brand.save!
render json: brand, status: :created
rescue StandardError => e
render json: e, status: :bad_request
end


def update
brand = Brand.find(params[:id])
brand.update!(brand_params)
render json: brand, status: :ok
rescue StandardError => e
render json: e, status: :bad_request
end

def delete
brand = Brand.find(params[:id])
brand.destroy!
render json: brand, status: :ok
rescue StandardError => e
render json: e, status: :bad_request
end



private
def brand_params
params.require(:brand).permit(:name)
end

end
45 changes: 45 additions & 0 deletions app/controllers/api/v1/category_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
class Api::V1::CategoryController < ApplicationController
def index
category = Category.all
render json: category, status: :ok
end

def show
category= Category.find(params[:id])
render json: category, status: :ok
rescue StandardError => e
render json: e, status: :not_found
end

def create
category = Category.new(category_params)
category.save!
render json: category, status: :created
rescue StandardError => e
render json: e, status: :bad_request
end


def update
category = Category.find(params[:id])
category.update!(category_params)
render json: category, status: :ok
rescue StandardError => e
render json: e, status: :bad_request
end

def delete
category = Category.find(params[:id])
category.destroy!
render json: category, status: :ok
rescue StandardError => e
render json: e, status: :bad_request
end



private
def category_params
params.require(:category).permit(:name)
end
end
43 changes: 43 additions & 0 deletions app/controllers/api/v1/product_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class Api::V1::ProductController < ApplicationController
def index
product = Product.all
render json: product, status: :ok
end

def show
product= Product.find(params[:id])
render json: product, status: :ok
rescue StandardError => e
render json: e, status: :not_found
end

def create
product = Product.new(product_params)
product.save!
render json: product, status: :created
rescue StandardError => e
render json: e, status: :bad_request
end


def update
product = Product.find(params[:id])
product.update!(product_params)
render json: product, status: :ok
rescue StandardError => e
render json: e, status: :bad_request
end

def delete
product = Product.find(params[:id])
product.destroy!
render json: product, status: :ok
rescue StandardError => e
render json: e, status: :bad_request
end

private
def product_params
params.require(:product).permit(:name,:description,:price,:stock,:brand_id,:category_id)
end
end
3 changes: 3 additions & 0 deletions app/models/product.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
class Product < ApplicationRecord
belongs_to :brand
belongs_to :category
has_many :carts
validates :name, presence: :true
validates :description, presence: :true
validates :price, presence: :true
validates :brand_id, presence: :true
validates :category_id, presence: :true
Expand Down
2 changes: 2 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
class User < ApplicationRecord
has_many :carts
validates :name, presence: :true
validates :email, presence: :true, uniqueness: :true
validates :password, presence: :true

end
26 changes: 25 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
Rails.application.routes.draw do
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
namespace'api' do
namespace 'v1' do
scope 'category' do
get 'index', to: 'category#index'
get 'show/:id', to: 'category#show'
post 'create', to: 'category#create'
patch 'update/:id', to: 'category#update'
delete 'delete/:id', to: 'category#delete'
end
scope 'brand' do
get 'index', to: 'brand#index'
get 'show/:id', to: 'brand#show'
post 'create', to: 'brand#create'
patch 'update/:id', to: 'brand#update'
delete 'delete/:id', to: 'brand#delete'
end
scope 'product' do
get 'index', to: 'product#index'
get 'show/:id', to: 'product#show'
post 'create', to: 'product#create'
patch 'update/:id', to: 'product#update'
delete 'delete/:id', to: 'product#delete'
end
end
end
end
28 changes: 21 additions & 7 deletions db/seeds.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the rails db:seed command (or created alongside the database with db:setup).
#
# Examples:
#
# movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }])
# Character.create(name: 'Luke', movie: movies.first)
Category.create!(name: "Gadgets")
Category.create!(name: "Clothes")
Category.create!(name: "Games")

Brand.create!(name: "Macrohard")
Brand.create!(name: "Vanz")
Brand.create!(name: "Nontendi")

Product.create!(id:1,name: "Nontendi Switch",description: "vg console ", price: 200,stock: 45,brand_id:1 ,category_id:1 )
Product.create!(id:2,name: "Mouse rgb 50000 dpi",description: "shiny mouse" ,price: 200,stock: 45,brand_id:1 ,category_id: 1)
Product.create!(id:3,name: "Drip shoes",description: "good quality shoes", price: 200,stock: 45,brand_id:1 ,category_id: 1)
Product.create!(id:4,name: "T-shirt",description: "cool shirt ", price: 200,stock: 45,brand_id:1 ,category_id: 1)
Product.create!(id:5,name: "Sunglasses",description: "emoji ", price: 25,stock: 50,brand_id:1 ,category_id: 1)
Product.create!(id:6,name: "Mario Golf ",description: "Fun game" ,price: 200,stock: 45,brand_id:1 ,category_id: 1)
Product.create!(id:7,name: "Galaxy M 44",description: "Powerfull Phone", price: 200,stock: 45,brand_id:1 ,category_id: 1)
Product.create!(id:8,name: "Ipear",description: "Stylish Phone", price: 200,stock: 45,brand_id:1 ,category_id: 1)
Product.create!(id:9,name: "Headphone",description: "Enhance your listening experience", price: 200,stock: 45,brand_id:1 ,category_id: 1)
Product.create!(id:10,name: "Pro Controller",description: "Made for pro gamers", price: 200,stock: 45,brand_id:1 ,category_id: 1)



4 changes: 2 additions & 2 deletions spec/factories/carts.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FactoryBot.define do
factory :cart do
product { nil }
user { nil }
product { create(:product) }
user { create(:user) }
end
end
5 changes: 4 additions & 1 deletion spec/factories/categories.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
FactoryBot.define do
sequence :categories do |e|
"category#{e}"
end
factory :category do
name { "MyString" }
name {generate(:categories)}
end
end
2 changes: 1 addition & 1 deletion spec/factories/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
email { "MyString" }
password { "MyString" }
wallet { 1 }
is_admin { false }
is_admin { false }
end
end
4 changes: 3 additions & 1 deletion spec/models/cart_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require 'rails_helper'

RSpec.describe Cart, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
context "Factory test" do
it {expect(build(:cart)).to be_valid}
end
end
2 changes: 2 additions & 0 deletions spec/models/product_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@
end
end



end
84 changes: 84 additions & 0 deletions spec/requests/api/v1/brand_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
require 'rails_helper'

RSpec.describe "Api::V1::Brands", type: :request do
describe "GET /index" do
before do
create(:brand, id:1, name: 'Beauty')
create(:brand, id:2, name: 'Housing')
end


it 'return http status ok' do
get '/api/v1/brand/index'
expect(response) .to have_http_status(:ok)
end
end
describe 'GET /show' do
let(:brand) {create(:brand)}
context 'id does exist' do
before do
get"/api/v1/brand/show/#{brand.id}"
end
end
context'id not found' do
before do
get "/api/v1/brand/show/-1"
end
it 'return http status not_found' do
expect(response).to have_http_status(:not_found)
end
end
end
describe 'POST /create' do
let(:brand_params) do
attributes_for(:brand)
end
context 'params check ok' do
it 'return http status created' do
p brand_params
post "/api/v1/brand/create", params:{brand: brand_params}
expect(response).to have_http_status(:created)
end
end
context 'bad params' do
it 'when param is nil' do
post "/api/v1/brand/create", params:{brand: nil}
expect(response).to have_http_status(:bad_request)
end
end
end
describe 'PATCH/update' do
let(:brand) do
create(:brand,name: "Kitchen")
end
context 'params ok' do
it 'return http status ok' do
patch "/api/v1/brand/update/#{brand.id}", params:{brand: {name:"Utility"}}
expect(response).to have_http_status(:ok)
end
end
context 'bad params'do
it 'when params is nill' do
patch "/api/v1/brand/update/#{brand.id}", params:{brand: {name: nil}}
expect(response).to have_http_status(:bad_request)
end
end
end
describe 'DELETE/delete_id' do
let(:brand) do
create(:brand)
end
context 'params ok' do
it 'return http status ok' do
delete "/api/v1/brand/delete/#{brand.id}"
expect(response).to have_http_status(:ok)
end
end
context 'bad params' do
it 'params is nill' do
delete "/api/v1/brand/delete/-1"
expect(response).to have_http_status(:bad_request)
end
end
end
end
Loading