|
@@ -0,0 +1,59 @@
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.Data.Entity.Infrastructure;
|
|
|
+using System.Linq;
|
|
|
+using System.Net;
|
|
|
+using System.Net.Http;
|
|
|
+using System.Web.Http;
|
|
|
+using System.Web.Http.Description;
|
|
|
+using WebLab.Entities;
|
|
|
+using WebLab.Models;
|
|
|
+
|
|
|
+namespace WebLab.Controllers
|
|
|
+{
|
|
|
+ public class PatientsController : ApiController
|
|
|
+ {
|
|
|
+ public user1Entities db = new user1Entities();
|
|
|
+ //get
|
|
|
+ [ResponseType(typeof(List<ResponsePatients>))]
|
|
|
+ public IHttpActionResult GetPatients()
|
|
|
+ {
|
|
|
+ return Ok(db.patients.ToList().ConvertAll(t => new ResponsePatients(t)));
|
|
|
+ }
|
|
|
+
|
|
|
+ //post
|
|
|
+ [ResponseType(typeof(patients))]
|
|
|
+ public IHttpActionResult PostPatients(patients p)
|
|
|
+ {
|
|
|
+ if(!ModelState.IsValid)
|
|
|
+ {
|
|
|
+ return BadRequest(ModelState);
|
|
|
+ }
|
|
|
+
|
|
|
+ db.patients.Add(p);
|
|
|
+
|
|
|
+ try
|
|
|
+ {
|
|
|
+ db.SaveChanges();
|
|
|
+ }
|
|
|
+ catch (DbUpdateException)
|
|
|
+ {
|
|
|
+ if (patinetsExist(p.Id))
|
|
|
+ {
|
|
|
+ return Conflict();
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ throw;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return CreatedAtRoute("DefaultApi", new { id = p.Id }, p);
|
|
|
+ }
|
|
|
+
|
|
|
+ private bool patinetsExist(int id)
|
|
|
+ {
|
|
|
+ return db.patients.Count(e => e.Id == id) > 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|