Guest User

Untitled

a guest
May 11th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. import { Injectable } from '@angular/core';
  2. import { HttpClient, HttpHeaders } from '@angular/common/http';
  3. import { Observable } from 'rxjs/Observable';
  4. import { catchError, map, tap } from 'rxjs/operators';
  5. import { of } from 'rxjs/observable/of';
  6. import { Plant } from './plant';
  7.  
  8. @Injectable()
  9. export class PlantService {
  10. private plantsUrl = 'api/plants';
  11.  
  12. constructor(private http: HttpClient) { }
  13.  
  14. getPlants(): Observable<Plant[]> {
  15. return this.http.get<Plant[]>(this.plantsUrl)
  16. .pipe(
  17. catchError(this.handleError('getPlants', []))
  18. );
  19. }
  20.  
  21. getPlant(id: number): Observable<Plant> {
  22. const url = `${this.plantsUrl}/${id}`;
  23. return this.http.get<Plant>(url).pipe(
  24. catchError(this.handleError<Plant>(`getPlant id=${id}`))
  25. );
  26. }
  27.  
  28. private handleError<T>(operation = 'operation', result?: T) {
  29. return (error: any): Observable<T> => {
  30. console.error(error);
  31. return of(result as T);
  32. };
  33. }
  34. }
  35.  
  36. import { Component, OnInit, Input } from '@angular/core';
  37. import { ActivatedRoute } from '@angular/router';
  38. import { Location } from '@angular/common';
  39.  
  40. import { Plant } from '../plant';
  41. import { PlantService } from '../plant.service';
  42.  
  43. @Component({
  44. selector: 'app-plant-detail',
  45. templateUrl: './plant-detail.component.html',
  46. styleUrls: ['./plant-detail.component.css']
  47. })
  48. export class PlantDetailComponent implements OnInit {
  49. plant: Plant;
  50.  
  51. constructor(private route: ActivatedRoute,
  52. private plantService: PlantService,
  53. private location: Location
  54. ) {}
  55.  
  56. ngOnInit(): void {
  57. this.getPlant();
  58.  
  59. }
  60.  
  61. getPlant(): void {
  62. const id = +this.route.snapshot.paramMap.get('id');
  63. this.plantService.getPlant(id)
  64. .subscribe(plant => this.plant = plant);
  65. }
  66.  
  67. goBack(): void {
  68. this.location.back();
  69. }
  70.  
  71. }
  72.  
  73. var express = require("express");
  74. var mysql = require('mysql');
  75. var connection = mysql.createConnection({
  76. host: 'localhost',
  77. user: 'root',
  78. password: 'root',
  79. database: 'plant_care',
  80. });
  81. var app = express();
  82.  
  83. app.get("/api/plants", function(req, res) {
  84. connection.query('SELECT * FROM plant', function(err, rows, fields) {
  85. if (!err)
  86. res.send(rows);
  87. else
  88. console.log('Error while performing Query.');
  89. });
  90. });
  91.  
  92. app.get("/api/plants/:id", function(req, res) {
  93. const requestedID = req.params.id;
  94. connection.query('SELECT * FROM plant WHERE ID = ' + requestedID, function(err, rows, fields) {
  95. if (!err)
  96. res.send(rows);
  97. else
  98. console.log('Error while performing Query.');
  99. });
  100. });
  101.  
  102. app.listen(3000, function() {
  103. console.log("Running...");
  104. });
Add Comment
Please, Sign In to add comment