Commit e0e4d1c7 by Aman Sharma

Made module of XML read

parent 91e4c288
......@@ -27,9 +27,11 @@ class Api::V1::EmployeesController < ApplicationController
end
def uploadFile
# print{'aaaaaaaaaaaaaaaa'}
# print(params.permit(:file),'aaaaaaaaaaaaaaaaa')
render json: {status: 'sucess'}
@filename = params[:file];
@lines = File.read(@filename.path)
doc = Nokogiri.XML(@lines, nil, 'utf-8')
print doc
render json: { name: 'vijay' }
end
private
......
......@@ -6,6 +6,7 @@ class Api::V1::RecipesController < ApplicationController
def create
recipe = Recipe.create!(recipe_params)
print recipe_params
if recipe
render json: recipe
else
......@@ -27,7 +28,15 @@ class Api::V1::RecipesController < ApplicationController
end
def uploadFile
render json: { status: 'sucess'}
@filename = params[:file];
@lines = File.read(@filename.path)
doc = Nokogiri.XML(@lines, nil, 'utf-8')
name = doc.search('NAME').text
ingredients = doc.search('INGREDIENTS').text
instruction = doc.search('INSTRUCTION').text
Recipe.create!(name: name, ingredients: ingredients, instruction: instruction)
# Recipe.create!(recipe_params)
render json: { msg: 'OK' }
end
private
......
......@@ -18,7 +18,7 @@ class Employee extends React.Component {
}
} = this.props;
const url = `/api/v1/show/${id}`;
const url = `/api/v1/employee/show/${id}`;
fetch(url)
.then(response => {
......@@ -115,7 +115,7 @@ class Employee extends React.Component {
params: { id }
}
} = this.props;
const url = `/api/v1/destroy/${id}`;
const url = `/api/v1/employee/destroy/${id}`;
const token = document.querySelector('meta[name="csrf-token"]').content;
fetch(url, {
......
......@@ -93,6 +93,36 @@ class NewRecipe extends React.Component {
.catch(error => console.log(error.message));
}
submitFile(event) {
event.preventDefault();
const url = "/api/v1/recipes/uploadFile";
const { file } = this.state;
if (file.length == 0)
return;
const body = { file };
console.log(body);
const token = document.querySelector('meta[name="csrf-token"]').content;
const formData = new FormData()
formData.append("file", file)
fetch(url, {
method: "POST",
headers: {
"X-CSRF-Token": token,
},
body: formData
})
.then(response => {
if (response.ok) {
alert("AAAAAAAAAAA")
return response.json();
}
throw new Error("Network response was not ok.");
})
.catch(error => console.log(error.message));
}
render() {
return (
<div className="container mt-5">
......
......@@ -18,7 +18,7 @@ class Recipe extends React.Component {
}
} = this.props;
const url = `/api/v1/show/${id}`;
const url = `/api/v1/recipe/show/${id}`;
fetch(url)
.then(response => {
......@@ -103,7 +103,7 @@ class Recipe extends React.Component {
params: { id }
}
} = this.props;
const url = `/api/v1/destroy/${id}`;
const url = `/api/v1/recipe/destroy/${id}`;
const token = document.querySelector('meta[name="csrf-token"]').content;
fetch(url, {
......
......@@ -3,8 +3,8 @@ Rails.application.routes.draw do
namespace :v1 do
get 'employees/index'
post 'employees/create'
get '/show/:id', to: 'employees#show'
delete '/destroy/:id', to: 'employees#destroy'
get 'employee/show/:id', to: 'employees#show'
delete 'employee/destroy/:id', to: 'employees#destroy'
post 'employees/uploadFile'
end
end
......@@ -12,8 +12,8 @@ Rails.application.routes.draw do
namespace :v1 do
get 'recipes/index'
post 'recipes/create'
get '/show/:id', to: 'recipes#show'
delete '/destroy/:id', to: 'recipes#destroy'
get 'recipe/show/:id', to: 'recipes#show'
delete 'recipe/destroy/:id', to: 'recipes#destroy'
post 'recipes/uploadFile'
end
end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment