Commit e0e4d1c7 by Aman Sharma

Made module of XML read

parent 91e4c288
...@@ -27,9 +27,11 @@ class Api::V1::EmployeesController < ApplicationController ...@@ -27,9 +27,11 @@ class Api::V1::EmployeesController < ApplicationController
end end
def uploadFile def uploadFile
# print{'aaaaaaaaaaaaaaaa'} @filename = params[:file];
# print(params.permit(:file),'aaaaaaaaaaaaaaaaa') @lines = File.read(@filename.path)
render json: {status: 'sucess'} doc = Nokogiri.XML(@lines, nil, 'utf-8')
print doc
render json: { name: 'vijay' }
end end
private private
......
...@@ -6,6 +6,7 @@ class Api::V1::RecipesController < ApplicationController ...@@ -6,6 +6,7 @@ class Api::V1::RecipesController < ApplicationController
def create def create
recipe = Recipe.create!(recipe_params) recipe = Recipe.create!(recipe_params)
print recipe_params
if recipe if recipe
render json: recipe render json: recipe
else else
...@@ -27,7 +28,15 @@ class Api::V1::RecipesController < ApplicationController ...@@ -27,7 +28,15 @@ class Api::V1::RecipesController < ApplicationController
end end
def uploadFile 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 end
private private
......
...@@ -18,7 +18,7 @@ class Employee extends React.Component { ...@@ -18,7 +18,7 @@ class Employee extends React.Component {
} }
} = this.props; } = this.props;
const url = `/api/v1/show/${id}`; const url = `/api/v1/employee/show/${id}`;
fetch(url) fetch(url)
.then(response => { .then(response => {
...@@ -115,7 +115,7 @@ class Employee extends React.Component { ...@@ -115,7 +115,7 @@ class Employee extends React.Component {
params: { id } params: { id }
} }
} = this.props; } = this.props;
const url = `/api/v1/destroy/${id}`; const url = `/api/v1/employee/destroy/${id}`;
const token = document.querySelector('meta[name="csrf-token"]').content; const token = document.querySelector('meta[name="csrf-token"]').content;
fetch(url, { fetch(url, {
......
...@@ -93,6 +93,36 @@ class NewRecipe extends React.Component { ...@@ -93,6 +93,36 @@ class NewRecipe extends React.Component {
.catch(error => console.log(error.message)); .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() { render() {
return ( return (
<div className="container mt-5"> <div className="container mt-5">
......
...@@ -18,7 +18,7 @@ class Recipe extends React.Component { ...@@ -18,7 +18,7 @@ class Recipe extends React.Component {
} }
} = this.props; } = this.props;
const url = `/api/v1/show/${id}`; const url = `/api/v1/recipe/show/${id}`;
fetch(url) fetch(url)
.then(response => { .then(response => {
...@@ -103,7 +103,7 @@ class Recipe extends React.Component { ...@@ -103,7 +103,7 @@ class Recipe extends React.Component {
params: { id } params: { id }
} }
} = this.props; } = this.props;
const url = `/api/v1/destroy/${id}`; const url = `/api/v1/recipe/destroy/${id}`;
const token = document.querySelector('meta[name="csrf-token"]').content; const token = document.querySelector('meta[name="csrf-token"]').content;
fetch(url, { fetch(url, {
......
...@@ -3,8 +3,8 @@ Rails.application.routes.draw do ...@@ -3,8 +3,8 @@ Rails.application.routes.draw do
namespace :v1 do namespace :v1 do
get 'employees/index' get 'employees/index'
post 'employees/create' post 'employees/create'
get '/show/:id', to: 'employees#show' get 'employee/show/:id', to: 'employees#show'
delete '/destroy/:id', to: 'employees#destroy' delete 'employee/destroy/:id', to: 'employees#destroy'
post 'employees/uploadFile' post 'employees/uploadFile'
end end
end end
...@@ -12,8 +12,8 @@ Rails.application.routes.draw do ...@@ -12,8 +12,8 @@ Rails.application.routes.draw do
namespace :v1 do namespace :v1 do
get 'recipes/index' get 'recipes/index'
post 'recipes/create' post 'recipes/create'
get '/show/:id', to: 'recipes#show' get 'recipe/show/:id', to: 'recipes#show'
delete '/destroy/:id', to: 'recipes#destroy' delete 'recipe/destroy/:id', to: 'recipes#destroy'
post 'recipes/uploadFile' post 'recipes/uploadFile'
end end
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