Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Injectable } from '@angular/core';
- import { Headers, Http } from '@angular/http';
- import 'rxjs/add/operator/toPromise';
- import { Hero } from './hero';
- @Injectable()
- export class HeroService {
- private headers = new Headers({'Content-Type': 'application/json'});
- private heroesUrl = '//rest.local/api/hero'; // URL to web api
- constructor(private http: Http) { }
- getHeroes(): Promise<Hero[]> {
- return this.http.get(this.heroesUrl)
- .toPromise()
- .then(response => response.json().data as Hero[])
- .catch(this.handleError);
- }
- getHero(id: number): Promise<Hero> {
- const url = `${this.heroesUrl}/${id}`;
- return this.http.get(url)
- .toPromise()
- .then(response => response.json().data as Hero)
- .catch(this.handleError);
- }
- delete(id: number): Promise<void> {
- const url = `${this.heroesUrl}/${id}`;
- return this.http.delete(url, {headers: this.headers})
- .toPromise()
- .then(() => null)
- .catch(this.handleError);
- }
- create(name: string): Promise<Hero> {
- return this.http
- .post(this.heroesUrl, JSON.stringify({name: name}), {headers: this.headers})
- .toPromise()
- .then(res => res.json().data)
- .catch(this.handleError);
- }
- update(hero: Hero): Promise<Hero> {
- const url = `${this.heroesUrl}/${hero.id}`;
- return this.http
- .put(url, JSON.stringify(hero), {headers: this.headers})
- .toPromise()
- .then(() => hero)
- .catch(this.handleError);
- }
- private handleError(error: any): Promise<any> {
- console.error('An error occurred', error); // for demo purposes only
- return Promise.reject(error.message || error);
- }
- }
- <?php
- namespace AppBundleController;
- use SensioBundleFrameworkExtraBundleConfigurationRoute;
- use SymfonyBundleFrameworkBundleControllerController;
- use FOSRestBundleControllerAnnotations as Rest;
- use FOSRestBundleControllerFOSRestController;
- use SymfonyComponentHttpFoundationRequest;
- use SymfonyComponentHttpFoundationResponse;
- use FOSRestBundleViewView;
- use AppBundleEntityHero;
- class HeroController extends FOSRestController {
- /**
- * @RestGet("api/hero")
- */
- public function getAction() {
- $restresult = $this->getDoctrine()->getRepository('AppBundle:Hero')->findAll();
- if ($restresult === null) {
- return new View("there are no task exist", Response::HTTP_NOT_FOUND);
- }
- $view = View::create();
- $view->setData($restresult);
- return $restresult;
- }
- /**
- * @RestGet("api/hero/{id}")
- */
- public function idAction($id) {
- $singleresult = $this->getDoctrine()->getRepository('AppBundle:Hero')->find($id);
- if ($singleresult === null) {
- return new View("task not found", Response::HTTP_NOT_FOUND);
- }
- return $singleresult;
- }
- /**
- * @RestPut("/hero/{id}")
- */
- public function updateAction($id, Request $request) {
- $data = new Hero;
- $name = $request->get('name');
- $sn = $this->getDoctrine()->getManager();
- $hero = $this->getDoctrine()->getRepository('AppBundle:Hero')->find($id);
- if (empty($hero)) {
- return new View("Hero not found", Response::HTTP_NOT_FOUND);
- } elseif (!empty($name)) {
- $hero->setName($title);
- $sn->flush();
- return new View("Hero Updated Successfully", Response::HTTP_OK);
- } else{
- return new View("Hero name cannot be empty", Response::HTTP_NOT_ACCEPTABLE);
- }
- }
- /**
- * @RestDelete("/hero/{id}")
- */
- public function deleteAction($id) {
- $data = new Hero;
- $sn = $this->getDoctrine()->getManager();
- $hero = $this->getDoctrine()->getRepository('AppBundle:Hero')->find($id);
- if (empty($hero)) {
- return new View("Hero not found", Response::HTTP_NOT_FOUND);
- } else {
- $sn->remove($hero);
- $sn->flush();
- }
- return new View("deleted successfully", Response::HTTP_OK);
- }
- }
- nelmio_cors:
- paths:
- '^/api/':
- allow_origin: ['*']
- allow_headers: ['Origin', 'X-Requested-With', 'Content-Type', 'Accept']
- allow_methods: ['POST', 'PUT', 'GET', 'DELETE']
- max_age: 3600
- fos_rest:
- body_listener: true
- format_listener:
- rules:
- - { path: '^/', priorities: ['json'], fallback_format: json, prefer_extension: false }
- param_fetcher_listener: true
- view:
- view_response_listener: 'force'
- formats:
- json: true
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement