Reputation: 111
I have a c# WEB API using Entity Framework - I am trying to make a put request through my angular front end to simulate a 'checkin' function but the request isn't going through.
Here is my Web api method
[HttpPut("checkin/{id}")]
[AuthorizeRoles(Role.Administrator, Role.User)]
public async Task<IActionResult> CheckIn(int id)
{
var reservation = await context.Reservations.FindAsync(id);
var username = User.Identity.Name;
if (reservation == null)
return NotFound();
// Ensure user is checking-in their own reservation and its not already checked-in
if (reservation.UserName == username && reservation.CheckInDate == null)
{
reservation.CheckInDate = DateTime.Now;
var count = await context.SaveChangesAsync();
if (count < 1)
return StatusCode((int)HttpStatusCode.InternalServerError);
}
return Ok();
}
Here is my two .ts files where the request is being initiated -note: in the second method I decided to pass the ID manually for testing purposes
-checkIn(id: number){
if (confirm('Would you like to check in')) {
this.reservationService.checki(7);
};
}
reservationservice.ts
checki(id: number) {
const headers = new Headers();
const url = `${this.url}+/checkin/${7}`;
return this.http
.put(url, JSON.stringify(7), {headers: headers})
.map(res => res.json());
}
Upvotes: 2
Views: 2847
Reputation: 556
With Angular HTTP client, all Http requests are Observables which means you need to subscribe to them to have them called.
So in your checkin function you need to do the following:
-checkIn(id: number){
if (confirm('Would you like to check in')) {
this.reservationService.checki(7).subscribe(res => {});
};
}
There is no need to handle the response explicitly.
As far as the backend handling the put I would look at this answer, I'm not a c# expert by any stretch. https://stackoverflow.com/a/32835329/8350917
Upvotes: 6