Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.53 KB | None | 0 0
  1. import { Injectable } from '@angular/core';
  2. import { Headers, Http } from '@angular/http';
  3. import 'rxjs/add/operator/toPromise';
  4. import { Hero } from './hero';
  5. @Injectable()
  6. export class HeroService {
  7. private headers = new Headers({'Content-Type': 'application/json'});
  8. private heroesUrl = '//rest.local/api/hero'; // URL to web api
  9. constructor(private http: Http) { }
  10. getHeroes(): Promise<Hero[]> {
  11. return this.http.get(this.heroesUrl)
  12. .toPromise()
  13. .then(response => response.json().data as Hero[])
  14. .catch(this.handleError);
  15. }
  16. getHero(id: number): Promise<Hero> {
  17. const url = `${this.heroesUrl}/${id}`;
  18. return this.http.get(url)
  19. .toPromise()
  20. .then(response => response.json().data as Hero)
  21. .catch(this.handleError);
  22. }
  23. delete(id: number): Promise<void> {
  24. const url = `${this.heroesUrl}/${id}`;
  25. return this.http.delete(url, {headers: this.headers})
  26. .toPromise()
  27. .then(() => null)
  28. .catch(this.handleError);
  29. }
  30. create(name: string): Promise<Hero> {
  31. return this.http
  32. .post(this.heroesUrl, JSON.stringify({name: name}), {headers: this.headers})
  33. .toPromise()
  34. .then(res => res.json().data)
  35. .catch(this.handleError);
  36. }
  37. update(hero: Hero): Promise<Hero> {
  38. const url = `${this.heroesUrl}/${hero.id}`;
  39. return this.http
  40. .put(url, JSON.stringify(hero), {headers: this.headers})
  41. .toPromise()
  42. .then(() => hero)
  43. .catch(this.handleError);
  44. }
  45. private handleError(error: any): Promise<any> {
  46. console.error('An error occurred', error); // for demo purposes only
  47. return Promise.reject(error.message || error);
  48. }
  49. }
  50.  
  51. <?php
  52.  
  53. namespace AppBundleController;
  54.  
  55. use SensioBundleFrameworkExtraBundleConfigurationRoute;
  56. use SymfonyBundleFrameworkBundleControllerController;
  57. use FOSRestBundleControllerAnnotations as Rest;
  58. use FOSRestBundleControllerFOSRestController;
  59. use SymfonyComponentHttpFoundationRequest;
  60. use SymfonyComponentHttpFoundationResponse;
  61. use FOSRestBundleViewView;
  62. use AppBundleEntityHero;
  63.  
  64. class HeroController extends FOSRestController {
  65.  
  66. /**
  67. * @RestGet("api/hero")
  68. */
  69. public function getAction() {
  70. $restresult = $this->getDoctrine()->getRepository('AppBundle:Hero')->findAll();
  71. if ($restresult === null) {
  72. return new View("there are no task exist", Response::HTTP_NOT_FOUND);
  73. }
  74. $view = View::create();
  75. $view->setData($restresult);
  76. return $restresult;
  77. }
  78.  
  79. /**
  80. * @RestGet("api/hero/{id}")
  81. */
  82. public function idAction($id) {
  83. $singleresult = $this->getDoctrine()->getRepository('AppBundle:Hero')->find($id);
  84. if ($singleresult === null) {
  85. return new View("task not found", Response::HTTP_NOT_FOUND);
  86. }
  87. return $singleresult;
  88. }
  89.  
  90.  
  91. /**
  92. * @RestPut("/hero/{id}")
  93. */
  94. public function updateAction($id, Request $request) {
  95. $data = new Hero;
  96. $name = $request->get('name');
  97. $sn = $this->getDoctrine()->getManager();
  98. $hero = $this->getDoctrine()->getRepository('AppBundle:Hero')->find($id);
  99. if (empty($hero)) {
  100. return new View("Hero not found", Response::HTTP_NOT_FOUND);
  101. } elseif (!empty($name)) {
  102. $hero->setName($title);
  103. $sn->flush();
  104. return new View("Hero Updated Successfully", Response::HTTP_OK);
  105. } else{
  106. return new View("Hero name cannot be empty", Response::HTTP_NOT_ACCEPTABLE);
  107. }
  108. }
  109.  
  110. /**
  111. * @RestDelete("/hero/{id}")
  112. */
  113.  
  114. public function deleteAction($id) {
  115. $data = new Hero;
  116. $sn = $this->getDoctrine()->getManager();
  117. $hero = $this->getDoctrine()->getRepository('AppBundle:Hero')->find($id);
  118. if (empty($hero)) {
  119. return new View("Hero not found", Response::HTTP_NOT_FOUND);
  120. } else {
  121. $sn->remove($hero);
  122. $sn->flush();
  123. }
  124. return new View("deleted successfully", Response::HTTP_OK);
  125. }
  126.  
  127. }
  128.  
  129. nelmio_cors:
  130. paths:
  131. '^/api/':
  132. allow_origin: ['*']
  133. allow_headers: ['Origin', 'X-Requested-With', 'Content-Type', 'Accept']
  134. allow_methods: ['POST', 'PUT', 'GET', 'DELETE']
  135. max_age: 3600
  136.  
  137. fos_rest:
  138. body_listener: true
  139. format_listener:
  140. rules:
  141. - { path: '^/', priorities: ['json'], fallback_format: json, prefer_extension: false }
  142. param_fetcher_listener: true
  143. view:
  144. view_response_listener: 'force'
  145. formats:
  146. json: true
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement