Last active
February 3, 2017 18:51
-
-
Save strawmr/15a22bf5118642ff130f215511982b80 to your computer and use it in GitHub Desktop.
Angular 2 Data reading
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { NgModule } from "@angular/core"; | |
import { BrowserModule } from "@angular/platform-browser"; | |
import { FormsModule } from "@angular/forms"; | |
import { HttpModule } from "@angular/http"; | |
import { NewsComponent } from "./news.component"; | |
import { IncidentComponent } from "./incident.component"; | |
@NgModule({ | |
imports: [ BrowserModule, FormsModule, HttpModule ], | |
declarations: [ NewsComponent, IncidentComponent ], | |
bootstrap: [NewsComponent, IncidentComponent] | |
}) | |
export class AppModule { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { NgModule } from "@angular/core"; | |
import { BrowserModule } from "@angular/platform-browser"; | |
import { FormsModule } from "@angular/forms"; | |
import { HttpModule } from "@angular/http"; | |
import { NewsComponent } from "./news.component"; | |
import { IncidentComponent } from "./incident.component"; | |
@NgModule({ | |
imports: [ BrowserModule, FormsModule, HttpModule ], | |
declarations: [ NewsComponent, IncidentComponent ], | |
bootstrap: [NewsComponent, IncidentComponent] | |
}) | |
export class AppModule { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="incidentarticle" *ngFor="let incident of incidents; let i = index; trackBy: incidentId"> | |
<h2> | |
{{incident.IncidentId}} | |
<span class="glyphicon glyphicon-edit" (click)="onEdit()"></span> | |
</h2> | |
<h3 *ngIf="incident.DateReported != null">Start: {{incident.DateReported | date:'medium'}}</h3> | |
<!--<div> <span>Applies To:</span> {{newsitem.AppliesTo}}</div>--> | |
<div> | |
<span>Description:</span> {{incident.IncidentDescription}} | |
</div> | |
<!--<div> | |
<span>Action Required:</span> {{newsitem.ActionRequired}} | |
</div> | |
<div><span>For Further Information:</span> {{newsitem.AdditionalInfo}}</div>--> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component } from "@angular/core"; | |
import { IncidentService } from "./incident.service"; | |
import { Incident } from "./incident"; | |
@Component({ | |
selector: "rpcs-incident", | |
templateUrl: "/app/incident.component.html", | |
providers: [IncidentService] | |
}) | |
export class IncidentComponent { | |
private incidents: Incident[]; | |
constructor(incidentService: IncidentService) { | |
incidentService.getAll() | |
.subscribe( | |
response => this.incidents = response, | |
err => console.log("Error: ", err), | |
() => console.log("Fetch incidents complete.", this.incidents) | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from "@angular/core"; | |
import { Http, Response } from "@angular/http"; | |
import { SpringfieldService } from "./springfield.service"; | |
import "rxjs/add/operator/map"; | |
import { Incident } from "./incident"; | |
@Injectable() | |
export class IncidentService extends SpringfieldService { | |
private url = this.baseUrl + "Incident"; | |
constructor(private http: Http) { | |
super(); | |
} | |
getAll() { | |
return this.http.get(this.url) | |
.map((res: Response) => res.json()); | |
} | |
getIncident(incidentId: number) { | |
return this.http.get(this.url, incidentId) | |
.map((res: Response) => res.json()); | |
} | |
saveIncident(incident: Incident) { | |
return this.http.put(this.url, incident) | |
.map((res: Response) => res.json()); | |
} | |
addIncident(incident: Incident) { | |
return this.http.post(this.url, incident) | |
.map((res: Response) => res.json()); | |
} | |
deleteIncident(incident: Incident) { | |
return this.http.delete(this.url, incident) | |
.map((res: Response) => res.json()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class Incident { | |
incidentId: number; | |
incidentDescription: string; | |
dateReported: Date; | |
dateOccurred: Date; | |
dateResolved: Date; | |
severity: number; | |
status: number; | |
createDateTime: Date; | |
modifiedDateTime: Date; | |
inactiveDateTime: Date; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Net; | |
using System.Net.Http; | |
using System.Web.Http; | |
using Springfield.Data; | |
namespace Springfield.API.Controllers | |
{ | |
public class IncidentController : ApiController | |
{ | |
private readonly IncidentManager _im = new IncidentManager(); | |
// GET: api/Incident | |
public IHttpActionResult GetIncident() | |
{ | |
var incidents = _im.GetIncident(); | |
if (incidents == null || incidents.Count <= 0) | |
return NotFound(); | |
return Ok(incidents); | |
} | |
// GET: api/Incident/5 | |
public IHttpActionResult GetIncident(int id) | |
{ | |
var incidents = _im.GetIncident(id); | |
if (incidents == null || incidents.Count <= 0) | |
return NotFound(); | |
return Ok(incidents); | |
} | |
// PUT: api/Incident/5 | |
public IHttpActionResult PutIncident(int id, Incident incident) | |
{ | |
if (!ModelState.IsValid) | |
return BadRequest(ModelState); | |
if (id != incident.IncidentId) | |
return BadRequest(); | |
_im.SaveIncident(incident); | |
return Ok(incident); | |
} | |
// POST: api/Incident | |
public IHttpActionResult PostIncident(Incident incident) | |
{ | |
if (!ModelState.IsValid) | |
return BadRequest(ModelState); | |
_im.AddIncident(incident); | |
return Ok(incident); | |
} | |
// DELETE: api/Incident/5 | |
public IHttpActionResult DeleteIncident(int id) | |
{ | |
var incident = _im.GetIncident(id).SingleOrDefault(); | |
if (incident == null) | |
return NotFound(); | |
_im.RemoveIncident(incident); | |
return Ok(incident); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Net; | |
using System.Net.Http; | |
using System.Web.Http; | |
using Springfield.Data; | |
namespace Springfield.API.Controllers | |
{ | |
public class IncidentController : ApiController | |
{ | |
private readonly IncidentManager _im = new IncidentManager(); | |
// GET: api/Incident | |
public IHttpActionResult GetIncident() | |
{ | |
var incidents = _im.GetIncident(); | |
if (incidents == null || incidents.Count <= 0) | |
return NotFound(); | |
return Ok(incidents); | |
} | |
// GET: api/Incident/5 | |
public IHttpActionResult GetIncident(int id) | |
{ | |
var incidents = _im.GetIncident(id); | |
if (incidents == null || incidents.Count <= 0) | |
return NotFound(); | |
return Ok(incidents); | |
} | |
// PUT: api/Incident/5 | |
public IHttpActionResult PutIncident(int id, Incident incident) | |
{ | |
if (!ModelState.IsValid) | |
return BadRequest(ModelState); | |
if (id != incident.IncidentId) | |
return BadRequest(); | |
_im.SaveIncident(incident); | |
return Ok(incident); | |
} | |
// POST: api/Incident | |
public IHttpActionResult PostIncident(Incident incident) | |
{ | |
if (!ModelState.IsValid) | |
return BadRequest(ModelState); | |
_im.AddIncident(incident); | |
return Ok(incident); | |
} | |
// DELETE: api/Incident/5 | |
public IHttpActionResult DeleteIncident(int id) | |
{ | |
var incident = _im.GetIncident(id).SingleOrDefault(); | |
if (incident == null) | |
return NotFound(); | |
_im.RemoveIncident(incident); | |
return Ok(incident); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment