From c784057d1b6adb683a2304d79cc0d25128de1873 Mon Sep 17 00:00:00 2001 From: Sunandar Date: Fri, 24 Feb 2017 14:19:25 +0630 Subject: [PATCH] check duplicate batch no --- app/controllers/api/batches_controller.rb | 34 +++++++++++++---------- lib/tasks/setup.rake | 4 ++- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/app/controllers/api/batches_controller.rb b/app/controllers/api/batches_controller.rb index 457ae3b..4e19526 100644 --- a/app/controllers/api/batches_controller.rb +++ b/app/controllers/api/batches_controller.rb @@ -5,28 +5,34 @@ class Api::BatchesController < ApplicationController check_member= Member.authenticate_session_token(session_token) if !check_member.nil? date = DateTime.now.beginning_of_day.utc.to_time.strftime("%Y-%m-%d") - order_ref = params[:order_ref] + order_ref = params[:order_ref] + order_ref=order_ref.delete(' ') adult_or_child=params[:adult_or_child] client_id=params[:client_id] product_category_id=params[:product_category_id] - find_client=Client.find_by_id(client_id) - if !find_client.nil? - find_product_category=ProductCategory.find_by_id(product_category_id) - if !find_product_category.nil? - user_id=check_member.user_id + find_batch=Batch.find_by_order_ref(order_ref) + if find_batch.nil? + find_client=Client.find_by_id(client_id) + if !find_client.nil? + find_product_category=ProductCategory.find_by_id(product_category_id) + if !find_product_category.nil? + user_id=check_member.user_id - batch = Batch.create_batch(date,order_ref,user_id,adult_or_child,client_id,product_category_id) - if !batch.nil? - @out=true,batch.id - else - @out=false,'Error occurs in creating batch!' - end + batch = Batch.create_batch(date,order_ref,user_id,adult_or_child,client_id,product_category_id) + if !batch.nil? + @out=true,batch.id + else + @out=false,'Error occurs in creating batch!' + end + else + @out=false,'Product Category does not exists!' + end else - @out=false,'Product Category does not exists!' + @out = false, "Client does not exist!" end else - @out = false, "Client does not exist!" + @out=false,order_ref.to_s+ " is already taken!" end else @out = false, "Sorry!Unauthorized user!" diff --git a/lib/tasks/setup.rake b/lib/tasks/setup.rake index 3989daf..58f0dca 100644 --- a/lib/tasks/setup.rake +++ b/lib/tasks/setup.rake @@ -26,9 +26,11 @@ namespace :setup do task :set_product_category, [:name,:product_type] => :environment do |t, args| find_product_type=ProductType.find_by_id(args.product_type) if !find_product_type.nil? + find_product_category=ProductCategory.find_by_name(args.name) - + if find_product_category.nil? + product_category=ProductCategory.new product_category.name=args.name product_category.product_type_id=args.product_type